MsCommonTesse.glsl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. layout(triangles, equal_spacing, ccw) in;
  6. #define IN_POS4(i_) gl_in[i_].gl_Position
  7. #define IN_POS3(i_) gl_in[i_].gl_Position.xyz
  8. //
  9. // Input
  10. //
  11. struct PNPatch
  12. {
  13. vec3 pos021;
  14. vec3 pos012;
  15. vec3 pos102;
  16. vec3 pos201;
  17. vec3 pos210;
  18. vec3 pos120;
  19. vec3 pos111;
  20. };
  21. struct PhongPatch
  22. {
  23. vec3 terms[3];
  24. };
  25. #if INSTANCE_ID_FRAGMENT_SHADER
  26. struct CommonPatch
  27. {
  28. uint instanceId;
  29. };
  30. #endif
  31. in gl_PerVertex
  32. {
  33. vec4 gl_Position;
  34. }
  35. gl_in[];
  36. in patch PNPatch pnPatch;
  37. in patch PhongPatch phongPatch;
  38. #if INSTANCE_ID_FRAGMENT_SHADER
  39. in patch CommonPatch commonPatch;
  40. #endif
  41. layout(location = 0) in vec2 inTexCoord[];
  42. layout(location = 1) in vec3 inNormal[];
  43. #if PASS == COLOR
  44. layout(location = 2) in vec4 inTangent[];
  45. #endif
  46. //
  47. // Output
  48. //
  49. out gl_PerVertex
  50. {
  51. vec4 gl_Position;
  52. };
  53. layout(location = 0) out highp vec2 outTexCoord;
  54. #if PASS == COLOR
  55. layout(location = 1) out mediump vec3 outNormal;
  56. layout(location = 2) out mediump vec4 outTangent;
  57. #endif
  58. #define INTERPOLATE(x_) (x_[0] * gl_TessCoord.x + x_[1] * gl_TessCoord.y + x_[2] * gl_TessCoord.z)
  59. // Smooth tessellation
  60. #define tessellatePNPositionNormalTangentTexCoord_DEFINED
  61. void tessellatePNPositionNormalTangentTexCoord(in mat4 mvp, in mat3 normalMat)
  62. {
  63. #if PASS == COLOR
  64. outNormal = normalize(normalMat * INTERPOLATE(inNormal));
  65. outTangent = INTERPOLATE(inTangent);
  66. outTangent.xyz = normalize(normalMat * outTangent.xyz);
  67. #endif
  68. outTexCoord = INTERPOLATE(inTexCoord);
  69. float u = gl_TessCoord.x;
  70. float v = gl_TessCoord.y;
  71. float w = gl_TessCoord.z;
  72. float uPow3 = pow(u, 3);
  73. float vPow3 = pow(v, 3);
  74. float wPow3 = pow(w, 3);
  75. float uPow2 = pow(u, 2);
  76. float vPow2 = pow(v, 2);
  77. float wPow2 = pow(w, 2);
  78. vec3 pos030 = IN_POS3(0);
  79. vec3 pos003 = IN_POS3(1);
  80. vec3 pos300 = IN_POS3(2);
  81. vec3 pos = pos300 * wPow3 + pos030 * uPow3 + pos003 * vPow3 + pnPatch.pos210 * 3.0 * wPow2 * u
  82. + pnPatch.pos120 * 3.0 * w * uPow2 + pnPatch.pos201 * 3.0 * wPow2 * v + pnPatch.pos021 * 3.0 * uPow2 * v
  83. + pnPatch.pos102 * 3.0 * w * vPow2 + pnPatch.pos012 * 3.0 * u * vPow2 + pnPatch.pos111 * 6.0 * w * u * v;
  84. gl_Position = mvp * vec4(pos, 1.0);
  85. }
  86. #define tessellatePhongPositionNormalTangentTexCoord_DEFINED
  87. void tessellatePhongPositionNormalTangentTexCoord(in mat4 mvp, in mat3 normalMat)
  88. {
  89. #if PASS == COLOR
  90. outNormal = normalize(normalMat * INTERPOLATE(inNormal));
  91. outTangent = INTERPOLATE(inTangent);
  92. outTangent.xyz = normalize(normalMat * outTangent.xyz);
  93. #endif
  94. outTexCoord = INTERPOLATE(inTexCoord);
  95. // interpolated position
  96. vec3 inpos[3] = vec3[](IN_POS3(0), IN_POS3(1), IN_POS3(2));
  97. vec3 barPos = INTERPOLATE(inpos);
  98. // build terms
  99. vec3 termIJ = vec3(phongPatch.terms[0][0], phongPatch.terms[1][0], phongPatch.terms[2][0]);
  100. vec3 termJK = vec3(phongPatch.terms[0][1], phongPatch.terms[1][1], phongPatch.terms[2][1]);
  101. vec3 termIK = vec3(phongPatch.terms[0][2], phongPatch.terms[1][2], phongPatch.terms[2][2]);
  102. vec3 tc2 = gl_TessCoord * gl_TessCoord;
  103. // phong tesselated pos
  104. vec3 phongPos = tc2[0] * inpos[0] + tc2[1] * inpos[1] + tc2[2] * inpos[2]
  105. + gl_TessCoord[0] * gl_TessCoord[1] * termIJ + gl_TessCoord[1] * gl_TessCoord[2] * termJK
  106. + gl_TessCoord[2] * gl_TessCoord[0] * termIK;
  107. float tessAlpha = 1.0;
  108. vec3 finalPos = (1.0 - tessAlpha) * barPos + tessAlpha * phongPos;
  109. gl_Position = mvp * vec4(finalPos, 1.0);
  110. }
  111. #define tessellateDispMapPositionNormalTangentTexCoord_DEFINED
  112. void tessellateDispMapPositionNormalTangentTexCoord(in mat4 mvp, in mat3 normalMat, in sampler2D dispMap)
  113. {
  114. vec3 norm = INTERPOLATE(inNormal);
  115. #if PASS == COLOR
  116. outNormal = normalize(normalMat * norm);
  117. outTangent = INTERPOLATE(inTangent);
  118. outTangent.xyz = normalize(normalMat * outTangent.xyz);
  119. #endif
  120. outTexCoord = INTERPOLATE(inTexCoord);
  121. float height = texture(dispMap, outTexCoord).r;
  122. height = height * 0.7 - 0.35;
  123. vec3 inpos[3] = vec3[](IN_POS3(0), IN_POS3(1), IN_POS3(2));
  124. vec3 pos = INTERPOLATE(inpos) + norm * height;
  125. gl_Position = mvp * vec4(pos, 1.0);
  126. }