three.js:如何将材质数组添加到八面体几何体中

时间:2018-02-11 08:07:59

标签: javascript three.js

我想向THREE.OctahedronGeometry添加多个素材。这适用于盒子对象,但对于THREE.OctahedronGeometry我只是获得所有面上的第一个素材。如何在每张脸上获得不同的材料?

        var geometry = new THREE.OctahedronGeometry(1, 0);

        var materials = [
            new THREE.MeshPhongMaterial( { color: 0x050505, dithering: true } ),
            new THREE.MeshPhongMaterial( { color: 0xeeeeee, dithering: true } ),
            new THREE.MeshPhongMaterial( { color: 0x060606, dithering: true } ),
            new THREE.MeshPhongMaterial( { color: 0xeeeeee, dithering: true } ),
            new THREE.MeshPhongMaterial( { color: 0x050505, dithering: true } ),
            new THREE.MeshPhongMaterial( { color: 0xeeeeee, dithering: true } ),
            new THREE.MeshPhongMaterial( { color: 0x070707, dithering: true } ),
            new THREE.MeshPhongMaterial( { color: 0xeeeeee, dithering: true } )
        ];

        var someMesh = new THREE.SceneUtils.createMultiMaterialObject( geometry , materials );

        someMesh.castShadow = true; //default is false
        someMesh.receiveShadow = true; //default
        scene.add( someMesh );

1 个答案:

答案 0 :(得分:2)

您可以通过指定组向任何THREE.BufferGeometry添加对多材料的支持。使用这样的模式:

var geometry = new THREE.OctahedronBufferGeometry( 5, 0 );

geometry.clearGroups(); // just in case
geometry.addGroup( 0, 3, 0 ); // first 3 vertices use material 0
geometry.addGroup( 3, 3, 1 ); // next 3 vertices use material 1
geometry.addGroup( 6, Infinity, 2 ); // remaining vertices use material 2

var materials = [
    new THREE.MeshBasicMaterial( { color: 0xff0000 } ),
    new THREE.MeshBasicMaterial( { color: 0x00ff00 } ),
    new THREE.MeshBasicMaterial( { color: 0x0000ff } ),
];


var mesh = new THREE.Mesh( geometry, materials );
scene.add( mesh );

如果必须使用THREE.Geometry,则可以通过为每个面指定材质索引来添加对多材质的支持。请参阅three.js updating geometry face materialindex

如果您想要做的就是在每个面上使用不同的颜色,则指定面颜色或顶点颜色并使用单个材质渲染整个几何体更有效。 (但这是一个单独的问题。)

three.js r.89