BufferGeometry.js 10 KB

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