ProxyGeometry.js 17 KB

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