Advertisement
Guest User

SnowTessellation.hlsl

a guest
Mar 11th, 2021
474
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; // world position built-in value              
  15.     float4 color : COLOR;
  16.     float3 normal : NORMAL;
  17.     float4 vertex : SV_POSITION;
  18.     float2 uv : TEXCOORD0;
  19.     float4 screenPos : TEXCOORD2;
  20.     float3 viewDir : TEXCOORD3;
  21.     float fogFactor : TEXCOORD5;
  22.     float4 shadowCoord              : TEXCOORD7;
  23.  
  24.  
  25. };
  26.  
  27. float _Tess;
  28. float _MaxTessDistance;
  29.  
  30. struct TessellationFactors
  31. {
  32.     float edge[3] : SV_TessFactor;
  33.     float inside : SV_InsideTessFactor;
  34. };
  35.  
  36. struct Attributes
  37. {
  38.     float4 vertex : POSITION;
  39.     float3 normal : NORMAL;
  40.     float2 uv : TEXCOORD0;
  41.     float4 color : COLOR;
  42.    
  43. };
  44.  
  45. struct ControlPoint
  46. {
  47.     float4 vertex : INTERNALTESSPOS;
  48.     float2 uv : TEXCOORD0;
  49.     float4 color : COLOR;
  50.     float3 normal : NORMAL;
  51.  
  52. };
  53.  
  54. [UNITY_domain("tri")]
  55. [UNITY_outputcontrolpoints(3)]
  56. [UNITY_outputtopology("triangle_cw")]
  57. [UNITY_partitioning("integer")]
  58. [UNITY_patchconstantfunc("patchConstantFunction")]
  59. ControlPoint hull(InputPatch<ControlPoint, 3> patch, uint id : SV_OutputControlPointID)
  60. {
  61.     return patch[id];
  62. }
  63.  
  64.  
  65. float ColorCalcDistanceTessFactor(float4 vertex, float minDist, float maxDist, float tess, float4 color)
  66. {
  67.     float3 worldPosition = mul(unity_ObjectToWorld, vertex).xyz;
  68.     float dist = distance(worldPosition, GetCameraPositionWS());
  69.     float f = clamp(1.0 - (dist - minDist) / (maxDist - minDist), 0.01, 1.0);
  70.   // no tessellation on no red vertex colors
  71.     if (color.r < 0.1)
  72.     {
  73.         f = 0.01;
  74.     }
  75.    
  76.     return f * tess;
  77. }
  78.  
  79. uniform float3 _Position;
  80. uniform sampler2D _GlobalEffectRT;
  81. uniform float _OrthographicCamSize;
  82.  
  83. sampler2D _Mask, _Noise;
  84. float _NoiseScale, _SnowHeight, _NoiseWeight, _SnowDepth;
  85.  
  86. TessellationFactors patchConstantFunction(InputPatch<ControlPoint, 3> patch)
  87. {
  88.     float minDist = 5.0;
  89.     float maxDist = _MaxTessDistance;
  90.     TessellationFactors f;
  91.    
  92.     float edge0 = ColorCalcDistanceTessFactor(patch[0].vertex, minDist, maxDist, _Tess, patch[0].color);
  93.     float edge1 = ColorCalcDistanceTessFactor(patch[1].vertex, minDist, maxDist, _Tess, patch[1].color);
  94.     float edge2 = ColorCalcDistanceTessFactor(patch[2].vertex, minDist, maxDist, _Tess, patch[2].color);
  95.    
  96.       // make sure there are no gaps between different tessellated distances, by averaging the edges out.
  97.     f.edge[0] = (edge1 + edge2) / 2;
  98.     f.edge[1] = (edge2 + edge0) / 2;
  99.     f.edge[2] = (edge0 + edge1) / 2;
  100.     f.inside = (edge0 + edge1 + edge2) / 3;
  101.     return f;
  102. }
  103.  
  104. float4 GetShadowPositionHClip(Attributes input)
  105. {
  106.     float3 positionWS = TransformObjectToWorld(input.vertex.xyz);
  107.     float3 normalWS = TransformObjectToWorldNormal(input.normal);
  108. #if _CASTING_PUNCTUAL_LIGHT_SHADOW
  109.     float3 lightDirectionWS = normalize(GetMainLight().position - positionWS);
  110. #else
  111.     float3 lightDirectionWS = GetMainLight().direction;
  112. #endif
  113.     float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, 0));
  114.    
  115. #if UNITY_REVERSED_Z
  116.     positionCS.z = min(positionCS.z, UNITY_NEAR_CLIP_VALUE);
  117. #else
  118.     positionCS.z = max(positionCS.z, UNITY_NEAR_CLIP_VALUE);
  119. #endif
  120.     return positionCS;
  121. }
  122.  
  123. Varyings vert(Attributes input)
  124. {
  125.     Varyings output;
  126.  
  127.     float3 worldPosition = mul(unity_ObjectToWorld, input.vertex).xyz;
  128.      // Effects RenderTexture Reading
  129.     float2 uv = worldPosition.xz - _Position.xz;
  130.     uv = uv / (_OrthographicCamSize * 2);
  131.     uv += 0.5;
  132.    
  133.     // Mask to prevent bleeding
  134.     float mask = tex2Dlod(_Mask, float4(uv, 0, 0)).a;
  135.     float4 RTEffect = tex2Dlod(_GlobalEffectRT, float4(uv, 0, 0));
  136.     RTEffect *= mask;
  137.                
  138.     float SnowNoise = tex2Dlod(_Noise, float4(worldPosition.xz * _NoiseScale * 5, 0, 0)).r;
  139.     output.viewDir = SafeNormalize(GetCameraPositionWS() - worldPosition);
  140.     // move vertices up where snow is
  141.     input.vertex.xyz += normalize(input.normal) * (saturate((input.color.r * _SnowHeight) + (SnowNoise * _NoiseWeight * input.color.r)));
  142.      // move down where there is a trail from the render texture particles
  143.     input.vertex.xyz -= normalize(input.normal) * saturate(RTEffect.g * saturate(input.color.r)) * _SnowDepth;
  144.    
  145.     VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);
  146.    
  147.     float3 offsetvertices = mul(unity_ObjectToWorld, float4(input.vertex.xyz, 1));
  148.     output.worldPos = vertexInput.positionWS;
  149. #ifdef SHADERPASS_SHADOWCASTER
  150.         output.vertex = GetShadowPositionHClip(input);
  151. #else
  152.     output.vertex = GetShadowPositionHClip(input);
  153.    // output.vertex = TransformWorldToHClip(offsetvertices);
  154. #endif
  155.    
  156.    // output.vertex = GetShadowPositionHClip(input);
  157.     float4 clipvertex = output.vertex / output.vertex.w;
  158.     output.screenPos = ComputeScreenPos(clipvertex);
  159.  
  160.     output.shadowCoord = TransformWorldToShadowCoord(mul(unity_ObjectToWorld, input.vertex).xyz);
  161.     //output.shadowCoord = GetShadowCoord(vertexInput);
  162.     // vertex color
  163.     output.color = input.color;
  164.     // adding some noise to the normals, and the path indent
  165.     output.normal = saturate(input.normal * SnowNoise);
  166.     output.normal.y += (RTEffect.g * input.color.r * 0.4);
  167.     output.uv = input.uv;
  168.    
  169.     float fogFactor = ComputeFogFactor(output.vertex.z);
  170.     output.fogFactor = fogFactor;
  171.    
  172.     return output;
  173. }
  174.  
  175.  
  176.  
  177.  
  178. [UNITY_domain("tri")]
  179.         Varyings domain(TessellationFactors factors, OutputPatch<ControlPoint, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
  180. {
  181.     Attributes v;
  182.  
  183. #define Tesselationing(fieldName) v.fieldName = \
  184.         patch[0].fieldName * barycentricCoordinates.x + \
  185.         patch[1].fieldName * barycentricCoordinates.y + \
  186.         patch[2].fieldName * barycentricCoordinates.z;
  187.      
  188.     Tesselationing(vertex)
  189.     Tesselationing(uv)
  190.     Tesselationing(color)
  191.     Tesselationing(normal)
  192.  
  193.     return vert(v);
  194. }
  195.  
  196.  
  197.    
  198.  
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement