Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

282 rindas
7.0 KiB

  1. #ifndef DIRECTIONAL_LIGHT_COUNT
  2. #define DIRECTIONAL_LIGHT_COUNT 0
  3. #endif
  4. #ifndef SPOT_LIGHT_COUNT
  5. #define SPOT_LIGHT_COUNT 0
  6. #endif
  7. #ifndef POINT_LIGHT_COUNT
  8. #define POINT_LIGHT_COUNT 0
  9. #endif
  10. #if (DIRECTIONAL_LIGHT_COUNT > 0) || (POINT_LIGHT_COUNT > 0) || (SPOT_LIGHT_COUNT > 0) || defined(OUTLINE) || defined(OUTLINE_WITH_Z) || defined(RIM_LIGHTING)
  11. #define LIGHTING
  12. #endif
  13. #ifndef TERRAIN_LAYER_COUNT
  14. #define TERRAIN_LAYER_COUNT 0
  15. #endif
  16. ///////////////////////////////////////////////////////////
  17. // Atributes
  18. attribute vec4 a_position;
  19. #if defined(SKINNING)
  20. attribute vec4 a_blendWeights;
  21. attribute vec4 a_blendIndices;
  22. #endif
  23. attribute vec2 a_texCoord;
  24. #if defined(LIGHTMAP)
  25. attribute vec2 a_texCoord1;
  26. #endif
  27. #if defined(LIGHTING)
  28. attribute vec3 a_normal;
  29. #if defined(BUMPED)
  30. attribute vec3 a_tangent;
  31. attribute vec3 a_binormal;
  32. #endif
  33. #endif
  34. attribute vec4 a_color;
  35. ///////////////////////////////////////////////////////////
  36. // Uniforms
  37. uniform mat4 u_worldViewProjectionMatrix;
  38. uniform mat4 u_projectionMatrix;
  39. uniform mat4 u_worldMatrix;
  40. #if defined(SKINNING)
  41. uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
  42. #endif
  43. #if defined(LIGHTING)
  44. uniform mat4 u_inverseTransposeWorldViewMatrix;
  45. #if defined(SPECULAR) || (POINT_LIGHT_COUNT > 0) || (SPOT_LIGHT_COUNT > 0)
  46. uniform mat4 u_worldViewMatrix;
  47. #endif
  48. #if defined(BUMPED) && (DIRECTIONAL_LIGHT_COUNT > 0)
  49. uniform vec3 u_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
  50. #endif
  51. #if (POINT_LIGHT_COUNT > 0)
  52. uniform vec3 u_pointLightPosition[POINT_LIGHT_COUNT];
  53. #endif
  54. #if (SPOT_LIGHT_COUNT > 0)
  55. uniform vec3 u_spotLightPosition[SPOT_LIGHT_COUNT];
  56. #if defined(BUMPED)
  57. uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT];
  58. #endif
  59. #endif
  60. #if defined(SPECULAR) || defined(RIM_LIGHTING)
  61. uniform vec3 u_cameraPosition;
  62. #endif
  63. #endif
  64. #if defined(TEXTURE_REPEAT)
  65. uniform vec2 u_textureRepeat;
  66. #endif
  67. #if defined(TEXTURE_OFFSET)
  68. uniform vec2 u_textureOffset;
  69. #endif
  70. ///////////////////////////////////////////////////////////
  71. // Varyings
  72. varying vec2 v_texCoord;
  73. varying vec4 v_color;
  74. #if defined(LIGHTMAP)
  75. varying vec2 v_texCoord1;
  76. #endif
  77. #if defined(LIGHTING)
  78. #if !defined(BUMPED)
  79. varying vec3 v_normalVector;
  80. #endif
  81. #if defined(BUMPED) && (DIRECTIONAL_LIGHT_COUNT > 0)
  82. varying vec3 v_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
  83. #endif
  84. #if (POINT_LIGHT_COUNT > 0)
  85. varying vec3 v_vertexToPointLightDirection[POINT_LIGHT_COUNT];
  86. #endif
  87. #if (SPOT_LIGHT_COUNT > 0)
  88. varying vec3 v_vertexToSpotLightDirection[SPOT_LIGHT_COUNT];
  89. #if defined(BUMPED)
  90. varying vec3 v_spotLightDirection[SPOT_LIGHT_COUNT];
  91. #endif
  92. #endif
  93. #if defined(SPECULAR)
  94. varying vec3 v_cameraDirection;
  95. #endif
  96. #include "lighting.vert"
  97. #endif
  98. #if defined(SKINNING)
  99. #include "skinning.vert"
  100. #else
  101. #include "skinning-none.vert"
  102. #endif
  103. #if defined(OUTLINE) || defined(OUTLINE_WITH_Z)
  104. // 外描边的描边粗细程度
  105. uniform float u_outlineSize;
  106. #endif
  107. #if defined(RIM_LIGHTING)
  108. varying vec3 v_worldNormal;
  109. varying vec4 v_worldPosition;
  110. #endif
  111. #if defined(NO_Z_FIGHTING)
  112. // 深度偏移值
  113. uniform float u_depthOffset;
  114. #endif
  115. #if TERRAIN_LAYER_COUNT >= 1
  116. varying vec2 v_texCoordTerrainLayer1;
  117. uniform vec2 v_texCoordTerrainRepeat1;
  118. #if TERRAIN_LAYER_COUNT >= 2
  119. varying vec2 v_texCoordTerrainLayer2;
  120. uniform vec2 v_texCoordTerrainRepeat2;
  121. #if TERRAIN_LAYER_COUNT >= 3
  122. varying vec2 v_texCoordTerrainLayer3;
  123. uniform vec2 v_texCoordTerrainRepeat3;
  124. #if TERRAIN_LAYER_COUNT >= 4
  125. varying vec2 v_texCoordTerrainLayer4;
  126. uniform vec2 v_texCoordTerrainRepeat4;
  127. #endif
  128. #endif
  129. #endif
  130. #endif
  131. varying float v_fogFactor; //fog factor
  132. //fog parameter
  133. uniform vec4 u_fogparam; // .x, .y, .z, .w stand for fog density, fog start, fog end, fog type. fog type 0, no fog, fog type 1 linear fog, fog type 2 exp fog, fog type 3 exp2 fog
  134. vec2 TransformViewToProjection (vec2 v)
  135. {
  136. return vec2(v.x*u_projectionMatrix[0][0], v.y*u_projectionMatrix[1][1]);
  137. }
  138. void main()
  139. {
  140. vec4 position = getPosition();
  141. gl_Position = u_worldViewProjectionMatrix * position;
  142. #if defined(LIGHTING)
  143. vec3 normal = getNormal();
  144. // Transform the normal, tangent and binormals to view space.
  145. mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz, u_inverseTransposeWorldViewMatrix[1].xyz, u_inverseTransposeWorldViewMatrix[2].xyz);
  146. vec3 normalVector = normalize(inverseTransposeWorldViewMatrix * normal);
  147. #if defined(BUMPED)
  148. vec3 tangent = getTangent();
  149. vec3 binormal = getBinormal();
  150. vec3 tangentVector = normalize(inverseTransposeWorldViewMatrix * tangent);
  151. vec3 binormalVector = normalize(inverseTransposeWorldViewMatrix * binormal);
  152. mat3 tangentSpaceTransformMatrix = mat3(tangentVector.x, binormalVector.x, normalVector.x, tangentVector.y, binormalVector.y, normalVector.y, tangentVector.z, binormalVector.z, normalVector.z);
  153. applyLight(position, tangentSpaceTransformMatrix);
  154. #else
  155. v_normalVector = normalVector;
  156. applyLight(position);
  157. #endif
  158. #endif
  159. // 计算外描边,让顶点向法线外移动几个像素
  160. #if defined(OUTLINE)
  161. vec2 offset = normalize(TransformViewToProjection(normalVector.xy));
  162. gl_Position.xy += offset * u_outlineSize;
  163. //gl_Position.z += u_outlineSize;
  164. #elif defined(OUTLINE_WITH_Z)
  165. vec2 offset = normalize(TransformViewToProjection(normalVector.xy));
  166. gl_Position.xy += offset * gl_Position.z * u_outlineSize;
  167. //gl_Position.z += gl_Position.z * u_outlineSize;
  168. #endif
  169. // 计算内描边,计算法线与摄像机的夹角,然后根据夹角与0度的差异计算颜色
  170. #if defined(RIM_LIGHTING)
  171. v_worldNormal = (u_worldMatrix * vec4(normal.xyz , 1)).xyz;
  172. v_worldPosition = u_worldMatrix * position;
  173. #endif
  174. #if defined(NO_Z_FIGHTING)
  175. gl_Position.z -= u_depthOffset;
  176. #endif
  177. v_texCoord = a_texCoord;
  178. #if defined(TEXTURE_REPEAT)
  179. v_texCoord *= u_textureRepeat;
  180. #endif
  181. #if defined(TEXTURE_OFFSET)
  182. v_texCoord += u_textureOffset;
  183. #endif
  184. #if defined(LIGHTMAP)
  185. v_texCoord1 = a_texCoord1;
  186. #endif
  187. #if defined(VERTEX_COLOR)
  188. v_color = a_color;
  189. #endif
  190. #if TERRAIN_LAYER_COUNT >= 1
  191. v_texCoordTerrainLayer1 = a_texCoord * v_texCoordTerrainRepeat1;
  192. #if TERRAIN_LAYER_COUNT >= 2
  193. v_texCoordTerrainLayer2 = a_texCoord * v_texCoordTerrainRepeat2;
  194. #if TERRAIN_LAYER_COUNT >= 3
  195. v_texCoordTerrainLayer3 = a_texCoord * v_texCoordTerrainRepeat3;
  196. #if TERRAIN_LAYER_COUNT >= 4
  197. v_texCoordTerrainLayer4 = a_texCoord * v_texCoordTerrainRepeat4;
  198. #endif
  199. #endif
  200. #endif
  201. #endif
  202. #if defined(FOG)
  203. // linear fog
  204. #if FOG == 1
  205. float z = gl_Position.z;//(u_worldViewMatrix * position).z;
  206. v_fogFactor = (u_fogparam.z - z) / (u_fogparam.z - u_fogparam.y);
  207. v_fogFactor = clamp(v_fogFactor, 0.0, 1.0);
  208. // exp fog
  209. #elif FOG == 2
  210. float z = gl_Position.z;//(u_worldViewMatrix * position).z;
  211. const float LOG2 = 1.442695;
  212. v_fogFactor = exp2( -u_fogparam.x *
  213. z *
  214. LOG2 );
  215. v_fogFactor = clamp(v_fogFactor, 0.0, 1.0);
  216. // exp2 fog
  217. #elif FOG == 3
  218. float z = gl_Position.z;//(u_worldViewMatrix * position).z;
  219. const float LOG2 = 1.442695;
  220. v_fogFactor = exp2( -u_fogparam.x *
  221. u_fogparam.x *
  222. z *
  223. z *
  224. LOG2 );
  225. v_fogFactor = clamp(v_fogFactor, 0.0, 1.0);
  226. #endif
  227. #endif
  228. }