Guest User

CustomTessellation.hlsl

a guest
Mar 19th, 2021
5,581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #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)
  3. #define UNITY_CAN_COMPILE_TESSELLATION 1
  4. #   define UNITY_domain                 domain
  5. #   define UNITY_partitioning           partitioning
  6. #   define UNITY_outputtopology         outputtopology
  7. #   define UNITY_patchconstantfunc      patchconstantfunc
  8. #   define UNITY_outputcontrolpoints    outputcontrolpoints
  9. #endif
  10.  
  11.  
  12.  
  13. // The structure definition defines which variables it contains.
  14. // This example uses the Attributes structure as an input structure in
  15. // the vertex shader.
  16.  
  17. // vertex to fragment struct
  18. struct Varyings
  19. {
  20.     float4 color : COLOR;
  21.     float3 normal : NORMAL;
  22.     float4 vertex : SV_POSITION;
  23.     float2 uv : TEXCOORD0;
  24. };
  25.  
  26.  
  27. // tessellation data
  28. struct TessellationFactors
  29. {
  30.     float edge[3] : SV_TessFactor;
  31.     float inside : SV_InsideTessFactor;
  32. };
  33.  
  34. // Extra vertex struct
  35. struct ControlPoint
  36. {
  37.     float4 vertex : INTERNALTESSPOS;
  38.     float2 uv : TEXCOORD0;
  39.     float4 color : COLOR;
  40.     float3 normal : NORMAL;
  41. };
  42.  
  43. // the original vertex struct
  44. struct Attributes
  45. {
  46.     float4 vertex : POSITION;
  47.     float3 normal : NORMAL;
  48.     float2 uv : TEXCOORD0;
  49.     float4 color : COLOR;
  50.  
  51. };
  52.  
  53. // tessellation variables, add these to your shader properties
  54. float _Tess;
  55. float _MaxTessDistance;
  56.  
  57. // info so the GPU knows what to do (triangles) and how to set it up , clockwise, fractional division
  58. // hull takes the original vertices and outputs more
  59. [UNITY_domain("tri")]
  60. [UNITY_outputcontrolpoints(3)]
  61. [UNITY_outputtopology("triangle_cw")]
  62. [UNITY_partitioning("fractional_odd")]
  63. //[UNITY_partitioning("fractional_even")]
  64. //[UNITY_partitioning("pow2")]
  65. //[UNITY_partitioning("integer")]
  66. [UNITY_patchconstantfunc("patchConstantFunction")]
  67. ControlPoint hull(InputPatch<ControlPoint, 3> patch, uint id : SV_OutputControlPointID)
  68. {
  69.     return patch[id];
  70. }
  71.  
  72.  
  73. // fade tessellation at a distance
  74. float CalcDistanceTessFactor(float4 vertex, float minDist, float maxDist, float tess)
  75. {
  76.     float3 worldPosition = TransformObjectToWorld(vertex.xyz);
  77.     float dist = distance(worldPosition, _WorldSpaceCameraPos);
  78.     float f = clamp(1.0 - (dist - minDist) / (maxDist - minDist), 0.01, 1.0) * tess;
  79.     return (f);
  80. }
  81.  
  82.  
  83. // tessellation
  84. TessellationFactors patchConstantFunction(InputPatch<ControlPoint, 3> patch)
  85. {
  86.     // values for distance fading the tessellation
  87.     float minDist = 5.0;
  88.     float maxDist = _MaxTessDistance;
  89.  
  90.     TessellationFactors f;
  91.  
  92.     float edge0 = CalcDistanceTessFactor(patch[0].vertex, minDist, maxDist, _Tess);
  93.     float edge1 = CalcDistanceTessFactor(patch[1].vertex, minDist, maxDist, _Tess);
  94.     float edge2 = CalcDistanceTessFactor(patch[2].vertex, minDist, maxDist, _Tess);
  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. // second vertex, copy to shader
  105. //Varyings vert(Attributes input)
  106. //{
  107. //  Varyings output;
  108. //  // put your vertex manipulation here , ie: input.vertex.xyz += distortiontexture
  109. //  output.vertex = TransformObjectToHClip(input.vertex.xyz);
  110. //  output.color = input.color;
  111. //  output.normal = input.normal;
  112. //  output.uv = input.uv;
  113. //  return output;
  114. //}
  115.  
  116. // domain, copy to shader
  117. //[UNITY_domain("tri")]
  118. //Varyings domain(TessellationFactors factors, OutputPatch<ControlPoint, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
  119. //{
  120. //  Attributes v;
  121. //
  122. //#define Tesselationing(fieldName) v.fieldName = \
  123. //              patch[0].fieldName * barycentricCoordinates.x + \
  124. //              patch[1].fieldName * barycentricCoordinates.y + \
  125. //              patch[2].fieldName * barycentricCoordinates.z;
  126. //
  127. //  Tesselationing(vertex)
  128. //      Tesselationing(uv)
  129. //      Tesselationing(color)
  130. //      Tesselationing(normal)
  131. //
  132. //      return vert(v);
  133. //}
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment