Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

275 řádky
6.3 KiB

  1. #ifdef OPENGL_ES
  2. #ifdef GL_FRAGMENT_PRECISION_HIGH
  3. precision highp float;
  4. #else
  5. precision mediump float;
  6. #endif
  7. #endif
  8. #ifndef DIRECTIONAL_LIGHT_COUNT
  9. #define DIRECTIONAL_LIGHT_COUNT 0
  10. #endif
  11. #ifndef SPOT_LIGHT_COUNT
  12. #define SPOT_LIGHT_COUNT 0
  13. #endif
  14. #ifndef POINT_LIGHT_COUNT
  15. #define POINT_LIGHT_COUNT 0
  16. #endif
  17. #if (DIRECTIONAL_LIGHT_COUNT > 0) || (POINT_LIGHT_COUNT > 0) || (SPOT_LIGHT_COUNT > 0) || defined(OUTLINE) || defined(RIM_LIGHTING)
  18. #define LIGHTING
  19. #endif
  20. #ifndef TERRAIN_LAYER_COUNT
  21. #define TERRAIN_LAYER_COUNT 0
  22. #endif
  23. ///////////////////////////////////////////////////////////
  24. // Uniforms
  25. uniform vec3 u_ambientColor;
  26. uniform sampler2D u_diffuseTexture;
  27. // 地形混合权重纹理
  28. uniform sampler2D u_splattingTexture;
  29. // 地形混合表面纹理
  30. #if TERRAIN_LAYER_COUNT > 1
  31. uniform sampler2D u_surfaceLayerMap2;
  32. #if TERRAIN_LAYER_COUNT > 2
  33. uniform sampler2D u_surfaceLayerMap3;
  34. #if TERRAIN_LAYER_COUNT > 3
  35. uniform sampler2D u_surfaceLayerMap4;
  36. #endif
  37. #endif
  38. #endif
  39. #if defined(LIGHTMAP)
  40. uniform sampler2D u_lightmapTexture;
  41. #endif
  42. #if defined(EMISSIVEMAP)
  43. uniform sampler2D u_emissiveTexture;
  44. #endif
  45. #if defined(SPECULARMAP)
  46. uniform sampler2D u_specularTexture;
  47. #endif
  48. #if defined(LIGHTING)
  49. #if defined(BUMPED)
  50. uniform sampler2D u_normalmapTexture;
  51. #endif
  52. #if (DIRECTIONAL_LIGHT_COUNT > 0)
  53. uniform vec3 u_directionalLightColor[DIRECTIONAL_LIGHT_COUNT];
  54. #if !defined(BUMPED)
  55. uniform vec3 u_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
  56. #endif
  57. #endif
  58. #if (POINT_LIGHT_COUNT > 0)
  59. uniform vec3 u_pointLightColor[POINT_LIGHT_COUNT];
  60. uniform vec3 u_pointLightPosition[POINT_LIGHT_COUNT];
  61. uniform float u_pointLightRangeInverse[POINT_LIGHT_COUNT];
  62. #endif
  63. #if (SPOT_LIGHT_COUNT > 0)
  64. uniform vec3 u_spotLightColor[SPOT_LIGHT_COUNT];
  65. uniform float u_spotLightRangeInverse[SPOT_LIGHT_COUNT];
  66. uniform float u_spotLightInnerAngleCos[SPOT_LIGHT_COUNT];
  67. uniform float u_spotLightOuterAngleCos[SPOT_LIGHT_COUNT];
  68. #if !defined(BUMPED)
  69. uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT];
  70. #endif
  71. #endif
  72. #if defined(SPECULAR)
  73. uniform float u_specularExponent;
  74. #endif
  75. #endif
  76. #if defined(MODULATE_COLOR)
  77. uniform vec3 u_modulateColor;
  78. #endif
  79. #if defined(ADDITIVE_COLOR)
  80. uniform vec3 u_additiveColor;
  81. #endif
  82. #if defined(BLEND_COLOR)
  83. uniform vec4 u_blendColor;
  84. #endif
  85. #if defined(MODULATE_ALPHA)
  86. uniform float u_modulateAlpha;
  87. #endif
  88. ///////////////////////////////////////////////////////////
  89. // Variables
  90. vec4 _baseColor;
  91. ///////////////////////////////////////////////////////////
  92. // Varyings
  93. varying vec2 v_texCoord;
  94. varying vec4 v_color;
  95. #if defined(LIGHTMAP) || defined(EMISSIVEMAP)
  96. varying vec2 v_texCoord1;
  97. #endif
  98. #if defined(LIGHTING)
  99. #if !defined(BUMPED)
  100. varying vec3 v_normalVector;
  101. #endif
  102. #if defined(BUMPED) && (DIRECTIONAL_LIGHT_COUNT > 0)
  103. varying vec3 v_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
  104. #endif
  105. #if (POINT_LIGHT_COUNT > 0)
  106. varying vec3 v_vertexToPointLightDirection[POINT_LIGHT_COUNT];
  107. #endif
  108. #if (SPOT_LIGHT_COUNT > 0)
  109. varying vec3 v_vertexToSpotLightDirection[SPOT_LIGHT_COUNT];
  110. #if defined(BUMPED)
  111. varying vec3 v_spotLightDirection[SPOT_LIGHT_COUNT];
  112. #endif
  113. #endif
  114. #if defined(SPECULAR)
  115. varying vec3 v_cameraDirection;
  116. #endif
  117. #include "lighting.frag"
  118. #endif
  119. #if defined(SPECULAR) || defined(RIM_LIGHTING)
  120. uniform vec3 u_cameraPosition;
  121. #endif
  122. // 内发光
  123. #if defined(RIM_LIGHTING)
  124. varying vec4 v_worldPosition;
  125. varying vec3 v_worldNormal;
  126. // 内边缘发光的强度
  127. uniform float u_rimPower;
  128. // 内边缘发光的颜色
  129. uniform vec4 u_rimColor;
  130. #endif
  131. // alphareject
  132. #if defined(TEXTURE_DISCARD_ALPHA)
  133. uniform float u_alphaReject;
  134. #endif
  135. // 地形坐标
  136. #if defined(TERRAIN_LAYER_COUNT)
  137. varying vec2 v_texCoordTerrainLayer1;
  138. #if TERRAIN_LAYER_COUNT > 1
  139. varying vec2 v_texCoordTerrainLayer2;
  140. #if TERRAIN_LAYER_COUNT > 2
  141. varying vec2 v_texCoordTerrainLayer3;
  142. #if TERRAIN_LAYER_COUNT > 3
  143. varying vec2 v_texCoordTerrainLayer4;
  144. #endif
  145. #endif
  146. #endif
  147. #endif
  148. uniform vec4 u_fogcolor;
  149. 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
  150. varying float v_fogFactor; //fog factor
  151. void blendLayer(sampler2D textureMap, vec2 texCoord, float alphaBlend)
  152. {
  153. vec3 diffuse = texture2D(textureMap, mod(texCoord, vec2(1,1))).rgb;
  154. _baseColor.rgb += diffuse * alphaBlend;
  155. }
  156. void main()
  157. {
  158. #if TERRAIN_LAYER_COUNT > 0
  159. vec4 splattingColor = texture2D(u_splattingTexture, v_texCoord);
  160. _baseColor.rgb = texture2D(u_diffuseTexture, mod(v_texCoordTerrainLayer1, vec2(1,1))).rgb * splattingColor.r;
  161. _baseColor.a = 1.0;
  162. #else
  163. _baseColor = texture2D(u_diffuseTexture, v_texCoord);
  164. #endif
  165. #if defined(VERTEX_COLOR)
  166. _baseColor *= v_color;
  167. #endif
  168. gl_FragColor.a = _baseColor.a;
  169. #if defined(TEXTURE_DISCARD_ALPHA)
  170. if (gl_FragColor.a < u_alphaReject)
  171. discard;
  172. #endif
  173. // 计算地形混合
  174. #if TERRAIN_LAYER_COUNT > 1
  175. blendLayer(u_surfaceLayerMap2, v_texCoordTerrainLayer2, splattingColor.g);
  176. #if TERRAIN_LAYER_COUNT > 2
  177. blendLayer(u_surfaceLayerMap3, v_texCoordTerrainLayer3, splattingColor.b);
  178. #if TERRAIN_LAYER_COUNT > 3
  179. blendLayer(u_surfaceLayerMap4, v_texCoordTerrainLayer4, splattingColor.a);
  180. #endif
  181. #endif
  182. #endif
  183. #if defined(LIGHTING)
  184. gl_FragColor.rgb = getLitPixel();
  185. #else
  186. gl_FragColor.rgb = _baseColor.rgb;
  187. #endif
  188. #if defined(LIGHTMAP)
  189. vec4 lightColor = texture2D(u_lightmapTexture, v_texCoord1);
  190. gl_FragColor.rgb *= lightColor.rgb;
  191. #endif
  192. #if defined(EMISSIVEMAP)
  193. vec4 emissiveColor = texture2D(u_emissiveTexture, v_texCoord1);
  194. gl_FragColor.rgb += emissiveColor.rgb;
  195. #endif
  196. #if defined(EMISSIVE_SELF)
  197. gl_FragColor.rgb += gl_FragColor.rgb;
  198. #endif
  199. #if defined(RIM_LIGHTING)
  200. float dotResult = saturate(dot(normalize(u_cameraPosition - v_worldPosition), normalize(v_worldNormal)));
  201. float rim = 1.0 - dotResult;
  202. vec3 emission = u_rimColor.rgb * pow (rim, u_rimPower);
  203. gl_FragColor.rgb += emission;
  204. #endif
  205. #if defined(MODULATE_COLOR)
  206. gl_FragColor.rgb *= u_modulateColor;
  207. #endif
  208. #if defined(ADDITIVE_COLOR)
  209. gl_FragColor.rgb += u_additiveColor;
  210. #endif
  211. #if defined(BLEND_COLOR)
  212. gl_FragColor.rgb = mix(gl_FragColor.rgb , u_blendColor.rgb , u_blendColor.a);
  213. #endif
  214. #if defined(MODULATE_ALPHA)
  215. gl_FragColor.a *= u_modulateAlpha;
  216. #endif
  217. #if defined(FOG)
  218. gl_FragColor.rgb = mix(u_fogcolor.rgb, gl_FragColor.rgb, v_fogFactor);
  219. #endif
  220. }