FaceNormalsHelper.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author WestLangley / http://github.com/WestLangley
  4. */
  5. THREE.FaceNormalsHelper = function ( object, size, hex ) {
  6. this.object = object;
  7. this.size = size || 1;
  8. var color = hex || 0x0000ff;
  9. var geometry = new THREE.Geometry();
  10. var faces = this.object.geometry.faces;
  11. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  12. geometry.vertices.push( new THREE.Vector3() );
  13. geometry.vertices.push( new THREE.Vector3() );
  14. }
  15. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color } ), THREE.LinePieces );
  16. this.matrixAutoUpdate = false;
  17. this.normalMatrix = new THREE.Matrix3();
  18. this.update();
  19. };
  20. THREE.FaceNormalsHelper.prototype = Object.create( THREE.Line.prototype );
  21. THREE.FaceNormalsHelper.prototype.update = ( function ( object ) {
  22. var v1 = new THREE.Vector3();
  23. return function( object ) {
  24. this.object.updateMatrixWorld( true );
  25. this.normalMatrix.getNormalMatrix( this.object.matrixWorld );
  26. var vertices = this.geometry.vertices;
  27. var faces = this.object.geometry.faces;
  28. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  29. var face = faces[ i ];
  30. v1.copy( face.normal ).applyMatrix3( this.normalMatrix ).normalize().multiplyScalar( this.size );
  31. var idx = 2 * i;
  32. vertices[ idx ].copy( face.centroid ).applyMatrix4( this.object.matrixWorld );
  33. vertices[ idx + 1 ].addVectors( vertices[ idx ], v1 );
  34. }
  35. this.geometry.verticesNeedUpdate = true;
  36. return this;
  37. }
  38. }());