VertexTangentsHelper.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {
  2. BufferGeometry,
  3. Float32BufferAttribute,
  4. LineSegments,
  5. LineBasicMaterial,
  6. Vector3
  7. } from 'three';
  8. const _v1 = new Vector3();
  9. const _v2 = new Vector3();
  10. /**
  11. * Visualizes an object's vertex tangents.
  12. *
  13. * Requires that tangents have been specified in the geometry as a buffer attribute or
  14. * have been calculated using {@link BufferGeometry#computeTangents}.
  15. * ```js
  16. * const helper = new VertexTangentsHelper( mesh, 1, 0xff0000 );
  17. * scene.add( helper );
  18. * ```
  19. *
  20. * @augments LineSegments
  21. * @three_import import { VertexTangentsHelper } from 'three/addons/helpers/VertexTangentsHelper.js';
  22. */
  23. class VertexTangentsHelper extends LineSegments {
  24. /**
  25. * Constructs a new vertex tangents helper.
  26. *
  27. * @param {Object3D} object - The object for which to visualize vertex tangents.
  28. * @param {number} [size=1] - The helper's size.
  29. * @param {number|Color|string} [color=0xff0000] - The helper's color.
  30. */
  31. constructor( object, size = 1, color = 0x00ffff ) {
  32. const geometry = new BufferGeometry();
  33. const nTangents = object.geometry.attributes.tangent.count;
  34. const positions = new Float32BufferAttribute( nTangents * 2 * 3, 3 );
  35. geometry.setAttribute( 'position', positions );
  36. super( geometry, new LineBasicMaterial( { color, toneMapped: false } ) );
  37. /**
  38. * The object for which to visualize vertex tangents.
  39. *
  40. * @type {Object3D}
  41. */
  42. this.object = object;
  43. /**
  44. * The helper's size.
  45. *
  46. * @type {number}
  47. * @default 1
  48. */
  49. this.size = size;
  50. this.type = 'VertexTangentsHelper';
  51. /**
  52. * Overwritten and set to `false` since the object's world transformation
  53. * is encoded in the helper's geometry data.
  54. *
  55. * @type {boolean}
  56. * @default false
  57. */
  58. this.matrixAutoUpdate = false;
  59. this.update();
  60. }
  61. /**
  62. * Updates the vertex normals preview based on the object's world transform.
  63. */
  64. update() {
  65. this.object.updateMatrixWorld( true );
  66. const matrixWorld = this.object.matrixWorld;
  67. const position = this.geometry.attributes.position;
  68. //
  69. const objGeometry = this.object.geometry;
  70. const objPos = objGeometry.attributes.position;
  71. const objTan = objGeometry.attributes.tangent;
  72. let idx = 0;
  73. // for simplicity, ignore index and drawcalls, and render every tangent
  74. for ( let j = 0, jl = objPos.count; j < jl; j ++ ) {
  75. _v1.fromBufferAttribute( objPos, j ).applyMatrix4( matrixWorld );
  76. _v2.fromBufferAttribute( objTan, j );
  77. _v2.transformDirection( matrixWorld ).multiplyScalar( this.size ).add( _v1 );
  78. position.setXYZ( idx, _v1.x, _v1.y, _v1.z );
  79. idx = idx + 1;
  80. position.setXYZ( idx, _v2.x, _v2.y, _v2.z );
  81. idx = idx + 1;
  82. }
  83. position.needsUpdate = true;
  84. }
  85. /**
  86. * Frees the GPU-related resources allocated by this instance. Call this
  87. * method whenever this instance is no longer used in your app.
  88. */
  89. dispose() {
  90. this.geometry.dispose();
  91. this.material.dispose();
  92. }
  93. }
  94. export { VertexTangentsHelper };