Advertisement
remo9211

L1 BIRP Grass

May 20th, 2024
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Godot GLSL 6.34 KB | Source Code | 0 0
  1. Shader "L01/Grass"{
  2.     Properties{
  3.         [Header(Shading)]
  4.         _TopColor("Top Color", Color) = (1,1,1,1)
  5.         _BottomColor("Bottom Color", Color) = (1,1,1,1)
  6.         _TranslucentGain("Translucent Gain", Range(0,1)) = 0.5
  7.  
  8.         _BendRotationRandom("Bend Rotation Random", Range(0, 1)) = 0.2
  9.  
  10.         _BladeWidth("Blade Width", Float) = 0.05
  11.         _BladeWidthRandom("Blade Width Random", Float) = 0.02
  12.         _BladeHeight("Blade Height", Float) = 0.5
  13.         _BladeHeightRandom("Blade Height Random", Float) = 0.3
  14.  
  15.         _TessellationUniform("Tessellation Uniform", Range(1, 64)) = 1
  16.  
  17.         _WindDistortionMap("Wind Distortion Map", 2D) = "white" {}
  18.         _WindFrequency("Wind Frequency", Vector) = (0.05, 0.05, 0, 0)
  19.  
  20.         _WindStrength("Wind Strength", Float) = 1
  21.  
  22.         _BladeForward("Blade Forward Amount", Float) = 0.38
  23.         _BladeCurve("Blade Curvature Amount", Range(1, 4)) = 2
  24.     }
  25.  
  26. CGINCLUDE
  27.     #include "UnityCG.cginc"
  28.     #include "Autolight.cginc"
  29.     #include "Shaders/CustomTessellation.cginc"
  30.  
  31.     #define BLADE_SEGMENTS 3
  32.  
  33.     float _BendRotationRandom;
  34.  
  35.     float _BladeHeight;
  36.     float _BladeHeightRandom;  
  37.     float _BladeWidth;
  38.     float _BladeWidthRandom;
  39.  
  40.     sampler2D _WindDistortionMap;
  41.     float4 _WindDistortionMap_ST;
  42.  
  43.     float2 _WindFrequency;
  44.     float _WindStrength;
  45.  
  46.     float _BladeForward;
  47.     float _BladeCurve;
  48.  
  49.     struct geometryOutput{
  50.         float4 pos : SV_POSITION;
  51.         float2 uv : TEXCOORD0;
  52.         unityShadowCoord4 _ShadowCoord : TEXCOORD1;
  53.         float3 normal : NORMAL;
  54.     };
  55.     float rand(float3 co){
  56.         return frac(sin(dot(co.xyz, float3(12.9898, 78.233, 53.539))) * 43758.5453);
  57.     }
  58.     float3x3 AngleAxis3x3(float angle, float3 axis){
  59.         float c, s;
  60.         sincos(angle, s, c);
  61.  
  62.         float t = 1 - c;
  63.         float x = axis.x;
  64.         float y = axis.y;
  65.         float z = axis.z;
  66.  
  67.         return float3x3(
  68.             t * x * x + c, t * x * y - s * z, t * x * z + s * y,
  69.             t * x * y + s * z, t * y * y + c, t * y * z - s * x,
  70.             t * x * z - s * y, t * y * z + s * x, t * z * z + c
  71.             );
  72.     }
  73.     geometryOutput VertexOutput(float3 pos, float2 uv, float3 normal){
  74.         geometryOutput o;
  75.         o.pos = UnityObjectToClipPos(pos);
  76.  
  77. #if UNITY_PASS_SHADOWCASTER
  78.         o.pos = UnityApplyLinearShadowBias(o.pos);
  79. #endif
  80.  
  81.         o.uv = uv;
  82.         o._ShadowCoord = ComputeScreenPos(o.pos);
  83.         o.normal = UnityObjectToWorldNormal(normal);
  84.         return o;
  85.     }
  86.  
  87.     geometryOutput GenerateGrassVertex(float3 vertexPosition, float width, float height, float forward, float2 uv, float3x3 transformMatrix){
  88.         float3 tangentPoint = float3(width, forward, height);
  89.  
  90.         float3 localPosition = vertexPosition + mul(transformMatrix, tangentPoint);
  91.  
  92.         float3 tangentNormal = normalize(float3(0, -1, forward));
  93.         float3 localNormal = mul(transformMatrix, tangentNormal);
  94.  
  95.  
  96.         return VertexOutput(localPosition, uv, localNormal);
  97.     }
  98.  
  99.     [maxvertexcount(BLADE_SEGMENTS * 2 + 1)]
  100.     void geo(triangle vertexOutput IN[3], inout TriangleStream<geometryOutput> triStream){ 
  101.         float3 vNormal = IN[0].normal;
  102.         float4 vTangent = IN[0].tangent;
  103.         float3 vBinormal = cross(vNormal, vTangent) * vTangent.w;
  104.  
  105.         float3x3 tangentToLocal = float3x3(
  106.             vTangent.x, vBinormal.x, vNormal.x,
  107.             vTangent.y, vBinormal.y, vNormal.y,
  108.             vTangent.z, vBinormal.z, vNormal.z
  109.             );
  110.         float3 pos = IN[0].vertex;
  111.  
  112.         float3x3 facingRotationMatrix = AngleAxis3x3(rand(pos) * UNITY_TWO_PI, float3(0, 0, 1));
  113.         float3x3 bendRotationMatrix = AngleAxis3x3(rand(pos.zzx) * _BendRotationRandom * UNITY_PI * 0.5, float3(-1, 0, 0));
  114.  
  115.         float2 uv = pos.xz * _WindDistortionMap_ST.xy + _WindDistortionMap_ST.zw + _WindFrequency * _Time.y;
  116.  
  117.         float2 windSample = (tex2Dlod(_WindDistortionMap, float4(uv, 0, 0)).xy * 2 - 1) * _WindStrength;
  118.  
  119.         float3 wind = normalize(float3(windSample.x, windSample.y, 0));
  120.  
  121.         float3x3 windRotation = AngleAxis3x3(UNITY_PI * windSample, wind);
  122.  
  123.         float3x3 transformationMatrix = mul(mul(mul(tangentToLocal, windRotation), facingRotationMatrix), bendRotationMatrix);
  124.  
  125.         float height = (rand(pos.zyx) * 2 - 1) * _BladeHeightRandom + _BladeHeight;
  126.         float width = (rand(pos.xzy) * 2 - 1) * _BladeWidthRandom + _BladeWidth;
  127.  
  128.         float forward = rand(pos.yyz) * _BladeForward;
  129.  
  130.         float3x3 transformationMatrixFacing = mul(tangentToLocal, facingRotationMatrix);
  131.  
  132.         for (int i = 0; i < BLADE_SEGMENTS; i++){
  133.             float t = i / (float)BLADE_SEGMENTS;
  134.             float segmentHeight = height * t;
  135.             float segmentWidth = width * (1 - t);
  136.             float segmentForward = pow(t, _BladeCurve) * forward;
  137.             float3x3 transformMatrix = i == 0 ? transformationMatrixFacing : transformationMatrix;
  138.             triStream.Append(GenerateGrassVertex(pos, segmentWidth, segmentHeight, segmentForward, float2(0, t), transformMatrix));
  139.             triStream.Append(GenerateGrassVertex(pos, -segmentWidth, segmentHeight, segmentForward, float2(1, t), transformMatrix));
  140.         }
  141.  
  142.         triStream.Append(GenerateGrassVertex(pos, 0, height, forward, float2(0.5, 1), transformationMatrix));
  143.     }
  144. ENDCG
  145.     SubShader{
  146.         Cull Off
  147.         Pass{
  148.             Tags{
  149.                 "RenderType" = "Opaque"
  150.                 "LightMode" = "ForwardBase"
  151.             }
  152.  
  153.             CGPROGRAM
  154.             #pragma vertex vert
  155.             #pragma fragment frag
  156.             #pragma hull hull
  157.             #pragma domain domain
  158.             #pragma geometry geo
  159.             #pragma target 4.6
  160.             #pragma multi_compile_fwdbase
  161.            
  162.             #include "Lighting.cginc"
  163.  
  164.             float4 _TopColor;
  165.             float4 _BottomColor;
  166.             float _TranslucentGain;
  167.            
  168.  
  169.             float4 frag (geometryOutput IN, fixed facing : VFACE) : SV_Target{ 
  170.                 float3 normal = facing > 0 ? IN.normal : -IN.normal;
  171.  
  172.  
  173.                 float shadow = SHADOW_ATTENUATION(IN);
  174.                 float NdotL = saturate(saturate(dot(normal, _WorldSpaceLightPos0)) + _TranslucentGain) * shadow;
  175.  
  176.                 float3 ambient = ShadeSH9(float4(normal, 1));
  177.                 float4 lightIntensity = NdotL * _LightColor0 + float4(ambient, 1);
  178.                 float4 col = lerp(_BottomColor, _TopColor * lightIntensity, IN.uv.y);
  179.  
  180.                 return col;
  181.            
  182.                 // return float4(normal * 0.5 + 0.5, 1);
  183.                 // return lerp(_BottomColor, _TopColor, IN.uv.y) * SHADOW_ATTENUATION(IN);
  184.                 // return lerp(_BottomColor, _TopColor, IN.uv.y);
  185.                 // return float4(1, 1, 1, 1);
  186.             }
  187.             ENDCG
  188.         }
  189.  
  190.         Pass{
  191.             Tags{
  192.                 "LightMode" = "ShadowCaster"
  193.             }
  194.             CGPROGRAM
  195.             #pragma vertex vert
  196.             #pragma geometry geo
  197.             #pragma fragment frag
  198.             #pragma hull hull
  199.             #pragma domain domain
  200.             #pragma target 4.6
  201.             #pragma multi_compile_shadowcaster
  202.  
  203.             float4 frag(geometryOutput IN) : SV_Target{
  204.                 SHADOW_CASTER_FRAGMENT(IN)
  205.             }
  206.             ENDCG
  207.         }
  208.     }
  209. }
Tags: grass BIRP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement