提交 f7c93b82 编写于 作者: M Mr.doob

Merge remote-tracking branch 'bhouston/vector-index-accessors' into dev

......@@ -201,7 +201,7 @@ THREE.Quaternion.prototype = {
length: function () {
return Math.sqrt( this.lengthSq() );
return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
},
......
......@@ -41,6 +41,30 @@ THREE.Vector2.prototype = {
},
setComponent: function ( index, value ) {
switch( index ) {
case 0: this.x = value; break;
case 1: this.y = value; break;
default: throw new Error( "index is out of range: " + index );
}
},
getComponent: function ( index ) {
switch( index ) {
case 0: return this.x;
case 1: return this.y;
default: throw new Error( "index is out of range: " + index );
}
},
copy: function ( v ) {
this.x = v.x;
......@@ -106,7 +130,7 @@ THREE.Vector2.prototype = {
divideScalar: function ( s ) {
if ( s ) {
if ( s !== 0 ) {
this.x /= s;
this.y /= s;
......@@ -205,7 +229,7 @@ THREE.Vector2.prototype = {
length: function () {
return Math.sqrt( this.lengthSq() );
return Math.sqrt( this.x * this.x + this.y * this.y );
},
......@@ -230,7 +254,14 @@ THREE.Vector2.prototype = {
setLength: function ( l ) {
return this.normalize().multiplyScalar( l );
var oldLength = this.length();
if ( oldLength !== 0 && l !== oldLength ) {
this.multiplyScalar( l / oldLength );
}
return this;
},
......@@ -255,4 +286,4 @@ THREE.Vector2.prototype = {
}
};
};
\ No newline at end of file
......@@ -54,6 +54,32 @@ THREE.Vector3.prototype = {
},
setComponent: function ( index, value ) {
switch( index ) {
case 0: this.x = value; break;
case 1: this.y = value; break;
case 2: this.z = value; break;
default: throw new Error( "index is out of range: " + index );
}
},
getComponent: function ( index ) {
switch( index ) {
case 0: return this.x;
case 1: return this.y;
case 2: return this.z;
default: throw new Error( "index is out of range: " + index );
}
},
copy: function ( v ) {
this.x = v.x;
......@@ -156,7 +182,7 @@ THREE.Vector3.prototype = {
divideScalar: function ( s ) {
if ( s ) {
if ( s !== 0 ) {
this.x /= s;
this.y /= s;
......@@ -280,7 +306,7 @@ THREE.Vector3.prototype = {
length: function () {
return Math.sqrt( this.lengthSq() );
return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
},
......@@ -298,7 +324,14 @@ THREE.Vector3.prototype = {
setLength: function ( l ) {
return this.normalize().multiplyScalar( l );
var oldLength = this.length();
if ( oldLength !== 0 && l !== oldLength ) {
this.multiplyScalar( l / oldLength );
}
return this;
},
......@@ -571,4 +604,4 @@ THREE.Vector3.prototype = {
}
};
};
\ No newline at end of file
......@@ -62,6 +62,34 @@ THREE.Vector4.prototype = {
},
setComponent: function ( index, value ) {
switch( index ) {
case 0: this.x = value; break;
case 1: this.y = value; break;
case 2: this.z = value; break;
case 3: this.w = value; break;
default: throw new Error( "index is out of range: " + index );
}
},
getComponent: function ( index ) {
switch( index ) {
case 0: return this.x;
case 1: return this.y;
case 2: return this.z;
case 3: return this.w;
default: throw new Error( "index is out of range: " + index );
}
},
copy: function ( v ) {
this.x = v.x;
......@@ -141,7 +169,7 @@ THREE.Vector4.prototype = {
divideScalar: function ( s ) {
if ( s ) {
if ( s !== 0 ) {
this.x /= s;
this.y /= s;
......@@ -283,13 +311,13 @@ THREE.Vector4.prototype = {
lengthSq: function () {
return this.dot( this );
return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
},
length: function () {
return Math.sqrt( this.lengthSq() );
return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
},
......@@ -307,8 +335,15 @@ THREE.Vector4.prototype = {
setLength: function ( l ) {
return this.normalize().multiplyScalar( l );
var oldLength = this.length();
if ( oldLength !== 0 && l !== oldLength ) {
this.multiplyScalar( l / oldLength );
}
return this;
},
lerpSelf: function ( v, alpha ) {
......@@ -486,4 +521,4 @@ THREE.Vector4.prototype = {
}
};
};
\ No newline at end of file
......@@ -30,7 +30,7 @@
<!-- add class-based unit tests below -->
<script src="core/Float32ArrayAccessPatterns.js"></script>
<script src="core/Float32Array.js"></script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ThreeJS Benchmark Tests - Using Files in /src</title>
</head>
<body>
During this Benchmarking test the browser will be unresponsive.<br/><br/>
Benchmark output is written to the JavaScript console. To access the JavaScript console presss Ctrl-Shift-J.
<script src="benchmark-1.0.0.js"></script>
<!-- add class-based unit tests below -->
<script src="core/Vector3Components.js"></script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ThreeJS Benchmark Tests - Using Files in /src</title>
</head>
<body>
During this Benchmarking test the browser will be unresponsive.<br/><br/>
Benchmark output is written to the JavaScript console. To access the JavaScript console presss Ctrl-Shift-J.
<script src="benchmark-1.0.0.js"></script>
<!-- add class-based unit tests below -->
<script src="core/Vector3Length.js"></script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ThreeJS Benchmark Tests - Using Files in /src</title>
</head>
<body>
During this Benchmarking test the browser will be unresponsive.<br/><br/>
Benchmark output is written to the JavaScript console. To access the JavaScript console presss Ctrl-Shift-J.
<script src="benchmark-1.0.0.js"></script>
<!-- add class-based unit tests below -->
<script src="core/Vector3Storage.js"></script>
</body>
</html>
\ No newline at end of file
var input = new Float32Array( 10000 * 3 );
var output = new Float32Array( 10000 * 3 );
for( var j = 0, jl = input.length; j < jl; j ++ ) {
input[j] = j;
}
var inputVectors = [];
var outputVectors = [];
for( var j = 0, jl = input.length/3; j < jl; j ++ ) {
inputVectors.push( new THREE.Vector3( j*3, j*3+1, j*3+2 ) );
outputVectors.push( new THREE.Vector3() );
}
var suite = new Benchmark.Suite;
suite.add('Float32Array-Float32Array', function() {
var value3 = new Float32Array( 3 );
for (var i = 0, il = input.length / 3; i < il; i += 3) {
value3[0] = input[i + 0];
value3[1] = input[i + 1];
value3[2] = input[i + 2];
value3[0] *= 1.01;
value3[1] *= 1.03;
value3[2] *= 0.98;
output[i + 0] = value3[0];
output[i + 1] = value3[1];
output[i + 2] = value3[2];
}
});
suite.add('Float32Array-Array', function() {
var value2 = [0,0,0];
for (var i = 0, il = input.length / 3; i < il; i += 3) {
value2[0] = input[i + 0];
value2[1] = input[i + 1];
value2[2] = input[i + 2];
value2[0] *= 1.01;
value2[1] *= 1.03;
value2[2] *= 0.98;
output[i + 0] = value2[0];
output[i + 1] = value2[1];
output[i + 2] = value2[2];
}
});
suite.add('Float32Array-Literal', function() {
var x, y, z;
for (var i = 0, il = input.length / 3; i < il; i += 3) {
x = input[i + 0];
y = input[i + 1];
z = input[i + 2];
x *= 1.01;
y *= 1.03;
z *= 0.98;
output[i + 0] = x;
output[i + 1] = y;
output[i + 2] = z;
}
});
suite.add('Float32Array-Vector3', function() {
var value = new THREE.Vector3();
for (var i = 0, il = input.length / 3; i < il; i += 3) {
value.x = input[i + 0];
value.y = input[i + 1];
value.z = input[i + 2];
value.x *= 1.01;
value.y *= 1.03;
value.z *= 0.98;
output[i + 0] = value.x;
output[i + 1] = value.y;
output[i + 2] = value.z;
}
});
suite.add('Vector3Array-Direct', function() {
for (var i = 0, il = inputVectors.length; i < il; i ++ ) {
outputVectors[i].copy( inputVectors[i] );
outputVectors[i].x *= 1.01;
outputVectors[i].y *= 1.03;
outputVectors[i].z *= 0.98;
}
});
suite.add('Vector3Array-Vector3', function() {
var value = new THREE.Vector3();
for (var i = 0, il = inputVectors.length; i < il; i ++ ) {
value.copy( inputVectors[i] );
value.x *= 1.01;
value.y *= 1.03;
value.z *= 0.98;
outputVectors[i].copy( value );
}
});
suite.on('cycle', function(event, bench) {
console.log(String(event.target));
});
suite.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
console.log( "Done" );
});
suite.run(true);
\ No newline at end of file
var array = new Float32Array( 10000 * 3 );
for( var j = 0, jl = array.length; j < jl; j ++ ) {
array[j] = j;
}
var vectorArray = [];
for( var j = 0, jl = array.length/3; j < jl; j ++ ) {
vectorArray.push( new THREE.Vector3( j*3, j*3+1, j*3+2 ) );
}
var Float32ArrayCopyTest = function( array ) {
var x, y, z;
for (var i = 0, il = array.length / 3; i < il; i += 3) {
x = array[i + 0];
y = array[i + 1];
z = array[i + 2];
x *= 1.01;
y *= 1.03;
z *= 0.98;
array[i + 0] = x;
array[i + 1] = y;
array[i + 2] = z;
}
};
var Float32ArrayDirectTest = function( array ) {
for (var i = 0, il = array.length / 3; i < il; i += 3) {
array[i + 0] *= 1.01;
array[i + 1] *= 1.03;
array[i + 2] *= 0.98;
}
};
var Float32ArrayVector3CopyTest = function( array ) {
var value = new THREE.Vector3();
for (var i = 0, il = array.length / 3; i < il; i += 3) {
value.x = array[i + 0];
value.y = array[i + 1];
value.z = array[i + 2];
value.x *= 1.01;
value.y *= 1.03;
value.z *= 0.98;
array[i + 0] = value.x;
array[i + 1] = value.y;
array[i + 2] = value.z;
}
};
var Float32ArrayArrayCopyTest = function( array ) {
var value2 = [0,0,0];
for (var i = 0, il = array.length / 3; i < il; i += 3) {
value2[0] = array[i + 0];
value2[1] = array[i + 1];
value2[2] = array[i + 2];
value2[0] *= 1.01;
value2[1] *= 1.03;
value2[2] *= 0.98;
array[i + 0] = value2[0];
array[i + 1] = value2[1];
array[i + 2] = value2[2];
}
};
var Float32ArrayFloat32ArrayCopyTest = function( array ) {
var value3 = new Float32Array( 3 );
for (var i = 0, il = array.length / 3; i < il; i += 3) {
value3[0] = array[i + 0];
value3[1] = array[i + 1];
value3[2] = array[i + 2];
value3[0] *= 1.01;
value3[1] *= 1.03;
value3[2] *= 0.98;
array[i + 0] = value3[0];
array[i + 1] = value3[1];
array[i + 2] = value3[2];
}
};
var Vector3ArrayVector3CopyTest = function( array ) {
var value = new THREE.Vector3();
for (var i = 0, il = vectorArray.length; i < il; i ++ ) {
value.copy( vectorArray[i] );
value.x *= 1.01;
value.y *= 1.03;
value.z *= 0.98;
vectorArray[i].copy( value );
}
};
var Vector3ArrayVector3RefTest = function( array ) {
for (var i = 0, il = vectorArray.length; i < il; i ++ ) {
var value = vectorArray[i];
value.x *= 1.01;
value.y *= 1.03;
value.z *= 0.98;
}
};
var Vector3ArrayVector3DirectTest = function( array ) {
for (var i = 0, il = vectorArray.length; i < il; i ++ ) {
vectorArray[i].x *= 1.01;
vectorArray[i].y *= 1.03;
vectorArray[i].z *= 0.98;
}
};
var suite = new Benchmark.Suite;
suite.add('Float32ArrayFloat32ArrayCopyTest', function() {
Float32ArrayFloat32ArrayCopyTest( array );
});
suite.add('Float32DirectArray', function() {
Float32ArrayDirectTest( array );
});
suite.add('Float32ArrayArrayCopyTest', function() {
Float32ArrayArrayCopyTest( array );
});
suite.add('Float32CopyArray', function() {
Float32ArrayCopyTest( array );
});
suite.add('Float32ArrayVector3CopyTest', function() {
Float32ArrayVector3CopyTest( array );
});
suite.add('Vector3ArrayVector3Ref', function() {
Vector3ArrayVector3RefTest( array );
});
suite.add('Vector3ArrayVector3Direct', function() {
Vector3ArrayVector3DirectTest( array );
});
suite.add('Vector3ArrayVector3Copy', function() {
Vector3ArrayVector3CopyTest( array );
});
suite.on('cycle', function(event, bench) {
console.log(String(event.target));
});
suite.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
console.log( "Done" );
});
suite.run(true);
\ No newline at end of file
THREE = {};
THREE.Vector3 = function ( x, y, z ) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
};
THREE.Vector3.prototype = {
constructor: THREE.Vector3,
setComponent: function ( index, value ) {
this[ THREE.Vector3.__indexToName[ index ] ] = value;
},
getComponent: function ( index ) {
return this[ THREE.Vector3.__indexToName[ index ] ];
},
setComponent2: function ( index, value ) {
switch( index ) {
case 0: this.x = value; break;
case 1: this.y = value; break;
case 2: this.z = value; break;
default: throw new Error( "index is out of range: " + index );
}
},
getComponent2: function ( index ) {
switch( index ) {
case 0: return this.x;
case 1: return this.y;
case 2: return this.z;
default: throw new Error( "index is out of range: " + index );
}
},
getComponent3: function ( index ) {
if ( index === 0 ) return this.x;
if ( index === 1 ) return this.y;
if ( index === 2 ) return this.z;
throw new Error( "index is out of range: " + index );
},
getComponent4: function ( index ) {
if ( index === 0 ) return this.x;
else if ( index === 1 ) return this.y;
else if ( index === 2 ) return this.z;
else throw new Error( "index is out of range: " + index );
}
};
THREE.Vector3.__indexToName = {
0: 'x',
1: 'y',
2: 'z'
};
var a = [];
for ( var i = 0; i < 100000; i ++ ) {
a[ i ] = new THREE.Vector3( i * 0.01, i * 2, i * -1.3 );
}
var suite = new Benchmark.Suite;
suite.add('IndexToName', function() {
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
result += a[i].getComponent( i % 3 );
}
});
suite.add('SwitchStatement', function() {
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
result += a[i].getComponent2( i % 3 );
}
});
suite.add('IfAndReturnSeries', function() {
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
result += a[i].getComponent3( i % 3 );
}
});
suite.add('IfReturnElseSeries', function() {
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
result += a[i].getComponent4( i % 3 );
}
});
suite.on('cycle', function(event, bench) {
console.log(String(event.target));
});
suite.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
console.log( "Done" );
});
suite.run(true);
\ No newline at end of file
THREE = {};
THREE.Vector3 = function ( x, y, z ) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
};
THREE.Vector3.prototype = {
constructor: THREE.Vector3,
lengthSq: function () {
return this.x * this.x + this.y * this.y + this.z * this.z;
},
length: function () {
return Math.sqrt( this.lengthSq() );
},
length2: function () {
return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
}
};
var a = [];
for ( var i = 0; i < 100000; i ++ ) {
a[ i ] = new THREE.Vector3( i * 0.01, i * 2, i * -1.3 );
}
var suite = new Benchmark.Suite;
suite.add('NoCallTest', function() {
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
var v = a[i];
result += Math.sqrt( v.x * v.x + v.y * v.y + v.z * v.z );
}
});
suite.add('InlineCallTest', function() {
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
result += a[ i ].length2();
}
});
suite.add('FunctionCallTest', function() {
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
result += a[ i ].length();
}
});
suite.on('cycle', function(event, bench) {
console.log(String(event.target));
});
suite.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
console.log( "Done" );
});
suite.run(true);
\ No newline at end of file
THREE = {};
THREE.Vector3 = function ( x, y, z ) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
};
THREE.Vector3.prototype = {
constructor: THREE.Vector3,
length: function () {
return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
}
};
THREE.Vector3X = function ( x, y, z ) {
var elements = this.elements = new Float32Array( 3 );
elements[0] = x || 0;
elements[1] = y || 1;
elements[2] = z || 2;
};
THREE.Vector3X.prototype = {
constructor: THREE.Vector3X,
length: function () {
return Math.sqrt( this.elements[0] * this.elements[0] + this.elements[1] * this.elements[1] + this.elements[2] * this.elements[2] );
}
};
THREE.Vector3Y = function ( x, y, z ) {
this.elements = [ x || 0, y || 1, z || 2 ];
};
THREE.Vector3Y.prototype = {
constructor: THREE.Vector3Y,
length: function () {
return Math.sqrt( this.elements[0] * this.elements[0] + this.elements[1] * this.elements[1] + this.elements[2] * this.elements[2] );
}
};
var suite = new Benchmark.Suite;
suite.add('Vector3-Set', function() {
var array = [];
for ( var i = 0; i < 100000; i ++ ) {
var v = new THREE.Vector3( i, i, i );
array.push( v );
}
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
var v = array[i];
result += v.length();
}
});
suite.add('Vector3-Float32Array', function() {
var array = [];
for ( var i = 0; i < 100000; i ++ ) {
var v = new THREE.Vector3X( i, i, i );
array.push( v );
}
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
var v = array[i];
result += v.length();
}
});
suite.add('Vector3-Array', function() {
var array = [];
for ( var i = 0; i < 100000; i ++ ) {
var v = new THREE.Vector3Y( i, i, i );
array.push( v );
}
var result = 0;
for ( var i = 0; i < 100000; i ++ ) {
var v = array[i];
result += v.length();
}
});
suite.on('cycle', function(event, bench) {
console.log(String(event.target));
});
suite.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
console.log( "Done" );
});
suite.run(true);
\ No newline at end of file
......@@ -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 );
......@@ -184,6 +195,11 @@ test( "setLength", function() {
ok( a.length() == x, "Passed!" );
a.setLength( y );
ok( a.length() == y, "Passed!" );
a = new THREE.Vector2( 0, 0 );
ok( a.length() == 0, "Passed!" );
a.setLength( y );
ok( a.length() == 0, "Passed!" );
});
test( "lerpSelf/clone", function() {
......
......@@ -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 );
......@@ -217,6 +231,12 @@ test( "setLength", function() {
ok( a.length() == x, "Passed!" );
a.setLength( y );
ok( a.length() == y, "Passed!" );
a = new THREE.Vector3( 0, 0, 0 );
ok( a.length() == 0, "Passed!" );
a.setLength( y );
ok( a.length() == 0, "Passed!" );
});
test( "lerpSelf/clone", function() {
......
......@@ -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 );
......@@ -254,6 +271,11 @@ test( "setLength", function() {
ok( a.length() == x, "Passed!" );
a.setLength( y );
ok( a.length() == y, "Passed!" );
a = new THREE.Vector4( 0, 0, 0, 0 );
ok( a.length() == 0, "Passed!" );
a.setLength( y );
ok( a.length() == 0, "Passed!" );
});
test( "lerpSelf/clone", function() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册