canvas - three.js:如何让多种 Material 为一个网格工作

标签 canvas three.js webgl javascript

我对 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);

但我收到此错误消息: “Uncaught TypeError: Cannot read property 'map' of undefined” 来自 three.js 中的第 19152 行。

对我来说,这似乎是 webgl 渲染器的问题。在我在这里和其他地方找到的大多数主题中,他们都在使用 Canvas 渲染器。但我想继续使用 webgl 渲染器。

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

最佳答案

试试这个

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

关于canvas - three.js:如何让多种 Material 为一个网格工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15759447/

相关文章:

animation - 如何创建彩色条纹背景,动画缓慢旋转 360 度,不使用光栅图像或 js?

javascript - 如何反转在​​ CylinderGeometry 对象中应用纹理的方向?

javascript - 如何在 Three.js 中处理动画模型?

javascript - WebGL 客户端状态跟踪

javascript - 嘿!我如何解决我的代码显然没有从浏览器的 KeyboardEvent 正确获取 .key 属性?

javascript - 使用 lineTo 绘制时,"break"处于直线状态

javascript - 如何在 Three.js 中使用延迟光照

three.js - 使用变形目标时光线相交不起作用

javascript - 如何在 webgl 中使用帧缓冲区?

javascript - 将背景中的 Canvas 元素放置到 html 表中