ccShader_3D_PositionNormalTex.vert 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. const char* cc3D_PositionNormalTex_vert = R"(
  2. #ifdef USE_NORMAL_MAPPING
  3. #if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
  4. uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
  5. #endif
  6. #endif
  7. #if (MAX_POINT_LIGHT_NUM > 0)
  8. uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
  9. #endif
  10. #if (MAX_SPOT_LIGHT_NUM > 0)
  11. uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
  12. #ifdef USE_NORMAL_MAPPING
  13. uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
  14. #endif
  15. #endif
  16. attribute vec4 a_position;
  17. attribute vec2 a_texCoord;
  18. attribute vec3 a_normal;
  19. #ifdef USE_NORMAL_MAPPING
  20. attribute vec3 a_tangent;
  21. attribute vec3 a_binormal;
  22. #endif
  23. varying vec2 TextureCoordOut;
  24. #ifdef USE_NORMAL_MAPPING
  25. #if MAX_DIRECTIONAL_LIGHT_NUM
  26. varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
  27. #endif
  28. #endif
  29. #if MAX_POINT_LIGHT_NUM
  30. varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
  31. #endif
  32. #if MAX_SPOT_LIGHT_NUM
  33. varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
  34. #ifdef USE_NORMAL_MAPPING
  35. varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
  36. #endif
  37. #endif
  38. #ifndef USE_NORMAL_MAPPING
  39. #if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
  40. varying vec3 v_normal;
  41. #endif
  42. #endif
  43. void main(void)
  44. {
  45. vec4 ePosition = CC_MVMatrix * a_position;
  46. #ifdef USE_NORMAL_MAPPING
  47. #if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
  48. vec3 eTangent = normalize(CC_NormalMatrix * a_tangent);
  49. vec3 eBinormal = normalize(CC_NormalMatrix * a_binormal);
  50. vec3 eNormal = normalize(CC_NormalMatrix * a_normal);
  51. #endif
  52. #if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
  53. for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
  54. {
  55. v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]);
  56. v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]);
  57. v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]);
  58. }
  59. #endif
  60. #if (MAX_POINT_LIGHT_NUM > 0)
  61. for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
  62. {
  63. vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
  64. v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir);
  65. v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir);
  66. v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir);
  67. }
  68. #endif
  69. #if (MAX_SPOT_LIGHT_NUM > 0)
  70. for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
  71. {
  72. vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz;
  73. v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir);
  74. v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir);
  75. v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
  76. v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]);
  77. v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]);
  78. v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]);
  79. }
  80. #endif
  81. #else
  82. #if (MAX_POINT_LIGHT_NUM > 0)
  83. for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
  84. {
  85. v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
  86. }
  87. #endif
  88. #if (MAX_SPOT_LIGHT_NUM > 0)
  89. for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
  90. {
  91. v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
  92. }
  93. #endif
  94. #if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
  95. v_normal = CC_NormalMatrix * a_normal;
  96. #endif
  97. #endif
  98. TextureCoordOut = a_texCoord;
  99. TextureCoordOut.y = 1.0 - TextureCoordOut.y;
  100. gl_Position = CC_PMatrix * ePosition;
  101. }
  102. )";
  103. const char* cc3D_SkinPositionNormalTex_vert = R"(
  104. #ifdef USE_NORMAL_MAPPING
  105. #if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
  106. uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
  107. #endif
  108. #endif
  109. #if (MAX_POINT_LIGHT_NUM > 0)
  110. uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
  111. #endif
  112. #if (MAX_SPOT_LIGHT_NUM > 0)
  113. uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
  114. #ifdef USE_NORMAL_MAPPING
  115. uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
  116. #endif
  117. #endif
  118. attribute vec3 a_position;
  119. attribute vec4 a_blendWeight;
  120. attribute vec4 a_blendIndex;
  121. attribute vec2 a_texCoord;
  122. attribute vec3 a_normal;
  123. #ifdef USE_NORMAL_MAPPING
  124. attribute vec3 a_tangent;
  125. attribute vec3 a_binormal;
  126. #endif
  127. const int SKINNING_JOINT_COUNT = 60;
  128. // Uniforms
  129. uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
  130. // Varyings
  131. varying vec2 TextureCoordOut;
  132. #ifdef USE_NORMAL_MAPPING
  133. #if MAX_DIRECTIONAL_LIGHT_NUM
  134. varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
  135. #endif
  136. #endif
  137. #if MAX_POINT_LIGHT_NUM
  138. varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
  139. #endif
  140. #if MAX_SPOT_LIGHT_NUM
  141. varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
  142. #ifdef USE_NORMAL_MAPPING
  143. varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
  144. #endif
  145. #endif
  146. #ifndef USE_NORMAL_MAPPING
  147. #if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
  148. varying vec3 v_normal;
  149. #endif
  150. #endif
  151. void getPositionAndNormal(out vec4 position, out vec3 normal, out vec3 tangent, out vec3 binormal)
  152. {
  153. float blendWeight = a_blendWeight[0];
  154. int matrixIndex = int (a_blendIndex[0]) * 3;
  155. vec4 matrixPalette1 = u_matrixPalette[matrixIndex] * blendWeight;
  156. vec4 matrixPalette2 = u_matrixPalette[matrixIndex + 1] * blendWeight;
  157. vec4 matrixPalette3 = u_matrixPalette[matrixIndex + 2] * blendWeight;
  158. blendWeight = a_blendWeight[1];
  159. if (blendWeight > 0.0)
  160. {
  161. matrixIndex = int(a_blendIndex[1]) * 3;
  162. matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight;
  163. matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight;
  164. matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight;
  165. blendWeight = a_blendWeight[2];
  166. if (blendWeight > 0.0)
  167. {
  168. matrixIndex = int(a_blendIndex[2]) * 3;
  169. matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight;
  170. matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight;
  171. matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight;
  172. blendWeight = a_blendWeight[3];
  173. if (blendWeight > 0.0)
  174. {
  175. matrixIndex = int(a_blendIndex[3]) * 3;
  176. matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight;
  177. matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight;
  178. matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight;
  179. }
  180. }
  181. }
  182. vec4 p = vec4(a_position, 1.0);
  183. position.x = dot(p, matrixPalette1);
  184. position.y = dot(p, matrixPalette2);
  185. position.z = dot(p, matrixPalette3);
  186. position.w = p.w;
  187. #if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
  188. vec4 n = vec4(a_normal, 0.0);
  189. normal.x = dot(n, matrixPalette1);
  190. normal.y = dot(n, matrixPalette2);
  191. normal.z = dot(n, matrixPalette3);
  192. #ifdef USE_NORMAL_MAPPING
  193. vec4 t = vec4(a_tangent, 0.0);
  194. tangent.x = dot(t, matrixPalette1);
  195. tangent.y = dot(t, matrixPalette2);
  196. tangent.z = dot(t, matrixPalette3);
  197. vec4 b = vec4(a_binormal, 0.0);
  198. binormal.x = dot(b, matrixPalette1);
  199. binormal.y = dot(b, matrixPalette2);
  200. binormal.z = dot(b, matrixPalette3);
  201. #endif
  202. #endif
  203. }
  204. void main()
  205. {
  206. vec4 position;
  207. vec3 normal;
  208. vec3 tangent;
  209. vec3 binormal;
  210. getPositionAndNormal(position, normal, tangent, binormal);
  211. vec4 ePosition = CC_MVMatrix * position;
  212. #ifdef USE_NORMAL_MAPPING
  213. #if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
  214. vec3 eTangent = normalize(CC_NormalMatrix * tangent);
  215. vec3 eBinormal = normalize(CC_NormalMatrix * binormal);
  216. vec3 eNormal = normalize(CC_NormalMatrix * normal);
  217. #endif
  218. #if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
  219. for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
  220. {
  221. v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]);
  222. v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]);
  223. v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]);
  224. }
  225. #endif
  226. #if (MAX_POINT_LIGHT_NUM > 0)
  227. for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
  228. {
  229. vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
  230. v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir);
  231. v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir);
  232. v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir);
  233. }
  234. #endif
  235. #if (MAX_SPOT_LIGHT_NUM > 0)
  236. for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
  237. {
  238. vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz;
  239. v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir);
  240. v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir);
  241. v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
  242. v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]);
  243. v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]);
  244. v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]);
  245. }
  246. #endif
  247. #else
  248. #if (MAX_POINT_LIGHT_NUM > 0)
  249. for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
  250. {
  251. v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz- ePosition.xyz;
  252. }
  253. #endif
  254. #if (MAX_SPOT_LIGHT_NUM > 0)
  255. for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
  256. {
  257. v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
  258. }
  259. #endif
  260. #if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
  261. v_normal = CC_NormalMatrix * normal;
  262. #endif
  263. #endif
  264. TextureCoordOut = a_texCoord;
  265. TextureCoordOut.y = 1.0 - TextureCoordOut.y;
  266. gl_Position = CC_PMatrix * ePosition;
  267. }
  268. )";