No Description

terrain.tesc 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. layout(set = 0, binding = 0) uniform UBO
  5. {
  6. mat4 projection;
  7. mat4 modelview;
  8. vec4 lightPos;
  9. vec4 frustumPlanes[6];
  10. float displacementFactor;
  11. float tessellationFactor;
  12. vec2 viewportDim;
  13. float tessellatedEdgeSize;
  14. } ubo;
  15. layout(set = 0, binding = 1) uniform sampler2D samplerHeight;
  16. layout (vertices = 4) out;
  17. layout (location = 0) in vec3 inNormal[];
  18. layout (location = 1) in vec2 inUV[];
  19. layout (location = 0) out vec3 outNormal[4];
  20. layout (location = 1) out vec2 outUV[4];
  21. // Calculate the tessellation factor based on screen space
  22. // dimensions of the edge
  23. float screenSpaceTessFactor(vec4 p0, vec4 p1)
  24. {
  25. // Calculate edge mid point
  26. vec4 midPoint = 0.5 * (p0 + p1);
  27. // Sphere radius as distance between the control points
  28. float radius = distance(p0, p1) / 2.0;
  29. // View space
  30. vec4 v0 = ubo.modelview * midPoint;
  31. // Project into clip space
  32. vec4 clip0 = (ubo.projection * (v0 - vec4(radius, vec3(0.0))));
  33. vec4 clip1 = (ubo.projection * (v0 + vec4(radius, vec3(0.0))));
  34. // Get normalized device coordinates
  35. clip0 /= clip0.w;
  36. clip1 /= clip1.w;
  37. // Convert to viewport coordinates
  38. clip0.xy *= ubo.viewportDim;
  39. clip1.xy *= ubo.viewportDim;
  40. // Return the tessellation factor based on the screen size
  41. // given by the distance of the two edge control points in screen space
  42. // and a reference (min.) tessellation size for the edge set by the application
  43. return clamp(distance(clip0, clip1) / ubo.tessellatedEdgeSize * ubo.tessellationFactor, 1.0, 64.0);
  44. }
  45. // Checks the current's patch visibility against the frustum using a sphere check
  46. // Sphere radius is given by the patch size
  47. bool frustumCheck()
  48. {
  49. // Fixed radius (increase if patch size is increased in example)
  50. const float radius = 8.0f;
  51. vec4 pos = gl_in[gl_InvocationID].gl_Position;
  52. pos.y -= textureLod(samplerHeight, inUV[0], 0.0).r * ubo.displacementFactor;
  53. // Check sphere against frustum planes
  54. for (int i = 0; i < 6; i++) {
  55. if (dot(pos, ubo.frustumPlanes[i]) + radius < 0.0)
  56. {
  57. return false;
  58. }
  59. }
  60. return true;
  61. }
  62. void main()
  63. {
  64. if (gl_InvocationID == 0)
  65. {
  66. if (!frustumCheck())
  67. {
  68. gl_TessLevelInner[0] = 0.0;
  69. gl_TessLevelInner[1] = 0.0;
  70. gl_TessLevelOuter[0] = 0.0;
  71. gl_TessLevelOuter[1] = 0.0;
  72. gl_TessLevelOuter[2] = 0.0;
  73. gl_TessLevelOuter[3] = 0.0;
  74. }
  75. else
  76. {
  77. if (ubo.tessellationFactor > 0.0)
  78. {
  79. gl_TessLevelOuter[0] = screenSpaceTessFactor(gl_in[3].gl_Position, gl_in[0].gl_Position);
  80. gl_TessLevelOuter[1] = screenSpaceTessFactor(gl_in[0].gl_Position, gl_in[1].gl_Position);
  81. gl_TessLevelOuter[2] = screenSpaceTessFactor(gl_in[1].gl_Position, gl_in[2].gl_Position);
  82. gl_TessLevelOuter[3] = screenSpaceTessFactor(gl_in[2].gl_Position, gl_in[3].gl_Position);
  83. gl_TessLevelInner[0] = mix(gl_TessLevelOuter[0], gl_TessLevelOuter[3], 0.5);
  84. gl_TessLevelInner[1] = mix(gl_TessLevelOuter[2], gl_TessLevelOuter[1], 0.5);
  85. }
  86. else
  87. {
  88. // Tessellation factor can be set to zero by example
  89. // to demonstrate a simple passthrough
  90. gl_TessLevelInner[0] = 1.0;
  91. gl_TessLevelInner[1] = 1.0;
  92. gl_TessLevelOuter[0] = 1.0;
  93. gl_TessLevelOuter[1] = 1.0;
  94. gl_TessLevelOuter[2] = 1.0;
  95. gl_TessLevelOuter[3] = 1.0;
  96. }
  97. }
  98. }
  99. gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
  100. outNormal[gl_InvocationID] = inNormal[gl_InvocationID];
  101. outUV[gl_InvocationID] = inUV[gl_InvocationID];
  102. }