FaceNormalsHelper.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 FaceNormalsHelper( object, size, hex, linewidth ) {
  12. // FaceNormalsHelper only supports THREE.Geometry
  13. this.object = object;
  14. this.size = ( size !== undefined ) ? size : 1;
  15. var color = ( hex !== undefined ) ? hex : 0xffff00;
  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;
  22. } else {
  23. console.warn( 'THREE.FaceNormalsHelper: only THREE.Geometry is supported. Use THREE.VertexNormalsHelper, instead.' );
  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. FaceNormalsHelper.prototype = Object.create( LineSegments.prototype );
  35. FaceNormalsHelper.prototype.constructor = FaceNormalsHelper;
  36. FaceNormalsHelper.prototype.update = ( function () {
  37. var v1 = new Vector3();
  38. var v2 = new Vector3();
  39. var normalMatrix = new Matrix3();
  40. return function update() {
  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. var vertices = objGeometry.vertices;
  48. var faces = objGeometry.faces;
  49. var idx = 0;
  50. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  51. var face = faces[ i ];
  52. var normal = face.normal;
  53. v1.copy( vertices[ face.a ] )
  54. .add( vertices[ face.b ] )
  55. .add( vertices[ face.c ] )
  56. .divideScalar( 3 )
  57. .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. position.needsUpdate = true;
  65. };
  66. }() );
  67. export { FaceNormalsHelper };
粤ICP备19079148号