BufferGeometry.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.BufferGeometry = function () {
  5. this.id = THREE.Math.generateUUID();
  6. // attributes
  7. this.attributes = {};
  8. // attributes typed arrays are kept only if dynamic flag is set
  9. this.dynamic = false;
  10. // offsets for chunks when using indexed elements
  11. this.offsets = [];
  12. // boundings
  13. this.boundingBox = null;
  14. this.boundingSphere = null;
  15. this.hasTangents = false;
  16. // for compatibility
  17. this.morphTargets = [];
  18. };
  19. THREE.BufferGeometry.prototype = {
  20. constructor: THREE.BufferGeometry,
  21. addEventListener: THREE.EventDispatcher.prototype.addEventListener,
  22. hasEventListener: THREE.EventDispatcher.prototype.hasEventListener,
  23. removeEventListener: THREE.EventDispatcher.prototype.removeEventListener,
  24. dispatchEvent: THREE.EventDispatcher.prototype.dispatchEvent,
  25. applyMatrix: function ( matrix ) {
  26. var positionArray;
  27. var normalArray;
  28. if ( this.attributes[ "position" ] ) positionArray = this.attributes[ "position" ].array;
  29. if ( this.attributes[ "normal" ] ) normalArray = this.attributes[ "normal" ].array;
  30. if ( positionArray !== undefined ) {
  31. matrix.multiplyVector3Array( positionArray );
  32. this.verticesNeedUpdate = true;
  33. }
  34. if ( normalArray !== undefined ) {
  35. var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  36. normalMatrix.multiplyVector3Array( normalArray );
  37. this.normalizeNormals();
  38. this.normalsNeedUpdate = true;
  39. }
  40. },
  41. computeBoundingBox: function () {
  42. if ( this.boundingBox === null ) {
  43. this.boundingBox = new THREE.Box3();
  44. }
  45. var positions = this.attributes[ "position" ].array;
  46. if ( positions ) {
  47. var bb = this.boundingBox;
  48. var x, y, z;
  49. if( positions.length >= 3 ) {
  50. bb.min.x = bb.max.x = positions[ 0 ];
  51. bb.min.y = bb.max.y = positions[ 1 ];
  52. bb.min.z = bb.max.z = positions[ 2 ];
  53. }
  54. for ( var i = 3, il = positions.length; i < il; i += 3 ) {
  55. x = positions[ i ];
  56. y = positions[ i + 1 ];
  57. z = positions[ i + 2 ];
  58. // bounding box
  59. if ( x < bb.min.x ) {
  60. bb.min.x = x;
  61. } else if ( x > bb.max.x ) {
  62. bb.max.x = x;
  63. }
  64. if ( y < bb.min.y ) {
  65. bb.min.y = y;
  66. } else if ( y > bb.max.y ) {
  67. bb.max.y = y;
  68. }
  69. if ( z < bb.min.z ) {
  70. bb.min.z = z;
  71. } else if ( z > bb.max.z ) {
  72. bb.max.z = z;
  73. }
  74. }
  75. }
  76. if ( positions === undefined || positions.length === 0 ) {
  77. this.boundingBox.min.set( 0, 0, 0 );
  78. this.boundingBox.max.set( 0, 0, 0 );
  79. }
  80. },
  81. computeBoundingSphere: function () {
  82. if ( this.boundingSphere === null ) {
  83. this.boundingSphere = new THREE.Sphere();
  84. }
  85. var positions = this.attributes[ "position" ].array;
  86. if ( positions ) {
  87. var radiusSq, maxRadiusSq = 0;
  88. var x, y, z;
  89. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  90. x = positions[ i ];
  91. y = positions[ i + 1 ];
  92. z = positions[ i + 2 ];
  93. radiusSq = x * x + y * y + z * z;
  94. if ( radiusSq > maxRadiusSq ) maxRadiusSq = radiusSq;
  95. }
  96. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  97. }
  98. },
  99. computeVertexNormals: function () {
  100. if ( this.attributes[ "position" ] ) {
  101. var i, il;
  102. var j, jl;
  103. var nVertexElements = this.attributes[ "position" ].array.length;
  104. if ( this.attributes[ "normal" ] === undefined ) {
  105. this.attributes[ "normal" ] = {
  106. itemSize: 3,
  107. array: new Float32Array( nVertexElements )
  108. };
  109. } else {
  110. // reset existing normals to zero
  111. for ( i = 0, il = this.attributes[ "normal" ].array.length; i < il; i ++ ) {
  112. this.attributes[ "normal" ].array[ i ] = 0;
  113. }
  114. }
  115. var positions = this.attributes[ "position" ].array;
  116. var normals = this.attributes[ "normal" ].array;
  117. var vA, vB, vC, x, y, z,
  118. pA = new THREE.Vector3(),
  119. pB = new THREE.Vector3(),
  120. pC = new THREE.Vector3(),
  121. cb = new THREE.Vector3(),
  122. ab = new THREE.Vector3();
  123. // indexed elements
  124. if ( this.attributes[ "index" ] ) {
  125. var indices = this.attributes[ "index" ].array;
  126. var offsets = this.offsets;
  127. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  128. var start = offsets[ j ].start;
  129. var count = offsets[ j ].count;
  130. var index = offsets[ j ].index;
  131. for ( i = start, il = start + count; i < il; i += 3 ) {
  132. vA = index + indices[ i ];
  133. vB = index + indices[ i + 1 ];
  134. vC = index + indices[ i + 2 ];
  135. x = positions[ vA * 3 ];
  136. y = positions[ vA * 3 + 1 ];
  137. z = positions[ vA * 3 + 2 ];
  138. pA.set( x, y, z );
  139. x = positions[ vB * 3 ];
  140. y = positions[ vB * 3 + 1 ];
  141. z = positions[ vB * 3 + 2 ];
  142. pB.set( x, y, z );
  143. x = positions[ vC * 3 ];
  144. y = positions[ vC * 3 + 1 ];
  145. z = positions[ vC * 3 + 2 ];
  146. pC.set( x, y, z );
  147. cb.subVectors( pC, pB );
  148. ab.subVectors( pA, pB );
  149. cb.cross( ab );
  150. normals[ vA * 3 ] += cb.x;
  151. normals[ vA * 3 + 1 ] += cb.y;
  152. normals[ vA * 3 + 2 ] += cb.z;
  153. normals[ vB * 3 ] += cb.x;
  154. normals[ vB * 3 + 1 ] += cb.y;
  155. normals[ vB * 3 + 2 ] += cb.z;
  156. normals[ vC * 3 ] += cb.x;
  157. normals[ vC * 3 + 1 ] += cb.y;
  158. normals[ vC * 3 + 2 ] += cb.z;
  159. }
  160. }
  161. // non-indexed elements (unconnected triangle soup)
  162. } else {
  163. for ( i = 0, il = positions.length; i < il; i += 9 ) {
  164. x = positions[ i ];
  165. y = positions[ i + 1 ];
  166. z = positions[ i + 2 ];
  167. pA.set( x, y, z );
  168. x = positions[ i + 3 ];
  169. y = positions[ i + 4 ];
  170. z = positions[ i + 5 ];
  171. pB.set( x, y, z );
  172. x = positions[ i + 6 ];
  173. y = positions[ i + 7 ];
  174. z = positions[ i + 8 ];
  175. pC.set( x, y, z );
  176. cb.subVectors( pC, pB );
  177. ab.subVectors( pA, pB );
  178. cb.cross( ab );
  179. normals[ i ] = cb.x;
  180. normals[ i + 1 ] = cb.y;
  181. normals[ i + 2 ] = cb.z;
  182. normals[ i + 3 ] = cb.x;
  183. normals[ i + 4 ] = cb.y;
  184. normals[ i + 5 ] = cb.z;
  185. normals[ i + 6 ] = cb.x;
  186. normals[ i + 7 ] = cb.y;
  187. normals[ i + 8 ] = cb.z;
  188. }
  189. }
  190. this.normalizeNormals();
  191. this.normalsNeedUpdate = true;
  192. }
  193. },
  194. normalizeNormals: function () {
  195. var normals = this.attributes[ "normal" ].array;
  196. var x, y, z, n;
  197. for ( var i = 0, il = normals.length; i < il; i += 3 ) {
  198. x = normals[ i ];
  199. y = normals[ i + 1 ];
  200. z = normals[ i + 2 ];
  201. n = 1.0 / Math.sqrt( x * x + y * y + z * z );
  202. normals[ i ] *= n;
  203. normals[ i + 1 ] *= n;
  204. normals[ i + 2 ] *= n;
  205. }
  206. },
  207. computeTangents: function () {
  208. // based on http://www.terathon.com/code/tangent.html
  209. // (per vertex tangents)
  210. if ( this.attributes[ "index" ] === undefined ||
  211. this.attributes[ "position" ] === undefined ||
  212. this.attributes[ "normal" ] === undefined ||
  213. this.attributes[ "uv" ] === undefined ) {
  214. console.warn( "Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()" );
  215. return;
  216. }
  217. var indices = this.attributes[ "index" ].array;
  218. var positions = this.attributes[ "position" ].array;
  219. var normals = this.attributes[ "normal" ].array;
  220. var uvs = this.attributes[ "uv" ].array;
  221. var nVertices = positions.length / 3;
  222. if ( this.attributes[ "tangent" ] === undefined ) {
  223. var nTangentElements = 4 * nVertices;
  224. this.attributes[ "tangent" ] = {
  225. itemSize: 4,
  226. array: new Float32Array( 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 ].add( sdir );
  282. tan1[ b ].add( sdir );
  283. tan1[ c ].add( sdir );
  284. tan2[ a ].add( tdir );
  285. tan2[ b ].add( tdir );
  286. tan2[ c ].add( 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. function handleVertex( v ) {
  307. n.x = normals[ v * 3 ];
  308. n.y = normals[ v * 3 + 1 ];
  309. n.z = normals[ v * 3 + 2 ];
  310. n2.copy( n );
  311. t = tan1[ v ];
  312. // Gram-Schmidt orthogonalize
  313. tmp.copy( t );
  314. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  315. // Calculate handedness
  316. tmp2.crossVectors( n2, t );
  317. test = tmp2.dot( tan2[ v ] );
  318. w = ( test < 0.0 ) ? -1.0 : 1.0;
  319. tangents[ v * 4 ] = tmp.x;
  320. tangents[ v * 4 + 1 ] = tmp.y;
  321. tangents[ v * 4 + 2 ] = tmp.z;
  322. tangents[ v * 4 + 3 ] = w;
  323. }
  324. for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
  325. var start = offsets[ j ].start;
  326. var count = offsets[ j ].count;
  327. var index = offsets[ j ].index;
  328. for ( i = start, il = start + count; i < il; i += 3 ) {
  329. iA = index + indices[ i ];
  330. iB = index + indices[ i + 1 ];
  331. iC = index + indices[ i + 2 ];
  332. handleVertex( iA );
  333. handleVertex( iB );
  334. handleVertex( iC );
  335. }
  336. }
  337. this.hasTangents = true;
  338. this.tangentsNeedUpdate = true;
  339. },
  340. dispose: function () {
  341. this.dispatchEvent( { type: 'dispose' } );
  342. }
  343. };