VertexTangentsHelper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. */
  22. class VertexTangentsHelper extends LineSegments {
  23. /**
  24. * Constructs a new vertex tangents helper.
  25. *
  26. * @param {Object3D} object - The object for which to visualize vertex tangents.
  27. * @param {number} [size=1] - The helper's size.
  28. * @param {number|Color|string} [color=0xff0000] - The helper's color.
  29. */
  30. constructor( object, size = 1, color = 0x00ffff ) {
  31. const geometry = new BufferGeometry();
  32. const nTangents = object.geometry.attributes.tangent.count;
  33. const positions = new Float32BufferAttribute( nTangents * 2 * 3, 3 );
  34. geometry.setAttribute( 'position', positions );
  35. super( geometry, new LineBasicMaterial( { color, toneMapped: false } ) );
  36. /**
  37. * The object for which to visualize vertex tangents.
  38. *
  39. * @type {Object3D}
  40. */
  41. this.object = object;
  42. /**
  43. * The helper's size.
  44. *
  45. * @type {number}
  46. * @default 1
  47. */
  48. this.size = size;
  49. this.type = 'VertexTangentsHelper';
  50. /**
  51. * Overwritten and set to `false` since the object's world transformation
  52. * is encoded in the helper's geometry data.
  53. *
  54. * @type {boolean}
  55. * @default false
  56. */
  57. this.matrixAutoUpdate = false;
  58. this.update();
  59. }
  60. /**
  61. * Updates the vertex normals preview based on the object's world transform.
  62. */
  63. update() {
  64. this.object.updateMatrixWorld( true );
  65. const matrixWorld = this.object.matrixWorld;
  66. const position = this.geometry.attributes.position;
  67. //
  68. const objGeometry = this.object.geometry;
  69. const objPos = objGeometry.attributes.position;
  70. const objTan = objGeometry.attributes.tangent;
  71. let idx = 0;
  72. // for simplicity, ignore index and drawcalls, and render every tangent
  73. for ( let j = 0, jl = objPos.count; j < jl; j ++ ) {
  74. _v1.fromBufferAttribute( objPos, j ).applyMatrix4( matrixWorld );
  75. _v2.fromBufferAttribute( objTan, j );
  76. _v2.transformDirection( matrixWorld ).multiplyScalar( this.size ).add( _v1 );
  77. position.setXYZ( idx, _v1.x, _v1.y, _v1.z );
  78. idx = idx + 1;
  79. position.setXYZ( idx, _v2.x, _v2.y, _v2.z );
  80. idx = idx + 1;
  81. }
  82. position.needsUpdate = true;
  83. }
  84. /**
  85. * Frees the GPU-related resources allocated by this instance. Call this
  86. * method whenever this instance is no longer used in your app.
  87. */
  88. dispose() {
  89. this.geometry.dispose();
  90. this.material.dispose();
  91. }
  92. }
  93. export { VertexTangentsHelper };
粤ICP备19079148号