gpt4 book ai didi

javascript - 在 three.js 中为形状的各个面着色

转载 作者:搜寻专家 更新时间:2023-11-01 05:24:27 25 4
gpt4 key购买 nike

我正在扩展一个显示旋转立方体的简单示例,我想更改各个面的颜色,但没有任何运气。这段代码有什么问题?谢谢

$(function(){
var camera, scene, renderer,
geometry, material, mesh;

init();
animate();

function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 800;

geometry = new THREE.CubeGeometry( 500, 70, 70 );

material_1 = new THREE.MeshBasicMaterial( {
color: 0xff0000,
shading: THREE.FlatShading,
overdraw: true
} );
material_2 = new THREE.MeshBasicMaterial( {
color: 0x00ff00,
shading: THREE.FlatShading,
overdraw: true
} );
geometry.materials = [material_1, material_2];
geometry.faces[0].materialIndex = 0;
geometry.faces[1].materialIndex = 1;
geometry.faces[2].materialIndex = 0;
geometry.faces[3].materialIndex = 1;
geometry.faces[4].materialIndex = 0;
geometry.faces[5].materialIndex = 1;
mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );

scene.add( mesh );
renderer = new THREE.CanvasRenderer();
renderer.setClearColorHex(0x000000, 1);
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
}

function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.05;
renderer.render( scene, camera );
}
});

最佳答案

您需要将一组 Material 作为参数传递给 MeshFaceMaterial 构造函数。

代替:

 mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );

你应该:

 mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( geometry.materials ) );

关于javascript - 在 three.js 中为形状的各个面着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14713997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com