ProxyGeometry.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author kile / http://kile.stravaganza.org/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author mikael emtinger / http://gomo.se/
  6. * @author zz85 / http://www.lab4games.net/zz85/blog
  7. * @author bhouston / http://exocortex.com
  8. * @author jbaicoianu / http://baicoianu.com
  9. */
  10. THREE.ProxyGeometry = function ( ) {
  11. THREE.BufferGeometry.call( this );
  12. this.addEventListener( 'allocate', this.onGeometryAllocate);
  13. // TODO - implement as BufferGeometry attributes
  14. this.morphTargets = [];
  15. this.morphColors = [];
  16. };
  17. THREE.ProxyGeometry.prototype = Object.create( THREE.IndexedGeometry2.prototype );
  18. Object.defineProperties(THREE.ProxyGeometry.prototype, {
  19. vertices: {
  20. enumerable: true,
  21. configurable: true,
  22. get: function() { return this.createVertexProxies(); }
  23. },
  24. faces: {
  25. enumerable: true,
  26. get: function() { return this.createFaceProxies() }
  27. },
  28. faceVertexUvs: {
  29. enumerable: true,
  30. get: function() { return this.createUvProxies() }
  31. },
  32. colors: {
  33. enumerable: true,
  34. get: function() { return this.createColorProxies() }
  35. },
  36. skinIndices: {
  37. enumerable: true,
  38. get: function() { return this.createSkinIndexProxies() }
  39. },
  40. skinWeights: {
  41. enumerable: true,
  42. get: function() { return this.createSkinWeightProxies() }
  43. },
  44. // TODO - fill in additional proxies:
  45. // - morphColors
  46. // - morphNormals
  47. // - morphTargets
  48. verticesNeedUpdate: {
  49. enumerable: true,
  50. get: function() { if (this.attributes[ 'position' ]) return this.attributes[ 'position' ].needsUpdate; } ,
  51. set: function(v) { if (this.attributes[ 'position' ]) this.attributes[ 'position' ].needsUpdate = v; }
  52. },
  53. colorsNeedUpdate: {
  54. enumerable: true,
  55. get: function() { if (this.attributes[ 'color' ]) return this.attributes[ 'color' ].needsUpdate; } ,
  56. set: function(v) { if (this.attributes[ 'color' ]) this.attributes[ 'color' ].needsUpdate = v; }
  57. },
  58. normalsNeedUpdate: {
  59. enumerable: true,
  60. get: function() { if (this.attributes[ 'normal' ]) return this.attributes[ 'normal' ].needsUpdate; } ,
  61. set: function(v) { if (this.attributes[ 'normal' ]) this.attributes[ 'normal' ].needsUpdate = v; }
  62. },
  63. });
  64. THREE.ProxyGeometry.prototype.createVertexProxies = function(values) {
  65. if (!this.hasOwnProperty('vertices')) {
  66. // Replace the prototype getter with a local array property
  67. Object.defineProperty( this, "vertices", { value: [], writable: true } );
  68. } else {
  69. // Start with a new, empty array
  70. this.vertices = [];
  71. }
  72. // If the attribute buffer has already been populated, set up proxy objects
  73. this.populateProxyFromBuffer(this.vertices, "position", THREE.ProxyVector3, 3);
  74. // If values were passed in, store them in the buffer via the proxy objects
  75. if (values) {
  76. for (var i = 0; i < values.length; i++) {
  77. this.vertices[i].copy(values[i]);
  78. }
  79. }
  80. // Return a reference to the newly-created array
  81. return this.vertices;
  82. }
  83. THREE.ProxyGeometry.prototype.createFaceProxies = function(values) {
  84. if (!this.hasOwnProperty("faces")) {
  85. // Replace the prototype getter with a local array property
  86. Object.defineProperty( this, "faces", { value: [], writable: true } );
  87. } else {
  88. // Start with a new, empty array
  89. this.faces = [];
  90. }
  91. // If the attribute buffer has already been populated, set up proxy objects
  92. var faces = this.faces,
  93. indexarray = false,
  94. positionarray = false,
  95. normalarray = false,
  96. colorarray = false,
  97. tangentarray = false;
  98. if ( this.attributes[ 'index' ] ) {
  99. indexarray = this.attributes[ 'index' ].array;
  100. }
  101. if ( this.attributes[ 'position' ] ) {
  102. positionarray = this.attributes[ 'position' ].array;
  103. }
  104. if (this.attributes[ 'normal' ]) {
  105. normalarray = this.attributes[ 'normal' ].array;
  106. }
  107. if (this.attributes[ 'color' ]) {
  108. colorarray = this.attributes[ 'color' ].array;
  109. }
  110. if (this.attributes[ 'tangent' ]) {
  111. tangentarray = this.attributes[ 'tangent' ].array;
  112. }
  113. // TODO - this should be accomplished using "virtual" functions on various classes (IndexedGeometry, SmoothGeometry, etc)
  114. if (indexarray) {
  115. for ( var i = 0, l = indexarray.length / 3; i < l; i ++ ) {
  116. var o = i * 3;
  117. var face = new THREE.ProxyFace3( indexarray, i * 3 );
  118. faces.push(face);
  119. }
  120. } else if (positionarray) {
  121. for ( var i = 0, l = positionarray.length / 3; i < l; i += 3 ) {
  122. var o = i * 3;
  123. var v1 = i, v2 = i+1, v3 = i+2;
  124. var face = new THREE.ProxyFace3( v1, v2, v3 );
  125. faces.push(face);
  126. }
  127. }
  128. // If values were passed in, store them in the buffer via the proxy objects
  129. if (values) {
  130. for (var i = 0, l = values.length; i < l; i++) {
  131. var f = faces[i],
  132. v = values[i];
  133. f.a = v.a;
  134. f.b = v.b;
  135. f.c = v.c;
  136. }
  137. }
  138. if (normalarray) {
  139. this.createFaceVertexNormalProxies(values);
  140. }
  141. if (colorarray) {
  142. this.createFaceVertexColorProxies(values);
  143. }
  144. if (tangentarray) {
  145. this.createFaceVertexTangentProxies(values);
  146. }
  147. // Return a reference to the newly-created array
  148. return this.faces;
  149. }
  150. THREE.ProxyGeometry.prototype.createFaceVertexNormalProxies = function(values) {
  151. if ( this.attributes[ 'normal' ] && this.attributes[ 'normal' ].array ) {
  152. var normalarray = this.attributes[ 'normal' ].array;
  153. for (var i = 0, l = this.faces.length; i < l; i++) {
  154. var f = this.faces[i];
  155. f.vertexNormals = [
  156. new THREE.ProxyVector3(normalarray, f.a * 3),
  157. new THREE.ProxyVector3(normalarray, f.b * 3),
  158. new THREE.ProxyVector3(normalarray, f.c * 3),
  159. ];
  160. f.normal = new THREE.MultiVector3(f.vertexNormals);
  161. }
  162. }
  163. // If values were passed in, store them in the buffer via the proxy objects
  164. if (values) {
  165. for (var i = 0, l = values.length; i < l; i++) {
  166. var f = this.faces[i],
  167. v = values[i];
  168. if (v.vertexNormals.length > 0) {
  169. for (var j = 0, l2 = f.vertexNormals.length; j < l2; j++) {
  170. f.vertexNormals[j].copy(v.vertexNormals[j]);
  171. }
  172. } else if (v.normal) {
  173. f.normal.copy(v.normal);
  174. }
  175. }
  176. }
  177. }
  178. THREE.ProxyGeometry.prototype.createFaceVertexColorProxies = function(values) {
  179. if ( this.attributes[ 'color' ] && this.attributes[ 'color' ].array ) {
  180. var colorarray = this.attributes[ 'color' ].array;
  181. for (var i = 0, l = this.faces.length; i < l; i++) {
  182. var f = this.faces[i];
  183. if ( this.attributes[ 'index' ] ) {
  184. f.vertexColors = [
  185. new THREE.ProxyColor(colorarray, f.a * 3),
  186. new THREE.ProxyColor(colorarray, f.b * 3),
  187. new THREE.ProxyColor(colorarray, f.c * 3),
  188. ];
  189. } else {
  190. var o = i * 9;
  191. f.vertexColors = [
  192. new THREE.ProxyColor(colorarray, o),
  193. new THREE.ProxyColor(colorarray, o + 3),
  194. new THREE.ProxyColor(colorarray, o + 6),
  195. ];
  196. }
  197. f.color = new THREE.MultiColor(f.vertexColors);
  198. }
  199. }
  200. // If values were passed in, store them in the buffer via the proxy objects
  201. if (values) {
  202. for (var i = 0, l = values.length; i < l; i++) {
  203. var f = this.faces[i],
  204. v = values[i];
  205. for (var j = 0, l2 = f.vertexColors.length; j < l2; j++) {
  206. if (v.vertexColors.length > 0) {
  207. f.vertexColors[j].copy(v.vertexColors[j]);
  208. } else if (v.color) {
  209. f.color.copy(v.color);
  210. }
  211. }
  212. }
  213. }
  214. }
  215. THREE.ProxyGeometry.prototype.createFaceVertexTangentProxies = function(values) {
  216. if ( this.attributes[ 'tangent' ] && this.attributes[ 'tangent' ].array ) {
  217. var tangentarray = this.attributes[ 'tangent' ].array;
  218. for (var i = 0, l = this.faces.length; i < l; i++) {
  219. var f = this.faces[i];
  220. f.vertexTangents = [
  221. new THREE.ProxyVector3(tangentarray, f.a * 3),
  222. new THREE.ProxyVector3(tangentarray, f.b * 3),
  223. new THREE.ProxyVector3(tangentarray, f.c * 3),
  224. ];
  225. }
  226. }
  227. // If values were passed in, store them in the buffer via the proxy objects
  228. if (values) {
  229. for (var i = 0, l = values.length; i < l; i++) {
  230. var f = this.faces[i],
  231. v = values[i];
  232. if (v.vertexTangents.length > 0) {
  233. for (var j = 0, l2 = f.vertexTangents.length; j < l2; j++) {
  234. f.vertexTangents[j].copy(v.vertexTangents[j]);
  235. }
  236. }
  237. }
  238. }
  239. }
  240. THREE.ProxyGeometry.prototype.createUvProxies = function(values) {
  241. // Replace the prototype getter with a local array property
  242. if (!this.hasOwnProperty("faceVertexUvs")) {
  243. Object.defineProperty( this, "faceVertexUvs", { value: [[]], writable: true } );
  244. } else {
  245. this.faceVertexUvs = [[]];
  246. }
  247. // If the attribute buffer has already been populated, set up proxy objects
  248. if ( this.attributes[ 'uv' ] && this.attributes[ 'uv' ].array ) {
  249. var faces = this.faces;
  250. var uvarray = this.attributes[ 'uv' ].array;
  251. for (var i = 0, l = faces.length; i < l; i++) {
  252. var f = faces[i];
  253. this.faceVertexUvs[0][i] = [];
  254. if ( this.attributes[ 'index' ] ) {
  255. this.faceVertexUvs[0][i][0] = new THREE.ProxyVector2(uvarray, f.a * 2);
  256. this.faceVertexUvs[0][i][1] = new THREE.ProxyVector2(uvarray, f.b * 2);
  257. this.faceVertexUvs[0][i][2] = new THREE.ProxyVector2(uvarray, f.c * 2);
  258. } else {
  259. var o = i * 6;
  260. this.faceVertexUvs[0][i][0] = new THREE.ProxyVector2(uvarray, o);
  261. this.faceVertexUvs[0][i][1] = new THREE.ProxyVector2(uvarray, o + 2);
  262. this.faceVertexUvs[0][i][2] = new THREE.ProxyVector2(uvarray, o + 4);
  263. }
  264. }
  265. }
  266. // If values were passed in, store them in the buffer via the proxy objects
  267. if (values) {
  268. for (var i = 0, l = values.length; i < l; i++) {
  269. for (var j = 0, l2 = values[i].length; j < l2; j++) {
  270. var uv = values[i][j];
  271. this.faceVertexUvs[0][i][j].copy(uv);
  272. }
  273. }
  274. }
  275. // Return a reference to the newly-created array
  276. return this.faceVertexUvs;
  277. }
  278. THREE.ProxyGeometry.prototype.createSkinIndexProxies = function(values) {
  279. // Replace the prototype getter with a local array property
  280. if (!this.hasOwnProperty('skinIndices')) {
  281. Object.defineProperty( this, "skinIndices", { value: [], writable: true } );
  282. } else {
  283. this.skinIndices = [];
  284. }
  285. // If the attribute buffer has already been populated, set up proxy objects
  286. this.populateProxyFromBuffer(this.skinIndices, "skinIndex", THREE.ProxyVector4, 4);
  287. // If values were passed in, store them in the buffer via the proxy objects
  288. if (values) {
  289. for (var i = 0; i < values.length; i++) {
  290. this.skinIndices[i].copy(values[i]);
  291. }
  292. }
  293. // Return a reference to the newly-created array
  294. return this.skinIndices;
  295. }
  296. THREE.ProxyGeometry.prototype.createSkinWeightProxies = function(values) {
  297. // Replace the prototype getter with a local array property
  298. if (!this.hasOwnProperty('skinWeights')) {
  299. Object.defineProperty( this, "skinWeights", { value: [], writable: true } );
  300. } else {
  301. this.skinWeights = [];
  302. }
  303. // If the attribute buffer has already been populated, set up proxy objects
  304. this.populateProxyFromBuffer(this.skinWeights, "skinWeight", THREE.ProxyVector4, 4);
  305. // If values were passed in, store them in the buffer via the proxy objects
  306. if (values) {
  307. for (var i = 0; i < values.length; i++) {
  308. this.skinWeights[i].copy(values[i]);
  309. }
  310. }
  311. // Return a reference to the newly-created array
  312. return this.skinWeights;
  313. }
  314. THREE.ProxyGeometry.prototype.createColorProxies = function(values) {
  315. // Replace the prototype getter with a local array property
  316. if (!this.hasOwnProperty('colors')) {
  317. Object.defineProperty( this, "colors", { value: [], writable: true } );
  318. } else {
  319. this.colors = [];
  320. }
  321. // If the attribute buffer has already been populated, set up proxy objects
  322. this.populateProxyFromBuffer(this.colors, "color", THREE.ProxyColor, 3);
  323. // If values were passed in, store them in the buffer via the proxy objects
  324. if (values) {
  325. for (var i = 0; i < values.length; i++) {
  326. this.colors[i].copy(values[i]);
  327. }
  328. }
  329. // Return a reference to the newly-created array
  330. return this.colors;
  331. }
  332. THREE.ProxyGeometry.prototype.populateProxyFromBuffer = function(attr, buffername, proxytype, itemsize, offset, count) {
  333. if ( this.attributes[ buffername ] && this.attributes[ buffername ].array ) {
  334. var array = this.attributes[ buffername ].array;
  335. var size = itemsize || this.attributes[ buffername ].itemSize;
  336. var start = offset || 0;
  337. count = count || (array.length / size - start);
  338. for ( var i = start, l = start + count; i < l; i ++ ) {
  339. attr.push( new proxytype( array, i * size ) );
  340. }
  341. }
  342. }
  343. /*
  344. * Checks for duplicate vertices with hashmap.
  345. * Duplicated vertices are removed
  346. * and faces' vertices are updated.
  347. */
  348. THREE.ProxyGeometry.prototype.mergeVertices = function () {
  349. var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
  350. var unique = [], changes = [];
  351. var v, key;
  352. var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
  353. var precision = Math.pow( 10, precisionPoints );
  354. var i,il, face;
  355. var indices, k, j, jl, u;
  356. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  357. v = this.vertices[ i ];
  358. key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
  359. if ( verticesMap[ key ] === undefined ) {
  360. verticesMap[ key ] = i;
  361. unique.push( this.vertices[ i ] );
  362. changes[ i ] = unique.length - 1;
  363. } else {
  364. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  365. changes[ i ] = changes[ verticesMap[ key ] ];
  366. }
  367. };
  368. // if faces are completely degenerate after merging vertices, we
  369. // have to remove them from the geometry.
  370. var faceIndicesToRemove = [];
  371. for( i = 0, il = this.faces.length; i < il; i ++ ) {
  372. face = this.faces[ i ];
  373. face.a = changes[ face.a ];
  374. face.b = changes[ face.b ];
  375. face.c = changes[ face.c ];
  376. indices = [ face.a, face.b, face.c ];
  377. var dupIndex = -1;
  378. // if any duplicate vertices are found in a Face3
  379. // we have to remove the face as nothing can be saved
  380. for ( var n = 0; n < 3; n ++ ) {
  381. if ( indices[ n ] == indices[ ( n + 1 ) % 3 ] ) {
  382. dupIndex = n;
  383. faceIndicesToRemove.push( i );
  384. break;
  385. }
  386. }
  387. }
  388. for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
  389. var idx = faceIndicesToRemove[ i ];
  390. this.faces.splice( idx, 1 );
  391. for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
  392. this.faceVertexUvs[ j ].splice( idx, 1 );
  393. }
  394. }
  395. // Use unique set of vertices
  396. var diff = this.vertices.length - unique.length;
  397. this.vertices = unique;
  398. return diff;
  399. }
  400. THREE.ProxyGeometry.prototype.onGeometryAllocate = function (ev) {
  401. // Prevent allocate event listener from firing multiple times
  402. this.removeEventListener( 'allocate', this.onGeometryAllocate);
  403. if (this.hasOwnProperty('vertices')) {
  404. var attr = new THREE.Float32Attribute(this.vertices.length, 3);
  405. this.addAttribute('position', attr);
  406. this.createVertexProxies(this.vertices);
  407. }
  408. if (this.hasOwnProperty('faces')) {
  409. var idxattr = new THREE.Uint16Attribute(this.faces.length, 3);
  410. this.addAttribute('index', idxattr);
  411. if (this.faces.length > 0) {
  412. var hasnormals = (this.hasOwnProperty('normals') || this.faces[0].normal || this.faces[0].vertexNormals.length > 0);
  413. var hascolors = (this.hasOwnProperty('colors') || this.faces[0].color || this.faces[0].vertexColors.length > 0);
  414. var hastangents = (this.faces[0].vertexTangents.length > 0);
  415. if (hasnormals) {
  416. var normalattr = new THREE.Float32Attribute(this.vertices.length, 3);
  417. this.addAttribute('normal', normalattr);
  418. }
  419. if (hascolors) {
  420. var colorattr = new THREE.Float32Attribute(this.faces.length * 3, 3);
  421. this.addAttribute('color', colorattr);
  422. }
  423. if (hastangents) {
  424. var tangentattr = new THREE.Float32Attribute(this.faces.length * 3, 3);
  425. this.addAttribute('tangent', tangentattr);
  426. }
  427. }
  428. this.createFaceProxies(this.faces);
  429. }
  430. if (this.hasOwnProperty('faceVertexUvs')) {
  431. var uvattr = new THREE.Float32Attribute(this.faces.length * 3, 2);
  432. this.addAttribute('uv', uvattr);
  433. this.createUvProxies(this.faceVertexUvs[0]);
  434. }
  435. if (this.hasOwnProperty('skinIndices')) {
  436. var skinidxattr = new THREE.Float32Attribute(this.skinIndices.length, 4);
  437. this.addAttribute('skinIndex', skinidxattr);
  438. this.createSkinIndexProxies(this.skinIndices);
  439. }
  440. if (this.hasOwnProperty('skinWeights')) {
  441. var skinweightattr = new THREE.Float32Attribute(this.skinWeights.length, 4);
  442. this.addAttribute('skinWeight', skinweightattr);
  443. this.createSkinWeightProxies(this.skinWeights);
  444. }
  445. }
  446. THREE.ProxyGeometry.prototype.computeFaceNormals = function() {
  447. this.dispatchEvent( { type: 'allocate' } );
  448. return THREE.BufferGeometry.prototype.computeFaceNormals.call(this);
  449. }
  450. THREE.ProxyGeometry.prototype.computeVertexNormals = function() {
  451. this.dispatchEvent( { type: 'allocate' } );
  452. return THREE.BufferGeometry.prototype.computeVertexNormals.call(this);
  453. }
  454. THREE.ProxyGeometry.prototype.computeTangents = function() {
  455. this.dispatchEvent( { type: 'allocate' } );
  456. var ret = THREE.BufferGeometry.prototype.computeTangents.call(this);
  457. // FIXME - this doesn't work yet
  458. //this.createFaceVertexTangentProxies();
  459. return ret;
  460. }
  461. THREE.ProxyGeometry.prototype.computeBoundingSphere = function() {
  462. this.dispatchEvent( { type: 'allocate' } );
  463. return THREE.BufferGeometry.prototype.computeBoundingSphere.call(this);
  464. }
  465. THREE.ProxyGeometry.prototype.computeBoundingBox = function () {
  466. this.dispatchEvent( { type: 'allocate' } );
  467. return THREE.BufferGeometry.prototype.computeBoundingBox.call(this);
  468. }
  469. THREE.ProxyGeometry.prototype.clone = function () {
  470. var buff = THREE.BufferGeometry.prototype.clone.call(this);
  471. var geo = new THREE.ProxyGeometry();
  472. geo.attributes = buff.attributes;
  473. geo.offsets = buff.offsets;
  474. return geo;
  475. }
  476. THREE.EventDispatcher.prototype.apply( THREE.ProxyGeometry.prototype );
  477. THREE.ProxyGeometryIdCount = 0;