three.js无法在three.Points几何体中渲染法线

three.js fails to render the normals in a THREE.Points Geometry

本文关键字:three Points js 几何体      更新时间:2024-05-23

我对threejs很陌生。我目前正在进行一个项目,该项目需要通过Qt5 Canvas3D使用three.js渲染点云。根据threejs.org的示例,我使用BufferGeometry并设置其属性(位置和法线)。然后我使用THREE.Points和THREE.PointsMaterial来包裹它。结果是我可以渲染场景中的点,但是,每个顶点上设置的法线似乎被忽略了。代码片段如下所示:

var vertexPositions = [
[10, 10, 0, 1, 0, 0],
[10, -10, 0, 1, 0, 0],
[-10, -10, 0, 1, 0, 0]
];
geometry = new THREE.BufferGeometry();
    var vertices = new Float32Array( vertexPositions.length * 3 );
    for ( var i = 0; i < vertexPositions.length; i++ )
        {
            vertices[ i*3 + 0 ] = vertexPositions[i][0];
            vertices[ i*3 + 1 ] = vertexPositions[i][1];
            vertices[ i*3 + 2 ] = vertexPositions[i][2];
        }
    var normals = new Float32Array( vertexPositions.length * 3 );
    for ( i = 0; i < vertexPositions.length; i++ )
        {
            normals[ i*3 + 0 ] = vertexPositions[i][3];
            normals[ i*3 + 1 ] = vertexPositions[i][4];
            normals[ i*3 + 2 ] = vertexPositions[i][5];
        }
    var colors = new Float32Array( vertexPositions.length * 3 );
    for ( i = 0; i < vertexPositions.length; i++ )
        {
            colors[ i*3 + 0 ] = 1;
            colors[ i*3 + 1 ] = 0;
            colors[ i*3 + 2 ] = 0;
        }
    geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
    geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
    geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
    material = new THREE.PointsMaterial({size:50, vertexColors:THREE.VertexColors});
    mesh = new THREE.Points(geometry, material);
    scene.add(mesh);

如何渲染在顶点上设置法线的点云?我错过了什么?如有任何建议,我们将不胜感激。谢谢

您想要渲染点云并使其与灯光交互。

为此,必须创建一个自定义ShaderMaterial

在这个答案中,您将找到与THREE.Points一起使用的自定义ShaderMaterial的示例。

three.js r.75