2015-11-22 132 views
1

好的,我已经用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); 
}); 

回答

2

如果使用MultiMaterial - 以前MeshFaceMaterial - 您可以访问和改变材料的颜色,像这样:

mesh.material.materials[ 0 ].color.set(0xff0000); 
mesh.material.materials[ 1 ].color.setRGB(1, 0, 0); 

研究THREE.Color()的文档以查看其他可接受的格式。

three.js r.73