BufferGeometry.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.BufferGeometry = function () {
  5. THREE.GeometryLibrary.push( this );
  6. this.id = THREE.GeometryIdCount ++;
  7. // attributes
  8. this.attributes = {};
  9. // attributes typed arrays are kept only if dynamic flag is set
  10. this.dynamic = false;
  11. // offsets for chunks when using indexed elements
  12. this.offsets = [];
  13. // boundings
  14. this.boundingBox = null;
  15. this.boundingSphere = null;
  16. this.hasTangents = false;
  17. // for compatibility
  18. this.morphTargets = [];
  19. };
  20. THREE.BufferGeometry.prototype = {
  21. constructor : THREE.BufferGeometry,
  22. applyMatrix: function ( matrix ) {
  23. var positionArray;
  24. var normalArray;
  25. if ( this.attributes[ "position" ] ) positionArray = this.attributes[ "position" ].array;
  26. if ( this.attributes[ "normal" ] ) normalArray = this.attributes[ "normal" ].array;
  27. if ( positionArray !== undefined ) {
  28. matrix.multiplyVector3Array( positionArray );
  29. this.verticesNeedUpdate = true;
  30. }
  31. if ( normalArray !== undefined ) {
  32. var normalMatrix = new THREE.Matrix3();
  33. normalMatrix.getInverse( matrix ).transpose();
  34. normalMatrix.multiplyVector3Array( normalArray );
  35. this.normalizeNormals();
  36. this.normalsNeedUpdate = true;
  37. }
  38. },
  39. computeBoundingBox: function () {
  40. if ( ! this.boundingBox ) {
  41. this.boundingBox = {
  42. min: new THREE.Vector3( Infinity, Infinity, Infinity ),
  43. max: new THREE.Vector3( -Infinity, -Infinity, -Infinity )
  44. };
  45. }
  46. var positions = this.attributes[ "position" ].array;
  47. if ( positions ) {
  48. var bb = this.boundingBox;
  49. var x, y, z;
  50. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  51. x = positions[ i ];
  52. y = positions[ i + 1 ];
  53. z = positions[ i + 2 ];
  54. // bounding box
  55. if ( x < bb.min.x ) {
  56. bb.min.x = x;
  57. } else if ( x > bb.max.x ) {
  58. bb.max.x = x;
  59. }
  60. if ( y < bb.min.y ) {
  61. bb.min.y = y;
  62. } else if ( y > bb.max.y ) {
  63. bb.max.y = y;
  64. }
  65. if ( z < bb.min.z ) {
  66. bb.min.z = z;
  67. } else if ( z > bb.max.z ) {
  68. bb.max.z = z;
  69. }
  70. }
  71. }
  72. if ( positions === undefined || positions.length === 0 ) {
  73. this.boundingBox.min.set( 0, 0, 0 );
  74. this.boundingBox.max.set( 0, 0, 0 );
  75. }
  76. },
  77. computeBoundingSphere: function () {
  78. if ( ! this.boundingSphere ) this.boundingSphere = { radius: 0 };
  79. var positions = this.attributes[ "position" ].array;
  80. if ( positions ) {
  81. var radiusSq, maxRadiusSq = 0;
  82. var x, y, z;
  83. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  84. x = positions[ i ];
  85. y = positions[ i + 1 ];
  86. z = positions[ i + 2 ];
  87. radiusSq = x * x + y * y + z * z;
  88. if ( radiusSq > maxRadiusSq ) maxRadiusSq = radiusSq;
  89. }
  90. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  91. }
  92. },
  93. computeVertexNormals: function () {
  94. if ( this.attributes[ "position" ] && this.attributes[ "index" ] ) {
  95. var i, il;
  96. var j, jl;
  97. var nVertexElements = this.attributes[ "position" ].array.length;
  98. if ( this.attributes[ "normal" ] === undefined ) {
  99. this.attributes[ "normal" ] = {
  100. itemSize: 3,
  101. array: new Float32Array( nVertexElements ),
  102. numItems: nVertexElements
  103. };
  104. } else {
  105. // reset existing normals to zero
  106. for ( i = 0, il = this.attributes[ "normal" ].array.length; i < il; i ++ ) {
  107. this.attributes[ "normal" ].array[ i ] = 0;
  108. }
  109. }
  110. var offsets = this.offsets;
  111. var indices = this.attributes[ "index" ].array;
  112. var positions = this.attributes[ "position" ].array;
  113. var normals = this.attributes[ "normal" ].array;
  114. var vA, vB, vC, x, y, z,
  115. pA = new THREE.Vector3(),
  116. pB = new THREE.Vector3(),
  117. pC = new THREE.Vector3(),
  118. cb = new THREE.Vector3(),
  119. ab = new THREE.Vector3();
  120. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  121. var start = offsets[ j ].start;
  122. var count = offsets[ j ].count;
  123. var index = offsets[ j ].index;
  124. for ( i = start, il = start + count; i < il; i += 3 ) {
  125. vA = index + indices[ i ];
  126. vB = index + indices[ i + 1 ];
  127. vC = index + indices[ i + 2 ];
  128. x = positions[ vA * 3 ];
  129. y = positions[ vA * 3 + 1 ];
  130. z = positions[ vA * 3 + 2 ];
  131. pA.set( x, y, z );
  132. x = positions[ vB * 3 ];
  133. y = positions[ vB * 3 + 1 ];
  134. z = positions[ vB * 3 + 2 ];
  135. pB.set( x, y, z );
  136. x = positions[ vC * 3 ];
  137. y = positions[ vC * 3 + 1 ];
  138. z = positions[ vC * 3 + 2 ];
  139. pC.set( x, y, z );
  140. cb.sub( pC, pB );
  141. ab.sub( pA, pB );
  142. cb.crossSelf( ab );
  143. normals[ vA * 3 ] += cb.x;
  144. normals[ vA * 3 + 1 ] += cb.y;
  145. normals[ vA * 3 + 2 ] += cb.z;
  146. normals[ vB * 3 ] += cb.x;
  147. normals[ vB * 3 + 1 ] += cb.y;
  148. normals[ vB * 3 + 2 ] += cb.z;
  149. normals[ vC * 3 ] += cb.x;
  150. normals[ vC * 3 + 1 ] += cb.y;
  151. normals[ vC * 3 + 2 ] += cb.z;
  152. }
  153. }
  154. this.normalizeNormals();
  155. this.normalsNeedUpdate = true;
  156. }
  157. },
  158. normalizeNormals: function () {
  159. var normals = this.attributes[ "normal" ].array;
  160. var x, y, z, n;
  161. for ( var i = 0, il = normals.length; i < il; i += 3 ) {
  162. x = normals[ i ];
  163. y = normals[ i + 1 ];
  164. z = normals[ i + 2 ];
  165. n = 1.0 / Math.sqrt( x * x + y * y + z * z );
  166. normals[ i ] *= n;
  167. normals[ i + 1 ] *= n;
  168. normals[ i + 2 ] *= n;
  169. }
  170. },
  171. computeTangents: function () {
  172. // based on http://www.terathon.com/code/tangent.html
  173. // (per vertex tangents)
  174. if ( this.attributes[ "index" ] === undefined ||
  175. this.attributes[ "position" ] === undefined ||
  176. this.attributes[ "normal" ] === undefined ||
  177. this.attributes[ "uv" ] === undefined ) {
  178. console.warn( "Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()" );
  179. return;
  180. }
  181. var indices = this.attributes[ "index" ].array;
  182. var positions = this.attributes[ "position" ].array;
  183. var normals = this.attributes[ "normal" ].array;
  184. var uvs = this.attributes[ "uv" ].array;
  185. var nVertices = positions.length / 3;
  186. if ( this.attributes[ "tangent" ] === undefined ) {
  187. var nTangentElements = 4 * nVertices;
  188. this.attributes[ "tangent" ] = {
  189. itemSize: 4,
  190. array: new Float32Array( nTangentElements ),
  191. numItems: nTangentElements
  192. };
  193. }
  194. var tangents = this.attributes[ "tangent" ].array;
  195. var tan1 = [], tan2 = [];
  196. for ( var k = 0; k < nVertices; k ++ ) {
  197. tan1[ k ] = new THREE.Vector3();
  198. tan2[ k ] = new THREE.Vector3();
  199. }
  200. var xA, yA, zA,
  201. xB, yB, zB,
  202. xC, yC, zC,
  203. uA, vA,
  204. uB, vB,
  205. uC, vC,
  206. x1, x2, y1, y2, z1, z2,
  207. s1, s2, t1, t2, r;
  208. var sdir = new THREE.Vector3(), tdir = new THREE.Vector3();
  209. function handleTriangle( a, b, c ) {
  210. xA = positions[ a * 3 ];
  211. yA = positions[ a * 3 + 1 ];
  212. zA = positions[ a * 3 + 2 ];
  213. xB = positions[ b * 3 ];
  214. yB = positions[ b * 3 + 1 ];
  215. zB = positions[ b * 3 + 2 ];
  216. xC = positions[ c * 3 ];
  217. yC = positions[ c * 3 + 1 ];
  218. zC = positions[ c * 3 + 2 ];
  219. uA = uvs[ a * 2 ];
  220. vA = uvs[ a * 2 + 1 ];
  221. uB = uvs[ b * 2 ];
  222. vB = uvs[ b * 2 + 1 ];
  223. uC = uvs[ c * 2 ];
  224. vC = uvs[ c * 2 + 1 ];
  225. x1 = xB - xA;
  226. x2 = xC - xA;
  227. y1 = yB - yA;
  228. y2 = yC - yA;
  229. z1 = zB - zA;
  230. z2 = zC - zA;
  231. s1 = uB - uA;
  232. s2 = uC - uA;
  233. t1 = vB - vA;
  234. t2 = vC - vA;
  235. r = 1.0 / ( s1 * t2 - s2 * t1 );
  236. sdir.set(
  237. ( t2 * x1 - t1 * x2 ) * r,
  238. ( t2 * y1 - t1 * y2 ) * r,
  239. ( t2 * z1 - t1 * z2 ) * r
  240. );
  241. tdir.set(
  242. ( s1 * x2 - s2 * x1 ) * r,
  243. ( s1 * y2 - s2 * y1 ) * r,
  244. ( s1 * z2 - s2 * z1 ) * r
  245. );
  246. tan1[ a ].addSelf( sdir );
  247. tan1[ b ].addSelf( sdir );
  248. tan1[ c ].addSelf( sdir );
  249. tan2[ a ].addSelf( tdir );
  250. tan2[ b ].addSelf( tdir );
  251. tan2[ c ].addSelf( tdir );
  252. }
  253. var i, il;
  254. var j, jl;
  255. var iA, iB, iC;
  256. var offsets = this.offsets;
  257. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  258. var start = offsets[ j ].start;
  259. var count = offsets[ j ].count;
  260. var index = offsets[ j ].index;
  261. for ( i = start, il = start + count; i < il; i += 3 ) {
  262. iA = index + indices[ i ];
  263. iB = index + indices[ i + 1 ];
  264. iC = index + indices[ i + 2 ];
  265. handleTriangle( iA, iB, iC );
  266. }
  267. }
  268. var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
  269. var n = new THREE.Vector3(), n2 = new THREE.Vector3();
  270. var w, t, test;
  271. var nx, ny, nz;
  272. function handleVertex( v ) {
  273. n.x = normals[ v * 3 ];
  274. n.y = normals[ v * 3 + 1 ];
  275. n.z = normals[ v * 3 + 2 ];
  276. n2.copy( n );
  277. t = tan1[ v ];
  278. // Gram-Schmidt orthogonalize
  279. tmp.copy( t );
  280. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  281. // Calculate handedness
  282. tmp2.cross( n2, t );
  283. test = tmp2.dot( tan2[ v ] );
  284. w = ( test < 0.0 ) ? -1.0 : 1.0;
  285. tangents[ v * 4 ] = tmp.x;
  286. tangents[ v * 4 + 1 ] = tmp.y;
  287. tangents[ v * 4 + 2 ] = tmp.z;
  288. tangents[ v * 4 + 3 ] = w;
  289. }
  290. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  291. var start = offsets[ j ].start;
  292. var count = offsets[ j ].count;
  293. var index = offsets[ j ].index;
  294. for ( i = start, il = start + count; i < il; i += 3 ) {
  295. iA = index + indices[ i ];
  296. iB = index + indices[ i + 1 ];
  297. iC = index + indices[ i + 2 ];
  298. handleVertex( iA );
  299. handleVertex( iB );
  300. handleVertex( iC );
  301. }
  302. }
  303. this.hasTangents = true;
  304. this.tangentsNeedUpdate = true;
  305. },
  306. deallocate: function () {
  307. var index = THREE.GeometryLibrary.indexOf( this );
  308. if ( index !== -1 ) THREE.GeometryLibrary.splice( index, 1 );
  309. }
  310. };