如何在three.js中将法线渲染到对象的表面

时间:2017-04-04 10:50:13

标签: javascript three.js quaternions normalize

我在threejs.org中发现了the example,它绘制了箭头法线,这正是我需要的。但箭头是复杂的对象,由ArrowHelper创建。 我查看了源代码并找到了setDirection方法。

function setDirection( dir ) {

    // dir is assumed to be normalized

    if ( dir.y > 0.99999 ) {

        this.quaternion.set( 0, 0, 0, 1 );

    } else if ( dir.y < - 0.99999 ) {

        this.quaternion.set( 1, 0, 0, 0 );

    } else {

        axis.set( dir.z, 0, - dir.x ).normalize();

        radians = Math.acos( dir.y );

        this.quaternion.setFromAxisAngle( axis, radians );

    }

};

我尝试使用四元数来改进为Vector3设置旋转的算法,但我仍然使用了{x: 0, y: 0, z: 0}的norlams。

我的问题是: 如何计算矢量以便从表面到空间绘制法线,以获得相同的图像:enter image description here

UPD
我使用CylinderGeometry。

UPD2
我已经解决了。 Look at my codepen

1 个答案:

答案 0 :(得分:2)

您想要查看网格的顶点法线。你可以这样使用VertexNormalsHelper

var vnh = new THREE.VertexNormalsHelper( mesh, 1, 0xff0000 );
scene.add( vnh );

VertexNormalsHelper

three.js r.84