VertexTangentsHelper.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. this.matrixAutoUpdate = false;
  18. this.update();
  19. }
  20. update() {
  21. this.object.updateMatrixWorld( true );
  22. const matrixWorld = this.object.matrixWorld;
  23. const position = this.geometry.attributes.position; //
  24. const objGeometry = this.object.geometry;
  25. const objPos = objGeometry.attributes.position;
  26. const objTan = objGeometry.attributes.tangent;
  27. let idx = 0; // for simplicity, ignore index and drawcalls, and render every tangent
  28. for ( let j = 0, jl = objPos.count; j < jl; j ++ ) {
  29. _v1.fromBufferAttribute( objPos, j ).applyMatrix4( matrixWorld );
  30. _v2.fromBufferAttribute( objTan, j );
  31. _v2.transformDirection( matrixWorld ).multiplyScalar( this.size ).add( _v1 );
  32. position.setXYZ( idx, _v1.x, _v1.y, _v1.z );
  33. idx = idx + 1;
  34. position.setXYZ( idx, _v2.x, _v2.y, _v2.z );
  35. idx = idx + 1;
  36. }
  37. position.needsUpdate = true;
  38. }
  39. }
  40. THREE.VertexTangentsHelper = VertexTangentsHelper;
  41. } )();