VertexNormalsHelper.js 3.3 KB

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