FaceNormalsHelper.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 = new Vector3();
  12. var _v2 = new Vector3();
  13. var _normalMatrix = new Matrix3();
  14. function FaceNormalsHelper( object, size, hex, linewidth ) {
  15. // FaceNormalsHelper only supports THREE.Geometry
  16. this.object = object;
  17. this.size = ( size !== undefined ) ? size : 1;
  18. var color = ( hex !== undefined ) ? hex : 0xffff00;
  19. var width = ( linewidth !== undefined ) ? linewidth : 1;
  20. //
  21. var nNormals = 0;
  22. var objGeometry = this.object.geometry;
  23. if ( objGeometry && objGeometry.isGeometry ) {
  24. nNormals = objGeometry.faces.length;
  25. } else {
  26. console.warn( 'THREE.FaceNormalsHelper: only THREE.Geometry is supported. Use THREE.VertexNormalsHelper, instead.' );
  27. }
  28. //
  29. var geometry = new BufferGeometry();
  30. var positions = new Float32BufferAttribute( nNormals * 2 * 3, 3 );
  31. geometry.addAttribute( 'position', positions );
  32. LineSegments.call( this, geometry, new LineBasicMaterial( { color: color, linewidth: width } ) );
  33. //
  34. this.matrixAutoUpdate = false;
  35. this.update();
  36. }
  37. FaceNormalsHelper.prototype = Object.create( LineSegments.prototype );
  38. FaceNormalsHelper.prototype.constructor = FaceNormalsHelper;
  39. FaceNormalsHelper.prototype.update = function () {
  40. this.object.updateMatrixWorld( true );
  41. _normalMatrix.getNormalMatrix( this.object.matrixWorld );
  42. var matrixWorld = this.object.matrixWorld;
  43. var position = this.geometry.attributes.position;
  44. //
  45. var objGeometry = this.object.geometry;
  46. var vertices = objGeometry.vertices;
  47. var faces = objGeometry.faces;
  48. var idx = 0;
  49. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  50. var face = faces[ i ];
  51. var normal = face.normal;
  52. _v1.copy( vertices[ face.a ] )
  53. .add( vertices[ face.b ] )
  54. .add( vertices[ face.c ] )
  55. .divideScalar( 3 )
  56. .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. position.needsUpdate = true;
  64. };
  65. export { FaceNormalsHelper };