VertexTangentsHelper.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {
  2. BufferGeometry,
  3. Float32BufferAttribute,
  4. LineSegments,
  5. LineBasicMaterial,
  6. Vector3
  7. } from '../../../build/three.module.js';
  8. const _v1 = new Vector3();
  9. const _v2 = new Vector3();
  10. class VertexTangentsHelper extends LineSegments {
  11. constructor( object, size = 1, color = 0x00ffff ) {
  12. const objGeometry = object.geometry;
  13. if ( ! ( objGeometry && objGeometry.isBufferGeometry ) ) {
  14. console.error( 'THREE.VertexTangentsHelper: geometry not an instance of THREE.BufferGeometry.', objGeometry );
  15. return;
  16. }
  17. const nTangents = objGeometry.attributes.tangent.count;
  18. //
  19. const geometry = new BufferGeometry();
  20. const positions = new Float32BufferAttribute( nTangents * 2 * 3, 3 );
  21. geometry.setAttribute( 'position', positions );
  22. super( geometry, new LineBasicMaterial( { color, toneMapped: false } ) );
  23. this.object = object;
  24. this.size = size;
  25. this.type = 'VertexTangentsHelper';
  26. //
  27. this.matrixAutoUpdate = false;
  28. this.update();
  29. }
  30. update() {
  31. this.object.updateMatrixWorld( true );
  32. const matrixWorld = this.object.matrixWorld;
  33. const position = this.geometry.attributes.position;
  34. //
  35. const objGeometry = this.object.geometry;
  36. const objPos = objGeometry.attributes.position;
  37. const objTan = objGeometry.attributes.tangent;
  38. let idx = 0;
  39. // for simplicity, ignore index and drawcalls, and render every tangent
  40. for ( let j = 0, jl = objPos.count; j < jl; j ++ ) {
  41. _v1.set( objPos.getX( j ), objPos.getY( j ), objPos.getZ( j ) ).applyMatrix4( matrixWorld );
  42. _v2.set( objTan.getX( j ), objTan.getY( j ), objTan.getZ( j ) );
  43. _v2.transformDirection( matrixWorld ).multiplyScalar( this.size ).add( _v1 );
  44. position.setXYZ( idx, _v1.x, _v1.y, _v1.z );
  45. idx = idx + 1;
  46. position.setXYZ( idx, _v2.x, _v2.y, _v2.z );
  47. idx = idx + 1;
  48. }
  49. position.needsUpdate = true;
  50. }
  51. }
  52. export { VertexTangentsHelper };