Geometry99.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. THREE.Geometry99 = function ( ) {
  2. THREE.BufferGeometry.call( this );
  3. };
  4. THREE.Geometry99.prototype = Object.create( THREE.BufferGeometry.prototype );
  5. Object.defineProperties(THREE.Geometry99.prototype, {
  6. vertices: {
  7. enumerable: true,
  8. get: function() { return this.createVertexProxies(); }
  9. },
  10. faces: {
  11. enumerable: true,
  12. get: function() { return this.createFaceProxies() }
  13. },
  14. faceVertexUvs: {
  15. enumerable: true,
  16. get: function() { return this.createUvProxies() }
  17. },
  18. // TODO - fill in additional proxies:
  19. // - colors
  20. // - morphColors
  21. // - morphNormals
  22. // - morphTargets
  23. // - skinIndex
  24. // - skinWeights
  25. });
  26. THREE.Geometry99.prototype.createVertexProxies = function() {
  27. // Replace the prototype getter with a local array property
  28. Object.defineProperty( this, "vertices", { value: [] } );
  29. // If the attribute buffer has already been populated, set up proxy objects
  30. this.populateProxyFromBuffer(this.vertices, "position", THREE.TypedVector3, 3);
  31. // Return a reference to the newly-created array
  32. return this.vertices;
  33. }
  34. THREE.Geometry99.prototype.createFaceProxies = function() {
  35. // Replace the prototype getter with a local array property
  36. Object.defineProperty( this, "faces", { value: [] } );
  37. // If the attribute buffer has already been populated, set up proxy objects
  38. if ( this.attributes.index ) {
  39. this.populateProxyFromBuffer(this.faces, "index", THREE.TypedFace3, 3);
  40. } else {
  41. // TODO - should be able to generate Face data even for non-indexed geometries
  42. }
  43. // Return a reference to the newly-created array
  44. return this.faces;
  45. }
  46. THREE.Geometry99.prototype.createUvProxies = function() {
  47. // Replace the prototype getter with a local array property
  48. Object.defineProperty( this, "faceVertexUvs", { value: [[]] } );
  49. // If the attribute buffer has already been populated, set up proxy objects
  50. if ( this.attributes.uv ) {
  51. var faces = this.faces;
  52. var uvarray = this.attributes.uv.array;
  53. for (var i = 0, l = faces.length; i < l; i++) {
  54. var f = faces[i];
  55. this.faceVertexUvs[0][i] = [];
  56. this.faceVertexUvs[0][i][0] = new THREE.TypedVector2(uvarray, f.a * 2);
  57. this.faceVertexUvs[0][i][1] = new THREE.TypedVector2(uvarray, f.b * 2);
  58. this.faceVertexUvs[0][i][2] = new THREE.TypedVector2(uvarray, f.c * 2);
  59. }
  60. }
  61. // Return a reference to the newly-created array
  62. return this.faceVertexUvs;
  63. }
  64. THREE.Geometry99.prototype.populateProxyFromBuffer = function(attr, buffername, proxytype, itemsize, offset, count) {
  65. if ( this.attributes[ buffername ] ) {
  66. var array = this.attributes[ buffername ].array;
  67. var size = itemsize || this.attributes[ buffername ].itemSize;
  68. var start = offset || 0;
  69. var count = count || (array.length / size - start);
  70. for ( var i = start, l = start + count; i < l; i ++ ) {
  71. attr.push( new proxytype( array, i * size ) );
  72. }
  73. }
  74. }
  75. THREE.TypedFace3 = function ( array, offset ) {
  76. this.array = array;
  77. this.offset = offset;
  78. //THREE.Face3.call( this, array[offset], array[offset+1], array[offset+2] /*, normal, color, materialIndex */);
  79. }
  80. THREE.TypedFace3.prototype = Object.create( THREE.Face3.prototype );
  81. Object.defineProperties( THREE.TypedFace3.prototype, {
  82. 'a': {
  83. enumerable: true,
  84. get: function () { return this.array[ this.offset ]; },
  85. set: function ( v ) { this.array[ this.offset ] = v; }
  86. },
  87. 'b': {
  88. enumerable: true,
  89. get: function () { return this.array[ this.offset + 1 ]; },
  90. set: function ( v ) { this.array[ this.offset + 1 ] = v; }
  91. },
  92. 'c': {
  93. enumerable: true,
  94. get: function () { return this.array[ this.offset + 2 ]; },
  95. set: function ( v ) { this.array[ this.offset + 2 ] = v; }
  96. },
  97. } );