在Lambert和Phong

时间:2017-04-29 10:53:02

标签: three.js

有没有办法通过更改object.material的atttribute .type而不是用新材料重新加载整个对象,将材质类型从Lambert更改为Phong(和反向)?

1 个答案:

答案 0 :(得分:1)

你不必丢失属性。如果您正在使用THREE.BufferGeometry,则可以改为使用群组。

<强>组

var boxGeo = new THREE.BoxBufferGeometry(10, 10, 10);

// set up your groups
boxGeo.clearGroups();
boxGeo.addGroup(0, boxGeo.index.count, 0);

<强>材料

在THREE.js r85之前

您将使用THREE.MultiMaterial

// add both of your materials to a multi-material
var mm = new THREE.MultiMaterial([
    new THREE.MeshLambertMaterial({color: "red"}),
    new THREE.MeshPhongMaterial({color: "red"}),
]);

var mesh = new THREE.Mesh(boxGeo, mm);

对于THREE.js r85及以上

您只需将一系列材料传递给THREE.Mesh

// add both of your materials to a multi-material
var mm = [
    new THREE.MeshLambertMaterial({color: "red"}),
    new THREE.MeshPhongMaterial({color: "red"}),
];

var mesh = new THREE.Mesh(boxGeo, mm);

交换材料

然后当你想要交换材料时:

if(mesh.geometry.groups[0].materialIndex === 0){
    mesh.geometry.groups[0].materialIndex = 1;
}
else{
    mesh.geometry.groups[0].materialIndex = 0
}