Geometry.js 16 KB

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