2
0

Geometry4.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. THREE.Geometry4 = function ( size ) {
  2. THREE.BufferGeometry.call( this );
  3. var verticesBuffer = new ArrayBuffer( size * 3 * 4 );
  4. var normalsBuffer = new ArrayBuffer( size * 3 * 4 );
  5. var uvsBuffer = new ArrayBuffer( size * 2 * 4 );
  6. this.attributes[ 'position' ] = { array: new Float32Array( verticesBuffer, 0, size * 3 ), itemSize: 3 };
  7. this.attributes[ 'normal' ] = { array: new Float32Array( normalsBuffer, 0, size * 3 ), itemSize: 3 };
  8. this.attributes[ 'uv' ] = { array: new Float32Array( uvsBuffer, 0, size * 2 ), itemSize: 2 };
  9. this.vertices = new THREE.VectorArrayProxy( this.attributes[ 'position' ] );
  10. this.normals = new THREE.VectorArrayProxy( this.attributes[ 'normal' ] );
  11. this.uvs = new THREE.VectorArrayProxy( this.attributes[ 'uv' ] );
  12. };
  13. THREE.Geometry4.prototype = Object.create( THREE.BufferGeometry.prototype );
  14. THREE.Geometry4.prototype.constructor = THREE.Geometry4;
  15. THREE.VectorArrayProxy = function(attribute) {
  16. // Acts as a proxy for an array of vectors, by setting up accessors which return THREE.Vector*Proxy objects
  17. this.attribute = attribute;
  18. for (var i = 0, l = this.attribute.array.length / this.attribute.itemSize; i < l; i ++) {
  19. Object.defineProperty(this, i, {
  20. get: (function(i) { return function() { return this.getValue(i); }})(i),
  21. set: (function(i) { return function(v) { return this.setValue(i, v); }})(i),
  22. });
  23. }
  24. }
  25. THREE.VectorArrayProxy.prototype.getValue = function(i) {
  26. // Allocates a new THREE.Vector2Proxy or THREE.Vector3Proxy depending on the itemSize of our attribute
  27. var subarray = this.attribute.array.subarray(i * this.attribute.itemSize, (i + 1) * this.attribute.itemSize);
  28. switch (this.attribute.itemSize) {
  29. case 2:
  30. return new THREE.Vector2Proxy(subarray);
  31. case 3:
  32. return new THREE.Vector3Proxy(subarray);
  33. }
  34. }
  35. THREE.VectorArrayProxy.prototype.setValue = function(i, v) {
  36. var vec = this[i];
  37. vec.copy(v);
  38. }
  39. // Vector Proxy Objects
  40. THREE.Vector2Proxy = function(subarray) {
  41. this.subarray = subarray;
  42. }
  43. THREE.Vector2Proxy.prototype = Object.create( THREE.Vector2.prototype );
  44. THREE.Vector2Proxy.prototype.constructor = THREE.Vector2Proxy;
  45. Object.defineProperty(THREE.Vector2Proxy.prototype, 'x', { get: function() { return this.subarray[0]; }, set: function(v) { this.subarray[0] = v; } });
  46. Object.defineProperty(THREE.Vector2Proxy.prototype, 'y', { get: function() { return this.subarray[1]; }, set: function(v) { this.subarray[1] = v; } });
  47. THREE.Vector3Proxy = function(subarray) {
  48. this.subarray = subarray;
  49. }
  50. THREE.Vector3Proxy.prototype = Object.create( THREE.Vector3.prototype );
  51. THREE.Vector3Proxy.prototype.constructor = THREE.Vector3Proxy;
  52. Object.defineProperty(THREE.Vector3Proxy.prototype, 'x', { get: function() { return this.subarray[0]; }, set: function(v) { this.subarray[0] = v; } });
  53. Object.defineProperty(THREE.Vector3Proxy.prototype, 'y', { get: function() { return this.subarray[1]; }, set: function(v) { this.subarray[1] = v; } });
  54. Object.defineProperty(THREE.Vector3Proxy.prototype, 'z', { get: function() { return this.subarray[2]; }, set: function(v) { this.subarray[2] = v; } });