VertexNormalsHelper.js 3.0 KB

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