BufferGeometry.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.BufferGeometry = function () {
  5. THREE.EventTarget.call( 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 === null ) {
  41. this.boundingBox = new THREE.Box3();
  42. }
  43. var positions = this.attributes[ "position" ].array;
  44. if ( positions ) {
  45. var bb = this.boundingBox;
  46. var x, y, z;
  47. if( positions.length >= 3 ) {
  48. bb.min.x = bb.max.x = positions[ 0 ];
  49. bb.min.y = bb.max.y = positions[ 1 ];
  50. bb.min.z = bb.max.z = positions[ 2 ];
  51. }
  52. for ( var i = 3, il = positions.length; i < il; i += 3 ) {
  53. x = positions[ i ];
  54. y = positions[ i + 1 ];
  55. z = positions[ i + 2 ];
  56. // bounding box
  57. if ( x < bb.min.x ) {
  58. bb.min.x = x;
  59. } else if ( x > bb.max.x ) {
  60. bb.max.x = x;
  61. }
  62. if ( y < bb.min.y ) {
  63. bb.min.y = y;
  64. } else if ( y > bb.max.y ) {
  65. bb.max.y = y;
  66. }
  67. if ( z < bb.min.z ) {
  68. bb.min.z = z;
  69. } else if ( z > bb.max.z ) {
  70. bb.max.z = z;
  71. }
  72. }
  73. }
  74. if ( positions === undefined || positions.length === 0 ) {
  75. this.boundingBox.min.set( 0, 0, 0 );
  76. this.boundingBox.max.set( 0, 0, 0 );
  77. }
  78. },
  79. computeBoundingSphere: function () {
  80. if ( this.boundingSphere === null ) {
  81. this.boundingSphere = new THREE.Sphere();
  82. }
  83. var positions = this.attributes[ "position" ].array;
  84. if ( positions ) {
  85. var radiusSq, maxRadiusSq = 0;
  86. var x, y, z;
  87. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  88. x = positions[ i ];
  89. y = positions[ i + 1 ];
  90. z = positions[ i + 2 ];
  91. radiusSq = x * x + y * y + z * z;
  92. if ( radiusSq > maxRadiusSq ) maxRadiusSq = radiusSq;
  93. }
  94. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  95. }
  96. },
  97. computeVertexNormals: function () {
  98. if ( this.attributes[ "position" ] ) {
  99. var i, il;
  100. var j, jl;
  101. var nVertexElements = this.attributes[ "position" ].array.length;
  102. if ( this.attributes[ "normal" ] === undefined ) {
  103. this.attributes[ "normal" ] = {
  104. itemSize: 3,
  105. array: new Float32Array( nVertexElements ),
  106. numItems: nVertexElements
  107. };
  108. } else {
  109. // reset existing normals to zero
  110. for ( i = 0, il = this.attributes[ "normal" ].array.length; i < il; i ++ ) {
  111. this.attributes[ "normal" ].array[ i ] = 0;
  112. }
  113. }
  114. var positions = this.attributes[ "position" ].array;
  115. var normals = this.attributes[ "normal" ].array;
  116. var vA, vB, vC, x, y, z,
  117. pA = new THREE.Vector3(),
  118. pB = new THREE.Vector3(),
  119. pC = new THREE.Vector3(),
  120. cb = new THREE.Vector3(),
  121. ab = new THREE.Vector3();
  122. // indexed elements
  123. if ( this.attributes[ "index" ] ) {
  124. var indices = this.attributes[ "index" ].array;
  125. var offsets = this.offsets;
  126. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  127. var start = offsets[ j ].start;
  128. var count = offsets[ j ].count;
  129. var index = offsets[ j ].index;
  130. for ( i = start, il = start + count; i < il; i += 3 ) {
  131. vA = index + indices[ i ];
  132. vB = index + indices[ i + 1 ];
  133. vC = index + indices[ i + 2 ];
  134. x = positions[ vA * 3 ];
  135. y = positions[ vA * 3 + 1 ];
  136. z = positions[ vA * 3 + 2 ];
  137. pA.set( x, y, z );
  138. x = positions[ vB * 3 ];
  139. y = positions[ vB * 3 + 1 ];
  140. z = positions[ vB * 3 + 2 ];
  141. pB.set( x, y, z );
  142. x = positions[ vC * 3 ];
  143. y = positions[ vC * 3 + 1 ];
  144. z = positions[ vC * 3 + 2 ];
  145. pC.set( x, y, z );
  146. cb.sub( pC, pB );
  147. ab.sub( pA, pB );
  148. cb.crossSelf( ab );
  149. normals[ vA * 3 ] += cb.x;
  150. normals[ vA * 3 + 1 ] += cb.y;
  151. normals[ vA * 3 + 2 ] += cb.z;
  152. normals[ vB * 3 ] += cb.x;
  153. normals[ vB * 3 + 1 ] += cb.y;
  154. normals[ vB * 3 + 2 ] += cb.z;
  155. normals[ vC * 3 ] += cb.x;
  156. normals[ vC * 3 + 1 ] += cb.y;
  157. normals[ vC * 3 + 2 ] += cb.z;
  158. }
  159. }
  160. // non-indexed elements (unconnected triangle soup)
  161. } else {
  162. for ( i = 0, il = positions.length; i < il; i += 9 ) {
  163. x = positions[ i ];
  164. y = positions[ i + 1 ];
  165. z = positions[ i + 2 ];
  166. pA.set( x, y, z );
  167. x = positions[ i + 3 ];
  168. y = positions[ i + 4 ];
  169. z = positions[ i + 5 ];
  170. pB.set( x, y, z );
  171. x = positions[ i + 6 ];
  172. y = positions[ i + 7 ];
  173. z = positions[ i + 8 ];
  174. pC.set( x, y, z );
  175. cb.sub( pC, pB );
  176. ab.sub( pA, pB );
  177. cb.crossSelf( ab );
  178. normals[ i ] = cb.x;
  179. normals[ i + 1 ] = cb.y;
  180. normals[ i + 2 ] = cb.z;
  181. normals[ i + 3 ] = cb.x;
  182. normals[ i + 4 ] = cb.y;
  183. normals[ i + 5 ] = cb.z;
  184. normals[ i + 6 ] = cb.x;
  185. normals[ i + 7 ] = cb.y;
  186. normals[ i + 8 ] = cb.z;
  187. }
  188. }
  189. this.normalizeNormals();
  190. this.normalsNeedUpdate = true;
  191. }
  192. },
  193. normalizeNormals: function () {
  194. var normals = this.attributes[ "normal" ].array;
  195. var x, y, z, n;
  196. for ( var i = 0, il = normals.length; i < il; i += 3 ) {
  197. x = normals[ i ];
  198. y = normals[ i + 1 ];
  199. z = normals[ i + 2 ];
  200. n = 1.0 / Math.sqrt( x * x + y * y + z * z );
  201. normals[ i ] *= n;
  202. normals[ i + 1 ] *= n;
  203. normals[ i + 2 ] *= n;
  204. }
  205. },
  206. computeTangents: function () {
  207. // based on http://www.terathon.com/code/tangent.html
  208. // (per vertex tangents)
  209. if ( this.attributes[ "index" ] === undefined ||
  210. this.attributes[ "position" ] === undefined ||
  211. this.attributes[ "normal" ] === undefined ||
  212. this.attributes[ "uv" ] === undefined ) {
  213. console.warn( "Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()" );
  214. return;
  215. }
  216. var indices = this.attributes[ "index" ].array;
  217. var positions = this.attributes[ "position" ].array;
  218. var normals = this.attributes[ "normal" ].array;
  219. var uvs = this.attributes[ "uv" ].array;
  220. var nVertices = positions.length / 3;
  221. if ( this.attributes[ "tangent" ] === undefined ) {
  222. var nTangentElements = 4 * nVertices;
  223. this.attributes[ "tangent" ] = {
  224. itemSize: 4,
  225. array: new Float32Array( nTangentElements ),
  226. numItems: nTangentElements
  227. };
  228. }
  229. var tangents = this.attributes[ "tangent" ].array;
  230. var tan1 = [], tan2 = [];
  231. for ( var k = 0; k < nVertices; k ++ ) {
  232. tan1[ k ] = new THREE.Vector3();
  233. tan2[ k ] = new THREE.Vector3();
  234. }
  235. var xA, yA, zA,
  236. xB, yB, zB,
  237. xC, yC, zC,
  238. uA, vA,
  239. uB, vB,
  240. uC, vC,
  241. x1, x2, y1, y2, z1, z2,
  242. s1, s2, t1, t2, r;
  243. var sdir = new THREE.Vector3(), tdir = new THREE.Vector3();
  244. function handleTriangle( a, b, c ) {
  245. xA = positions[ a * 3 ];
  246. yA = positions[ a * 3 + 1 ];
  247. zA = positions[ a * 3 + 2 ];
  248. xB = positions[ b * 3 ];
  249. yB = positions[ b * 3 + 1 ];
  250. zB = positions[ b * 3 + 2 ];
  251. xC = positions[ c * 3 ];
  252. yC = positions[ c * 3 + 1 ];
  253. zC = positions[ c * 3 + 2 ];
  254. uA = uvs[ a * 2 ];
  255. vA = uvs[ a * 2 + 1 ];
  256. uB = uvs[ b * 2 ];
  257. vB = uvs[ b * 2 + 1 ];
  258. uC = uvs[ c * 2 ];
  259. vC = uvs[ c * 2 + 1 ];
  260. x1 = xB - xA;
  261. x2 = xC - xA;
  262. y1 = yB - yA;
  263. y2 = yC - yA;
  264. z1 = zB - zA;
  265. z2 = zC - zA;
  266. s1 = uB - uA;
  267. s2 = uC - uA;
  268. t1 = vB - vA;
  269. t2 = vC - vA;
  270. r = 1.0 / ( s1 * t2 - s2 * t1 );
  271. sdir.set(
  272. ( t2 * x1 - t1 * x2 ) * r,
  273. ( t2 * y1 - t1 * y2 ) * r,
  274. ( t2 * z1 - t1 * z2 ) * r
  275. );
  276. tdir.set(
  277. ( s1 * x2 - s2 * x1 ) * r,
  278. ( s1 * y2 - s2 * y1 ) * r,
  279. ( s1 * z2 - s2 * z1 ) * r
  280. );
  281. tan1[ a ].addSelf( sdir );
  282. tan1[ b ].addSelf( sdir );
  283. tan1[ c ].addSelf( sdir );
  284. tan2[ a ].addSelf( tdir );
  285. tan2[ b ].addSelf( tdir );
  286. tan2[ c ].addSelf( tdir );
  287. }
  288. var i, il;
  289. var j, jl;
  290. var iA, iB, iC;
  291. var offsets = this.offsets;
  292. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  293. var start = offsets[ j ].start;
  294. var count = offsets[ j ].count;
  295. var index = offsets[ j ].index;
  296. for ( i = start, il = start + count; i < il; i += 3 ) {
  297. iA = index + indices[ i ];
  298. iB = index + indices[ i + 1 ];
  299. iC = index + indices[ i + 2 ];
  300. handleTriangle( iA, iB, iC );
  301. }
  302. }
  303. var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
  304. var n = new THREE.Vector3(), n2 = new THREE.Vector3();
  305. var w, t, test;
  306. var nx, ny, nz;
  307. function handleVertex( v ) {
  308. n.x = normals[ v * 3 ];
  309. n.y = normals[ v * 3 + 1 ];
  310. n.z = normals[ v * 3 + 2 ];
  311. n2.copy( n );
  312. t = tan1[ v ];
  313. // Gram-Schmidt orthogonalize
  314. tmp.copy( t );
  315. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  316. // Calculate handedness
  317. tmp2.cross( n2, t );
  318. test = tmp2.dot( tan2[ v ] );
  319. w = ( test < 0.0 ) ? -1.0 : 1.0;
  320. tangents[ v * 4 ] = tmp.x;
  321. tangents[ v * 4 + 1 ] = tmp.y;
  322. tangents[ v * 4 + 2 ] = tmp.z;
  323. tangents[ v * 4 + 3 ] = w;
  324. }
  325. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  326. var start = offsets[ j ].start;
  327. var count = offsets[ j ].count;
  328. var index = offsets[ j ].index;
  329. for ( i = start, il = start + count; i < il; i += 3 ) {
  330. iA = index + indices[ i ];
  331. iB = index + indices[ i + 1 ];
  332. iC = index + indices[ i + 2 ];
  333. handleVertex( iA );
  334. handleVertex( iB );
  335. handleVertex( iC );
  336. }
  337. }
  338. this.hasTangents = true;
  339. this.tangentsNeedUpdate = true;
  340. },
  341. deallocate: function () {
  342. this.dispatchEvent( { type: 'deallocate' } );
  343. }
  344. };