VertexNormalsHelper.js 3.3 KB

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