VertexTangentsHelper.js 1.8 KB

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