2
0

Geometry4.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.VectorArrayProxy = function(attribute) {
  15. // Acts as a proxy for an array of vectors, by setting up accessors which return THREE.Vector*Proxy objects
  16. this.attribute = attribute;
  17. for (var i = 0, l = this.attribute.array.length / this.attribute.itemSize; i < l; i++) {
  18. Object.defineProperty(this, i, {
  19. get: (function(i) { return function() { return this.getValue(i); }})(i),
  20. set: (function(i) { return function(v) { return this.setValue(i, v); }})(i),
  21. });
  22. }
  23. }
  24. THREE.VectorArrayProxy.prototype.getValue = function(i) {
  25. // Allocates a new THREE.Vector2Proxy or THREE.Vector3Proxy depending on the itemSize of our attribute
  26. var subarray = this.attribute.array.subarray(i * this.attribute.itemSize, (i + 1) * this.attribute.itemSize);
  27. switch (this.attribute.itemSize) {
  28. case 2:
  29. return new THREE.Vector2Proxy(subarray);
  30. case 3:
  31. return new THREE.Vector3Proxy(subarray);
  32. }
  33. }
  34. THREE.VectorArrayProxy.prototype.setValue = function(i, v) {
  35. var vec = this[i];
  36. vec.copy(v);
  37. }
  38. // Vector Proxy Objects
  39. THREE.Vector2Proxy = function(subarray) {
  40. this.subarray = subarray;
  41. }
  42. THREE.Vector2Proxy.prototype = Object.create( THREE.Vector2.prototype );
  43. Object.defineProperty(THREE.Vector2Proxy.prototype, 'x', { get: function() { return this.subarray[0]; }, set: function(v) { this.subarray[0] = v; } });
  44. Object.defineProperty(THREE.Vector2Proxy.prototype, 'y', { get: function() { return this.subarray[1]; }, set: function(v) { this.subarray[1] = v; } });
  45. THREE.Vector3Proxy = function(subarray) {
  46. this.subarray = subarray;
  47. }
  48. THREE.Vector3Proxy.prototype = Object.create( THREE.Vector3.prototype );
  49. Object.defineProperty(THREE.Vector3Proxy.prototype, 'x', { get: function() { return this.subarray[0]; }, set: function(v) { this.subarray[0] = v; } });
  50. Object.defineProperty(THREE.Vector3Proxy.prototype, 'y', { get: function() { return this.subarray[1]; }, set: function(v) { this.subarray[1] = v; } });
  51. Object.defineProperty(THREE.Vector3Proxy.prototype, 'z', { get: function() { return this.subarray[2]; }, set: function(v) { this.subarray[2] = v; } });