我目前有一个自定义着色器在天穹上绘制渐变,并希望在天穹前面有一个太阳/月亮(从用户的角度来看)。最简单的方法是为太阳和月亮制作精灵,但出现的问题是精灵卡在天穹内(精灵一半在天穹前面,一半在天穹后面)。我试图用多边形偏移来解决这个问题,但这似乎不适用于精灵对象。
所以我的问题是,如何在天穹顶部设置太阳/月亮,而无需修改我的自定义天穹着色器以添加带有渐变的太阳/月亮纹理(这最终可能非常困难)?
下面是我的天穹天空着色器的代码。
new THREE.ShaderMaterial({
uniforms: {
glow: {
type: "t",
value: THREE.ImageUtils.loadTexture(DEFAULT_PATH+"glow2.png")
},
color: {
type: "t",
value: THREE.ImageUtils.loadTexture(DEFAULT_PATH+"sky2.png")
},
lightDir: {
type: "v3",
value: self.lightPos
}
},
vertexShader: [
"varying vec3 vWorldPosition;",
"varying vec3 vPosition;",
"void main() {",
"vec4 worldPosition = modelMatrix * vec4(position, 1.0);",
"vWorldPosition = worldPosition.xyz;",
"vPosition = position;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);",
"}"
].join("\n"),
fragmentShader: [
"uniform sampler2D glow;",
"uniform sampler2D color;",
"uniform vec3 lightDir;",
"varying vec3 vWorldPosition;",
"varying vec3 vPosition;",
"void main() {",
"vec3 V = normalize(vWorldPosition.xzy);",
"vec3 L = normalize(lightDir.xzy);",
"float vl = dot(V, L);",
"vec4 Kc = texture2D(color, vec2((L.y + 1.0) / 2.0, V.y));",
"vec4 Kg = texture2D(glow, vec2((L.y + 1.0) / 2.0, vl));",
"gl_FragColor = vec4(Kc.rgb + Kg.rgb * Kg.a / 2.0, Kc.a);",
"}",
].join("\n")
});