Guest User

CustomTesselllation.hlsl

a guest
Dec 23rd, 2020
335
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. // original 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 second 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_patchconstantfunc("patchConstantFunction")]
  64. ControlPoint hull(InputPatch<ControlPoint, 3> patch, uint id : SV_OutputControlPointID)
  65. {
  66.     return patch[id];
  67. }
  68.  
  69.  
  70. // fade tessellation at a distance
  71. float CalcDistanceTessFactor(float4 vertex, float minDist, float maxDist, float tess)
  72. {
  73.     float3 worldPosition = mul(unity_ObjectToWorld, vertex).xyz;
  74.     float dist = distance(worldPosition, GetCameraPositionWS());
  75.     float f = clamp(1.0 - (dist - minDist) / (maxDist - minDist), 0.01, 1.0) * tess;
  76.     return (f);
  77. }
  78.  
  79.  
  80. // tessellation
  81. TessellationFactors patchConstantFunction(InputPatch<ControlPoint, 3> patch)
  82. {
  83.     // values for distance fading the tessellation
  84.     float minDist = 5.0;
  85.     float maxDist = _MaxTessDistance;
  86.  
  87.     TessellationFactors f;
  88.  
  89.     float edge0 = CalcDistanceTessFactor(patch[0].vertex, minDist, maxDist, _Tess);
  90.     float edge1 = CalcDistanceTessFactor(patch[1].vertex, minDist, maxDist, _Tess);
  91.     float edge2 = CalcDistanceTessFactor(patch[2].vertex, minDist, maxDist, _Tess);
  92.  
  93.     // make sure there are no gaps between different tessellated distances, by averaging the edges out.
  94.     f.edge[0] = (edge1 + edge2) / 2;
  95.     f.edge[1] = (edge2 + edge0) / 2;
  96.     f.edge[2] = (edge0 + edge1) / 2;
  97.     f.inside = (edge0 + edge1 + edge2) / 3;
  98.     return f;
  99. }
  100.  
  101. // second vertex, copy to shader
  102. //Varyings vert(Attributes input)
  103. //{
  104. //  Varyings output;
  105. //  // put your vertex manipulation here , ie: input.vertex.xyz += distortiontexture
  106. //  output.vertex = TransformObjectToHClip(input.vertex.xyz);
  107. //  output.color = input.color;
  108. //  output.normal = input.normal;
  109. //  output.uv = input.uv;
  110. //  return output;
  111. //}
  112.  
  113. // domain, copy to shader
  114. //[UNITY_domain("tri")]
  115. //Varyings domain(TessellationFactors factors, OutputPatch<ControlPoint, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
  116. //{
  117. //  Attributes v;
  118. //
  119. //#define Tesselationing(fieldName) v.fieldName = \
  120. //              patch[0].fieldName * barycentricCoordinates.x + \
  121. //              patch[1].fieldName * barycentricCoordinates.y + \
  122. //              patch[2].fieldName * barycentricCoordinates.z;
  123. //
  124. //  Tesselationing(vertex)
  125. //      Tesselationing(uv)
  126. //      Tesselationing(color)
  127. //      Tesselationing(normal)
  128. //
  129. //      return vert(v);
  130. //}
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
Advertisement
Add Comment
Please, Sign In to add comment