VertexNormalsHelper.js 3.3 KB

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