您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字

140 行
3.5 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)
  18. #define LIGHTING
  19. #endif
  20. ///////////////////////////////////////////////////////////
  21. // Uniforms
  22. uniform vec3 u_ambientColor;
  23. #if defined(LIGHTING)
  24. #if (DIRECTIONAL_LIGHT_COUNT > 0)
  25. uniform vec3 u_directionalLightColor[DIRECTIONAL_LIGHT_COUNT];
  26. uniform vec3 u_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
  27. #endif
  28. #if (POINT_LIGHT_COUNT > 0)
  29. uniform vec3 u_pointLightColor[POINT_LIGHT_COUNT];
  30. uniform vec3 u_pointLightPosition[POINT_LIGHT_COUNT];
  31. uniform float u_pointLightRangeInverse[POINT_LIGHT_COUNT];
  32. #endif
  33. #if (SPOT_LIGHT_COUNT > 0)
  34. uniform vec3 u_spotLightColor[SPOT_LIGHT_COUNT];
  35. uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT];
  36. uniform float u_spotLightRangeInverse[SPOT_LIGHT_COUNT];
  37. uniform float u_spotLightInnerAngleCos[SPOT_LIGHT_COUNT];
  38. uniform float u_spotLightOuterAngleCos[SPOT_LIGHT_COUNT];
  39. #endif
  40. #if defined (NORMAL_MAP)
  41. uniform sampler2D u_normalMap;
  42. uniform mat4 u_normalMatrix;
  43. #endif
  44. #endif
  45. #if defined(DEBUG_PATCHES)
  46. uniform float u_row;
  47. uniform float u_column;
  48. #endif
  49. #if (LAYER_COUNT > 0)
  50. uniform sampler2D u_surfaceLayerMaps[SAMPLER_COUNT];
  51. #endif
  52. ///////////////////////////////////////////////////////////
  53. // Variables
  54. vec4 _baseColor;
  55. ///////////////////////////////////////////////////////////
  56. // Varyings
  57. #if defined(LIGHTING)
  58. #if !defined(NORMAL_MAP)
  59. varying vec3 v_normalVector;
  60. #else
  61. vec3 v_normalVector;
  62. #endif
  63. #endif
  64. varying vec2 v_texCoord0;
  65. #if (LAYER_COUNT > 0)
  66. varying vec2 v_texCoordLayer0;
  67. #endif
  68. #if (LAYER_COUNT > 1)
  69. varying vec2 v_texCoordLayer1;
  70. #endif
  71. #if (LAYER_COUNT > 2)
  72. varying vec2 v_texCoordLayer2;
  73. #endif
  74. #if (LAYER_COUNT > 1)
  75. void blendLayer(sampler2D textureMap, vec2 texCoord, float alphaBlend)
  76. {
  77. vec3 diffuse = texture2D(textureMap, mod(texCoord, vec2(1,1))).rgb;
  78. _baseColor.rgb = _baseColor.rgb * (1.0 - alphaBlend) + diffuse * alphaBlend;
  79. }
  80. #endif
  81. #if defined(LIGHTING)
  82. #include "lighting.frag"
  83. #endif
  84. void main()
  85. {
  86. #if (LAYER_COUNT > 0)
  87. // Sample base texture
  88. _baseColor.rgb = texture2D(u_surfaceLayerMaps[TEXTURE_INDEX_0], mod(v_texCoordLayer0, vec2(1,1))).rgb;
  89. _baseColor.a = 1.0;
  90. #else
  91. // If no layers are defined, simply use a white color
  92. _baseColor = vec4(1, 1, 1, 1);
  93. #endif
  94. #if (LAYER_COUNT > 1)
  95. blendLayer(u_surfaceLayerMaps[TEXTURE_INDEX_1], v_texCoordLayer1, texture2D(u_surfaceLayerMaps[BLEND_INDEX_1], v_texCoord0)[BLEND_CHANNEL_1]);
  96. #endif
  97. #if (LAYER_COUNT > 2)
  98. blendLayer(u_surfaceLayerMaps[TEXTURE_INDEX_2], v_texCoordLayer2, texture2D(u_surfaceLayerMaps[BLEND_INDEX_2], v_texCoord0)[BLEND_CHANNEL_2]);
  99. #endif
  100. #if defined(DEBUG_PATCHES)
  101. float tint = mod(u_row + mod(u_column, 2.0), 2.0);
  102. _baseColor.rgb = _baseColor.rgb * 0.75 + vec3(1.0-tint, tint, 0) * 0.25;
  103. #endif
  104. #if defined(LIGHTING)
  105. #if defined(NORMAL_MAP)
  106. v_normalVector = texture2D(u_normalMap, v_texCoord0).xyz * 2.0 - 1.0;
  107. v_normalVector = (u_normalMatrix * vec4(v_normalVector.x, v_normalVector.y, v_normalVector.z, 0)).xyz;
  108. #endif
  109. gl_FragColor.a = _baseColor.a;
  110. gl_FragColor.rgb = getLitPixel();
  111. #else
  112. gl_FragColor.rgb = _baseColor.rgb;
  113. #endif
  114. }