terrain.vertex.fx 3.2 KB

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