VertexNormalsHelper.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {
  2. BufferGeometry,
  3. Float32BufferAttribute,
  4. LineSegments,
  5. LineBasicMaterial,
  6. Matrix3,
  7. Vector3
  8. } from '../../../build/three.module.js';
  9. var _v1 = new Vector3();
  10. var _v2 = new Vector3();
  11. var _normalMatrix = new Matrix3();
  12. var _keys = [ 'a', 'b', 'c' ];
  13. function VertexNormalsHelper( object, size, hex ) {
  14. this.object = object;
  15. this.size = ( size !== undefined ) ? size : 0.1;
  16. var color = ( hex !== undefined ) ? hex : 0xff0000;
  17. //
  18. var nNormals = 0;
  19. var objGeometry = this.object.geometry;
  20. if ( objGeometry && objGeometry.isGeometry ) {
  21. nNormals = objGeometry.faces.length * 3;
  22. } else if ( objGeometry && objGeometry.isBufferGeometry ) {
  23. nNormals = objGeometry.attributes.normal.count;
  24. }
  25. //
  26. var geometry = new BufferGeometry();
  27. var positions = new Float32BufferAttribute( nNormals * 2 * 3, 3 );
  28. geometry.setAttribute( 'position', positions );
  29. LineSegments.call( this, geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  30. this.type = 'VertexNormalsHelper';
  31. //
  32. this.matrixAutoUpdate = false;
  33. this.update();
  34. }
  35. VertexNormalsHelper.prototype = Object.create( LineSegments.prototype );
  36. VertexNormalsHelper.prototype.constructor = VertexNormalsHelper;
  37. VertexNormalsHelper.prototype.update = function () {
  38. this.object.updateMatrixWorld( true );
  39. _normalMatrix.getNormalMatrix( this.object.matrixWorld );
  40. var matrixWorld = this.object.matrixWorld;
  41. var position = this.geometry.attributes.position;
  42. //
  43. var objGeometry = this.object.geometry;
  44. if ( objGeometry && objGeometry.isGeometry ) {
  45. var vertices = objGeometry.vertices;
  46. var faces = objGeometry.faces;
  47. var idx = 0;
  48. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  49. var face = faces[ i ];
  50. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  51. var vertex = vertices[ face[ _keys[ j ] ] ];
  52. var normal = face.vertexNormals[ j ];
  53. _v1.copy( vertex ).applyMatrix4( matrixWorld );
  54. _v2.copy( normal ).applyMatrix3( _normalMatrix ).normalize().multiplyScalar( this.size ).add( _v1 );
  55. position.setXYZ( idx, _v1.x, _v1.y, _v1.z );
  56. idx = idx + 1;
  57. position.setXYZ( idx, _v2.x, _v2.y, _v2.z );
  58. idx = idx + 1;
  59. }
  60. }
  61. } else if ( objGeometry && objGeometry.isBufferGeometry ) {
  62. var objPos = objGeometry.attributes.position;
  63. var objNorm = objGeometry.attributes.normal;
  64. var idx = 0;
  65. // for simplicity, ignore index and drawcalls, and render every normal
  66. for ( var j = 0, jl = objPos.count; j < jl; j ++ ) {
  67. _v1.set( objPos.getX( j ), objPos.getY( j ), objPos.getZ( j ) ).applyMatrix4( matrixWorld );
  68. _v2.set( objNorm.getX( j ), objNorm.getY( j ), objNorm.getZ( j ) );
  69. _v2.applyMatrix3( _normalMatrix ).normalize().multiplyScalar( this.size ).add( _v1 );
  70. position.setXYZ( idx, _v1.x, _v1.y, _v1.z );
  71. idx = idx + 1;
  72. position.setXYZ( idx, _v2.x, _v2.y, _v2.z );
  73. idx = idx + 1;
  74. }
  75. }
  76. position.needsUpdate = true;
  77. };
  78. export { VertexNormalsHelper };