Advertisement
Guest User

SnowTessellation.hlsl

a guest
Nov 28th, 2024
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#ifndef TESSELLATION_CGINC_INCLUDED
  2. //#define TESSELLATION_CGINC_INCLUDED
  3. #if defined(SHADER_API_D3D11) || defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE) || defined(SHADER_API_VULKAN) || defined(SHADER_API_METAL) || defined(SHADER_API_PSSL)
  4.     #define UNITY_CAN_COMPILE_TESSELLATION 1
  5.     #   define UNITY_domain                 domain
  6.     #   define UNITY_partitioning           partitioning
  7.     #   define UNITY_outputtopology         outputtopology
  8.     #   define UNITY_patchconstantfunc      patchconstantfunc
  9.     #   define UNITY_outputcontrolpoints    outputcontrolpoints
  10. #endif
  11.  
  12. // Shadow Casting Light geometric parameters. These variables are used when applying the shadow Normal Bias and are set by UnityEngine.Rendering.Universal.ShadowUtils.SetupShadowCasterConstantBuffer in com.unity.render-pipelines.universal/Runtime/ShadowUtils.cs
  13. // For Directional lights, _LightDirection is used when applying shadow Normal Bias.
  14. // For Spot lights and Point lights, _LightPosition is used to compute the actual light direction because it is different at each shadow caster geometry vertex.
  15. float3 _LightDirection;
  16. float3 _LightPosition;
  17.  
  18. struct Varyings2
  19. {      
  20.     float3 worldPos : TEXCOORD1;
  21.     float3 normal : NORMAL;
  22.     float4 vertex : SV_POSITION;
  23.     float2 uv : TEXCOORD0;
  24.     float3 viewDir : TEXCOORD3;
  25.     float fogFactor : TEXCOORD4;
  26.   float3 tangent : TEXCOORD2; // tangent.x, bitangent.x, normal.x
  27.   float3 bitangent : TEXCOORD5; // tangent.x, bitangent.x, normal.x
  28. };
  29.  
  30. float _Tess;
  31. float _MaxTessDistance;
  32.  
  33. struct TessellationFactors
  34. {
  35.     float edge[3] : SV_TessFactor;
  36.     float inside : SV_InsideTessFactor;
  37. };
  38.  
  39. struct Attributes2
  40. {
  41.     float4 vertex : POSITION;
  42.     float3 normal : NORMAL;
  43.     float2 uv : TEXCOORD0;    
  44.     float4 tangent : TANGENT;
  45. };
  46.  
  47. struct ControlPoint
  48. {
  49.     float4 vertex : INTERNALTESSPOS;
  50.     float2 uv : TEXCOORD0;
  51.     float3 normal : NORMAL;  
  52.     float4 tangent : TANGENT;
  53. };
  54.  
  55. [UNITY_domain("tri")]
  56. [UNITY_outputcontrolpoints(3)]
  57. [UNITY_outputtopology("triangle_cw")]
  58. [UNITY_partitioning("fractional_odd")]
  59. [UNITY_patchconstantfunc("patchConstantFunction")]
  60. ControlPoint hull(InputPatch<ControlPoint, 3> patch, uint id : SV_OutputControlPointID)
  61. {
  62.     return patch[id];
  63. }
  64.  
  65. TessellationFactors UnityCalcTriEdgeTessFactors (float3 triVertexFactors)
  66. {
  67.     TessellationFactors tess;
  68.     tess.edge[0] = 0.5 * (triVertexFactors.y + triVertexFactors.z);
  69.     tess.edge[1] = 0.5 * (triVertexFactors.x + triVertexFactors.z);
  70.     tess.edge[2] = 0.5 * (triVertexFactors.x + triVertexFactors.y);
  71.     tess.inside = (triVertexFactors.x + triVertexFactors.y + triVertexFactors.z) / 3.0f;
  72.     return tess;
  73. }
  74.  
  75. float CalcDistanceTessFactor(float4 vertex, float minDist, float maxDist, float tess)
  76. {
  77.                 float3 worldPosition = mul(unity_ObjectToWorld, vertex).xyz;
  78.                 float dist = distance(worldPosition, _WorldSpaceCameraPos);
  79.                 float f = clamp(1.0 - (dist - minDist) / (maxDist), 0, 1.0);
  80.                
  81.                 return (f * tess) + 1;
  82. }
  83.  
  84. TessellationFactors DistanceBasedTess(float4 v0, float4 v1, float4 v2, float minDist, float maxDist, float tess)
  85. {
  86.                 float3 f;
  87.                 f.x = CalcDistanceTessFactor(v0, minDist, maxDist, tess);
  88.                 f.y = CalcDistanceTessFactor(v1, minDist, maxDist, tess);
  89.                 f.z = CalcDistanceTessFactor(v2, minDist, maxDist, tess);
  90.  
  91.                 return UnityCalcTriEdgeTessFactors(f);
  92. }
  93.  
  94. uniform float3 _Position;
  95. uniform sampler2D _GlobalEffectRT;
  96. uniform float _OrthographicCamSize;
  97.  
  98. sampler2D  _Noise, _Normal;
  99. float _NoiseScale, _SnowHeight, _NoiseWeight, _SnowDepth;
  100.  
  101. TessellationFactors patchConstantFunction(InputPatch<ControlPoint, 3> patch)
  102. {
  103.     float minDist = 2.0;
  104.     float maxDist = _MaxTessDistance;
  105.     TessellationFactors f;
  106.     return DistanceBasedTess(patch[0].vertex, patch[1].vertex, patch[2].vertex, minDist, maxDist, _Tess);
  107.    
  108. }
  109.  
  110. float4 GetShadowPositionHClip2(Attributes2 input)
  111. {
  112.     float3 positionWS = TransformObjectToWorld(input.vertex.xyz);
  113.     float3 normalWS = (input.normal);
  114.  
  115. #if _CASTING_PUNCTUAL_LIGHT_SHADOW
  116.     float3 lightDirectionWS = normalize(_LightPosition - positionWS);
  117. #else
  118.     float3 lightDirectionWS = _LightDirection;
  119. #endif
  120.  
  121.     float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, 1));
  122.     positionCS = ApplyShadowClamping(positionCS);
  123.     return positionCS;
  124. }
  125.  
  126.  
  127. Varyings2 vert(Attributes2 input)
  128. {
  129.     Varyings2 output;
  130.    
  131.     float3 worldPosition = mul(unity_ObjectToWorld, input.vertex).xyz;
  132.     //create local uv
  133.     float2 uv = worldPosition.xz - _Position.xz;
  134.     uv = uv / (_OrthographicCamSize * 2);
  135.     uv += 0.5;
  136.    
  137.     // Effects RenderTexture Reading
  138.     float4 RTEffect = tex2Dlod(_GlobalEffectRT, float4(uv, 0, 0));
  139.     // smoothstep to prevent bleeding
  140.  
  141.     RTEffect *=  smoothstep(0.99, 0.9, uv.x) * smoothstep(0.99, 0.9,1- uv.x);
  142. RTEffect *=  smoothstep(0.99, 0.9, uv.y) * smoothstep(0.99, 0.9,1- uv.y);
  143.    
  144.     // worldspace noise texture
  145.     float SnowNoise = tex2Dlod(_Noise, float4(worldPosition.xz * _NoiseScale, 0, 0)).r;
  146.  
  147.     // move vertices up where snow is
  148.     input.vertex.xyz += SafeNormalize(input.normal) * saturate(( _SnowHeight) + (SnowNoise * _NoiseWeight)) * saturate(1-(RTEffect.g * _SnowDepth));
  149. //input.vertex.xyz +=  step(0.5,RTEffect.g) * input.normal ;
  150.  
  151.     // transform to clip space
  152.     #ifdef SHADERPASS_SHADOWCASTER
  153.         output.vertex = GetShadowPositionHClip(input);
  154.     #else
  155.         output.vertex = TransformObjectToHClip(input.vertex.xyz);
  156.     #endif
  157.  
  158.     //outputs
  159.     output.worldPos =  mul(unity_ObjectToWorld, input.vertex).xyz;
  160.  
  161.     output.viewDir = SafeNormalize(GetCameraPositionWS() - output.worldPos);
  162.     output.normal = input.normal;
  163.  
  164.     // output the tangent
  165.     output.tangent = input.tangent;
  166.     output.bitangent = cross(input.tangent, output.normal);
  167.    
  168.     output.uv = input.uv;
  169.     output.fogFactor = ComputeFogFactor(output.vertex.z);
  170.     return output;
  171. }
  172.  
  173. [UNITY_domain("tri")]
  174. Varyings2 domain(TessellationFactors factors, OutputPatch<ControlPoint, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
  175. {
  176.     Attributes2 v;
  177.    
  178.     #define Interpolate(fieldName) v.fieldName = \
  179.                 patch[0].fieldName * barycentricCoordinates.x + \
  180.                 patch[1].fieldName * barycentricCoordinates.y + \
  181.                 patch[2].fieldName * barycentricCoordinates.z;
  182.  
  183.     Interpolate(vertex)
  184.     Interpolate(uv)
  185.     Interpolate(normal)
  186.     Interpolate(tangent)
  187.    
  188.     return vert(v);
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement