triplanar.vertex.fx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. precision highp float;
  2. // Attributes
  3. attribute vec3 position;
  4. #ifdef NORMAL
  5. attribute vec3 normal;
  6. #endif
  7. #ifdef VERTEXCOLOR
  8. attribute vec4 color;
  9. #endif
  10. #ifdef BONES
  11. attribute vec4 matricesIndices;
  12. attribute vec4 matricesWeights;
  13. #endif
  14. // Uniforms
  15. #ifdef INSTANCES
  16. attribute vec4 world0;
  17. attribute vec4 world1;
  18. attribute vec4 world2;
  19. attribute vec4 world3;
  20. #else
  21. uniform mat4 world;
  22. #endif
  23. uniform mat4 view;
  24. uniform mat4 viewProjection;
  25. #ifdef DIFFUSEX
  26. varying vec2 vTextureUVX;
  27. #endif
  28. #ifdef DIFFUSEY
  29. varying vec2 vTextureUVY;
  30. #endif
  31. #ifdef DIFFUSEZ
  32. varying vec2 vTextureUVZ;
  33. #endif
  34. uniform float tileSize;
  35. #ifdef BONES
  36. uniform mat4 mBones[BonesPerMesh];
  37. #endif
  38. #ifdef POINTSIZE
  39. uniform float pointSize;
  40. #endif
  41. // Output
  42. varying vec3 vPositionW;
  43. #ifdef NORMAL
  44. varying mat3 tangentSpace;
  45. #endif
  46. #ifdef VERTEXCOLOR
  47. varying vec4 vColor;
  48. #endif
  49. #ifdef CLIPPLANE
  50. uniform vec4 vClipPlane;
  51. varying float fClipDistance;
  52. #endif
  53. #ifdef FOG
  54. varying float fFogDistance;
  55. #endif
  56. #ifdef SHADOWS
  57. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  58. uniform mat4 lightMatrix0;
  59. varying vec4 vPositionFromLight0;
  60. #endif
  61. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  62. uniform mat4 lightMatrix1;
  63. varying vec4 vPositionFromLight1;
  64. #endif
  65. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  66. uniform mat4 lightMatrix2;
  67. varying vec4 vPositionFromLight2;
  68. #endif
  69. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  70. uniform mat4 lightMatrix3;
  71. varying vec4 vPositionFromLight3;
  72. #endif
  73. #endif
  74. void main(void) {
  75. mat4 finalWorld;
  76. #ifdef INSTANCES
  77. finalWorld = mat4(world0, world1, world2, world3);
  78. #else
  79. finalWorld = world;
  80. #endif
  81. #ifdef BONES
  82. mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;
  83. mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;
  84. mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;
  85. #ifdef BONES4
  86. mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;
  87. finalWorld = finalWorld * (m0 + m1 + m2 + m3);
  88. #else
  89. finalWorld = finalWorld * (m0 + m1 + m2);
  90. #endif
  91. #endif
  92. gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
  93. vec4 worldPos = finalWorld * vec4(position, 1.0);
  94. vPositionW = vec3(worldPos);
  95. #ifdef DIFFUSEX
  96. vTextureUVX = worldPos.zy / tileSize;
  97. #endif
  98. #ifdef DIFFUSEY
  99. vTextureUVY = worldPos.xz / tileSize;
  100. #endif
  101. #ifdef DIFFUSEZ
  102. vTextureUVZ = worldPos.xy / tileSize;
  103. #endif
  104. #ifdef NORMAL
  105. // Compute tangent space (used for normal mapping + tri planar color mapping)
  106. vec3 xtan = vec3(0,0,1);//tangent space for the X aligned plane
  107. vec3 xbin = vec3(0,1,0);
  108. vec3 ytan = vec3(1,0,0);//tangent space for the Y aligned plane
  109. vec3 ybin = vec3(0,0,1);
  110. vec3 ztan = vec3(1,0,0);//tangent space for the Z aligned plane
  111. vec3 zbin = vec3(0,1,0);
  112. vec3 normalizedNormal = normalize(normal);
  113. normalizedNormal *= normalizedNormal;
  114. vec3 worldBinormal = normalize(xbin * normalizedNormal.x + ybin * normalizedNormal.y + zbin * normalizedNormal.z);
  115. vec3 worldTangent = normalize(xtan * normalizedNormal.x + ytan * normalizedNormal.y + ztan * normalizedNormal.z);
  116. worldTangent = (world * vec4(worldTangent, 1.0)).xyz;
  117. worldBinormal = (world * vec4(worldBinormal, 1.0)).xyz;
  118. vec3 worldNormal = normalize(cross(worldTangent, worldBinormal));
  119. tangentSpace[0] = worldTangent;
  120. tangentSpace[1] = worldBinormal;
  121. tangentSpace[2] = worldNormal;
  122. #endif
  123. // Clip plane
  124. #ifdef CLIPPLANE
  125. fClipDistance = dot(worldPos, vClipPlane);
  126. #endif
  127. // Fog
  128. #ifdef FOG
  129. fFogDistance = (view * worldPos).z;
  130. #endif
  131. // Shadows
  132. #ifdef SHADOWS
  133. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  134. vPositionFromLight0 = lightMatrix0 * worldPos;
  135. #endif
  136. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  137. vPositionFromLight1 = lightMatrix1 * worldPos;
  138. #endif
  139. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  140. vPositionFromLight2 = lightMatrix2 * worldPos;
  141. #endif
  142. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  143. vPositionFromLight3 = lightMatrix3 * worldPos;
  144. #endif
  145. #endif
  146. // Vertex color
  147. #ifdef VERTEXCOLOR
  148. vColor = color;
  149. #endif
  150. // Point size
  151. #ifdef POINTSIZE
  152. gl_PointSize = pointSize;
  153. #endif
  154. }