提交 a861d97a 编写于 作者: M Mikael Emtinger

Merged with Alterq.

因为 它太大了无法显示 source diff 。你可以改为 查看blob
此差异已折叠。
......@@ -99,7 +99,7 @@
for ( i = 0; i < points.length * n_sub; i ++ ) {
index = i / ( points.length * n_sub );
position = spline.get2DPoint( points, index );
position = spline.getPoint( points, index );
geometry.vertices[ i ] = new THREE.Vertex( new THREE.Vector3( position.x, position.y, position.z ) );
......
/**
* @author mikael emtinger / http://gomo.se/
* @author mrdoob / http://mrdoob.com/
* @author alteredq / http://alteredqualia.com/
*/
THREE.Animation = function( root, data, interpolationType, JITCompile ) {
......@@ -14,11 +16,13 @@ THREE.Animation = function( root, data, interpolationType, JITCompile ) {
this.loop = true;
this.interpolationType = interpolationType !== undefined ? interpolationType : THREE.AnimationHandler.LINEAR;
this.JITCompile = JITCompile !== undefined ? JITCompile : true;
}
/*
* Play
*/
this.points = [];
this.target = new THREE.Vector3();
};
// Play
THREE.Animation.prototype.play = function( loop, startTimeMS ) {
......@@ -31,22 +35,32 @@ THREE.Animation.prototype.play = function( loop, startTimeMS ) {
// reset key cache
for ( var h = 0; h < this.hierarchy.length; h++ ) {
var h, hl = this.hierarchy.length,
object;
for ( h = 0; h < hl; h++ ) {
this.hierarchy[ h ].useQuaternion = true;
this.hierarchy[ h ].matrixAutoUpdate = true;
object = this.hierarchy[ h ];
if ( this.interpolationType !== THREE.AnimationHandler.CATMULLROM_FORWARD ) {
if ( this.hierarchy[ h ].animationCache === undefined ) {
object.useQuaternion = true;
this.hierarchy[ h ].animationCache = {};
this.hierarchy[ h ].animationCache.prevKey = { pos: 0, rot: 0, scl: 0 };
this.hierarchy[ h ].animationCache.nextKey = { pos: 0, rot: 0, scl: 0 };
this.hierarchy[ h ].animationCache.originalMatrix = this.hierarchy[ h ] instanceof THREE.Bone ? this.hierarchy[ h ].skinMatrix : this.hierarchy[ h ].matrix;
}
object.matrixAutoUpdate = true;
if ( object.animationCache === undefined ) {
var prevKey = this.hierarchy[ h ].animationCache.prevKey;
var nextKey = this.hierarchy[ h ].animationCache.nextKey;
object.animationCache = {};
object.animationCache.prevKey = { pos: 0, rot: 0, scl: 0 };
object.animationCache.nextKey = { pos: 0, rot: 0, scl: 0 };
object.animationCache.originalMatrix = object instanceof THREE.Bone ? object.skinMatrix : object.matrix;
}
var prevKey = object.animationCache.prevKey;
var nextKey = object.animationCache.nextKey;
prevKey.pos = this.data.hierarchy[ h ].keys[ 0 ];
prevKey.rot = this.data.hierarchy[ h ].keys[ 0 ];
......@@ -55,20 +69,22 @@ THREE.Animation.prototype.play = function( loop, startTimeMS ) {
nextKey.pos = this.getNextKeyWith( "pos", h, 1 );
nextKey.rot = this.getNextKeyWith( "rot", h, 1 );
nextKey.scl = this.getNextKeyWith( "scl", h, 1 );
}
this.update( 0 );
}
this.isPaused = false;
THREE.AnimationHandler.addToUpdate( this );
};
/*
* Pause
*/
// Pause
THREE.Animation.prototype.pause = function() {
......@@ -83,12 +99,11 @@ THREE.Animation.prototype.pause = function() {
}
this.isPaused = !this.isPaused;
}
};
/*
* Stop
*/
// Stop
THREE.Animation.prototype.stop = function() {
......@@ -110,19 +125,20 @@ THREE.Animation.prototype.stop = function() {
} else {
this.hierarchy[ h ].matrix = this.hierarchy[ h ].animationCache.originalMatrix;
}
delete this.hierarchy[ h ].animationCache;
delete this.hierarchy[ h ].animationCache;
}
}
}
};
/*
* Update
*/
// Update
THREE.Animation.prototype.update = function( deltaTimeMS ) {
......@@ -144,6 +160,7 @@ THREE.Animation.prototype.update = function( deltaTimeMS ) {
var frame;
var JIThierarchy = this.data.JIT.hierarchy;
var currentTime, unloopedCurrentTime;
var currentPoint, forwardPoint, angle;
// update
......@@ -172,13 +189,14 @@ THREE.Animation.prototype.update = function( deltaTimeMS ) {
object.matrixAutoUpdate = false;
object.matrixWorldNeedsUpdate = false;
}
else {
} else {
object.matrix = JIThierarchy[ h ][ frame ];
object.matrixAutoUpdate = false;
object.matrixWorldNeedsUpdate = true;
}
// use interpolation
......@@ -277,39 +295,54 @@ THREE.Animation.prototype.update = function( deltaTimeMS ) {
if ( type === "pos" ) {
vector = object.position;
if( this.interpolationType === THREE.AnimationHandler.LINEAR ) {
vector = object.position;
vector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
vector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
} else {
var points = [];
} else if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
points[ 0 ] = this.getPrevKeyWith( type, h, prevKey.index - 1 )[ type ];
points[ 1 ] = prevXYZ;
points[ 2 ] = nextXYZ;
points[ 3 ] = this.getNextKeyWith( type, h, nextKey.index + 1 )[ type ];
this.points[ 0 ] = this.getPrevKeyWith( "pos", h, prevKey.index - 1 )[ "pos" ];
this.points[ 1 ] = prevXYZ;
this.points[ 2 ] = nextXYZ;
this.points[ 3 ] = this.getNextKeyWith( "pos", h, nextKey.index + 1 )[ "pos" ];
scale = scale * 0.33 + 0.33;
var result = this.interpolateCatmullRom( points, scale );
currentPoint = this.interpolateCatmullRom( this.points, scale );
object.position.x = result[ 0 ];
object.position.y = result[ 1 ];
object.position.z = result[ 2 ];
vector.x = currentPoint[ 0 ];
vector.y = currentPoint[ 1 ];
vector.z = currentPoint[ 2 ];
if( this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
forwardPoint = this.interpolateCatmullRom( this.points, scale * 1.01 );
this.target.set( forwardPoint[ 0 ], forwardPoint[ 1 ], forwardPoint[ 2 ] );
this.target.subSelf( vector );
this.target.y = 0;
this.target.normalize();
angle = Math.atan2( this.target.x, this.target.z );
object.rotation.set( 0, angle, 0 );
}
}
}
else if ( type === "rot" ) {
} else if ( type === "rot" ) {
THREE.Quaternion.slerp( prevXYZ, nextXYZ, object.quaternion, scale );
}
else if( type === "scl" ) {
} else if( type === "scl" ) {
vector = object.scale;
vector = object.scale;
vector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
vector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
......@@ -350,16 +383,7 @@ THREE.Animation.prototype.update = function( deltaTimeMS ) {
};
/**
* Spline from Tween.js, slightly optimized
* Modified to fit to THREE.Animation.js
*
* http://sole.github.com/tween.js/examples/05_spline.html
*
* @author mrdoob / http://mrdoob.com/
*/
// Catmull-Rom spline
THREE.Animation.prototype.interpolateCatmullRom = function ( points, scale ) {
......@@ -389,8 +413,8 @@ THREE.Animation.prototype.interpolateCatmullRom = function ( points, scale ) {
v3[ 2 ] = this.interpolate( pa[ 2 ], pb[ 2 ], pc[ 2 ], pd[ 2 ], weight, w2, w3 );
return v3;
}
};
THREE.Animation.prototype.interpolate = function( p0, p1, p2, p3, t, t2, t3 ) {
......@@ -398,17 +422,27 @@ THREE.Animation.prototype.interpolate = function( p0, p1, p2, p3, t, t2, t3 ) {
v1 = ( p3 - p1 ) * 0.5;
return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
}
};
/*
* Get next key with
*/
// Get next key with
THREE.Animation.prototype.getNextKeyWith = function( type, h, key ) {
var keys = this.data.hierarchy[ h ].keys;
key = key % keys.length;
if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
key = key < keys.length - 1 ? key : keys.length - 1;
} else {
key = key % keys.length;
}
for ( ; key < keys.length; key++ ) {
......@@ -422,12 +456,25 @@ THREE.Animation.prototype.getNextKeyWith = function( type, h, key ) {
return this.data.hierarchy[ h ].keys[ 0 ];
}
};
// Get previous key with
THREE.Animation.prototype.getPrevKeyWith = function( type, h, key ) {
var keys = this.data.hierarchy[ h ].keys;
key = key >= 0 ? key : key + keys.length;
if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
key = key > 0 ? key : 0;
} else {
key = key >= 0 ? key : key + keys.length;
}
for ( ; key >= 0; key-- ) {
......@@ -440,4 +487,5 @@ THREE.Animation.prototype.getPrevKeyWith = function( type, h, key ) {
}
return this.data.hierarchy[ h ].keys[ keys.length - 1 ];
}
};
......@@ -50,6 +50,7 @@ THREE.AnimationHandler = (function() {
library[ data.name ] = data;
initData( data );
};
......@@ -66,12 +67,14 @@ THREE.AnimationHandler = (function() {
} else {
console.log( "THREE.AnimationHandler.get: Couldn't find animation " + name );
return null;
return null;
}
}
else {
} else {
// todo: add simple tween library
}
};
......@@ -99,6 +102,7 @@ THREE.AnimationHandler = (function() {
}
return hierarchy;
};
var parseRecurseHierarchy = function( root, hierarchy ) {
......@@ -107,6 +111,7 @@ THREE.AnimationHandler = (function() {
for( var c = 0; c < root.children.length; c++ )
parseRecurseHierarchy( root.children[ c ], hierarchy );
}
......@@ -215,6 +220,7 @@ THREE.AnimationHandler = (function() {
data.hierarchy[ h ].keys[ k ].index = k;
}
}
......@@ -236,7 +242,7 @@ THREE.AnimationHandler = (function() {
};
// interpolation typess
// interpolation types
that.LINEAR = 0;
that.CATMULLROM = 1;
......
/**
* @author alteredq / http://alteredqualia.com/
*
* parameters = {
* fov: <float>,
* aspect: <float>,
* near: <float>,
* far: <float>,
* target: <THREE.Object3D>,
* waypoints: <Array>, // [ [x,y,z], [x,y,z] ... ]
* duration: <float>, // seconds
* createDebugPath: <bool>,
* createDebugDummy: <bool>,
* lookSpeed: <float>,
* lookVertical: <bool>,
* lookHorizontal: <bool>,
* verticalAngleMap: { srcRange: [ <float>, <float> ], dstRange: [ <float>, <float> ] }
* horizontalAngleMap: { srcRange: [ <float>, <float> ], dstRange: [ <float>, <float> ] }
* domElement: <HTMLElement>,
* }
*/
THREE.PathCamera = function ( parameters ) {
THREE.Camera.call( this, parameters.fov, parameters.aspect, parameters.near, parameters.far, parameters.target );
this.duration = 10 * 1000; // milliseconds
this.waypoints = [];
this.debugPath = new THREE.Object3D();
this.debugDummy = new THREE.Object3D();
this.animationParent = new THREE.Object3D();
this.lookSpeed = 0.005;
this.lookVertical = true;
this.lookHorizontal = true;
this.verticalAngleMap = { srcRange: [ 0, 6.28 ], dstRange: [ 0, 6.28 ] };
this.horizontalAngleMap = { srcRange: [ 0, 6.28 ], dstRange: [ 0, 6.28 ] };
this.domElement = document;
if ( parameters ) {
if ( parameters.duration !== undefined ) this.duration = parameters.duration * 1000;
if ( parameters.waypoints !== undefined ) this.waypoints = parameters.waypoints;
if ( parameters.createDebugPath !== undefined ) this.createDebugPath = parameters.createDebugPath;
if ( parameters.createDebugDummy !== undefined ) this.createDebugDummy = parameters.createDebugDummy;
if ( parameters.lookSpeed !== undefined ) this.lookSpeed = parameters.lookSpeed;
if ( parameters.lookVertical !== undefined ) this.lookVertical = parameters.lookVertical;
if ( parameters.lookHorizontal !== undefined ) this.lookHorizontal = parameters.lookHorizontal;
if ( parameters.verticalAngleMap !== undefined ) this.verticalAngleMap = parameters.verticalAngleMap;
if ( parameters.horizontalAngleMap !== undefined ) this.horizontalAngleMap = parameters.horizontalAngleMap;
if ( parameters.domElement !== undefined ) this.domElement = parameters.domElement;
}
this.mouseX = 0;
this.mouseY = 0;
this.lat = 0;
this.lon = 0;
this.phi = 0;
this.theta = 0;
this.windowHalfX = window.innerWidth / 2;
this.windowHalfY = window.innerHeight / 2;
var PI2 = Math.PI * 2,
PI180 = Math.PI / 180;
// methods
this.updateManual = function () {
var srcRange, dstRange;
if( this.lookHorizontal ) this.lon += this.mouseX * this.lookSpeed;
if( this.lookVertical ) this.lat -= this.mouseY * this.lookSpeed;
this.lon = Math.max( 0, Math.min( 360, this.lon ) );
this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
this.phi = ( 90 - this.lat ) * PI180;
this.theta = this.lon * PI180;
this.phi = normalize_angle_rad( this.phi );
// constrain vertical look angle
srcRange = this.verticalAngleMap.srcRange;
dstRange = this.verticalAngleMap.dstRange;
this.phi = map_linear( this.phi, srcRange[ 0 ], srcRange[ 1 ],
dstRange[ 0 ], dstRange[ 1 ] );
// constrain horizontal look angle
srcRange = this.horizontalAngleMap.srcRange;
dstRange = this.horizontalAngleMap.dstRange;
this.theta = map_linear( this.theta, srcRange[ 0 ], srcRange[ 1 ],
dstRange[ 0 ], dstRange[ 1 ] );
var targetPosition = this.target.position,
position = this.position;
targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
targetPosition.y = position.y + 100 * Math.cos( this.phi );
targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
this.supr.update.call( this );
};
this.onMouseMove = function ( event ) {
this.mouseX = event.clientX - this.windowHalfX;
this.mouseY = event.clientY - this.windowHalfY;
};
// utils
function normalize_angle_rad( a ) {
var b = a % PI2;
return b >= 0 ? b : b + PI2;
};
function cap( x, a, b ) {
return ( x < a ) ? a : ( ( x > b ) ? b : x );
};
function map_linear( x, sa, sb, ea, eb ) {
return ( x - sa ) * ( eb - ea ) / ( sb - sa ) + ea;
};
function distance( a, b ) {
var dx = a[ 0 ] - b[ 0 ],
dy = a[ 1 ] - b[ 1 ],
dz = a[ 2 ] - b[ 2 ];
return Math.sqrt( dx * dx + dy * dy + dz * dz );
};
function bind( scope, fn ) {
return function () {
fn.apply( scope, arguments );
};
};
function initAnimationPath( parent, path, name, duration ) {
var animationData = {
name: name,
fps: 0.6,
length: duration,
hierarchy: []
};
var i,
parentAnimation, childAnimation,
pl = path.length,
totalLength = splineLength( path ),
t = 0,
first = 0,
last = pl - 1;
parentAnimation = { parent: -1, keys: [] };
parentAnimation.keys[ first ] = { time: 0, pos: path[ first ], rot: [ 0, 0, 0, 1 ], scl: [ 1, 1, 1 ] };
parentAnimation.keys[ last ] = { time: duration, pos: path[ last ], rot: [ 0, 0, 0, 1 ], scl: [ 1, 1, 1 ] };
console.log( "path totalLength: ", totalLength );
for ( i = first + 1; i < last; i++ ) {
t += duration * distance( path[ i ], path[ i - 1 ] ) / totalLength;
parentAnimation.keys[ i ] = { time: t, pos: path[ i ] };
}
animationData.hierarchy[ 0 ] = parentAnimation;
THREE.AnimationHandler.add( animationData );
return new THREE.Animation( parent, name, THREE.AnimationHandler.CATMULLROM_FORWARD, false );
};
function createSplineGeometryFromPoints( points, n_sub ) {
var i, index, position,
geometry = new THREE.Geometry(),
spline = new THREE.Spline(),
p, coords = [];
for ( i = 0; i < points.length; i ++ ) {
p = points[ i ];
coords[ i ] = { x: p[ 0 ], y: p[ 1 ], z: p[ 2 ] };
}
for ( i = 0; i < coords.length * n_sub; i ++ ) {
index = i / ( coords.length * n_sub );
position = spline.getPoint( coords, index );
geometry.vertices[ i ] = new THREE.Vertex( new THREE.Vector3( position.x, position.y, position.z ) );
}
return geometry;
};
function splineLength( points ) {
var i, index, p, coords = [],
spline = new THREE.Spline(),
n_sub = 100, totalLength = 0;
for ( i = 0; i < points.length; i ++ ) {
p = points[ i ];
coords[ i ] = { x: p[ 0 ], y: p[ 1 ], z: p[ 2 ] };
}
var oldPosition = [ points[ 0 ][ 0 ], points[ 0 ][ 1 ], points[ 0 ][ 2 ] ];
for ( i = 1; i < coords.length * n_sub; i ++ ) {
index = i / ( coords.length * n_sub );
position = spline.getPoint( coords, index );
totalLength += distance( [ position.x, position.y, position.z ], oldPosition );
oldPosition = [ position.x, position.y, position.z ];
}
return totalLength;
};
function createWaypointGeometryFromPoints( points ) {
var i, position,
geometry = new THREE.Geometry();
for ( i = 0; i < points.length; i ++ ) {
position = points[ i ];
geometry.vertices[ i ] = new THREE.Vertex( new THREE.Vector3( position.x, position.y, position.z ) );
}
return geometry;
};
function createPath( parent, path ) {
var lineGeo = createSplineGeometryFromPoints( path, 10 ),
lineMat = new THREE.LineBasicMaterial( { color: 0xff0000, linewidth: 3 } );
lineObj = new THREE.Line( lineGeo, lineMat );
lineObj.scale.set( 1, 1, 1 );
parent.addChild( lineObj );
var waypoint,
geo = new Sphere( 1, 16, 8 ),
mat = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
for( i = 0; i < path.length; i++ ) {
waypoint = new THREE.Mesh( geo, mat );
waypoint.position.set( path[ i ][ 0 ], path[ i ][ 1 ], path[ i ][ 2 ] );
waypoint.updateMatrix();
parent.addChild( waypoint );
}
};
if ( this.createDebugDummy ) {
var dummyParentMaterial = new THREE.MeshLambertMaterial( { color: 0x0077ff } ),
dummyChildMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff00 } ),
dummyParentGeo = new Cube( 10, 10, 20 ),
dummyChildGeo = new Cube( 2, 2, 10 );
this.animationParent = new THREE.Mesh( dummyParentGeo, dummyParentMaterial );
var dummyChild = new THREE.Mesh( dummyChildGeo, dummyChildMaterial );
dummyChild.position.set( 0, 10, 0 );
this.animation = initAnimationPath( this.animationParent, this.waypoints, "cameraPath01", this.duration );
this.animationParent.addChild( this );
this.animationParent.addChild( dummyChild );
} else {
this.animation = initAnimationPath( this.animationParent, this.waypoints, "cameraPath01", this.duration );
this.animationParent.addChild( this );
}
if ( this.createDebugPath ) {
createPath( this.debugPath, this.waypoints );
}
this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
};
THREE.PathCamera.prototype = new THREE.Camera();
THREE.PathCamera.prototype.constructor = THREE.PathCamera;
THREE.PathCamera.prototype.supr = THREE.Camera.prototype;
......@@ -72,7 +72,7 @@ THREE.QuakeCamera = function ( parameters ) {
this.lat = 0;
this.lon = 0;
this.phy = 0;
this.phi = 0;
this.theta = 0;
this.moveForward = false;
......@@ -125,7 +125,7 @@ THREE.QuakeCamera = function ( parameters ) {
};
this.onMouseMove = function (event) {
this.onMouseMove = function ( event ) {
this.mouseX = event.clientX - this.windowHalfX;
this.mouseY = event.clientY - this.windowHalfY;
......@@ -235,7 +235,7 @@ THREE.QuakeCamera = function ( parameters ) {
};
}
};
function clamp_bottom( x, a ) {
......
......@@ -76,6 +76,7 @@ THREE.Matrix4.prototype = {
if ( z.length() === 0 ) {
z.z = 1;
}
x.cross( up, z ).normalize();
......
......@@ -11,7 +11,7 @@ THREE.Spline = function () {
point, intPoint, weight, w2, w3,
pa, pb, pc, pd;
this.get2DPoint = function ( points, k ) {
this.getPoint = function ( points, k ) {
point = ( points.length - 1 ) * k;
intPoint = Math.floor( point );
......
......@@ -33,6 +33,7 @@ COMMON_FILES = [
'animation/Animation.js',
'cameras/Camera.js',
'cameras/QuakeCamera.js',
'cameras/PathCamera.js',
'lights/Light.js',
'lights/AmbientLight.js',
'lights/DirectionalLight.js',
......@@ -236,6 +237,8 @@ WEBGL_FILES = [
'core/UV.js',
'core/Geometry.js',
'cameras/Camera.js',
'cameras/QuakeCamera.js',
'cameras/PathCamera.js',
'lights/Light.js',
'lights/AmbientLight.js',
'lights/DirectionalLight.js',
......
......@@ -242,7 +242,7 @@ def get_name(fname):
"""Create model name based of filename ("path/fname.js" -> "fname").
"""
return os.path.splitext(os.path.basename(fname))
return os.path.splitext(os.path.basename(fname))[0]
def bbox(vertices):
"""Compute bounding box of vertex array.
......
......@@ -242,7 +242,7 @@ def get_name(fname):
"""Create model name based of filename ("path/fname.js" -> "fname").
"""
return os.path.splitext(os.path.basename(fname))
return os.path.splitext(os.path.basename(fname))[0]
def bbox(vertices):
"""Compute bounding box of vertex array.
......
......@@ -242,7 +242,7 @@ def get_name(fname):
"""Create model name based of filename ("path/fname.js" -> "fname").
"""
return os.path.splitext(os.path.basename(fname))
return os.path.splitext(os.path.basename(fname))[0]
def bbox(vertices):
"""Compute bounding box of vertex array.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册