BufferGeometryUtils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.BufferGeometryUtils = {
  5. computeTangents: function ( geometry ) {
  6. var index = geometry.index;
  7. var attributes = geometry.attributes;
  8. // based on http://www.terathon.com/code/tangent.html
  9. // (per vertex tangents)
  10. if ( index === null ||
  11. attributes.position === undefined ||
  12. attributes.normal === undefined ||
  13. attributes.uv === undefined ) {
  14. console.warn( 'THREE.BufferGeometry: Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()' );
  15. return;
  16. }
  17. var indices = index.array;
  18. var positions = attributes.position.array;
  19. var normals = attributes.normal.array;
  20. var uvs = attributes.uv.array;
  21. var nVertices = positions.length / 3;
  22. if ( attributes.tangent === undefined ) {
  23. geometry.addAttribute( 'tangent', new THREE.BufferAttribute( new Float32Array( 4 * nVertices ), 4 ) );
  24. }
  25. var tangents = attributes.tangent.array;
  26. var tan1 = [], tan2 = [];
  27. for ( var k = 0; k < nVertices; k ++ ) {
  28. tan1[ k ] = new THREE.Vector3();
  29. tan2[ k ] = new THREE.Vector3();
  30. }
  31. var vA = new THREE.Vector3(),
  32. vB = new THREE.Vector3(),
  33. vC = new THREE.Vector3(),
  34. uvA = new THREE.Vector2(),
  35. uvB = new THREE.Vector2(),
  36. uvC = new THREE.Vector2(),
  37. sdir = new THREE.Vector3(),
  38. tdir = new THREE.Vector3();
  39. function handleTriangle( a, b, c ) {
  40. vA.fromArray( positions, a * 3 );
  41. vB.fromArray( positions, b * 3 );
  42. vC.fromArray( positions, c * 3 );
  43. uvA.fromArray( uvs, a * 2 );
  44. uvB.fromArray( uvs, b * 2 );
  45. uvC.fromArray( uvs, c * 2 );
  46. var x1 = vB.x - vA.x;
  47. var x2 = vC.x - vA.x;
  48. var y1 = vB.y - vA.y;
  49. var y2 = vC.y - vA.y;
  50. var z1 = vB.z - vA.z;
  51. var z2 = vC.z - vA.z;
  52. var s1 = uvB.x - uvA.x;
  53. var s2 = uvC.x - uvA.x;
  54. var t1 = uvB.y - uvA.y;
  55. var t2 = uvC.y - uvA.y;
  56. var r = 1.0 / ( s1 * t2 - s2 * t1 );
  57. sdir.set(
  58. ( t2 * x1 - t1 * x2 ) * r,
  59. ( t2 * y1 - t1 * y2 ) * r,
  60. ( t2 * z1 - t1 * z2 ) * r
  61. );
  62. tdir.set(
  63. ( s1 * x2 - s2 * x1 ) * r,
  64. ( s1 * y2 - s2 * y1 ) * r,
  65. ( s1 * z2 - s2 * z1 ) * r
  66. );
  67. tan1[ a ].add( sdir );
  68. tan1[ b ].add( sdir );
  69. tan1[ c ].add( sdir );
  70. tan2[ a ].add( tdir );
  71. tan2[ b ].add( tdir );
  72. tan2[ c ].add( tdir );
  73. }
  74. var groups = geometry.groups;
  75. if ( groups.length === 0 ) {
  76. groups = [ {
  77. start: 0,
  78. count: indices.length
  79. } ];
  80. }
  81. for ( var j = 0, jl = groups.length; j < jl; ++ j ) {
  82. var group = groups[ j ];
  83. var start = group.start;
  84. var count = group.count;
  85. for ( var i = start, il = start + count; i < il; i += 3 ) {
  86. handleTriangle(
  87. indices[ i + 0 ],
  88. indices[ i + 1 ],
  89. indices[ i + 2 ]
  90. );
  91. }
  92. }
  93. var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
  94. var n = new THREE.Vector3(), n2 = new THREE.Vector3();
  95. var w, t, test;
  96. function handleVertex( v ) {
  97. n.fromArray( normals, v * 3 );
  98. n2.copy( n );
  99. t = tan1[ v ];
  100. // Gram-Schmidt orthogonalize
  101. tmp.copy( t );
  102. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  103. // Calculate handedness
  104. tmp2.crossVectors( n2, t );
  105. test = tmp2.dot( tan2[ v ] );
  106. w = ( test < 0.0 ) ? - 1.0 : 1.0;
  107. tangents[ v * 4 ] = tmp.x;
  108. tangents[ v * 4 + 1 ] = tmp.y;
  109. tangents[ v * 4 + 2 ] = tmp.z;
  110. tangents[ v * 4 + 3 ] = w;
  111. }
  112. for ( var j = 0, jl = groups.length; j < jl; ++ j ) {
  113. var group = groups[ j ];
  114. var start = group.start;
  115. var count = group.count;
  116. for ( var i = start, il = start + count; i < il; i += 3 ) {
  117. handleVertex( indices[ i + 0 ] );
  118. handleVertex( indices[ i + 1 ] );
  119. handleVertex( indices[ i + 2 ] );
  120. }
  121. }
  122. }
  123. };