BufferGeometry.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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" ] ) {
  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 positions = this.attributes[ "position" ].array;
  111. var normals = this.attributes[ "normal" ].array;
  112. var vA, vB, vC, x, y, z,
  113. pA = new THREE.Vector3(),
  114. pB = new THREE.Vector3(),
  115. pC = new THREE.Vector3(),
  116. cb = new THREE.Vector3(),
  117. ab = new THREE.Vector3();
  118. // indexed elements
  119. if ( this.attributes[ "index" ] ) {
  120. var indices = this.attributes[ "index" ].array;
  121. var offsets = this.offsets;
  122. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  123. var start = offsets[ j ].start;
  124. var count = offsets[ j ].count;
  125. var index = offsets[ j ].index;
  126. for ( i = start, il = start + count; i < il; i += 3 ) {
  127. vA = index + indices[ i ];
  128. vB = index + indices[ i + 1 ];
  129. vC = index + indices[ i + 2 ];
  130. x = positions[ vA * 3 ];
  131. y = positions[ vA * 3 + 1 ];
  132. z = positions[ vA * 3 + 2 ];
  133. pA.set( x, y, z );
  134. x = positions[ vB * 3 ];
  135. y = positions[ vB * 3 + 1 ];
  136. z = positions[ vB * 3 + 2 ];
  137. pB.set( x, y, z );
  138. x = positions[ vC * 3 ];
  139. y = positions[ vC * 3 + 1 ];
  140. z = positions[ vC * 3 + 2 ];
  141. pC.set( x, y, z );
  142. cb.sub( pC, pB );
  143. ab.sub( pA, pB );
  144. cb.crossSelf( ab );
  145. normals[ vA * 3 ] += cb.x;
  146. normals[ vA * 3 + 1 ] += cb.y;
  147. normals[ vA * 3 + 2 ] += cb.z;
  148. normals[ vB * 3 ] += cb.x;
  149. normals[ vB * 3 + 1 ] += cb.y;
  150. normals[ vB * 3 + 2 ] += cb.z;
  151. normals[ vC * 3 ] += cb.x;
  152. normals[ vC * 3 + 1 ] += cb.y;
  153. normals[ vC * 3 + 2 ] += cb.z;
  154. }
  155. }
  156. // non-indexed elements (unconnected triangle soup)
  157. } else {
  158. for ( i = 0, il = positions.length; i < il; i += 9 ) {
  159. x = positions[ i ];
  160. y = positions[ i + 1 ];
  161. z = positions[ i + 2 ];
  162. pA.set( x, y, z );
  163. x = positions[ i + 3 ];
  164. y = positions[ i + 4 ];
  165. z = positions[ i + 5 ];
  166. pB.set( x, y, z );
  167. x = positions[ i + 6 ];
  168. y = positions[ i + 7 ];
  169. z = positions[ i + 8 ];
  170. pC.set( x, y, z );
  171. cb.sub( pC, pB );
  172. ab.sub( pA, pB );
  173. cb.crossSelf( ab );
  174. normals[ i ] = cb.x;
  175. normals[ i + 1 ] = cb.y;
  176. normals[ i + 2 ] = cb.z;
  177. normals[ i + 3 ] = cb.x;
  178. normals[ i + 4 ] = cb.y;
  179. normals[ i + 5 ] = cb.z;
  180. normals[ i + 6 ] = cb.x;
  181. normals[ i + 7 ] = cb.y;
  182. normals[ i + 8 ] = cb.z;
  183. }
  184. }
  185. this.normalizeNormals();
  186. this.normalsNeedUpdate = true;
  187. }
  188. },
  189. normalizeNormals: function () {
  190. var normals = this.attributes[ "normal" ].array;
  191. var x, y, z, n;
  192. for ( var i = 0, il = normals.length; i < il; i += 3 ) {
  193. x = normals[ i ];
  194. y = normals[ i + 1 ];
  195. z = normals[ i + 2 ];
  196. n = 1.0 / Math.sqrt( x * x + y * y + z * z );
  197. normals[ i ] *= n;
  198. normals[ i + 1 ] *= n;
  199. normals[ i + 2 ] *= n;
  200. }
  201. },
  202. computeTangents: function () {
  203. // based on http://www.terathon.com/code/tangent.html
  204. // (per vertex tangents)
  205. if ( this.attributes[ "index" ] === undefined ||
  206. this.attributes[ "position" ] === undefined ||
  207. this.attributes[ "normal" ] === undefined ||
  208. this.attributes[ "uv" ] === undefined ) {
  209. console.warn( "Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()" );
  210. return;
  211. }
  212. var indices = this.attributes[ "index" ].array;
  213. var positions = this.attributes[ "position" ].array;
  214. var normals = this.attributes[ "normal" ].array;
  215. var uvs = this.attributes[ "uv" ].array;
  216. var nVertices = positions.length / 3;
  217. if ( this.attributes[ "tangent" ] === undefined ) {
  218. var nTangentElements = 4 * nVertices;
  219. this.attributes[ "tangent" ] = {
  220. itemSize: 4,
  221. array: new Float32Array( nTangentElements ),
  222. numItems: nTangentElements
  223. };
  224. }
  225. var tangents = this.attributes[ "tangent" ].array;
  226. var tan1 = [], tan2 = [];
  227. for ( var k = 0; k < nVertices; k ++ ) {
  228. tan1[ k ] = new THREE.Vector3();
  229. tan2[ k ] = new THREE.Vector3();
  230. }
  231. var xA, yA, zA,
  232. xB, yB, zB,
  233. xC, yC, zC,
  234. uA, vA,
  235. uB, vB,
  236. uC, vC,
  237. x1, x2, y1, y2, z1, z2,
  238. s1, s2, t1, t2, r;
  239. var sdir = new THREE.Vector3(), tdir = new THREE.Vector3();
  240. function handleTriangle( a, b, c ) {
  241. xA = positions[ a * 3 ];
  242. yA = positions[ a * 3 + 1 ];
  243. zA = positions[ a * 3 + 2 ];
  244. xB = positions[ b * 3 ];
  245. yB = positions[ b * 3 + 1 ];
  246. zB = positions[ b * 3 + 2 ];
  247. xC = positions[ c * 3 ];
  248. yC = positions[ c * 3 + 1 ];
  249. zC = positions[ c * 3 + 2 ];
  250. uA = uvs[ a * 2 ];
  251. vA = uvs[ a * 2 + 1 ];
  252. uB = uvs[ b * 2 ];
  253. vB = uvs[ b * 2 + 1 ];
  254. uC = uvs[ c * 2 ];
  255. vC = uvs[ c * 2 + 1 ];
  256. x1 = xB - xA;
  257. x2 = xC - xA;
  258. y1 = yB - yA;
  259. y2 = yC - yA;
  260. z1 = zB - zA;
  261. z2 = zC - zA;
  262. s1 = uB - uA;
  263. s2 = uC - uA;
  264. t1 = vB - vA;
  265. t2 = vC - vA;
  266. r = 1.0 / ( s1 * t2 - s2 * t1 );
  267. sdir.set(
  268. ( t2 * x1 - t1 * x2 ) * r,
  269. ( t2 * y1 - t1 * y2 ) * r,
  270. ( t2 * z1 - t1 * z2 ) * r
  271. );
  272. tdir.set(
  273. ( s1 * x2 - s2 * x1 ) * r,
  274. ( s1 * y2 - s2 * y1 ) * r,
  275. ( s1 * z2 - s2 * z1 ) * r
  276. );
  277. tan1[ a ].addSelf( sdir );
  278. tan1[ b ].addSelf( sdir );
  279. tan1[ c ].addSelf( sdir );
  280. tan2[ a ].addSelf( tdir );
  281. tan2[ b ].addSelf( tdir );
  282. tan2[ c ].addSelf( tdir );
  283. }
  284. var i, il;
  285. var j, jl;
  286. var iA, iB, iC;
  287. var offsets = this.offsets;
  288. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  289. var start = offsets[ j ].start;
  290. var count = offsets[ j ].count;
  291. var index = offsets[ j ].index;
  292. for ( i = start, il = start + count; i < il; i += 3 ) {
  293. iA = index + indices[ i ];
  294. iB = index + indices[ i + 1 ];
  295. iC = index + indices[ i + 2 ];
  296. handleTriangle( iA, iB, iC );
  297. }
  298. }
  299. var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
  300. var n = new THREE.Vector3(), n2 = new THREE.Vector3();
  301. var w, t, test;
  302. var nx, ny, nz;
  303. function handleVertex( v ) {
  304. n.x = normals[ v * 3 ];
  305. n.y = normals[ v * 3 + 1 ];
  306. n.z = normals[ v * 3 + 2 ];
  307. n2.copy( n );
  308. t = tan1[ v ];
  309. // Gram-Schmidt orthogonalize
  310. tmp.copy( t );
  311. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  312. // Calculate handedness
  313. tmp2.cross( n2, t );
  314. test = tmp2.dot( tan2[ v ] );
  315. w = ( test < 0.0 ) ? -1.0 : 1.0;
  316. tangents[ v * 4 ] = tmp.x;
  317. tangents[ v * 4 + 1 ] = tmp.y;
  318. tangents[ v * 4 + 2 ] = tmp.z;
  319. tangents[ v * 4 + 3 ] = w;
  320. }
  321. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  322. var start = offsets[ j ].start;
  323. var count = offsets[ j ].count;
  324. var index = offsets[ j ].index;
  325. for ( i = start, il = start + count; i < il; i += 3 ) {
  326. iA = index + indices[ i ];
  327. iB = index + indices[ i + 1 ];
  328. iC = index + indices[ i + 2 ];
  329. handleVertex( iA );
  330. handleVertex( iB );
  331. handleVertex( iC );
  332. }
  333. }
  334. this.hasTangents = true;
  335. this.tangentsNeedUpdate = true;
  336. },
  337. deallocate: function () {
  338. var index = THREE.GeometryLibrary.indexOf( this );
  339. if ( index !== -1 ) THREE.GeometryLibrary.splice( index, 1 );
  340. }
  341. };