VertexTangentsHelper.js 2.1 KB

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