【问题标题】:Three.js - Change JSON Material at runtimeThree.js - 在运行时更改 JSON 材质
【发布时间】:2016-02-24 09:02:39
【问题描述】:

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

【问题讨论】:

    标签: javascript json three.js


    【解决方案1】:

    如果您使用的是MultiMaterial——以前的MeshFaceMaterial——您可以像这样访问和更改材质颜色:

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

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

    three.js r.73

    【讨论】:

      猜你喜欢
      • 2012-09-15
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      相关资源
      最近更新 更多