BufferGeometry.js 11 KB

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