terrain.vertex.fx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. precision highp float;
  2. // Attributes
  3. attribute vec3 position;
  4. #ifdef NORMAL
  5. attribute vec3 normal;
  6. #endif
  7. #ifdef UV1
  8. attribute vec2 uv;
  9. #endif
  10. #ifdef UV2
  11. attribute vec2 uv2;
  12. #endif
  13. #ifdef VERTEXCOLOR
  14. attribute vec4 color;
  15. #endif
  16. #if NUM_BONE_INFLUENCERS > 0
  17. uniform mat4 mBones[BonesPerMesh];
  18. attribute vec4 matricesIndices;
  19. attribute vec4 matricesWeights;
  20. #if NUM_BONE_INFLUENCERS > 4
  21. attribute vec4 matricesIndicesExtra;
  22. attribute vec4 matricesWeightsExtra;
  23. #endif
  24. #endif
  25. // Uniforms
  26. #ifdef INSTANCES
  27. attribute vec4 world0;
  28. attribute vec4 world1;
  29. attribute vec4 world2;
  30. attribute vec4 world3;
  31. #else
  32. uniform mat4 world;
  33. #endif
  34. uniform mat4 view;
  35. uniform mat4 viewProjection;
  36. #ifdef DIFFUSE
  37. varying vec2 vTextureUV;
  38. uniform mat4 textureMatrix;
  39. uniform vec2 vTextureInfos;
  40. #endif
  41. #ifdef POINTSIZE
  42. uniform float pointSize;
  43. #endif
  44. // Output
  45. varying vec3 vPositionW;
  46. #ifdef NORMAL
  47. varying vec3 vNormalW;
  48. #endif
  49. #ifdef VERTEXCOLOR
  50. varying vec4 vColor;
  51. #endif
  52. #ifdef CLIPPLANE
  53. uniform vec4 vClipPlane;
  54. varying float fClipDistance;
  55. #endif
  56. #ifdef FOG
  57. varying float fFogDistance;
  58. #endif
  59. #ifdef SHADOWS
  60. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  61. uniform mat4 lightMatrix0;
  62. varying vec4 vPositionFromLight0;
  63. #endif
  64. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  65. uniform mat4 lightMatrix1;
  66. varying vec4 vPositionFromLight1;
  67. #endif
  68. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  69. uniform mat4 lightMatrix2;
  70. varying vec4 vPositionFromLight2;
  71. #endif
  72. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  73. uniform mat4 lightMatrix3;
  74. varying vec4 vPositionFromLight3;
  75. #endif
  76. #endif
  77. void main(void) {
  78. mat4 finalWorld;
  79. #ifdef INSTANCES
  80. finalWorld = mat4(world0, world1, world2, world3);
  81. #else
  82. finalWorld = world;
  83. #endif
  84. #if NUM_BONE_INFLUENCERS > 0
  85. mat4 influence;
  86. influence = mBones[int(matricesIndices[0])] * matricesWeights[0];
  87. #if NUM_BONE_INFLUENCERS > 1
  88. influence += mBones[int(matricesIndices[1])] * matricesWeights[1];
  89. #endif
  90. #if NUM_BONE_INFLUENCERS > 2
  91. influence += mBones[int(matricesIndices[2])] * matricesWeights[2];
  92. #endif
  93. #if NUM_BONE_INFLUENCERS > 3
  94. influence += mBones[int(matricesIndices[3])] * matricesWeights[3];
  95. #endif
  96. #if NUM_BONE_INFLUENCERS > 4
  97. influence += mBones[int(matricesIndicesExtra[0])] * matricesWeightsExtra[0];
  98. #endif
  99. #if NUM_BONE_INFLUENCERS > 5
  100. influence += mBones[int(matricesIndicesExtra[1])] * matricesWeightsExtra[1];
  101. #endif
  102. #if NUM_BONE_INFLUENCERS > 6
  103. influence += mBones[int(matricesIndicesExtra[2])] * matricesWeightsExtra[2];
  104. #endif
  105. #if NUM_BONE_INFLUENCERS > 7
  106. influence += mBones[int(matricesIndicesExtra[3])] * matricesWeightsExtra[3];
  107. #endif
  108. finalWorld = finalWorld * influence;
  109. #endif
  110. gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
  111. vec4 worldPos = finalWorld * vec4(position, 1.0);
  112. vPositionW = vec3(worldPos);
  113. #ifdef NORMAL
  114. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  115. #endif
  116. // Texture coordinates
  117. #ifndef UV1
  118. vec2 uv = vec2(0., 0.);
  119. #endif
  120. #ifndef UV2
  121. vec2 uv2 = vec2(0., 0.);
  122. #endif
  123. #ifdef DIFFUSE
  124. if (vTextureInfos.x == 0.)
  125. {
  126. vTextureUV = vec2(textureMatrix * vec4(uv, 1.0, 0.0));
  127. }
  128. else
  129. {
  130. vTextureUV = vec2(textureMatrix * vec4(uv2, 1.0, 0.0));
  131. }
  132. #endif
  133. // Clip plane
  134. #ifdef CLIPPLANE
  135. fClipDistance = dot(worldPos, vClipPlane);
  136. #endif
  137. // Fog
  138. #ifdef FOG
  139. fFogDistance = (view * worldPos).z;
  140. #endif
  141. // Shadows
  142. #ifdef SHADOWS
  143. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  144. vPositionFromLight0 = lightMatrix0 * worldPos;
  145. #endif
  146. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  147. vPositionFromLight1 = lightMatrix1 * worldPos;
  148. #endif
  149. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  150. vPositionFromLight2 = lightMatrix2 * worldPos;
  151. #endif
  152. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  153. vPositionFromLight3 = lightMatrix3 * worldPos;
  154. #endif
  155. #endif
  156. // Vertex color
  157. #ifdef VERTEXCOLOR
  158. vColor = color;
  159. #endif
  160. // Point size
  161. #ifdef POINTSIZE
  162. gl_PointSize = pointSize;
  163. #endif
  164. }