浏览代码

Merge branch 'vector-index-accessors' into dev

Ben Houston 12 年之前
父节点
当前提交
a1ac92dbb2
共有 6 个文件被更改,包括 96 次插入0 次删除
  1. 17 0
      src/math/Vector2.js
  2. 18 0
      src/math/Vector3.js
  3. 19 0
      src/math/Vector4.js
  4. 11 0
      test/unit/math/Vector2.js
  5. 14 0
      test/unit/math/Vector3.js
  6. 17 0
      test/unit/math/Vector4.js

+ 17 - 0
src/math/Vector2.js

@@ -41,6 +41,18 @@ THREE.Vector2.prototype = {
 
 	},
 
+	setComponent: function ( index, value ) {
+
+		this[ THREE.Vector2.__indexToName[ index ] ] = value;
+
+	},
+
+	getComponent: function ( index ) {
+
+		return this[ THREE.Vector2.__indexToName[ index ] ];
+		
+	},
+
 	copy: function ( v ) {
 
 		this.x = v.x;
@@ -263,3 +275,8 @@ THREE.Vector2.prototype = {
 	}
 
 };
+
+THREE.Vector2.__indexToName = {
+  0: 'x',
+  1: 'y'
+};

+ 18 - 0
src/math/Vector3.js

@@ -54,6 +54,18 @@ THREE.Vector3.prototype = {
 
 	},
 
+	setComponent: function ( index, value ) {
+
+		this[ THREE.Vector3.__indexToName[ index ] ] = value;
+		
+	},
+
+	getComponent: function ( index ) {
+
+		return this[ THREE.Vector3.__indexToName[ index ] ];
+
+	},
+
 	copy: function ( v ) {
 
 		this.x = v.x;
@@ -579,3 +591,9 @@ THREE.Vector3.prototype = {
 	}
 
 };
+
+THREE.Vector3.__indexToName = {
+  0: 'x',
+  1: 'y',
+  2: 'z'
+};

+ 19 - 0
src/math/Vector4.js

@@ -62,6 +62,18 @@ THREE.Vector4.prototype = {
 
 	},
 
+	setComponent: function ( index, value ) {
+
+		this[ THREE.Vector4.__indexToName[ index ] ] = value;
+
+	},
+
+	getComponent: function ( index ) {
+
+		return this[ THREE.Vector4.__indexToName[ index ] ];
+		
+	},
+
 	copy: function ( v ) {
 
 		this.x = v.x;
@@ -494,3 +506,10 @@ THREE.Vector4.prototype = {
 	}
 
 };
+
+THREE.Vector4.__indexToName = {
+  0: 'x',
+  1: 'y',
+  2: 'z',
+  3: 'w'
+};

+ 11 - 0
test/unit/math/Vector2.js

@@ -48,6 +48,17 @@ test( "setX,setY", function() {
 	ok( a.y == y, "Passed!" );
 });
 
+test( "setComponent,getComponent", function() {
+	var a = new THREE.Vector2();
+	ok( a.x == 0, "Passed!" );
+	ok( a.y == 0, "Passed!" );
+
+	a.setComponent( 0, 1 );
+	a.setComponent( 1, 2 );
+	ok( a.getComponent( 0 ) == 1, "Passed!" );
+	ok( a.getComponent( 1 ) == 2, "Passed!" );
+});
+
 test( "add", function() {
 	var a = new THREE.Vector2( x, y );
 	var b = new THREE.Vector2( -x, -y );

+ 14 - 0
test/unit/math/Vector3.js

@@ -59,6 +59,20 @@ test( "setX,setY,setZ", function() {
 	ok( a.z == z, "Passed!" );
 });
 
+test( "setComponent,getComponent", function() {
+	var a = new THREE.Vector3();
+	ok( a.x == 0, "Passed!" );
+	ok( a.y == 0, "Passed!" );
+	ok( a.z == 0, "Passed!" );
+
+	a.setComponent( 0, 1 );
+	a.setComponent( 1, 2 );
+	a.setComponent( 2, 3 );
+	ok( a.getComponent( 0 ) == 1, "Passed!" );
+	ok( a.getComponent( 1 ) == 2, "Passed!" );
+	ok( a.getComponent( 2 ) == 3, "Passed!" );
+});
+
 test( "add", function() {
 	var a = new THREE.Vector3( x, y, z );
 	var b = new THREE.Vector3( -x, -y, -z );

+ 17 - 0
test/unit/math/Vector4.js

@@ -69,6 +69,23 @@ test( "setX,setY,setZ,setW", function() {
 	ok( a.w == w, "Passed!" );
 });
 
+test( "setComponent,getComponent", function() {
+	var a = new THREE.Vector4();
+	ok( a.x == 0, "Passed!" );
+	ok( a.y == 0, "Passed!" );
+	ok( a.z == 0, "Passed!" );
+	ok( a.w == 1, "Passed!" );
+
+	a.setComponent( 0, 1 );
+	a.setComponent( 1, 2 );
+	a.setComponent( 2, 3 );
+	a.setComponent( 3, 4 );
+	ok( a.getComponent( 0 ) == 1, "Passed!" );
+	ok( a.getComponent( 1 ) == 2, "Passed!" );
+	ok( a.getComponent( 2 ) == 3, "Passed!" );
+	ok( a.getComponent( 3 ) == 4, "Passed!" );
+});
+
 test( "add", function() {
 	var a = new THREE.Vector4( x, y, z, w );
 	var b = new THREE.Vector4( -x, -y, -z, -w );