获取相机坐标系;然后对xy轴求偏导,根据两个偏导叉积获取法线,通过点乘法线和向上向量确定颜色混合;3dtiles模型customshader也可用此方法单独加积雪效果;此原理还可以用来对3dtiles的顶面和侧面做不同类型贴图。
const fragmentShaderSource = `
uniform sampler2D colorTexture;
uniform sampler2D depthTexture;
in vec2 v_textureCoordinates;
uniform vec3 snowColor;
void main() {
vec4 color = texture(colorTexture, v_textureCoordinates);
out_FragColor =color;
float depth =czm_unpackDepth(texture(depthTexture, v_textureCoordinates));
if(depth>=1.)return;
vec4 eyeCoordinate4 = czm_windowToEyeCoordinates(gl_FragCoord.xy, depth);
vec4 positionEC =eyeCoordinate4/eyeCoordinate4.w;
vec3 dx = dFdx(positionEC.xyz);
vec3 dy = dFdy(positionEC.xyz);
vec3 nor = normalize(cross(dx, dy));
vec4 positionWC = normalize(czm_inverseView * positionEC);
vec3 normalWC = normalize(czm_inverseViewRotation * nor);
float dotNumWC = dot(positionWC.xyz, normalWC);
out_FragColor = mix(color, vec4(snowColor,1.0), dotNumWC);
}
`;
const postProcessStage = new Cesium.PostProcessStage({
fragmentShader: fragmentShaderSource,
uniforms: {
snowColor: Cesium.Color.WHITE,
},
})
viewer.scene.postProcessStages.add(postProcessStage);