Shader "URP/Environment/Grass/GrassOriginBurn"
{
	Properties
	{
		[HideInInSpector]_Color("Color", Color) = (1,1,1,1)
		[HideInInSpector]_Cutoff("Alpha cutoff", Range(0,1)) = 0.5
		[HideInInSpector]_SpecularPower("Specular Power", Range(0.01, 1)) = 0.078125
		[HideInInSpector]_Gloss("Gloss", Range(0, 4)) = 0.5
		[HideInInSpector]_MainTex("Albedo (RGB)", 2D) = "white" {}
		[HideInInSpector]_SpecularMap("Specular Map", 2D) = "Black" {}

		//_SectorWPosInfo("SectorWPosInfo",Vector) = (0,0,0,0)
		//_SectorUVScaleInfo("SectorUVScaleInfo",Vector) = (0 ,0, 0, 0)
		//_SectorMaskSelector("SectorMaskSelector",Vector) = (0, 0, 0, 0)
		//_SectorShadowMaskTex("SectorShadowMaskTex",2D) = "white" {}
		//_ShadowCoef("Shadow Coef", Range(-1, 1)) = 0.3
		[HideInInSpector]_WindIntensity("Wind Intensity", Float) = 0.3
		[HideInInSpector]_FireColor("FireColor", Color) = (0.3, 0.3, 0.3, 1)
		
		[Header(_Curvature_Height(Xy_MaskR_Scale ZW_Scale))]
		_BladeCurve("Blade Curvature", Range(-0.5, 0.5)) = 0
		_BladeHeight("Blade Height",Vector) = (0,0,1.12,1)
		[Header(_Wind)]
		_Wind("Wind",2D) = "white"{}//突变风采样纹理
		_Windvector("Windvector",Vector) = (1,0,0,2)//风的方向（世界空间）
		_WaveSpeed("Wave Speed",Range(-20, 20)) = 1
		
			[Space(4)]
		[Header(Cutoff Grass Animation Settings)]
		[Space(2)]
		[Header(Lifetime)]
		_AnimateTime("animate time", Float) = 0.6
		_AnimateTimeEnd(" ", Float) = 1.5
		[HideInInSpector] _AnimateTimeRand(" ", Float) = 0

		[Space(2)]
		[Header(Grass Density)]
		[HideInInSpector]_Density("grass density", Range(0,1)) = 1

		[Space(2)]
		[Header(Wind Intense)]
		[HideInInSpector]_WindDirX("wind intense in X component", Range(-1,1)) = 0.0
		[HideInInSpector]_WindDirZ("wind intense in Z component", Range(-1,1)) = 0.0

		[Space(2)]
		[Header(Horizontal Velocity)]
		[HideInInSpector]_VelocityInit("horizontal velocity", Float) = 2
		[HideInInSpector]_VelocityInitEnd(" ", Float) = 2
		[HideInInSpector] _VelocityRand(" ", Float) = 1
		[HideInInSpector]_DirRand("direction rand", Float) = 1
		//_VelocityRandZ("velocity rand in z", Float) = 1
		[HideInInSpector]_VelocityFinal("final horizontal velocity", FLoat) = 1
		[HideInInSpector]_VelocityDamp("horizontal velocity damp", Range(1,10)) = 5

		[Space(2)]
		[Header(Vertical Velocity)]
		[HideInInSpector]_AriseVelocityInit("vertical velocity", Float) = 1.1
		[HideInInSpector]_AriseVelocityInitEnd(" ", Float) = 1.1
		[HideInInSpector] _AriseVRand(" ", Float) = 0
		[HideInInSpector]_AriseGravity("gravity", Float) = 0.11
		[HideInInSpector]_AriseGravityEnd(" ", Float) = 0.11
		[HideInInSpector] _AriseGRand(" ", Float) = 0

		[Space(2)]
		[Header(Vertical Wave Noise)]
		[HideInInSpector] _AnimateNoiseAmp("vertical wave noise amplitude", Float) = 0.15
		[HideInInSpector] _AnimateNoiseFreq("vertical wave noise freq", Float) = 3

		[Space(2)]
		[Header(Grass Blade Scale)]
		//_InitScale("init grass scale", Range(1,2)) = 1
		[HideInInSpector]_ScaleDamp("scale decrease rate", Range(0,1)) = 0.5

		[Space(2)]
		[Header(Rotation Velocity)]
		[HideInInSpector]_RotVelocityInit("rotation velocity", Float) = 2
		[HideInInSpector]_RotVelocityInitEnd(" ", Float) = 2
		[HideInInSpector] _RotRand(" ", Float) = 1
		//_RotVelocityFinal("final rotation veloctiy", Float) = 2
		[HideInInSpector]_RotVelocityDamp("rotation damp", Range(0,10)) = 2

		[Space(2)]
		[Header(Transparent Transition)]
		_TransparentTime("Transparent time percent", Range(0, 1)) = 0.8

		[Space(2)]
		[Header(Grass Burn)]
		_BurnTex("Burn Map", 2D) = "white" {}
		_BurnAmount("Burn Amount", Range(0,1)) = 0.2
		_BurnRemain("Ash Remain", Range(0,0.5)) = 0.3
		_BurnSpeed("Burn Speed", Range(0, 1)) = 0.5
		_LineWidth("Burn Line Width", Range(0,1)) = 0.7

		[Space(2)]
		[Header(Burn Color Set 1)]
		[HDR]_InitColor1("burn colorA", Color) = (23.96863, 5.270588,0,1)
		[HDR]_MiddleColor1("burn colorB", Color) = (20.8302, 0.4362345,0,1)
		_TransitColorTime("final color time percent", Range(0,0.7)) = 0.5
		_FinalColor1("final color", Color) = (0.3,0.3,0.3,1)

		[Space(2)]
		[Header(Burn Color Set 2)]
		[HDR]_InitColor2("burn colorA", Color) = (23.96863, 5.270588, 0, 1)
		[HDR]_MiddleColor2("burn colorB", Color) = (20.8302, 0.4362345, 0, 1)
		_FinalColor2("final color", Color) = (0.3, 0.3, 0.3, 1)

		[Space(2)]
		[Header(Burn Color Set 3)]
		[HDR]_InitColor3("burn colorA", Color) = (23.96863, 5.270588, 0, 1)
		[HDR]_MiddleColor3("burn colorB", Color) = (20.8302, 0.4362345, 0, 1)
		_FinalColor3("final color", Color) = (0.3, 0.3, 0.3, 1)

		//for satisfy srp shader property requirement
		[HideInInSpector]_SkillforGrassDirectionEncodeTex_TexelSize("", Vector) = (0, 0, 0, 0)
		[HideInInSpector]_SkillforGrassTimeEncodeTex_TexelSize("", Vector) = (0, 0, 0, 0)
		[HideInInSpector]_BurnTex_Scale("", Vector) = (0, 0, 0, 0)
		//[HideInInSpector]_MainCharPos("", Vector) = (0, 0, 0, 0)
		//[HideInInSpector]_WorldOffset("", Vector) = (0, 0, 0, 0)
		//[HideInInSpector]_TerrainBasePos("", Vector) = (0, 0, 0, 0)
		[HideInInSpector]_SkillDirection("", int) = 0

	}

	
	SubShader{
		//Tags{ "Queue" = "AlphaTest" "RenderType" = "TransparentCutout_WorldOffset" }
		Tags{ "Queue" = "Transparent" "RenderPipeline" = "LightweightPipeline" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
		Cull Off
		LOD 200

		HLSLINCLUDE
		#define SAMPLE_SH_X2MSCENE
		#define X2M_ENVGI
		#define X2M_GI_BAKED

		#include "/Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
		#include "/Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
		#include "/Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
		#include "/Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl" 
		#include "../../Common/Function/Scene_Common.hlsl"
		#include "FoliageWindField.hlsl"


		TEXTURE2D(_MainTex);							SAMPLER(sampler_MainTex);
		TEXTURE2D(_SpecularMap);						SAMPLER(sampler_SpecularMap);
		TEXTURE2D(_BurnTex);							SAMPLER(sampler_BurnTex);
		TEXTURE2D(_SkillforGrassDirectionEncodeTex);	SAMPLER(sampler_SkillforGrassDirectionEncodeTex);
		TEXTURE2D(_SkillforGrassTimeEncodeTex);			SAMPLER(sampler_SkillforGrassTimeEncodeTex);
		
		sampler2D _Wind;
		float4 _Wind_ST;

		CBUFFER_START(UnityPerMaterial)
			float4 _WorldOffset;
			float4 _MainCharPos;
			float4 _SkillforGrassDirectionEncodeTex_TexelSize;
			float4 _SkillforGrassTimeEncodeTex_TexelSize;
			float4 _MainTex_ST;
			float4 _TimeEditor;
			float4 _BladeHeight;

			half4 _FireColor;
			half4 _InitColor1;
			half4 _MiddleColor1;
			half4 _FinalColor1;

			half4 _InitColor2;
			half4 _MiddleColor2;
			half4 _FinalColor2;

			half4 _InitColor3;
			half4 _MiddleColor3;
			half4 _FinalColor3;

			half4 _Color;
			half4 _GrassRect;

			float _WaveSpeed; 
			float _BladeCurve;
			//for grass animation
			//for grass fly animate direction
			int _SkillDirection;
			//for grass fly settings

			half2 _BurnTex_Scale;
			half _Cutoff;
			half _TexScaleX;

			half _BurnAmount;
			half _BurnRemain;
			half _BurnSpeed;
			half _LineWidth;
			half _TransitColorTime;

			half _WindDirX;
			half _WindDirZ;
			half _WindIntensity;

			half _VelocityInit;
			half _VelocityInitEnd;
			half _VelocityRand;// = _VelocityInitEnd - _VelocityInit;
			half _DirRand;
			half _VelocityFinal;
			half _VelocityDamp;

			half _AnimateNoiseAmp;
			half _AnimateNoiseFreq;

			half _AnimateTime;
			half _AnimateTimeEnd;
			half _AnimateTimeRand;// = _AnimateTimeEnd - _AnimateTime;

			half _Density;

			half _AriseGravity;
			half _AriseGravityEnd;
			half _AriseGRand;// = _AriseGravityEnd - _AriseGravity;

			half _AriseVelocityInit;
			half _AriseVelocityInitEnd;
			half _AriseVRand;// = _AriseVelocityInitEnd - _AriseVelocityInit;
			half _ScaleDamp;

			half _RotVelocityInit;
			half _RotVelocityInitEnd;
			half _RotVelocityDamp;
			half _RotRand;// = _RotVelocityInitEnd - _RotVelocityInit;

			half _TransparentTime;
			
			half _SpecularPower;
			
			half _ShadowCoef;
			half _Gloss;
			//end grass animation
			CBUFFER_END



		half4 GetSkillforGrassDirectionEncode(float3 worldPos)
		{
			half4 ret = half4(0, 0, 0, 0);
			float2 uv = floor(worldPos.xz * 5) - floor(_MainCharPos.xz * 5);
			uv *= _SkillforGrassDirectionEncodeTex_TexelSize.xy;
			uv += float2(0.5, 0.5);

	#if UNITY_UV_STARTS_AT_TOP
			// DX
			uv.y = 1.0 - uv.y;
			// OpenGL, don't need 1.0 - y
	#endif

			if (uv.x >= 0 && uv.x <= 1 && uv.y >= 0 && uv.y <= 1)
			{
				ret = SAMPLE_TEXTURE2D_LOD(_SkillforGrassDirectionEncodeTex, sampler_SkillforGrassDirectionEncodeTex, uv, 0);
			}

			return ret;
		}

		half4 GetSkillforGrassTimeEncode(float3 worldPos)
		{
			half4 ret = half4(0, 0, 0, 0);
			float2 uv = floor(worldPos.xz * 5) - floor(_MainCharPos.xz * 5);
			uv *= _SkillforGrassTimeEncodeTex_TexelSize.xy;
			uv += float2(0.5, 0.5);

	#if UNITY_UV_STARTS_AT_TOP
			// DX
			uv.y = 1.0 - uv.y;
			// OpenGL, don't need 1.0 - y
	#endif
			if (uv.x >= 0 && uv.x <= 1 && uv.y >= 0 && uv.y <= 1)
			{
				ret = SAMPLE_TEXTURE2D_LOD(_SkillforGrassTimeEncodeTex, sampler_SkillforGrassTimeEncodeTex, uv, 0);
			}

			return ret;
		}

		bool ReduceDensity(half density, half key)
		{
			if (key > density)
				return true;
			else
				return false;
		}

		//Goldman fomula
		float3 RotateAboutAxis(float angle, half3 axis, float3 verts) {
			float3 rotVert;
			float cosA = cos(angle);
			float sinA = sin(angle);
			float one_cosA = 1 - cosA;
			float azs = axis.z*sinA;
			//half ays = axis.y*sinA;
			//half ays = 0;
			float axs = axis.x*sinA;
			//half axay1_c = axis.x*axis.y*one_cosA;
			//half axay1_c = 0;
			float axaz1_c = axis.x*axis.z*one_cosA;
			//half ayaz1_c = axis.y*axis.z*one_cosA;
			//half ayaz1_c = 0;
			float axax1_cpc = axis.x*axis.x*one_cosA + cosA;
			//half ayay1_cpc =  axis.y*axis.y*one_cosA + cosA;
			float ayay1_cpc = cosA;
			float azaz1_cpc = axis.z*axis.z*one_cosA + cosA;

			//float3x3 rotMatrix = float3x3(
			//	axax1_cpc, axay1_c - azs, axaz1_c + ays,
			//	axay1_c + azs, ayay1_cpc, ayaz1_c - axs,
			//	axaz1_c - ays, ayaz1_c + axs, azaz1_cpc
			//	);
			float3x3 rotMatrix = float3x3(
				axax1_cpc, -azs, axaz1_c,
				azs, ayay1_cpc, -axs,
				axaz1_c, axs, azaz1_cpc
				);
			return rotVert = mul(rotMatrix, verts);
		}
		ENDHLSL

	Pass{
		Name "ForwardLit"
		Tags{"LightMode" = "LightweightForward" }
		Cull Off
		ZWrite Off
		//ZWrite On
		Blend SrcAlpha OneMinusSrcAlpha

		//ZTest[_x2m_AlphaTestZTestMode]
		
		HLSLPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag

		#define SAMPLE_SH_X2MSCENE
		#define X2M_ENVGI
		#define X2M_GI_BAKED

		//#pragma multi_compile_fwdbase
//#pragma multi_compile DIRECTIONAL 
//#pragma multi_compile LIGHTPROBE_SH
#pragma multi_compile_instancing
		//#pragma shader_feature INSTANCING_ON
		//#pragma multi_compile  _ _SCENE_EDITOR



		//#pragma multi_compile  _ _X2M_SHUOLD_SAMPLE_SH
		//use shadowmask
		//#pragma multi_compile  _ _X2M_SHADOW_ATTENUATION	

		//use terrain sector shadowmask
#define _X2M_WORLDPOS_SHADOW 1

	struct v2f
			{
				float4 pos : SV_POSITION;
				half4 color : COLOR;
				float2 uv : TEXCOORD0;
				float3 worldpos : TEXCOORD1;
				float3 burnGrass : TEXCOORD2;
			#ifdef SHADOW_SWITCH
				half4 posW:TEXCOORD5;
			#endif
				float3 worldNormal : TEXCOORD6;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};

		struct appdata
		{
			float4 vertex : POSITION;
			float4 texcoord : TEXCOORD0;
			float3 normal :NORMAL;
			half4 color : COLOR;
			UNITY_VERTEX_INPUT_INSTANCE_ID
		};

	v2f vert(appdata v)
	{
		v2f o = (v2f)0;
		UNITY_SETUP_INSTANCE_ID(v);
		UNITY_TRANSFER_INSTANCE_ID(v, o);
		o.uv = v.texcoord.xy;
		float3 center = float3(0, 0, 0);
		float4 worldpos = mul(UNITY_MATRIX_M, float4(center, 1));

		float windmutation = 1 - tex2Dlod(_Wind, float4(worldpos.x / 5 + _Time.x, worldpos.z / 5, 0, 0)).b;
		float wavemutation = 1 - tex2Dlod(_Wind, float4((worldpos.x) / _Wind_ST.x + _Time.y*_WaveSpeed, (worldpos.z) / _Wind_ST.x + _Time.y*_WaveSpeed, 0, 0)).r;
		half3 WaveMask = tex2Dlod(_Wind, float4(worldpos.x / 200 - _Time.x, worldpos.z / 200 - _Time.x, 0, 0)).g;
		float BladeCurve = 1 - tex2Dlod(_Wind, float4(worldpos.x, worldpos.z, 0, 0)).b;
		float HeightMap = tex2Dlod(_Wind, float4(worldpos.x / _Wind_ST.w, worldpos.z / _Wind_ST.w, 0, 0)).g;
		v.vertex.xyz += v.vertex.xyz*_BladeHeight.zwz;
		v.vertex.xy += v.vertex.xy*HeightMap*_BladeHeight.xy;

		float4 offset = float4(0, 0, 0, 0);
		offset.xz = sin(3.1416 * _Time.y * v.texcoord.y) * _Wind_ST.z*0.2;
		//offset.x = sin(3.1416 * _Time.y * clamp(v.texcoord.y-0.5, 0, 1)) * _Windvector.w;	
		v.vertex.xz += offset * lerp(1, -1, windmutation);
		v.vertex.xz += sin(windmutation*0.2)*cos(windmutation*0.2)*v.texcoord.y;

		float3 localPos = v.vertex.xyz;
		//---风
		localPos += sin(wavemutation*WaveMask*_Wind_ST.y)*cos(wavemutation*WaveMask*_Wind_ST.y)*v.texcoord.y;
		localPos += BladeCurve * _BladeCurve * smoothstep(0, 1, v.texcoord.y - 0.2); 

		VertexPositionInputs vertexInput = GetVertexPositionInputs(localPos);
		o.pos = vertexInput.positionCS;
		o.worldpos = vertexInput.positionWS;
		o.worldNormal = normalize(v.normal);

		//begin grass animation
		_AnimateTimeRand = _AnimateTimeEnd - _AnimateTime;
		_VelocityRand = _VelocityInitEnd - _VelocityInit;
		_AriseGRand = _AriseGravityEnd - _AriseGravity;
		_AriseVRand = _AriseVelocityInitEnd - _AriseVelocityInit;
		_RotRand = _RotVelocityInitEnd - _RotVelocityInit;
		_RotVelocityInit = (_RotVelocityInitEnd + _RotVelocityInit) / 2;

		half4 SkillforGrassTimeEncode = GetSkillforGrassTimeEncode(worldpos);
		half4 SkillforGrassDirectionEncode = GetSkillforGrassDirectionEncode(worldpos);

		//throw away grass not being cut or has already been cut
		if (SkillforGrassTimeEncode.g < 0.9 || SkillforGrassTimeEncode.r > SkillforGrassTimeEncode.g)
		{
			o.pos.w = 0;
			return o;
		}

		half t = (SkillforGrassTimeEncode.y - 1);
		o.burnGrass.x = t;
		half randomValue = fmod(1024 * (worldpos.x + worldpos.z), 127) / 127;
		o.burnGrass.y = randomValue;
		o.burnGrass.z = SkillforGrassDirectionEncode.b;
		

		if (t >= _AnimateTime + _AnimateTimeRand * o.burnGrass.y)
		{
			o.pos.w = 0;
			return o;
		}

		half3 lightDir = normalize(_MainLightPosition.xyz);
		half3 viewDir = normalize(UnityWorldSpaceViewDir(worldpos.xyz));
		half nl = abs(dot(lightDir, o.worldNormal))*0.5 + 0.5;
		half3 h = normalize(lightDir + viewDir);
		half nh = max(0, dot(o.worldNormal, h));
		half spec = pow(nh, _SpecularPower*128.0) * _Gloss;
		spec = clamp(spec, 0.0, 100.0);
		//o.specular = _MainLightColor.rgb * spec;
		
		return o;
	}

	half4 frag(v2f i) : COLOR
	{	
		UNITY_SETUP_INSTANCE_ID(i);
	//return 1;
		half2 uv = i.uv.xy;
		//half4 c = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv) * _Color;
		half4 c = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_MainTex, sampler_MainTex));

		//return i.burnGrass.z;
		clip(c.a - _Cutoff);

		half t = i.burnGrass.x;
		half alpha = 1;
		_AnimateTimeRand = _AnimateTimeEnd - _AnimateTime;
		half termiTime = _AnimateTime + _AnimateTimeRand * i.burnGrass.y;
		half transitTransparentTime = _TransparentTime * termiTime;

		half4 burnNoise = SAMPLE_TEXTURE2D(_BurnTex, sampler_BurnTex, uv);

		half burnAmount = _BurnAmount + _BurnSpeed * t;
		burnAmount = clamp(burnAmount, 0, 1 - _BurnRemain);

		//burned pixel disappear
		clip(burnNoise.r - burnAmount);

		half4 initColor = _InitColor1;
		half4 middleColor = _MiddleColor1;
		half4 finalColor = _FinalColor1;
		if (i.burnGrass.z < 0.1) //burnColorIndex == 0
		{
			initColor = _InitColor1;
			middleColor = _MiddleColor1;
			finalColor = _FinalColor1;
		}
		else if (i.burnGrass.z > 0.9 && i.burnGrass.z< 1.1) //burnColorIndex == 1
		{
			initColor = _InitColor2;
			middleColor = _MiddleColor2;
			finalColor = _FinalColor2;
		}
		else if (i.burnGrass.z >1.9 && i.burnGrass.z< 2.1) //burnColorIndex = 2
		{
			initColor = _InitColor3;
			middleColor = _MiddleColor3;
			finalColor = _FinalColor3;
		}

		half percent = burnAmount / burnNoise.r - 0.6;
		half lerpEdge = sign(percent - _LineWidth);
		half4 edgeColor = lerp(middleColor, initColor, saturate(lerpEdge));
		half lerpOut = sign(percent);
		
		//random FadeOut
		half tp = (t - transitTransparentTime) / (termiTime - transitTransparentTime);
		if (t > transitTransparentTime) {
			alpha = lerp(1, 0, tp);
		}

		c.rgb = initColor;
		c = lerp(c, edgeColor, saturate(lerpOut));

		half s = (termiTime - t) / termiTime;
		//if (t > _TransitColorTime * termiTime ) 
		{
			c.rgb = lerp(finalColor.rgb, c.rgb, saturate(s));
		}
		return half4(c.rgb, alpha);
	}


		ENDHLSL
	}
		///////////////////////////////////////////////////////////////////////////////////
		//		Pass{
		//		Name "Caster"
		//		Tags{ "LightMode" = "ShadowCaster" }
		//
		//		CGPROGRAM
		//#pragma vertex vert
		//#pragma fragment frag
		//#pragma target 2.0
		//#pragma multi_compile_shadowcaster
		//#pragma multi_compile_instancing // allow instanced shadow pass for most of the shaders
		//
		//#pragma multi_compile  _ _SCENE_EDITOR
		//#include "UnityCG.cginc"
		//#include "../X2mIncludes/X2MDissolvingFade.cginc"
		//#include "../X2mIncludes/FoliageWindField.cginc"
		//
		//		struct v2f {
		//		V2F_SHADOW_CASTER;
		//		float2  uv0 : TEXCOORD1;
		//		float3 posWorld : TEXCOORD4;
		//		half3 weaponClip : TEXCOORD5;
		//		half3 cutGrass: TEXCOORD6;
		//		//UNITY_VERTEX_OUTPUT_STEREO
		//	};
		//
		//	uniform float4 _MainTex_ST;
		//
		//	uniform sampler2D _MainTex;
		//	uniform half _Cutoff;
		//	uniform half4 _Color;
		//
		//	half _TexScaleX;
		//	half4 _GrassRect;
		//
		//
		//	v2f vert(appdata_base v)
		//	{
		//
		//		v2f o;
		//		UNITY_SETUP_INSTANCE_ID(v);
		//		//UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
		//		half3 verts = v.vertex.xyz;
		//		half3x3 RSMat = half3x3(unity_ObjectToWorld[0][0], unity_ObjectToWorld[0][1], unity_ObjectToWorld[0][2],
		//			unity_ObjectToWorld[1][0], unity_ObjectToWorld[1][1], unity_ObjectToWorld[1][2],
		//			unity_ObjectToWorld[2][0], unity_ObjectToWorld[2][1], unity_ObjectToWorld[2][2]
		//			);
		//		verts = mul(RSMat, v.vertex);
		//
		//		//begin grass animation
		//		_AnimateTimeRand = _AnimateTimeEnd - _AnimateTime;
		//		_VelocityRand = _VelocityInitEnd - _VelocityInit;
		//		_AriseGRand = _AriseGravityEnd - _AriseGravity;
		//		_AriseVRand = _AriseVelocityInitEnd - _AriseVelocityInit;
		//		_RotRand = _RotVelocityInitEnd - _RotVelocityInit;
		//		_RotVelocityInit = (_RotVelocityInitEnd + _RotVelocityInit) / 2;
		//
		//		float3 rootPos = float3(unity_ObjectToWorld[0][3], unity_ObjectToWorld[1][3], unity_ObjectToWorld[2][3]);
		//		half3 root = rootPos - _WorldOffset.xyz;
		//		half4 posWorld = half4(root + verts, 1);
		//
		//		half4 SkillforGrassDirectionEncode = GetSkillforGrassDirectionEncode(root);
		//		half t = (SkillforGrassDirectionEncode.x - 1);
		//		o.cutGrass.x = t;
		//		//translation diretion
		//		half2 animGrassDir = half2(0, 0);
		//		half2 skillDir = half2(0, 0);
		//		half2 initDir = half2(0, 0);
		//
		//		//decode skill direction
		//		half temp = SkillforGrassDirectionEncode.a / 100;
		//		half tempx = 100 * frac(temp);
		//		skillDir.x = (tempx - 10) / 10;
		//		half tempz = temp - frac(temp);
		//		skillDir.y = (tempz - 10) / 10;
		//
		//		//decode character position when cast skill 
		//		float2 castSkillPos = float2(_TerrainBasePos.x + SkillforGrassDirectionEncode.g, _TerrainBasePos.z + SkillforGrassDirectionEncode.b);
		//		half2 centerDir = normalize(root.xz - castSkillPos);
		//		//initDir的y分量对应世界坐标的z分量
		//		if (_SkillDirection == 0)
		//		{
		//			initDir = centerDir;
		//		}
		//		else
		//		{
		//			initDir = skillDir;
		//		}
		//		half3 rotAxis = half3(centerDir.x, 0, centerDir.y);
		//		//for direction vary, 
		//		half randomValue = fmod(8191 * rootPos, 127) / 127;
		//		half r1 = fmod(997.0*(randomValue), 211);
		//		r1 = r1 / 211 - 0.5;
		//
		//		half r2 = fmod(524287.0*(randomValue), 127);
		//		r2 = r2 / 127 - 0.5;
		//
		//		half2 grassDir;
		//		grassDir.x = initDir.x + _DirRand * r1;
		//		grassDir.y = initDir.y + _DirRand * r2;
		//		grassDir = normalize(grassDir);
		//
		//		animGrassDir.x = grassDir.x + t * _WindDirX;
		//		animGrassDir.y = grassDir.y + t * _WindDirZ;
		//
		//		////for height vary, increase height noise
		//		half hnoise;
		//		hnoise = fmod(8191.0*rootPos, 131);
		//		hnoise = hnoise / 131;
		//
		//		half hVelocityInitNoise;
		//		hVelocityInitNoise = fmod(8191.0*(hnoise), 11);
		//		hVelocityInitNoise = _AriseVRand * hVelocityInitNoise / 11;
		//
		//		half hGravityNoise;
		//		hGravityNoise = fmod(8191.0*(hVelocityInitNoise), 131);
		//		hGravityNoise = _AriseGRand * hGravityNoise / 131;
		//
		//		half y = (_AriseVelocityInit + hVelocityInitNoise)*t - (_AriseGravity + hGravityNoise)*t*t;
		//
		//		////for velocity vary
		//		half animateVelocity = 0;
		//		half animDeltaDisp = 0;
		//		animateVelocity = (_VelocityInit + _VelocityRand * randomValue) * (1 - 1 / (_VelocityDamp * t + 1)) / t + _VelocityFinal;
		//		animDeltaDisp = (animateVelocity)* t;
		//
		//		if (t >= _AnimateTime + _AnimateTimeRand * randomValue)
		//		{
		//			o.pos.w = 0;
		//			return o;
		//		}
		//
		//		if (SkillforGrassDirectionEncode.x > 0)
		//		{
		//			////scale
		//			//half scale = saturate(1 - _ScaleDamp*t);
		//			//verts *= scale;
		//
		//			////rotate
		//			//half r0 = fmod(127.0*(root.x + root.z), 127);
		//			//r0 = r0 / 127 - 0.5;
		//			//half rotRad = (_RotVelocityInit + r0 * _RotRand) * (1 / (_RotVelocityDamp * t + 1));
		//			//verts = RotateAboutAxis(rotRad * t, rotAxis, verts);
		//
		//			////translate
		//			////X;
		//			//root.x += animDeltaDisp*animGrassDir.x + 0.3*sin(7 * t*r1);
		//			////Z
		//			//root.z += animDeltaDisp*animGrassDir.y + 0.2*sin(5 * t*r2 - 1.57);
		//			////Y
		//			//half r3 = fmod(524287.0*(r2), 127);
		//			//r3 = r3 / 127;
		//			//root.y += y;// +(_AnimateNoiseAmp * r3)*sin(_AnimateNoiseFreq*t*randomValue);// +animateNoiseAmp*sin(2 * animateNoiseFreq*o.pos.x*randomValue);
		//			//posWorld = half4(root + verts, 1);
		//			////增加一些花穗的随风摆动
		//			//posWorld.xyz = CalRunWindPos(posWorld.xyz, v.vertex.y, 1.5);
		//		}
		//
		//		o.posWorld = posWorld;
		//
		//		v.vertex = mul(unity_WorldToObject, posWorld);
		//		TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
		//			//o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
		//			o.uv0 = TRANSFORM_TEX(v.texcoord, _MainTex);
		//
		//		o.posWorld = posWorld;
		//		return o;
		//	}
		//
		//
		//	float4 frag(v2f i) : SV_Target
		//	{
		//		float2 uv = i.uv0.xy;
		//		half4 c = tex2D(_MainTex, uv) * _Color;
		//		clip(c.a - _Cutoff);
		//
		//		SHADOW_CASTER_FRAGMENT(i)
		//	}
		//		ENDCG
		//
		//	}
	}
		//FallBack "Diffuse"
}
