#ifdef OPENGL_ES #ifdef GL_FRAGMENT_PRECISION_HIGH precision highp float; #else precision mediump float; #endif #endif #ifndef DIRECTIONAL_LIGHT_COUNT #define DIRECTIONAL_LIGHT_COUNT 0 #endif #ifndef SPOT_LIGHT_COUNT #define SPOT_LIGHT_COUNT 0 #endif #ifndef POINT_LIGHT_COUNT #define POINT_LIGHT_COUNT 0 #endif #if (DIRECTIONAL_LIGHT_COUNT > 0) || (POINT_LIGHT_COUNT > 0) || (SPOT_LIGHT_COUNT > 0) || defined(OUTLINE) || defined(RIM_LIGHTING) #define LIGHTING #endif #ifndef TERRAIN_LAYER_COUNT #define TERRAIN_LAYER_COUNT 0 #endif /////////////////////////////////////////////////////////// // Uniforms uniform vec3 u_ambientColor; uniform sampler2D u_diffuseTexture; // 地形混合权重纹理 uniform sampler2D u_splattingTexture; // 地形混合表面纹理 #if TERRAIN_LAYER_COUNT > 1 uniform sampler2D u_surfaceLayerMap2; #if TERRAIN_LAYER_COUNT > 2 uniform sampler2D u_surfaceLayerMap3; #if TERRAIN_LAYER_COUNT > 3 uniform sampler2D u_surfaceLayerMap4; #endif #endif #endif #if defined(LIGHTMAP) uniform sampler2D u_lightmapTexture; #endif #if defined(EMISSIVEMAP) uniform sampler2D u_emissiveTexture; #endif #if defined(SPECULARMAP) uniform sampler2D u_specularTexture; #endif #if defined(LIGHTING) #if defined(BUMPED) uniform sampler2D u_normalmapTexture; #endif #if (DIRECTIONAL_LIGHT_COUNT > 0) uniform vec3 u_directionalLightColor[DIRECTIONAL_LIGHT_COUNT]; #if !defined(BUMPED) uniform vec3 u_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT]; #endif #endif #if (POINT_LIGHT_COUNT > 0) uniform vec3 u_pointLightColor[POINT_LIGHT_COUNT]; uniform vec3 u_pointLightPosition[POINT_LIGHT_COUNT]; uniform float u_pointLightRangeInverse[POINT_LIGHT_COUNT]; #endif #if (SPOT_LIGHT_COUNT > 0) uniform vec3 u_spotLightColor[SPOT_LIGHT_COUNT]; uniform float u_spotLightRangeInverse[SPOT_LIGHT_COUNT]; uniform float u_spotLightInnerAngleCos[SPOT_LIGHT_COUNT]; uniform float u_spotLightOuterAngleCos[SPOT_LIGHT_COUNT]; #if !defined(BUMPED) uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT]; #endif #endif #if defined(SPECULAR) uniform float u_specularExponent; #endif #endif #if defined(MODULATE_COLOR) uniform vec3 u_modulateColor; #endif #if defined(ADDITIVE_COLOR) uniform vec3 u_additiveColor; #endif #if defined(BLEND_COLOR) uniform vec4 u_blendColor; #endif #if defined(MODULATE_ALPHA) uniform float u_modulateAlpha; #endif /////////////////////////////////////////////////////////// // Variables vec4 _baseColor; /////////////////////////////////////////////////////////// // Varyings varying vec2 v_texCoord; varying vec4 v_color; #if defined(LIGHTMAP) || defined(EMISSIVEMAP) varying vec2 v_texCoord1; #endif #if defined(LIGHTING) #if !defined(BUMPED) varying vec3 v_normalVector; #endif #if defined(BUMPED) && (DIRECTIONAL_LIGHT_COUNT > 0) varying vec3 v_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT]; #endif #if (POINT_LIGHT_COUNT > 0) varying vec3 v_vertexToPointLightDirection[POINT_LIGHT_COUNT]; #endif #if (SPOT_LIGHT_COUNT > 0) varying vec3 v_vertexToSpotLightDirection[SPOT_LIGHT_COUNT]; #if defined(BUMPED) varying vec3 v_spotLightDirection[SPOT_LIGHT_COUNT]; #endif #endif #if defined(SPECULAR) varying vec3 v_cameraDirection; #endif #include "lighting.frag" #endif #if defined(SPECULAR) || defined(RIM_LIGHTING) uniform vec3 u_cameraPosition; #endif // 内发光 #if defined(RIM_LIGHTING) varying vec4 v_worldPosition; varying vec3 v_worldNormal; // 内边缘发光的强度 uniform float u_rimPower; // 内边缘发光的颜色 uniform vec4 u_rimColor; #endif // alphareject #if defined(TEXTURE_DISCARD_ALPHA) uniform float u_alphaReject; #endif // 地形坐标 #if defined(TERRAIN_LAYER_COUNT) varying vec2 v_texCoordTerrainLayer1; #if TERRAIN_LAYER_COUNT > 1 varying vec2 v_texCoordTerrainLayer2; #if TERRAIN_LAYER_COUNT > 2 varying vec2 v_texCoordTerrainLayer3; #if TERRAIN_LAYER_COUNT > 3 varying vec2 v_texCoordTerrainLayer4; #endif #endif #endif #endif uniform vec4 u_fogcolor; 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 varying float v_fogFactor; //fog factor void blendLayer(sampler2D textureMap, vec2 texCoord, float alphaBlend) { vec3 diffuse = texture2D(textureMap, mod(texCoord, vec2(1,1))).rgb; _baseColor.rgb += diffuse * alphaBlend; } void main() { #if TERRAIN_LAYER_COUNT > 0 vec4 splattingColor = texture2D(u_splattingTexture, v_texCoord); _baseColor.rgb = texture2D(u_diffuseTexture, mod(v_texCoordTerrainLayer1, vec2(1,1))).rgb * splattingColor.r; _baseColor.a = 1.0; #else _baseColor = texture2D(u_diffuseTexture, v_texCoord); #endif #if defined(VERTEX_COLOR) _baseColor *= v_color; #endif gl_FragColor.a = _baseColor.a; #if defined(TEXTURE_DISCARD_ALPHA) if (gl_FragColor.a < u_alphaReject) discard; #endif // 计算地形混合 #if TERRAIN_LAYER_COUNT > 1 blendLayer(u_surfaceLayerMap2, v_texCoordTerrainLayer2, splattingColor.g); #if TERRAIN_LAYER_COUNT > 2 blendLayer(u_surfaceLayerMap3, v_texCoordTerrainLayer3, splattingColor.b); #if TERRAIN_LAYER_COUNT > 3 blendLayer(u_surfaceLayerMap4, v_texCoordTerrainLayer4, splattingColor.a); #endif #endif #endif #if defined(LIGHTING) gl_FragColor.rgb = getLitPixel(); #else gl_FragColor.rgb = _baseColor.rgb; #endif #if defined(LIGHTMAP) vec4 lightColor = texture2D(u_lightmapTexture, v_texCoord1); gl_FragColor.rgb *= lightColor.rgb; #endif #if defined(EMISSIVEMAP) vec4 emissiveColor = texture2D(u_emissiveTexture, v_texCoord1); gl_FragColor.rgb += emissiveColor.rgb; #endif #if defined(EMISSIVE_SELF) gl_FragColor.rgb += gl_FragColor.rgb; #endif #if defined(RIM_LIGHTING) float dotResult = saturate(dot(normalize(u_cameraPosition - v_worldPosition), normalize(v_worldNormal))); float rim = 1.0 - dotResult; vec3 emission = u_rimColor.rgb * pow (rim, u_rimPower); gl_FragColor.rgb += emission; #endif #if defined(MODULATE_COLOR) gl_FragColor.rgb *= u_modulateColor; #endif #if defined(ADDITIVE_COLOR) gl_FragColor.rgb += u_additiveColor; #endif #if defined(BLEND_COLOR) gl_FragColor.rgb = mix(gl_FragColor.rgb , u_blendColor.rgb , u_blendColor.a); #endif #if defined(MODULATE_ALPHA) gl_FragColor.a *= u_modulateAlpha; #endif #if defined(FOG) gl_FragColor.rgb = mix(u_fogcolor.rgb, gl_FragColor.rgb, v_fogFactor); #endif }