【发布时间】:2020-03-01 14:09:00
【问题描述】:
我在threejs 上使用动态纹理。当我使用不同形状的对象时,纹理无法正常工作。但是,当我使用 boxGeometry 时,它工作正常。
setCanvas =()=>{
this.canvas = window.document.getElementById("canvas");
var ctx = this.canvas.getContext("2d");
ctx.font = "20pt Arial";
ctx.fillStyle = "red";
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
ctx.fillStyle = "white";
ctx.fillRect(10, 10, this.canvas.width - 20, this.canvas.height - 20);
ctx.fillStyle = "yellow";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("asdasdasd", this.canvas.width / 2, this.canvas.height / 2);
}
不同形状的对象创建者
threeObjectCreator(mesh, position, color) {
const object = this.objectLoader.parse(mesh);
object.position.set(position.x, position.z, position.y);
object.rotation.x = Math.PI / 2;
let texture = new THREE.Texture(this.canvas);
object.material = new THREE.MeshPhongMaterial({
color,
side: THREE.DoubleSide,
shininess: 30,
//flatShading: THREE.FlatShading,
transparent: false,
map:texture
});
texture.needsUpdate = true;
return object;
}
带盒子几何
let geometry = new THREE.BoxGeometry( 200, 200, 200 );
let mesh = new THREE.Mesh(geometry, material);
this.scene.add(mesh);
我该如何解决这个问题?
【问题讨论】:
-
如果你想使用纹理,你需要每个有纹理的面的 UV 坐标。根据您加载资源的方式,它们可能会或可能不会附带此映射,因此您需要制作/生成自己的 UV 映射,例如:stackoverflow.com/questions/20774648/…(UV 映射告诉如何将纹理转换为面部)
-
感谢您的评论。你能把它作为答案再次发送吗?我想给你点。
标签: javascript reactjs three.js