【发布时间】:2020-04-14 08:26:28
【问题描述】:
我正在尝试将 2 个图像加载为纹理,在片段着色器中在它们之间进行插值并将颜色应用于平面。不幸的是,我什至无法显示单个纹理。
我正在创建平面并按如下方式加载图像:
const planeGeometry = new THREE.PlaneBufferGeometry(imageSize * screenRatio, imageSize);
loader.load(
"textures/IMG_6831.jpeg",
(image) => {
texture1 = new THREE.MeshBasicMaterial({ map: image });
//texture1 = new THREE.Texture({ image: image });
}
)
当我像往常一样直接使用 MeshBasicMaterial 作为 Mesh 的材质时,它可以正确地在平面上显示图像。当尝试在自定义着色器中做同样的事情时,我只得到黑色:
const uniforms = {
texture1: { type: "sampler2D", value: texture1 },
texture2: { type: "sampler2D", value: texture2 }
};
const planeMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
fragmentShader: fragmentShader(),
vertexShader: vertexShader()
});
const plane = new THREE.Mesh(planeGeometry, planeMaterial);
scene.add(plane);
着色器:
const vertexShader = () => {
return `
varying vec2 vUv;
void main() {
vUv = uv;
vec4 modelViewPosition = modelViewMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * modelViewPosition;
}
`;
}
const fragmentShader = () => {
return `
uniform sampler2D texture1;
uniform sampler2D texture2;
varying vec2 vUv;
void main() {
vec4 color1 = texture2D(texture1, vUv);
vec4 color2 = texture2D(texture2, vUv);
//vec4 fColor = mix(color1, color2, vUv.y);
//fColor.a = 1.0;
gl_FragColor = color1;
}
`;
}
为了修复它,我尝试了:
- 通过使用简单的颜色对其进行着色来确保平面可见
- 通过将 uv 坐标可视化为颜色来确保将 uv 坐标传递给着色器
- 确保在将
texture1和texture2传递给着色器之前定义它们 - 使用
THREE.Texture而不是THREE.MeshBasicMaterial - 更改 js 中的统一类型
texture1: { type: "t", value: texture1 },
我认为问题可能在于将纹理作为统一传递给着色器。我可能在某处使用了错误的类型。如果有任何帮助,我将不胜感激!
【问题讨论】:
标签: javascript three.js shader