Three.js - 在运行时更改JSON材质

时间:2015-11-22 02:32:30

标签: javascript json three.js

好的,所以我用JSON加载器导入了一个多材质对象,我试图在运行时访问每种材质。
基本上我喜欢修改导入材料的颜色。

任何人都可以指导我如何完成它,或者它是否可能完成?

感谢。

JSON

{
    "vertices": [-0.889175,-0.389516,-0.407662,-0.889175,0.521047,-0.407662,-0.889175,-0.389516,-1.31822,-0.889175,0.521046,-1.31822,0.021387,-0.389516,-0.407662,0.021387,0.521047,-0.407662,0.021387,-0.389516,-1.31822,0.021387,0.521046,-1.31822],
    "normals": [-0.577349,-0.577349,-0.577349,-0.577349,-0.577349,0.577349,-0.577349,0.577349,0.577349,-0.577349,0.577349,-0.577349,0.577349,0.577349,-0.577349,0.577349,-0.577349,-0.577349,0.577349,0.577349,0.577349,0.577349,-0.577349,0.577349],
    "faces": [35,2,0,1,3,0,0,1,2,3,35,3,7,6,2,1,3,4,5,0,35,7,5,4,6,0,4,6,7,5,35,0,4,5,1,1,1,7,6,2,35,0,2,6,4,1,1,0,5,7,35,5,7,3,1,1,6,4,3,2],
    "name": "CubeGeometry.2",
    "uvs": [],
    "colors": [],
    "metadata": {
        "version": 3,
        "normals": 8,
        "faces": 6,
        "uvs": 0,
        "colors": 0,
        "materials": 2,
        "vertices": 8,
        "generator": "io_three",
        "type": "Geometry"
    },
    "materials": [{
        "specularCoef": 50,
        "DbgIndex": 1,
        "wireframe": false,
        "opacity": 1,
        "visible": true,
        "DbgName": "material2",
        "depthTest": true,
        "blending": "NormalBlending",
        "transparent": false,
        "shading": "phong",
        "colorEmissive": [0,0,0],
        "colorDiffuse": [0.64,0.64,0.64],
        "DbgColor": 15597568,
        "depthWrite": true,
        "colorSpecular": [0.5,0.5,0.5],
        "vertexColors": false,
        "colorAmbient": [0.64,0.64,0.64]
    },{
        "specularCoef": 50,
        "DbgIndex": 0,
        "wireframe": false,
        "opacity": 1,
        "visible": true,
        "DbgName": "material1",
        "depthTest": true,
        "blending": "NormalBlending",
        "transparent": false,
        "shading": "phong",
        "colorEmissive": [0,0,0],
        "colorDiffuse": [0.64,0.64,0.64],
        "DbgColor": 15658734,
        "depthWrite": true,
        "colorSpecular": [0.5,0.5,0.5],
        "vertexColors": false,
        "colorAmbient": [0.64,0.64,0.64]
    }]
}

JSON加载程序并添加到场景

var loader = new THREE.JSONLoader();

loader.load( "test.js", function(geometry, materials){
var mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
scene.add(mesh);
});

1 个答案:

答案 0 :(得分:2)

如果您使用function unless(test, then) { if (!test) then(); } function repeat(times, body) { for (var i = 0; i < times; i++) body(i); } repeat(3, function(n) { unless(n % 2, function() { console.log(n, "is even"); }); }); // → 0 is even // → 2 is even - 以前为MultiMaterial - 您可以访问和更改材料颜色,如下所示:

MeshFaceMaterial

研究mesh.material.materials[ 0 ].color.set( 0xff0000 ); mesh.material.materials[ 1 ].color.setRGB( 1, 0, 0 ); 的文档以查看其他可接受的格式。

three.js r.73