VertexNormalsHelper.js 3.0 KB

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