我如何为三个js实例对象设置颜色?

问题描述 投票:0回答:1

我有一个框架组件,该组件利用三个js实例化有效地渲染大量球体。基于来自三个js库的示例,例如https://github.com/mrdoob/three.js/blob/master/examples/webgl_instancing_scatter.html,我应该能够分别为每个实例渲染设置颜色。我以为我已经遵循了示例,但是我的实例颜色没有生效。

AFRAME.registerComponent('spheres', {
    schema: {
        count: {type: 'number'},
        radius: {type: 'number'},
        scale: {type: 'number'},
        colors: {type: 'array'},
        positions: {type: 'array'}
    },
    init: function() {
        const {count, radius, scale, colors, positions} = this.data;  

        const material = new THREE.MeshNormalMaterial();

        const geometry = new THREE.SphereBufferGeometry( radius, 3, 2 );
        const instancedGeometry = new THREE.InstancedBufferGeometry().copy(geometry); 
        var instanceColors = [];

        for ( var i = 0; i < count; i ++ ) {

            instanceColors.push( Math.random() );
            instanceColors.push( Math.random() );
            instanceColors.push( Math.random() );

        }
        instancedGeometry.setAttribute("instanceColor", new THREE.InstancedBufferAttribute( new Float32Array( instanceColors ), 3 ))

        instancedGeometry.computeVertexNormals();

        material.vertexColors = true;

        const matrix = new THREE.Matrix4();
        const mesh = new THREE.InstancedMesh( instancedGeometry, material, count );

        for ( var i = 0; i < count; i ++ ) {

            this.setMatrix(positions[i], scale)( matrix );
            mesh.setMatrixAt( i, matrix );

        }

        this.el.object3D.add( mesh );
    },

我在做什么错?

three.js aframe virtual-reality
1个回答
0
投票

您尝试使用实际的相同属性'color',例如在示例中(为

blossomGeometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( color, 3 ) );

因为我认为属性的名称为'color'非常重要。

© www.soinside.com 2019 - 2024. All rights reserved.