uniform float opacity; //uniform float mapScale; uniform vec3 baseColor; #if defined use_map varying vec2 vUv; uniform sampler2D map; uniform vec3 mapColor; vec4 applyMapColor(vec4 color){ vec2 uv = vUv; /*if(mapScale != 1.0){ uv = (vUv - 0.5) / mapScale + 0.5; if(uv.x > 1.0 || uv.y > 1.0 || uv.x < 0.0 || uv.y < 0.0){ //color = vec4(0.0,0.0,0.0,0.0); discard; } }*/ vec4 colorFromMap = texture2D(map, uv); #if defined mapOverlay //贴图叠加在基础色上,而非相乘 colorFromMap.rgb *= mapColor; color = color * (1.0-colorFromMap.a) + colorFromMap; #else color *= colorFromMap; #endif return color; } #endif #if defined(GL_EXT_frag_depth) && defined(useDepth) //似乎通过gl.getExtension('EXT_frag_depth')得到的GL_EXT_frag_depth uniform sampler2D depthTexture; uniform vec2 resolution; uniform vec2 viewportOffset; // viewportOffset 范围从0-整个画布的像素 uniform vec3 backColor; uniform float occlusionDistance; uniform float clipDistance; uniform float startClipDis; uniform float startOcclusDis; uniform float maxClipFactor; uniform float maxOcclusionFactor; //uniform bool uUseOrthographicCamera; #endif #if defined(GL_EXT_frag_depth) && defined(useDepth) || defined(FadeFar) uniform float nearPlane; uniform float farPlane; float convertToLinear(float zValue){ //if(uUseOrthographicCamera){ // return zValue*(farPlane-nearPlane)+nearPlane; //}else{ float z = zValue * 2.0 - 1.0; return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane)); //} } #endif #if defined(FadeFar) uniform float fadeFar; #endif //#include void main() { //#include vec4 color = vec4(baseColor, opacity); #if defined(GL_EXT_frag_depth) && defined(useDepth) || defined(FadeFar) float fragDepth = convertToLinear(gl_FragCoord.z); #if defined(FadeFar) float fadeOutFar = fadeFar * 1.3; //完全消失距离 if(fragDepth > fadeOutFar){ discard; }else if(fragDepth > fadeFar){ color.a *= (fadeOutFar - fragDepth) / (fadeOutFar - fadeFar); } #endif float mixFactor = 0.0; float clipFactor = 0.0; #if defined(GL_EXT_frag_depth) && defined(useDepth) vec2 depthTxtCoords = vec2(gl_FragCoord.x-viewportOffset.x, gl_FragCoord.y - viewportOffset.y) / resolution; //gl_FragCoord大小为 viewport client大小 float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r); float delta = fragDepth - textureDepth; if (delta > 0.0)//差距 { // occlusionDistance and clipDistance define the width of the respective zones and // mixFactor and clipFactor express the interpolation between the two colors depending on the position // of the current fragment withing those zones. mixFactor = clamp((delta - startOcclusDis) / (occlusionDistance - startOcclusDis), 0.0, maxOcclusionFactor); clipFactor = clamp((delta - startClipDis) / (clipDistance - startClipDis), 0.0, maxClipFactor); } #endif // If the fragment is totally transparent, don't bother drawing it if (clipFactor == 1.0) { discard; }else{ #if defined use_map color = applyMapColor(color); #endif #if defined(GL_EXT_frag_depth) && defined(useDepth) float r = pow(1.0 - clipFactor, 3.0); //更透明些 color = vec4(mix(color.rgb, backColor, mixFactor), color.a * r); #endif } #else #if defined use_map color = applyMapColor(color); #endif #endif gl_FragColor = color; }