WavingGrassPasses.hlsl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #ifndef UNIVERSAL_WAVING_GRASS_PASSES_INCLUDED
  2. #define UNIVERSAL_WAVING_GRASS_PASSES_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
  6. struct GrassVertexInput
  7. {
  8. float4 vertex : POSITION;
  9. float3 normal : NORMAL;
  10. float4 tangent : TANGENT;
  11. half4 color : COLOR;
  12. float2 texcoord : TEXCOORD0;
  13. float2 lightmapUV : TEXCOORD1;
  14. UNITY_VERTEX_INPUT_INSTANCE_ID
  15. };
  16. struct GrassVertexOutput
  17. {
  18. float2 uv : TEXCOORD0;
  19. DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
  20. float4 posWSShininess : TEXCOORD2; // xyz: posWS, w: Shininess * 128
  21. half3 normal : TEXCOORD3;
  22. half3 viewDir : TEXCOORD4;
  23. half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light
  24. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  25. float4 shadowCoord : TEXCOORD6;
  26. #endif
  27. half4 color : TEXCOORD7;
  28. float4 clipPos : SV_POSITION;
  29. UNITY_VERTEX_INPUT_INSTANCE_ID
  30. UNITY_VERTEX_OUTPUT_STEREO
  31. };
  32. void InitializeInputData(GrassVertexOutput input, out InputData inputData)
  33. {
  34. inputData = (InputData)0;
  35. inputData.positionWS = input.posWSShininess.xyz;
  36. half3 viewDirWS = input.viewDir;
  37. #if SHADER_HINT_NICE_QUALITY
  38. viewDirWS = SafeNormalize(viewDirWS);
  39. #endif
  40. inputData.normalWS = NormalizeNormalPerPixel(input.normal);
  41. inputData.viewDirectionWS = viewDirWS;
  42. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  43. inputData.shadowCoord = input.shadowCoord;
  44. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  45. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  46. #else
  47. inputData.shadowCoord = float4(0, 0, 0, 0);
  48. #endif
  49. #if defined(_FOG_FRAGMENT)
  50. float clipZ = input.clipPos.z;
  51. #if !UNITY_REVERSED_Z
  52. clipZ = lerp(UNITY_NEAR_CLIP_VALUE, 1, clipZ); // OpenGL NDC, -1 < z < 1
  53. #endif
  54. clipZ *= input.clipPos.w;
  55. inputData.fogCoord = ComputeFogFactor(clipZ);
  56. #else
  57. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  58. #endif
  59. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  60. #if defined(DYNAMICLIGHTMAP_ON)
  61. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, NOT_USED, input.vertexSH, inputData.normalWS);
  62. #else
  63. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  64. #endif
  65. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.clipPos);
  66. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  67. #if defined(DEBUG_DISPLAY)
  68. #if defined(DYNAMICLIGHTMAP_ON)
  69. inputData.staticLightmapUV = input.lightmapUV;
  70. #elif defined(LIGHTMAP_ON)
  71. inputData.staticLightmapUV = input.lightmapUV;
  72. #else
  73. inputData.vertexSH = input.vertexSH;
  74. #endif
  75. #endif
  76. }
  77. void InitializeVertData(GrassVertexInput input, inout GrassVertexOutput vertData)
  78. {
  79. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);
  80. vertData.uv = input.texcoord;
  81. vertData.posWSShininess.xyz = vertexInput.positionWS;
  82. vertData.posWSShininess.w = 32;
  83. vertData.clipPos = vertexInput.positionCS;
  84. vertData.viewDir = GetCameraPositionWS() - vertexInput.positionWS;
  85. #if !SHADER_QUALITY_NICE_HINT
  86. vertData.viewDir = SafeNormalize(vertData.viewDir);
  87. #endif
  88. vertData.normal = TransformObjectToWorldNormal(input.normal);
  89. // We either sample GI from lightmap or SH.
  90. // Lightmap UV and vertex SH coefficients use the same interpolator ("float2 lightmapUV" for lightmap or "half3 vertexSH" for SH)
  91. // see DECLARE_LIGHTMAP_OR_SH macro.
  92. // The following funcions initialize the correct variable with correct data
  93. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, vertData.lightmapUV);
  94. OUTPUT_SH(vertData.normal, vertData.vertexSH);
  95. half3 vertexLight = VertexLighting(vertexInput.positionWS, vertData.normal.xyz);
  96. #if defined(_FOG_FRAGMENT)
  97. half fogFactor = 0;
  98. #else
  99. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  100. #endif
  101. vertData.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  102. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  103. vertData.shadowCoord = GetShadowCoord(vertexInput);
  104. #endif
  105. }
  106. ///////////////////////////////////////////////////////////////////////////////
  107. // Vertex and Fragment functions //
  108. ///////////////////////////////////////////////////////////////////////////////
  109. // Grass: appdata_full usage
  110. // color - .xyz = color, .w = wave scale
  111. // normal - normal
  112. // tangent.xy - billboard extrusion
  113. // texcoord - UV coords
  114. // texcoord1 - 2nd UV coords
  115. GrassVertexOutput WavingGrassVert(GrassVertexInput v)
  116. {
  117. GrassVertexOutput o = (GrassVertexOutput)0;
  118. UNITY_SETUP_INSTANCE_ID(v);
  119. UNITY_TRANSFER_INSTANCE_ID(v, o);
  120. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  121. // MeshGrass v.color.a: 1 on top vertices, 0 on bottom vertices
  122. // _WaveAndDistance.z == 0 for MeshLit
  123. float waveAmount = v.color.a * _WaveAndDistance.z;
  124. o.color = TerrainWaveGrass (v.vertex, waveAmount, v.color);
  125. InitializeVertData(v, o);
  126. return o;
  127. }
  128. GrassVertexOutput WavingGrassBillboardVert(GrassVertexInput v)
  129. {
  130. GrassVertexOutput o = (GrassVertexOutput)0;
  131. UNITY_SETUP_INSTANCE_ID(v);
  132. UNITY_TRANSFER_INSTANCE_ID(v, o);
  133. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  134. TerrainBillboardGrass (v.vertex, v.tangent.xy);
  135. // wave amount defined by the grass height
  136. float waveAmount = v.tangent.y;
  137. o.color = TerrainWaveGrass (v.vertex, waveAmount, v.color);
  138. InitializeVertData(v, o);
  139. return o;
  140. }
  141. inline void InitializeSimpleLitSurfaceData(GrassVertexOutput input, out SurfaceData outSurfaceData)
  142. {
  143. half4 diffuseAlpha = SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_MainTex, sampler_MainTex));
  144. half3 diffuse = diffuseAlpha.rgb * input.color.rgb;
  145. half alpha = diffuseAlpha.a;
  146. AlphaDiscard(alpha, _Cutoff);
  147. alpha *= input.color.a;
  148. outSurfaceData = (SurfaceData)0;
  149. outSurfaceData.alpha = alpha;
  150. outSurfaceData.albedo = diffuse;
  151. outSurfaceData.metallic = 0.0; // unused
  152. outSurfaceData.specular = 0.1;// SampleSpecularSmoothness(uv, diffuseAlpha.a, _SpecColor, TEXTURE2D_ARGS(_SpecGlossMap, sampler_SpecGlossMap));
  153. outSurfaceData.smoothness = input.posWSShininess.w;
  154. outSurfaceData.normalTS = 0.0; // unused
  155. outSurfaceData.occlusion = 1.0;
  156. outSurfaceData.emission = 0.0;
  157. }
  158. // Used for StandardSimpleLighting shader
  159. #ifdef TERRAIN_GBUFFER
  160. FragmentOutput LitPassFragmentGrass(GrassVertexOutput input)
  161. #else
  162. half4 LitPassFragmentGrass(GrassVertexOutput input) : SV_Target
  163. #endif
  164. {
  165. UNITY_SETUP_INSTANCE_ID(input);
  166. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  167. SurfaceData surfaceData;
  168. InitializeSimpleLitSurfaceData(input, surfaceData);
  169. InputData inputData;
  170. InitializeInputData(input, inputData);
  171. SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv, _MainTex);
  172. #ifdef TERRAIN_GBUFFER
  173. half4 color = half4(inputData.bakedGI * surfaceData.albedo + surfaceData.emission, surfaceData.alpha);
  174. return SurfaceDataToGbuffer(surfaceData, inputData, color.rgb, kLightingSimpleLit);
  175. #else
  176. half4 color = UniversalFragmentBlinnPhong(inputData, surfaceData);
  177. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  178. return color;
  179. #endif
  180. };
  181. struct GrassVertexDepthOnlyInput
  182. {
  183. float4 vertex : POSITION;
  184. float4 tangent : TANGENT;
  185. half4 color : COLOR;
  186. float2 texcoord : TEXCOORD0;
  187. UNITY_VERTEX_INPUT_INSTANCE_ID
  188. };
  189. struct GrassVertexDepthOnlyOutput
  190. {
  191. float2 uv : TEXCOORD0;
  192. half4 color : TEXCOORD1;
  193. float4 clipPos : SV_POSITION;
  194. UNITY_VERTEX_INPUT_INSTANCE_ID
  195. UNITY_VERTEX_OUTPUT_STEREO
  196. };
  197. void InitializeVertData(GrassVertexDepthOnlyInput input, inout GrassVertexDepthOnlyOutput vertData)
  198. {
  199. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);
  200. vertData.uv = input.texcoord;
  201. vertData.clipPos = vertexInput.positionCS;
  202. }
  203. GrassVertexDepthOnlyOutput DepthOnlyVertex(GrassVertexDepthOnlyInput v)
  204. {
  205. GrassVertexDepthOnlyOutput o = (GrassVertexDepthOnlyOutput)0;
  206. UNITY_SETUP_INSTANCE_ID(v);
  207. UNITY_TRANSFER_INSTANCE_ID(v, o);
  208. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  209. // MeshGrass v.color.a: 1 on top vertices, 0 on bottom vertices
  210. // _WaveAndDistance.z == 0 for MeshLit
  211. float waveAmount = v.color.a * _WaveAndDistance.z;
  212. o.color = TerrainWaveGrass(v.vertex, waveAmount, v.color);
  213. InitializeVertData(v, o);
  214. return o;
  215. }
  216. half4 DepthOnlyFragment(GrassVertexDepthOnlyOutput input) : SV_TARGET
  217. {
  218. Alpha(SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_MainTex, sampler_MainTex)).a, input.color, _Cutoff);
  219. return 0;
  220. }
  221. #endif