Advertisement
Guest User

SnowTessellation.hlsl

a guest
Jul 9th, 2022
2,950
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. struct Varyings
  13. {      
  14.     float3 worldPos : TEXCOORD1;
  15.     float3 normal : NORMAL;
  16.     float4 vertex : SV_POSITION;
  17.     float2 uv : TEXCOORD0;
  18.     float3 viewDir : TEXCOORD3;
  19.     float fogFactor : TEXCOORD4;
  20. };
  21.  
  22. float _Tess;
  23. float _MaxTessDistance;
  24.  
  25. struct TessellationFactors
  26. {
  27.     float edge[3] : SV_TessFactor;
  28.     float inside : SV_InsideTessFactor;
  29. };
  30.  
  31. struct Attributes
  32. {
  33.     float4 vertex : POSITION;
  34.     float3 normal : NORMAL;
  35.     float2 uv : TEXCOORD0;    
  36. };
  37.  
  38. struct ControlPoint
  39. {
  40.     float4 vertex : INTERNALTESSPOS;
  41.     float2 uv : TEXCOORD0;
  42.     float3 normal : NORMAL;  
  43. };
  44.  
  45. [UNITY_domain("tri")]
  46. [UNITY_outputcontrolpoints(3)]
  47. [UNITY_outputtopology("triangle_cw")]
  48. [UNITY_partitioning("fractional_odd")]
  49. [UNITY_patchconstantfunc("patchConstantFunction")]
  50. ControlPoint hull(InputPatch<ControlPoint, 3> patch, uint id : SV_OutputControlPointID)
  51. {
  52.     return patch[id];
  53. }
  54.  
  55. TessellationFactors UnityCalcTriEdgeTessFactors (float3 triVertexFactors)
  56. {
  57.     TessellationFactors tess;
  58.     tess.edge[0] = 0.5 * (triVertexFactors.y + triVertexFactors.z);
  59.     tess.edge[1] = 0.5 * (triVertexFactors.x + triVertexFactors.z);
  60.     tess.edge[2] = 0.5 * (triVertexFactors.x + triVertexFactors.y);
  61.     tess.inside = (triVertexFactors.x + triVertexFactors.y + triVertexFactors.z) / 3.0f;
  62.     return tess;
  63. }
  64.  
  65. float CalcDistanceTessFactor(float4 vertex, float minDist, float maxDist, float tess)
  66. {
  67.                 float3 worldPosition = mul(unity_ObjectToWorld, vertex).xyz;
  68.                 float dist = distance(worldPosition, _WorldSpaceCameraPos);
  69.                 float f = clamp(1.0 - (dist - minDist) / (maxDist - minDist), 0.01, 1.0);
  70.                 return f * tess;
  71. }
  72.  
  73. TessellationFactors DistanceBasedTess(float4 v0, float4 v1, float4 v2, float minDist, float maxDist, float tess)
  74. {
  75.                 float3 f;
  76.                 f.x = CalcDistanceTessFactor(v0, minDist, maxDist, tess);
  77.                 f.y = CalcDistanceTessFactor(v1, minDist, maxDist, tess);
  78.                 f.z = CalcDistanceTessFactor(v2, minDist, maxDist, tess);
  79.  
  80.                 return UnityCalcTriEdgeTessFactors(f);
  81. }
  82.  
  83. uniform float3 _Position;
  84. uniform sampler2D _GlobalEffectRT;
  85. uniform float _OrthographicCamSize;
  86.  
  87. sampler2D  _Noise;
  88. float _NoiseScale, _SnowHeight, _NoiseWeight, _SnowDepth;
  89.  
  90. TessellationFactors patchConstantFunction(InputPatch<ControlPoint, 3> patch)
  91. {
  92.     float minDist = 2.0;
  93.     float maxDist = _MaxTessDistance;
  94.     TessellationFactors f;
  95.     return DistanceBasedTess(patch[0].vertex, patch[1].vertex, patch[2].vertex, minDist, maxDist, _Tess);
  96.    
  97. }
  98.  
  99. float4 GetShadowPositionHClip(Attributes input)
  100. {
  101.     float3 positionWS = TransformObjectToWorld(input.vertex.xyz);
  102.     float3 normalWS = TransformObjectToWorldNormal(input.normal);
  103.  
  104.     float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, 0));
  105.  
  106. #if UNITY_REVERSED_Z
  107.     positionCS.z = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
  108. #else
  109.     positionCS.z = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
  110. #endif
  111.     return positionCS;
  112. }
  113.  
  114. Varyings vert(Attributes input)
  115. {
  116.     Varyings output;
  117.    
  118.     float3 worldPosition = mul(unity_ObjectToWorld, input.vertex).xyz;
  119.     //create local uv
  120.     float2 uv = worldPosition.xz - _Position.xz;
  121.     uv = uv / (_OrthographicCamSize * 2);
  122.     uv += 0.5;
  123.    
  124.     // Effects RenderTexture Reading
  125.     float4 RTEffect = tex2Dlod(_GlobalEffectRT, float4(uv, 0, 0));
  126.     // smoothstep to prevent bleeding
  127.     RTEffect *=  smoothstep(0.99, 0.9, uv.x) * smoothstep(0.99, 0.9,1- uv.x);
  128.     RTEffect *=  smoothstep(0.99, 0.9, uv.y) * smoothstep(0.99, 0.9,1- uv.y);
  129.    
  130.     // worldspace noise texture
  131.     float SnowNoise = tex2Dlod(_Noise, float4(worldPosition.xz * _NoiseScale, 0, 0)).r;
  132.     output.viewDir = SafeNormalize(GetCameraPositionWS() - worldPosition);
  133.  
  134.     // move vertices up where snow is
  135.     input.vertex.xyz += SafeNormalize(input.normal) * saturate(( _SnowHeight) + (SnowNoise * _NoiseWeight)) * saturate(1-(RTEffect.g * _SnowDepth));
  136.  
  137.     // transform to clip space
  138.     #ifdef SHADERPASS_SHADOWCASTER
  139.         output.vertex = GetShadowPositionHClip(input);
  140.     #else
  141.         output.vertex = TransformObjectToHClip(input.vertex.xyz);
  142.     #endif
  143.  
  144.     //outputs
  145.     output.worldPos =  mul(unity_ObjectToWorld, input.vertex).xyz;
  146.     output.normal = input.normal;
  147.     output.uv = input.uv;
  148.     output.fogFactor = ComputeFogFactor(output.vertex.z);
  149.     return output;
  150. }
  151.  
  152. [UNITY_domain("tri")]
  153. Varyings domain(TessellationFactors factors, OutputPatch<ControlPoint, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
  154. {
  155.     Attributes v;
  156.    
  157.     #define Interpolate(fieldName) v.fieldName = \
  158.                 patch[0].fieldName * barycentricCoordinates.x + \
  159.                 patch[1].fieldName * barycentricCoordinates.y + \
  160.                 patch[2].fieldName * barycentricCoordinates.z;
  161.  
  162.     Interpolate(vertex)
  163.     Interpolate(uv)
  164.     Interpolate(normal)
  165.    
  166.     return vert(v);
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement