VertexTangentsHelper.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ( function () {
  2. const _v1 = new THREE.Vector3();
  3. const _v2 = new THREE.Vector3();
  4. class VertexTangentsHelper extends THREE.LineSegments {
  5. constructor( object, size = 1, color = 0x00ffff ) {
  6. const geometry = new THREE.BufferGeometry();
  7. const nTangents = object.geometry.attributes.tangent.count;
  8. const positions = new THREE.Float32BufferAttribute( nTangents * 2 * 3, 3 );
  9. geometry.setAttribute( 'position', positions );
  10. super( geometry, new THREE.LineBasicMaterial( {
  11. color,
  12. toneMapped: false
  13. } ) );
  14. this.object = object;
  15. this.size = size;
  16. this.type = 'VertexTangentsHelper';
  17. //
  18. this.matrixAutoUpdate = false;
  19. this.update();
  20. }
  21. update() {
  22. this.object.updateMatrixWorld( true );
  23. const matrixWorld = this.object.matrixWorld;
  24. const position = this.geometry.attributes.position;
  25. //
  26. const objGeometry = this.object.geometry;
  27. const objPos = objGeometry.attributes.position;
  28. const objTan = objGeometry.attributes.tangent;
  29. let idx = 0;
  30. // for simplicity, ignore index and drawcalls, and render every tangent
  31. for ( let j = 0, jl = objPos.count; j < jl; j ++ ) {
  32. _v1.fromBufferAttribute( objPos, j ).applyMatrix4( matrixWorld );
  33. _v2.fromBufferAttribute( objTan, j );
  34. _v2.transformDirection( matrixWorld ).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. position.needsUpdate = true;
  41. }
  42. dispose() {
  43. this.geometry.dispose();
  44. this.material.dispose();
  45. }
  46. }
  47. THREE.VertexTangentsHelper = VertexTangentsHelper;
  48. } )();