pbr.vertex.fx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 ALBEDO
  37. varying vec2 vAlbedoUV;
  38. uniform mat4 albedoMatrix;
  39. uniform vec2 vAlbedoInfos;
  40. #endif
  41. #ifdef AMBIENT
  42. varying vec2 vAmbientUV;
  43. uniform mat4 ambientMatrix;
  44. uniform vec2 vAmbientInfos;
  45. #endif
  46. #ifdef OPACITY
  47. varying vec2 vOpacityUV;
  48. uniform mat4 opacityMatrix;
  49. uniform vec2 vOpacityInfos;
  50. #endif
  51. #ifdef EMISSIVE
  52. varying vec2 vEmissiveUV;
  53. uniform vec2 vEmissiveInfos;
  54. uniform mat4 emissiveMatrix;
  55. #endif
  56. #ifdef LIGHTMAP
  57. varying vec2 vLightmapUV;
  58. uniform vec2 vLightmapInfos;
  59. uniform mat4 lightmapMatrix;
  60. #endif
  61. #if defined(REFLECTIVITY)
  62. varying vec2 vReflectivityUV;
  63. uniform vec2 vReflectivityInfos;
  64. uniform mat4 reflectivityMatrix;
  65. #endif
  66. #ifdef BUMP
  67. varying vec2 vBumpUV;
  68. uniform vec2 vBumpInfos;
  69. uniform mat4 bumpMatrix;
  70. #endif
  71. #ifdef POINTSIZE
  72. uniform float pointSize;
  73. #endif
  74. // Output
  75. varying vec3 vPositionW;
  76. #ifdef NORMAL
  77. varying vec3 vNormalW;
  78. #endif
  79. #ifdef VERTEXCOLOR
  80. varying vec4 vColor;
  81. #endif
  82. #ifdef CLIPPLANE
  83. uniform vec4 vClipPlane;
  84. varying float fClipDistance;
  85. #endif
  86. #ifdef FOG
  87. varying float fFogDistance;
  88. #endif
  89. #ifdef SHADOWS
  90. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  91. uniform mat4 lightMatrix0;
  92. varying vec4 vPositionFromLight0;
  93. #endif
  94. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  95. uniform mat4 lightMatrix1;
  96. varying vec4 vPositionFromLight1;
  97. #endif
  98. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  99. uniform mat4 lightMatrix2;
  100. varying vec4 vPositionFromLight2;
  101. #endif
  102. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  103. uniform mat4 lightMatrix3;
  104. varying vec4 vPositionFromLight3;
  105. #endif
  106. #endif
  107. #ifdef REFLECTIONMAP_SKYBOX
  108. varying vec3 vPositionUVW;
  109. #endif
  110. #ifdef REFLECTIONMAP_EQUIRECTANGULAR
  111. varying vec3 vDirectionW;
  112. #endif
  113. #ifdef LOGARITHMICDEPTH
  114. uniform float logarithmicDepthConstant;
  115. varying float vFragmentDepth;
  116. #endif
  117. void main(void) {
  118. #ifdef REFLECTIONMAP_SKYBOX
  119. vPositionUVW = position;
  120. #endif
  121. #ifdef INSTANCES
  122. mat4 finalWorld = mat4(world0, world1, world2, world3);
  123. #else
  124. mat4 finalWorld = world;
  125. #endif
  126. #if NUM_BONE_INFLUENCERS > 0
  127. mat4 influence;
  128. influence = mBones[int(matricesIndices[0])] * matricesWeights[0];
  129. #if NUM_BONE_INFLUENCERS > 1
  130. influence += mBones[int(matricesIndices[1])] * matricesWeights[1];
  131. #endif
  132. #if NUM_BONE_INFLUENCERS > 2
  133. influence += mBones[int(matricesIndices[2])] * matricesWeights[2];
  134. #endif
  135. #if NUM_BONE_INFLUENCERS > 3
  136. influence += mBones[int(matricesIndices[3])] * matricesWeights[3];
  137. #endif
  138. #if NUM_BONE_INFLUENCERS > 4
  139. influence += mBones[int(matricesIndicesExtra[0])] * matricesWeightsExtra[0];
  140. #endif
  141. #if NUM_BONE_INFLUENCERS > 5
  142. influence += mBones[int(matricesIndicesExtra[1])] * matricesWeightsExtra[1];
  143. #endif
  144. #if NUM_BONE_INFLUENCERS > 6
  145. influence += mBones[int(matricesIndicesExtra[2])] * matricesWeightsExtra[2];
  146. #endif
  147. #if NUM_BONE_INFLUENCERS > 7
  148. influence += mBones[int(matricesIndicesExtra[3])] * matricesWeightsExtra[3];
  149. #endif
  150. finalWorld = finalWorld * influence;
  151. #endif
  152. gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
  153. vec4 worldPos = finalWorld * vec4(position, 1.0);
  154. vPositionW = vec3(worldPos);
  155. #ifdef NORMAL
  156. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  157. #endif
  158. #ifdef REFLECTIONMAP_EQUIRECTANGULAR
  159. vDirectionW = normalize(vec3(finalWorld * vec4(position, 0.0)));
  160. #endif
  161. // Texture coordinates
  162. #ifndef UV1
  163. vec2 uv = vec2(0., 0.);
  164. #endif
  165. #ifndef UV2
  166. vec2 uv2 = vec2(0., 0.);
  167. #endif
  168. #ifdef ALBEDO
  169. if (vAlbedoInfos.x == 0.)
  170. {
  171. vAlbedoUV = vec2(albedoMatrix * vec4(uv, 1.0, 0.0));
  172. }
  173. else
  174. {
  175. vAlbedoUV = vec2(albedoMatrix * vec4(uv2, 1.0, 0.0));
  176. }
  177. #endif
  178. #ifdef AMBIENT
  179. if (vAmbientInfos.x == 0.)
  180. {
  181. vAmbientUV = vec2(ambientMatrix * vec4(uv, 1.0, 0.0));
  182. }
  183. else
  184. {
  185. vAmbientUV = vec2(ambientMatrix * vec4(uv2, 1.0, 0.0));
  186. }
  187. #endif
  188. #ifdef OPACITY
  189. if (vOpacityInfos.x == 0.)
  190. {
  191. vOpacityUV = vec2(opacityMatrix * vec4(uv, 1.0, 0.0));
  192. }
  193. else
  194. {
  195. vOpacityUV = vec2(opacityMatrix * vec4(uv2, 1.0, 0.0));
  196. }
  197. #endif
  198. #ifdef EMISSIVE
  199. if (vEmissiveInfos.x == 0.)
  200. {
  201. vEmissiveUV = vec2(emissiveMatrix * vec4(uv, 1.0, 0.0));
  202. }
  203. else
  204. {
  205. vEmissiveUV = vec2(emissiveMatrix * vec4(uv2, 1.0, 0.0));
  206. }
  207. #endif
  208. #ifdef LIGHTMAP
  209. if (vLightmapInfos.x == 0.)
  210. {
  211. vLightmapUV = vec2(lightmapMatrix * vec4(uv, 1.0, 0.0));
  212. }
  213. else
  214. {
  215. vLightmapUV = vec2(lightmapMatrix * vec4(uv2, 1.0, 0.0));
  216. }
  217. #endif
  218. #if defined(REFLECTIVITY)
  219. if (vReflectivityInfos.x == 0.)
  220. {
  221. vReflectivityUV = vec2(reflectivityMatrix * vec4(uv, 1.0, 0.0));
  222. }
  223. else
  224. {
  225. vReflectivityUV = vec2(reflectivityMatrix * vec4(uv2, 1.0, 0.0));
  226. }
  227. #endif
  228. #ifdef BUMP
  229. if (vBumpInfos.x == 0.)
  230. {
  231. vBumpUV = vec2(bumpMatrix * vec4(uv, 1.0, 0.0));
  232. }
  233. else
  234. {
  235. vBumpUV = vec2(bumpMatrix * vec4(uv2, 1.0, 0.0));
  236. }
  237. #endif
  238. // Clip plane
  239. #ifdef CLIPPLANE
  240. fClipDistance = dot(worldPos, vClipPlane);
  241. #endif
  242. // Fog
  243. #ifdef FOG
  244. fFogDistance = (view * worldPos).z;
  245. #endif
  246. // Shadows
  247. #ifdef SHADOWS
  248. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  249. vPositionFromLight0 = lightMatrix0 * worldPos;
  250. #endif
  251. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  252. vPositionFromLight1 = lightMatrix1 * worldPos;
  253. #endif
  254. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  255. vPositionFromLight2 = lightMatrix2 * worldPos;
  256. #endif
  257. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  258. vPositionFromLight3 = lightMatrix3 * worldPos;
  259. #endif
  260. #endif
  261. // Vertex color
  262. #ifdef VERTEXCOLOR
  263. vColor = color;
  264. #endif
  265. // Point size
  266. #ifdef POINTSIZE
  267. gl_PointSize = pointSize;
  268. #endif
  269. // Log. depth
  270. #ifdef LOGARITHMICDEPTH
  271. vFragmentDepth = 1.0 + gl_Position.w;
  272. gl_Position.z = log2(max(0.000001, vFragmentDepth)) * logarithmicDepthConstant;
  273. #endif
  274. }