BufferGeometry.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.BufferGeometry = function () {
  5. this.id = THREE.GeometryIdCount ++;
  6. this.uuid = THREE.Math.generateUUID();
  7. this.name = '';
  8. // attributes
  9. this.attributes = {};
  10. // attributes typed arrays are kept only if dynamic flag is set
  11. this.dynamic = true;
  12. // offsets for chunks when using indexed elements
  13. this.offsets = [];
  14. // boundings
  15. this.boundingBox = null;
  16. this.boundingSphere = null;
  17. this.hasTangents = false;
  18. // for compatibility
  19. this.morphTargets = [];
  20. };
  21. THREE.BufferGeometry.prototype = {
  22. constructor: THREE.BufferGeometry,
  23. addAttribute: function( name, type, numItems, itemSize ) {
  24. this.attributes[ name ] = {
  25. itemSize: itemSize,
  26. array: new type( numItems * itemSize )
  27. };
  28. },
  29. applyMatrix: function ( matrix ) {
  30. var positionArray;
  31. var normalArray;
  32. if ( this.attributes[ "position" ] ) positionArray = this.attributes[ "position" ].array;
  33. if ( this.attributes[ "normal" ] ) normalArray = this.attributes[ "normal" ].array;
  34. if ( positionArray !== undefined ) {
  35. matrix.multiplyVector3Array( positionArray );
  36. this.verticesNeedUpdate = true;
  37. }
  38. if ( normalArray !== undefined ) {
  39. var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  40. normalMatrix.multiplyVector3Array( normalArray );
  41. this.normalizeNormals();
  42. this.normalsNeedUpdate = true;
  43. }
  44. },
  45. computeBoundingBox: function () {
  46. if ( this.boundingBox === null ) {
  47. this.boundingBox = new THREE.Box3();
  48. }
  49. var positions = this.attributes[ "position" ].array;
  50. if ( positions ) {
  51. var bb = this.boundingBox;
  52. var x, y, z;
  53. if( positions.length >= 3 ) {
  54. bb.min.x = bb.max.x = positions[ 0 ];
  55. bb.min.y = bb.max.y = positions[ 1 ];
  56. bb.min.z = bb.max.z = positions[ 2 ];
  57. }
  58. for ( var i = 3, il = positions.length; i < il; i += 3 ) {
  59. x = positions[ i ];
  60. y = positions[ i + 1 ];
  61. z = positions[ i + 2 ];
  62. // bounding box
  63. if ( x < bb.min.x ) {
  64. bb.min.x = x;
  65. } else if ( x > bb.max.x ) {
  66. bb.max.x = x;
  67. }
  68. if ( y < bb.min.y ) {
  69. bb.min.y = y;
  70. } else if ( y > bb.max.y ) {
  71. bb.max.y = y;
  72. }
  73. if ( z < bb.min.z ) {
  74. bb.min.z = z;
  75. } else if ( z > bb.max.z ) {
  76. bb.max.z = z;
  77. }
  78. }
  79. }
  80. if ( positions === undefined || positions.length === 0 ) {
  81. this.boundingBox.min.set( 0, 0, 0 );
  82. this.boundingBox.max.set( 0, 0, 0 );
  83. }
  84. },
  85. computeBoundingSphere: function () {
  86. var box = new THREE.Box3();
  87. var vector = new THREE.Vector3();
  88. return function () {
  89. if ( this.boundingSphere === null ) {
  90. this.boundingSphere = new THREE.Sphere();
  91. }
  92. var positions = this.attributes[ "position" ].array;
  93. if ( positions ) {
  94. var center = this.boundingSphere.center;
  95. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  96. vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  97. box.addPoint( vector );
  98. }
  99. box.center( center );
  100. var maxRadiusSq = 0;
  101. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  102. vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  103. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  104. }
  105. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  106. }
  107. }
  108. }(),
  109. computeVertexNormals: function () {
  110. if ( this.attributes[ "position" ] ) {
  111. var i, il;
  112. var j, jl;
  113. var nVertexElements = this.attributes[ "position" ].array.length;
  114. if ( this.attributes[ "normal" ] === undefined ) {
  115. this.attributes[ "normal" ] = {
  116. itemSize: 3,
  117. array: new Float32Array( nVertexElements )
  118. };
  119. } else {
  120. // reset existing normals to zero
  121. for ( i = 0, il = this.attributes[ "normal" ].array.length; i < il; i ++ ) {
  122. this.attributes[ "normal" ].array[ i ] = 0;
  123. }
  124. }
  125. var positions = this.attributes[ "position" ].array;
  126. var normals = this.attributes[ "normal" ].array;
  127. var vA, vB, vC, x, y, z,
  128. pA = new THREE.Vector3(),
  129. pB = new THREE.Vector3(),
  130. pC = new THREE.Vector3(),
  131. cb = new THREE.Vector3(),
  132. ab = new THREE.Vector3();
  133. // indexed elements
  134. if ( this.attributes[ "index" ] ) {
  135. var indices = this.attributes[ "index" ].array;
  136. var offsets = this.offsets;
  137. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  138. var start = offsets[ j ].start;
  139. var count = offsets[ j ].count;
  140. var index = offsets[ j ].index;
  141. for ( i = start, il = start + count; i < il; i += 3 ) {
  142. vA = index + indices[ i ];
  143. vB = index + indices[ i + 1 ];
  144. vC = index + indices[ i + 2 ];
  145. x = positions[ vA * 3 ];
  146. y = positions[ vA * 3 + 1 ];
  147. z = positions[ vA * 3 + 2 ];
  148. pA.set( x, y, z );
  149. x = positions[ vB * 3 ];
  150. y = positions[ vB * 3 + 1 ];
  151. z = positions[ vB * 3 + 2 ];
  152. pB.set( x, y, z );
  153. x = positions[ vC * 3 ];
  154. y = positions[ vC * 3 + 1 ];
  155. z = positions[ vC * 3 + 2 ];
  156. pC.set( x, y, z );
  157. cb.subVectors( pC, pB );
  158. ab.subVectors( pA, pB );
  159. cb.cross( ab );
  160. normals[ vA * 3 ] += cb.x;
  161. normals[ vA * 3 + 1 ] += cb.y;
  162. normals[ vA * 3 + 2 ] += cb.z;
  163. normals[ vB * 3 ] += cb.x;
  164. normals[ vB * 3 + 1 ] += cb.y;
  165. normals[ vB * 3 + 2 ] += cb.z;
  166. normals[ vC * 3 ] += cb.x;
  167. normals[ vC * 3 + 1 ] += cb.y;
  168. normals[ vC * 3 + 2 ] += cb.z;
  169. }
  170. }
  171. // non-indexed elements (unconnected triangle soup)
  172. } else {
  173. for ( i = 0, il = positions.length; i < il; i += 9 ) {
  174. x = positions[ i ];
  175. y = positions[ i + 1 ];
  176. z = positions[ i + 2 ];
  177. pA.set( x, y, z );
  178. x = positions[ i + 3 ];
  179. y = positions[ i + 4 ];
  180. z = positions[ i + 5 ];
  181. pB.set( x, y, z );
  182. x = positions[ i + 6 ];
  183. y = positions[ i + 7 ];
  184. z = positions[ i + 8 ];
  185. pC.set( x, y, z );
  186. cb.subVectors( pC, pB );
  187. ab.subVectors( pA, pB );
  188. cb.cross( ab );
  189. normals[ i ] = cb.x;
  190. normals[ i + 1 ] = cb.y;
  191. normals[ i + 2 ] = cb.z;
  192. normals[ i + 3 ] = cb.x;
  193. normals[ i + 4 ] = cb.y;
  194. normals[ i + 5 ] = cb.z;
  195. normals[ i + 6 ] = cb.x;
  196. normals[ i + 7 ] = cb.y;
  197. normals[ i + 8 ] = cb.z;
  198. }
  199. }
  200. this.normalizeNormals();
  201. this.normalsNeedUpdate = true;
  202. }
  203. },
  204. normalizeNormals: function () {
  205. var normals = this.attributes[ "normal" ].array;
  206. var x, y, z, n;
  207. for ( var i = 0, il = normals.length; i < il; i += 3 ) {
  208. x = normals[ i ];
  209. y = normals[ i + 1 ];
  210. z = normals[ i + 2 ];
  211. n = 1.0 / Math.sqrt( x * x + y * y + z * z );
  212. normals[ i ] *= n;
  213. normals[ i + 1 ] *= n;
  214. normals[ i + 2 ] *= n;
  215. }
  216. },
  217. computeTangents: function () {
  218. // based on http://www.terathon.com/code/tangent.html
  219. // (per vertex tangents)
  220. if ( this.attributes[ "index" ] === undefined ||
  221. this.attributes[ "position" ] === undefined ||
  222. this.attributes[ "normal" ] === undefined ||
  223. this.attributes[ "uv" ] === undefined ) {
  224. console.warn( "Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()" );
  225. return;
  226. }
  227. var indices = this.attributes[ "index" ].array;
  228. var positions = this.attributes[ "position" ].array;
  229. var normals = this.attributes[ "normal" ].array;
  230. var uvs = this.attributes[ "uv" ].array;
  231. var nVertices = positions.length / 3;
  232. if ( this.attributes[ "tangent" ] === undefined ) {
  233. var nTangentElements = 4 * nVertices;
  234. this.attributes[ "tangent" ] = {
  235. itemSize: 4,
  236. array: new Float32Array( nTangentElements )
  237. };
  238. }
  239. var tangents = this.attributes[ "tangent" ].array;
  240. var tan1 = [], tan2 = [];
  241. for ( var k = 0; k < nVertices; k ++ ) {
  242. tan1[ k ] = new THREE.Vector3();
  243. tan2[ k ] = new THREE.Vector3();
  244. }
  245. var xA, yA, zA,
  246. xB, yB, zB,
  247. xC, yC, zC,
  248. uA, vA,
  249. uB, vB,
  250. uC, vC,
  251. x1, x2, y1, y2, z1, z2,
  252. s1, s2, t1, t2, r;
  253. var sdir = new THREE.Vector3(), tdir = new THREE.Vector3();
  254. function handleTriangle( a, b, c ) {
  255. xA = positions[ a * 3 ];
  256. yA = positions[ a * 3 + 1 ];
  257. zA = positions[ a * 3 + 2 ];
  258. xB = positions[ b * 3 ];
  259. yB = positions[ b * 3 + 1 ];
  260. zB = positions[ b * 3 + 2 ];
  261. xC = positions[ c * 3 ];
  262. yC = positions[ c * 3 + 1 ];
  263. zC = positions[ c * 3 + 2 ];
  264. uA = uvs[ a * 2 ];
  265. vA = uvs[ a * 2 + 1 ];
  266. uB = uvs[ b * 2 ];
  267. vB = uvs[ b * 2 + 1 ];
  268. uC = uvs[ c * 2 ];
  269. vC = uvs[ c * 2 + 1 ];
  270. x1 = xB - xA;
  271. x2 = xC - xA;
  272. y1 = yB - yA;
  273. y2 = yC - yA;
  274. z1 = zB - zA;
  275. z2 = zC - zA;
  276. s1 = uB - uA;
  277. s2 = uC - uA;
  278. t1 = vB - vA;
  279. t2 = vC - vA;
  280. r = 1.0 / ( s1 * t2 - s2 * t1 );
  281. sdir.set(
  282. ( t2 * x1 - t1 * x2 ) * r,
  283. ( t2 * y1 - t1 * y2 ) * r,
  284. ( t2 * z1 - t1 * z2 ) * r
  285. );
  286. tdir.set(
  287. ( s1 * x2 - s2 * x1 ) * r,
  288. ( s1 * y2 - s2 * y1 ) * r,
  289. ( s1 * z2 - s2 * z1 ) * r
  290. );
  291. tan1[ a ].add( sdir );
  292. tan1[ b ].add( sdir );
  293. tan1[ c ].add( sdir );
  294. tan2[ a ].add( tdir );
  295. tan2[ b ].add( tdir );
  296. tan2[ c ].add( tdir );
  297. }
  298. var i, il;
  299. var j, jl;
  300. var iA, iB, iC;
  301. var offsets = this.offsets;
  302. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  303. var start = offsets[ j ].start;
  304. var count = offsets[ j ].count;
  305. var index = offsets[ j ].index;
  306. for ( i = start, il = start + count; i < il; i += 3 ) {
  307. iA = index + indices[ i ];
  308. iB = index + indices[ i + 1 ];
  309. iC = index + indices[ i + 2 ];
  310. handleTriangle( iA, iB, iC );
  311. }
  312. }
  313. var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
  314. var n = new THREE.Vector3(), n2 = new THREE.Vector3();
  315. var w, t, test;
  316. function handleVertex( v ) {
  317. n.x = normals[ v * 3 ];
  318. n.y = normals[ v * 3 + 1 ];
  319. n.z = normals[ v * 3 + 2 ];
  320. n2.copy( n );
  321. t = tan1[ v ];
  322. // Gram-Schmidt orthogonalize
  323. tmp.copy( t );
  324. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  325. // Calculate handedness
  326. tmp2.crossVectors( n2, t );
  327. test = tmp2.dot( tan2[ v ] );
  328. w = ( test < 0.0 ) ? -1.0 : 1.0;
  329. tangents[ v * 4 ] = tmp.x;
  330. tangents[ v * 4 + 1 ] = tmp.y;
  331. tangents[ v * 4 + 2 ] = tmp.z;
  332. tangents[ v * 4 + 3 ] = w;
  333. }
  334. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  335. var start = offsets[ j ].start;
  336. var count = offsets[ j ].count;
  337. var index = offsets[ j ].index;
  338. for ( i = start, il = start + count; i < il; i += 3 ) {
  339. iA = index + indices[ i ];
  340. iB = index + indices[ i + 1 ];
  341. iC = index + indices[ i + 2 ];
  342. handleVertex( iA );
  343. handleVertex( iB );
  344. handleVertex( iC );
  345. }
  346. }
  347. this.hasTangents = true;
  348. this.tangentsNeedUpdate = true;
  349. },
  350. clone: function () {
  351. var geometry = new THREE.BufferGeometry();
  352. var types = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array ];
  353. for ( var attr in this.attributes ) {
  354. var sourceAttr = this.attributes[ attr ];
  355. var sourceArray = sourceAttr.array;
  356. var attribute = {
  357. itemSize: sourceAttr.itemSize,
  358. numItems: sourceAttr.numItems,
  359. array: null
  360. };
  361. for ( var i = 0, il = types.length; i < il; i ++ ) {
  362. var type = types[ i ];
  363. if ( sourceArray instanceof type ) {
  364. attribute.array = new type( sourceArray );
  365. break;
  366. }
  367. }
  368. geometry.attributes[ attr ] = attribute;
  369. }
  370. for ( var i = 0, il = this.offsets.length; i < il; i ++ ) {
  371. var offset = this.offsets[ i ];
  372. geometry.offsets.push( {
  373. start: offset.start,
  374. index: offset.index,
  375. count: offset.count
  376. } );
  377. }
  378. return geometry;
  379. },
  380. dispose: function () {
  381. this.dispatchEvent( { type: 'dispose' } );
  382. }
  383. };
  384. THREE.EventDispatcher.prototype.apply( THREE.BufferGeometry.prototype );