three.js:如何让多个材质适用于一个网格

use*_*189 4 javascript canvas webgl three.js

我对three.js有一个很大的问题:

我想要一个每个脸上都有不同颜色的简单立方体.我试过这种方式:

// set the scene size
    var WIDTH = jQuery('#showcase').width() - 20, HEIGHT = jQuery('#showcase').height();

    // set some camera attributes
    var VIEW_ANGLE = 45, ASPECT = WIDTH / HEIGHT, NEAR = 0.1, FAR = 10000000;

    this.container = jQuery('#showcase');

    this.renderer = new THREE.WebGLRenderer();
    this.camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
    this.scene = new THREE.Scene();
    this.scene.add(this.camera);

    // camera start position
    this.camera.position.z = this.model.radius;
    this.camera.position.y = this.model.cameraY;
    this.camera.position.x = 0;
    this.camera.lookAt(this.scene.position);

    this.renderer.setSize(WIDTH, HEIGHT);
    this.container.append(this.renderer.domElement);

    //Multiple Colors
    var materials = [new THREE.MeshBasicMaterial({
        color : 0xFF0000
    }), new THREE.MeshBasicMaterial({
        color : 0x00FF00
    }), new THREE.MeshBasicMaterial({
        color : 0x0000FF
    }), new THREE.MeshBasicMaterial({
        color : 0xAA0000
    }), new THREE.MeshBasicMaterial({
        color : 0x00AA00
    }), new THREE.MeshBasicMaterial({
        color : 0x0000AA
    })];

    this.geometry = new THREE.CubeGeometry(100, 100, 100, materials);

    this.mesh = new THREE.Mesh(this.geometry,  new THREE.MeshFaceMaterial());

    this.scene.add(this.mesh);

    // create a point light
    this.pointLight = new THREE.PointLight(0xFFFFFF);
    this.pointLight.position = this.scene.position;
    this.scene.add(this.pointLight);

    this.renderer.render(this.scene, this.camera);
Run Code Online (Sandbox Code Playgroud)

但我收到此错误消息:"未捕获的TypeError:无法从three.js中的第19152行读取未定义的属性'map'.

对我而言,这似乎是webgl渲染器的问题.在我在这里和其他地方找到的大多数主题中,他们都使用了画布渲染器.但我想继续使用webgl渲染器.

有谁知道这个问题?非常感谢:-)

sam*_*und 6

试试这个

this.geometry = new THREE.CubeGeometry(100, 100, 100);
this.mesh = new THREE.Mesh(this.geometry, new THREE.MeshFaceMaterial(materials));
Run Code Online (Sandbox Code Playgroud)

  • 但它如何将材料分配到面孔? (7认同)