VertexTangentsHelper.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author WestLangley / http://github.com/WestLangley
  4. */
  5. THREE.VertexTangentsHelper = function ( object, size, hex, linewidth ) {
  6. this.object = object;
  7. this.size = ( size !== undefined ) ? size : 1;
  8. var color = ( hex !== undefined ) ? hex : 0x0000ff;
  9. var width = ( linewidth !== undefined ) ? linewidth : 1;
  10. var geometry = new THREE.Geometry();
  11. var faces = object.geometry.faces;
  12. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  13. var face = faces[ i ];
  14. for ( var j = 0, jl = face.vertexTangents.length; j < jl; j ++ ) {
  15. geometry.vertices.push( new THREE.Vector3() );
  16. geometry.vertices.push( new THREE.Vector3() );
  17. }
  18. }
  19. THREE.SegmentsLine.call( this, geometry, new THREE.LineBasicMaterial( { color: color, linewidth: width } ) );
  20. this.matrixAutoUpdate = false;
  21. this.update();
  22. };
  23. THREE.VertexTangentsHelper.prototype = Object.create( THREE.SegmentsLine.prototype );
  24. THREE.VertexTangentsHelper.prototype.constructor = THREE.VertexTangentsHelper;
  25. THREE.VertexTangentsHelper.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. var vertices = this.geometry.vertices;
  31. var verts = this.object.geometry.vertices;
  32. var faces = this.object.geometry.faces;
  33. var worldMatrix = this.object.matrixWorld;
  34. var idx = 0;
  35. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  36. var face = faces[ i ];
  37. for ( var j = 0, jl = face.vertexTangents.length; j < jl; j ++ ) {
  38. var vertexId = face[ keys[ j ] ];
  39. var vertex = verts[ vertexId ];
  40. var tangent = face.vertexTangents[ j ];
  41. vertices[ idx ].copy( vertex ).applyMatrix4( worldMatrix );
  42. v1.copy( tangent ).transformDirection( worldMatrix ).multiplyScalar( this.size );
  43. v1.add( vertices[ idx ] );
  44. idx = idx + 1;
  45. vertices[ idx ].copy( v1 );
  46. idx = idx + 1;
  47. }
  48. }
  49. this.geometry.verticesNeedUpdate = true;
  50. return this;
  51. }
  52. }());