VertexNormalsHelper.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ( function () {
  2. const _v1 = new THREE.Vector3();
  3. const _v2 = new THREE.Vector3();
  4. const _normalMatrix = new THREE.Matrix3();
  5. class VertexNormalsHelper extends THREE.LineSegments {
  6. constructor( object, size = 1, color = 0xff0000 ) {
  7. const geometry = new THREE.BufferGeometry();
  8. const nNormals = object.geometry.attributes.normal.count;
  9. const positions = new THREE.Float32BufferAttribute( nNormals * 2 * 3, 3 );
  10. geometry.setAttribute( 'position', positions );
  11. super( geometry, new THREE.LineBasicMaterial( {
  12. color,
  13. toneMapped: false
  14. } ) );
  15. this.object = object;
  16. this.size = size;
  17. this.type = 'VertexNormalsHelper'; //
  18. this.matrixAutoUpdate = false;
  19. this.update();
  20. }
  21. update() {
  22. this.object.updateMatrixWorld( true );
  23. _normalMatrix.getNormalMatrix( this.object.matrixWorld );
  24. const matrixWorld = this.object.matrixWorld;
  25. const position = this.geometry.attributes.position; //
  26. const objGeometry = this.object.geometry;
  27. if ( objGeometry ) {
  28. const objPos = objGeometry.attributes.position;
  29. const objNorm = objGeometry.attributes.normal;
  30. let idx = 0; // for simplicity, ignore index and drawcalls, and render every normal
  31. for ( let j = 0, jl = objPos.count; j < jl; j ++ ) {
  32. _v1.fromBufferAttribute( objPos, j ).applyMatrix4( matrixWorld );
  33. _v2.fromBufferAttribute( objNorm, j );
  34. _v2.applyMatrix3( _normalMatrix ).normalize().multiplyScalar( this.size ).add( _v1 );
  35. position.setXYZ( idx, _v1.x, _v1.y, _v1.z );
  36. idx = idx + 1;
  37. position.setXYZ( idx, _v2.x, _v2.y, _v2.z );
  38. idx = idx + 1;
  39. }
  40. }
  41. position.needsUpdate = true;
  42. }
  43. }
  44. THREE.VertexNormalsHelper = VertexNormalsHelper;
  45. } )();