VertexNormalsHelper.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author WestLangley / http://github.com/WestLangley
  4. */
  5. THREE.VertexNormalsHelper = function ( object, size, hex, linewidth ) {
  6. this.object = object;
  7. this.size = ( size !== undefined ) ? size : 1;
  8. var color = ( hex !== undefined ) ? hex : 0xff0000;
  9. var width = ( linewidth !== undefined ) ? linewidth : 1;
  10. var geometry = new THREE.Geometry();
  11. var vertices = object.geometry.vertices;
  12. var faces = object.geometry.faces;
  13. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  14. var face = faces[ i ];
  15. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  16. geometry.vertices.push( new THREE.Vector3(), new THREE.Vector3() );
  17. }
  18. }
  19. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color, linewidth: width } ), THREE.LinePieces );
  20. this.matrixAutoUpdate = false;
  21. this.normalMatrix = new THREE.Matrix3();
  22. this.update();
  23. };
  24. THREE.VertexNormalsHelper.prototype = Object.create( THREE.Line.prototype );
  25. THREE.VertexNormalsHelper.prototype.update = ( function ( object ) {
  26. var v1 = new THREE.Vector3();
  27. return function( object ) {
  28. var keys = [ 'a', 'b', 'c', 'd' ];
  29. this.object.updateMatrixWorld( true );
  30. this.normalMatrix.getNormalMatrix( this.object.matrixWorld );
  31. var vertices = this.geometry.vertices;
  32. var verts = this.object.geometry.vertices;
  33. var faces = this.object.geometry.faces;
  34. var worldMatrix = this.object.matrixWorld;
  35. var idx = 0;
  36. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  37. var face = faces[ i ];
  38. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  39. var vertexId = face[ keys[ j ] ];
  40. var vertex = verts[ vertexId ];
  41. var normal = face.vertexNormals[ j ];
  42. vertices[ idx ].copy( vertex ).applyMatrix4( worldMatrix );
  43. v1.copy( normal ).applyMatrix3( this.normalMatrix ).normalize().multiplyScalar( this.size );
  44. v1.add( vertices[ idx ] );
  45. idx = idx + 1;
  46. vertices[ idx ].copy( v1 );
  47. idx = idx + 1;
  48. }
  49. }
  50. this.geometry.verticesNeedUpdate = true;
  51. return this;
  52. }
  53. }());