WavingGrassDepthNormalsPass.hlsl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef UNIVERSAL_WAVING_GRASS_DEPTH_NORMAL_PASSE_INCLUDED
  2. #define UNIVERSAL_WAVING_GRASS_DEPTH_NORMAL_PASSE_INCLUDED
  3. struct GrassVertexDepthNormalInput
  4. {
  5. float4 vertex : POSITION;
  6. float3 normal : NORMAL;
  7. float4 tangent : TANGENT;
  8. half4 color : COLOR;
  9. float2 texcoord : TEXCOORD0;
  10. UNITY_VERTEX_INPUT_INSTANCE_ID
  11. };
  12. struct GrassVertexDepthNormalOutput
  13. {
  14. float2 uv : TEXCOORD0;
  15. half3 normal : TEXCOORD1;
  16. half4 color : TEXCOORD2;
  17. float3 viewDirWS : TEXCOORD3;
  18. float4 clipPos : SV_POSITION;
  19. UNITY_VERTEX_INPUT_INSTANCE_ID
  20. UNITY_VERTEX_OUTPUT_STEREO
  21. };
  22. GrassVertexDepthNormalOutput DepthNormalOnlyVertex(GrassVertexDepthNormalInput v)
  23. {
  24. GrassVertexDepthNormalOutput o = (GrassVertexDepthNormalOutput)0;
  25. UNITY_SETUP_INSTANCE_ID(v);
  26. UNITY_TRANSFER_INSTANCE_ID(v, o);
  27. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  28. // MeshGrass v.color.a: 1 on top vertices, 0 on bottom vertices
  29. // _WaveAndDistance.z == 0 for MeshLit
  30. float waveAmount = v.color.a * _WaveAndDistance.z;
  31. o.color = TerrainWaveGrass(v.vertex, waveAmount, v.color);
  32. VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
  33. o.uv = v.texcoord;
  34. o.normal = TransformObjectToWorldNormal(v.normal);
  35. o.clipPos = vertexInput.positionCS;
  36. o.viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  37. return o;
  38. }
  39. half4 DepthNormalOnlyFragment(GrassVertexDepthNormalOutput input) : SV_TARGET
  40. {
  41. Alpha(SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_MainTex, sampler_MainTex)).a, input.color, _Cutoff);
  42. #if defined(_GBUFFER_NORMALS_OCT)
  43. float3 normalWS = normalize(input.normal);
  44. float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on Nintendo Switch.
  45. float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); // values between [ 0, 1]
  46. half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1]
  47. return half4(packedNormalWS, 0.0);
  48. #else
  49. half3 normalWS = half3(normalize(cross(ddy(input.viewDirWS), ddx(input.viewDirWS))));
  50. return half4(normalWS, 0.0);
  51. #endif
  52. }
  53. #endif