Shader "Unlit/GrassShaderLearn" { Properties { _MainTex ("Texture", 2D) = "white" {} _Color ("Color", COlor) = (1,1,1,1) _BottomCol ("Bottom", COlor) = (1,1,1,1) _TopCol ("Top", COlor) = (1,1,1,1) _BendRotationRandom("Bend Rotation Random", Range(0, 1)) = 0.2 _BladeWidth("Blade Width", Float) = 0.05 _BladeWidthRandom("Blade Width Random", Float) = 0.02 _BladeHeight("Blade Height", Float) = 0.5 _BladeHeightRandom("Blade Height Random", Float) = 0.3 _TessellationUniform("Tessellation Uniform", Range(1, 64)) = 1 } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma geometry geo //#pragma hull hull //#pragma domain domain // make fog work #pragma multi_compile_fog #include "UnityCG.cginc" //#include "Shaders/CustomTessellation.cginc" struct appdata { float2 uv : TEXCOORD0; float4 vertex : POSITION; float3 normal : NORMAL; float4 tangent : TANGENT; }; struct v2f { float2 uv : TEXCOORD0; //UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; float3 normal : NORMAL; float4 tangent : TANGENT; }; struct geometryOutput { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; geometryOutput VertexOutput(float3 pos, float2 uv){ geometryOutput o; o.pos = UnityObjectToClipPos(pos); o.uv = uv; return o; } float rand(float3 co){ return frac(sin(dot(co.xyz, float3(12.9898, 78.233, 53.539))) * 43758.5453); } float3x3 AngleAxis3x3(float angle, float3 axis){ float c, s; sincos(angle, s, c); float t = 1 - c; float x = axis.x; float y = axis.y; float z = axis.z; return float3x3( t * x * x + c, t * x * y - s * z, t * x * z + s * y, t * x * y + s * z, t * y * y + c, t * y * z - s * x, t * x * z - s * y, t * y * z + s * x, t * z * z + c ); } sampler2D _MainTex; float4 _MainTex_ST; fixed4 _Color, _BottomCol, _TopCol; float _BendRotationRandom; float _BladeHeight; float _BladeHeightRandom; float _BladeWidth; float _BladeWidthRandom; v2f vert (appdata v) { v2f o; o.vertex = v.vertex;//UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.normal = v.normal; o.tangent = v.tangent; //UNITY_TRANSFER_FOG(o,o.vertex); return o; } [maxvertexcount(3)] void geo(triangle v2f IN[3] : SV_POSITION, inout TriangleStream triStream) { float3 pos = IN[0].vertex; float3 vNormal = IN[0].normal; float4 vTangent = IN[0].tangent; float3 vBinormal = cross (vNormal,vTangent)*vTangent.w; float height = (rand(pos.zyx) * 2 - 1) * _BladeHeightRandom + _BladeHeight; float width = (rand(pos.xzy) * 2 - 1) * _BladeWidthRandom + _BladeWidth; float3x3 tangentToLocal = float3x3( vTangent.x, vBinormal.x, vNormal.x, vTangent.y, vBinormal.y, vNormal.y, vTangent.z, vBinormal.z, vNormal.z ); float3x3 facingRotationMatrix = AngleAxis3x3(rand(pos) * UNITY_TWO_PI, float3(0, 0, 1)); float3x3 bendRotationMatrix = AngleAxis3x3(rand(pos.zzx) * _BendRotationRandom * UNITY_PI * 0.5, float3(-1, 0, 0)); float3x3 transformationMatrix = mul(mul(tangentToLocal, facingRotationMatrix),bendRotationMatrix); triStream.Append(VertexOutput(pos + mul(transformationMatrix, float3(width, 0, 0 )), float2(0 ,0) )); triStream.Append(VertexOutput(pos + mul(transformationMatrix, float3(-width, 0, 0)), float2(1 ,0) )); triStream.Append(VertexOutput(pos + mul(transformationMatrix, float3(height, 0, 1 )), float2(0.5,1) )); } fixed4 frag (geometryOutput i) : SV_Target { // sample the texture fixed4 col = tex2D(_MainTex, i.uv) * _Color; // apply fog //UNITY_APPLY_FOG(i.fogCoord, col); return lerp(_BottomCol, _TopCol, i.uv.y) * _Color; } ENDCG } } }