Guest User

GrassShaderForCompute.shader

a guest
Jul 16th, 2021
2,745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/GrassForCompute"
  2. {
  3.     Properties
  4.     {
  5.         [Toggle(FADE)] _TransparentBottom("Transparency at Bottom", Float) = 0
  6.         _Fade("Fade Multiplier", Range(1,10)) = 6
  7.     }
  8.  
  9.     CGINCLUDE
  10.     #include "UnityCG.cginc"
  11.     #include "Lighting.cginc"
  12.     #include "AutoLight.cginc"
  13.     #pragma multi_compile _SHADOWS_SCREEN
  14.     #pragma multi_compile_fwdbase_fullforwardshadows
  15.     #pragma multi_compile_fog
  16.     #pragma shader_feature FADE
  17.  
  18.     //
  19.     struct DrawVertex
  20.     {
  21.         float3 positionWS; // The position in world space
  22.         float2 uv;
  23.         float3 diffuseColor;
  24.     };
  25.  
  26.     // A triangle on the generated mesh
  27.     struct DrawTriangle
  28.     {
  29.         float3 normalOS;
  30.         DrawVertex vertices[3]; // The three points on the triangle
  31.     };
  32.  
  33.     StructuredBuffer<DrawTriangle> _DrawTriangles;
  34.  
  35.     struct v2f
  36.     {
  37.         float4 pos : SV_POSITION; // Position in clip space
  38.         float2 uv : TEXCOORD0;          // The height of this vertex on the grass blade
  39.         float3 positionWS : TEXCOORD1; // Position in world space
  40.         float3 normalWS : TEXCOORD2;   // Normal vector in world space
  41.         float3 diffuseColor : COLOR;
  42.         LIGHTING_COORDS(3, 4)
  43.         UNITY_FOG_COORDS(5)
  44.     };
  45.  
  46.     // Properties
  47.     float4 _TopTint;
  48.     float4 _BottomTint;
  49.     float _AmbientStrength;
  50.     float _Fade;
  51.  
  52.     // Vertex function
  53.     struct unityTransferVertexToFragmentSucksHack
  54.     {
  55.         float3 vertex : POSITION;
  56.     };
  57.  
  58.     // -- retrieve data generated from compute shader
  59.     v2f vert(uint vertexID : SV_VertexID)
  60.     {
  61.         // Initialize the output struct
  62.         v2f output = (v2f)0;
  63.  
  64.         // Get the vertex from the buffer
  65.         // Since the buffer is structured in triangles, we need to divide the vertexID by three
  66.         // to get the triangle, and then modulo by 3 to get the vertex on the triangle
  67.         DrawTriangle tri = _DrawTriangles[vertexID / 3];
  68.         DrawVertex input = tri.vertices[vertexID % 3];
  69.  
  70.         output.pos = UnityObjectToClipPos(input.positionWS);
  71.         output.positionWS = input.positionWS;
  72.        
  73.         // float3 faceNormal = GetMainLight().direction * tri.normalOS;
  74.         float3 faceNormal = tri.normalOS;
  75.         // output.normalWS = TransformObjectToWorldNormal(faceNormal, true);
  76.         output.normalWS = faceNormal;
  77.        
  78.         output.uv = input.uv;
  79.  
  80.         output.diffuseColor = input.diffuseColor;
  81.  
  82.         // making pointlights work requires v.vertex
  83.         unityTransferVertexToFragmentSucksHack v;
  84.         v.vertex = output.pos;
  85.  
  86.         TRANSFER_VERTEX_TO_FRAGMENT(output);
  87.         UNITY_TRANSFER_FOG(output,  output.pos);
  88.  
  89.         return output;
  90.     }
  91.  
  92.  
  93.    
  94.    
  95.     ENDCG
  96.     SubShader
  97.     {
  98.         Cull Off
  99.         Blend SrcAlpha OneMinusSrcAlpha // for the transparency
  100.         Pass // basic color with directional lights
  101.         {
  102.             Tags
  103.             {              
  104.                 "LightMode" = "ForwardBase"
  105.             }
  106.  
  107.             CGPROGRAM
  108.             #pragma vertex vert
  109.             #pragma fragment frag            
  110.            
  111.             float4 frag(v2f i) : SV_Target
  112.             {
  113.                
  114.                 // take shadow data
  115.                 float shadow = 1;
  116.                 #if defined(SHADOWS_SCREEN)
  117.                     shadow = (SAMPLE_DEPTH_TEXTURE_PROJ(_ShadowMapTexture, UNITY_PROJ_COORD(i._ShadowCoord)).r);
  118.                 #endif         
  119.                 // base color by lerping 2 colors over the UVs
  120.                 float4 baseColor = lerp(_BottomTint , _TopTint , saturate(i.uv.y)) * float4(i.diffuseColor, 1);
  121.                 // multiply with lighting color
  122.                 float4 litColor = (baseColor * _LightColor0);
  123.                 // multiply with vertex color, and shadows
  124.                 float4 final = litColor;
  125.                 final.rgb = litColor * shadow;
  126.                 // add in baseColor when lights turned off
  127.                 final += saturate((1 - shadow) * baseColor * 0.2);
  128.                 // add in ambient color
  129.                 final += (unity_AmbientSky * baseColor * _AmbientStrength);
  130.                
  131.                 // add fog
  132.                 UNITY_APPLY_FOG(i.fogCoord, final);
  133.                 // fade the bottom based on the vertical uvs
  134.                 #if FADE
  135.                     float alpha = lerp(0, 1, saturate(i.uv.y * _Fade));
  136.                     final.a = alpha;
  137.                 #endif
  138.                 return final;              
  139.             }
  140.             ENDCG
  141.         }
  142.  
  143.         Pass
  144.         // point lights
  145.         {
  146.             Tags
  147.             {              
  148.                 "LightMode" = "ForwardAdd"
  149.             }
  150.             Blend OneMinusDstColor One
  151.             ZWrite Off
  152.             Cull Off
  153.  
  154.             CGPROGRAM
  155.             #pragma vertex vert
  156.             #pragma fragment frag                                  
  157.             #pragma multi_compile_fwdadd_fullforwardshadows
  158.  
  159.             float4 frag(v2f i) : SV_Target
  160.             {
  161.                 UNITY_LIGHT_ATTENUATION(atten, i, i.positionWS);
  162.  
  163.                 // base color by lerping 2 colors over the UVs
  164.                 float3 baseColor = lerp(_BottomTint , _TopTint , saturate(i.uv.y)) * i.diffuseColor;
  165.                
  166.                 float3 pointlights = atten * _LightColor0.rgb * baseColor;
  167.                 #if FADE
  168.                     float alpha = lerp(0, 1, saturate(i.uv.y * _Fade));
  169.                     pointlights *= alpha;
  170.                 #endif
  171.  
  172.                 return float4(pointlights, 1);
  173.             }
  174.             ENDCG
  175.         }
  176.  
  177.         Pass // shadow pass
  178.         {
  179.            
  180.             Tags
  181.             {
  182.                 "LightMode" = "ShadowCaster"
  183.             }
  184.  
  185.             CGPROGRAM
  186.             #pragma vertex vert
  187.             #pragma fragment frag
  188.             #pragma multi_compile_shadowcaster
  189.  
  190.             float4 frag(v2f i) : SV_Target
  191.             {
  192.  
  193.                 SHADOW_CASTER_FRAGMENT(i)
  194.             }
  195.             ENDCG
  196.         }
  197.  
  198.  
  199.     }     Fallback "VertexLit"
  200. }
  201.  
Advertisement
Add Comment
Please, Sign In to add comment