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

Moved MorphBlendMesh to examples/js.

上级 ce7ef05f
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="../../../" />
<script src="list.js"></script>
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>
[page:Mesh] &rarr;
<h1>[name]</h1>
<div class="desc">A mesh that can blend together multiple animated morph targets.</div>
<h2>Example</h2>
[example:webgl_morphtargets_md2_control morphtargets / md2 / controll]
<h2>Constructor</h2>
<h3>[name]([page:Geometry geometry], [page:Material material])</h3>
<div>
geometry — An instance of [page:Geometry].<br />
material — An instance of [page:Material] (optional).
</div>
<h2>Properties</h2>
<h3>[property:object animationsMap]</h3>
<div>
An object of named animations as added by [page:MorphBlendMesh.createAnimation].
</div>
<h3>[property:array animationsList]</h3>
<div>
The list of animations as added by [page:MorphBlendMesh.createAnimation].
</div>
<h2>Methods</h2>
<h3>[method:null setAnimationWeight]([page:String name], [page:Float weight])</h3>
<div>
name -- The name of the animation<br />
weight -- Weight of the animation, typically 0-1
</div>
<div>
Set the weight of how much this animation will apply to the overall morph. 0 is off, 1 is full weight.
</div>
<h3>[method:null setAnimationFPS]([page:String name], [page:Float fps])</h3>
<div>
name -- The name of the animation <br />
fps -- The number of frames (morphTargets) per second
</div>
<div>
A frame is typically 1 morph target.
</div>
<h3>[method:null createAnimation]([page:String name], [page:Integer start], [page:Integer end], [page:Float fps])</h3>
<div>
name -- The name of the animation <br />
start -- The starting frame (morph)<br />
end -- The ending frame (morph)<br />
fps -- How many frames (morphs) to play per second
</div>
<div>
Creates an animation object that gets added to both the [page:MorphBlendMesh.animationsMap animationsMap] and
[page:MorphBlendMesh.animationsList animationsList].<br/><br/>
Animation object:<br/><br/>
startFrame -- Starting frame<br/>
endFrame -- Ending frame<br/>
length -- The number of frames<br/>
fps -- The frames per second<br/>
duration -- The length of the animation in seconds<br/>
lastFrame -- The previous frame that was played<br/>
currentFrame -- The current frame<br/>
active -- Whether or not the animation is being played<br/>
time -- The time in seconds of the animation<br/>
direction -- Which way to play the animation<br/>
weight -- The weight of the animation<br/>
directionBackwards -- Is playing backwards<br/>
mirroredLoop -- Loop back and forth
</div>
<h3>[method:null playAnimation]([page:String name])</h3>
<div>
name -- The name of the animation
</div>
<div>
Sets the animation to active and animation time to 0
</div>
<h3>[method:null update]([page:Float delta])</h3>
<div>
delta -- Time in seconds
</div>
<div>
Updates and plays the animation
</div>
<h3>[method:null autoCreateAnimations]([page:Float fps])</h3>
<div>
fps -- Frames per second
</div>
<div>
Goes through the geometry's morphTargets and generates animations based on the morphTargets' names. Names
are of the form "walk_01", "walk_02", "walk_03", etc or "run001", "run002", "run003".
</div>
<h3>[method:null setAnimationDuration]([page:String name], [page:Float duration])</h3>
<div>
name -- The name of the animation <br />
duration -- How long in seconds to play the animation
</div>
<div>
Updates the animation object with proper values to update the duration.
</div>
<h3>[method:null setAnimationDirectionForward]([page:String name])</h3>
<div>
name -- The name of the animation
</div>
<div>
Sets the animation to play forwards
</div>
<h3>[method:null setAnimationDirectionBackward]([page:String name])</h3>
<div>
name -- The name of the animation
</div>
<div>
Sets the animation to play backwards
</div>
<h3>[method:Float getAnimationDuration]([page:String name])</h3>
<div>
name -- The name of the animation
</div>
<div>
Returns the duration in seconds of the animation. Returns -1 if it can't be found.
</div>
<h3>[method:Float getAnimationTime]([page:String name])</h3>
<div>
name -- The name of the animation
</div>
<div>
Returns the current time position of the animation.
</div>
<h3>[method:null setAnimationTime]([page:String name], [page:Float time])</h3>
<div>
name -- The name of the animation <br />
time -- The time in seconds
</div>
<div>
Sets the current time position of the animation
</div>
<h3>[method:null stopAnimation]([page:String name])</h3>
<div>
name -- The name of the animation
</div>
<div>
Stops the playback of the animation
</div>
<h2>Source</h2>
[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
</body>
</html>
......@@ -14,7 +14,7 @@
<div class="desc">
Class representing triangular [link:https://en.wikipedia.org/wiki/Polygon_mesh polygon mesh] based objects.
Also serves as a base for other classes such as [page:MorphBlendMesh] and [page:SkinnedMesh].
Also serves as a base for other classes such as [page:SkinnedMesh].
</div>
......
......@@ -132,7 +132,6 @@ var list = {
"Extras / Objects": {
"ImmediateRenderObject": "api/extras/objects/ImmediateRenderObject",
"MorphBlendMesh": "api/extras/objects/MorphBlendMesh"
},
"Geometries": {
......
......@@ -1731,74 +1731,6 @@
"!doc": "base class for immediate rendering objects.",
"!type": "fn()"
},
"MorphBlendMesh": {
"!url": "http://threejs.org/docs/#Reference/extras/objects/MorphBlendMesh",
"prototype": {
"!proto": "THREE.Mesh.prototype",
"animationsMap": {
"!type": "object",
"!doc": "todo"
},
"animationsList": {
"!type": "array",
"!doc": "todo"
},
"setAnimationWeight": {
"!type": "fn(name: todo, weight: todo) -> todo",
"!doc": "todo"
},
"setAnimationFPS": {
"!type": "fn(name: todo, fps: todo) -> todo",
"!doc": "todo"
},
"createAnimation": {
"!type": "fn(name: todo, start: todo, end: todo, fps: todo) -> todo",
"!doc": "todo"
},
"playAnimation": {
"!type": "fn(name: todo) -> todo",
"!doc": "todo"
},
"update": {
"!type": "fn(delta: todo) -> todo",
"!doc": "todo"
},
"autoCreateAnimations": {
"!type": "fn(fps: todo) -> todo",
"!doc": "todo"
},
"setAnimationDuration": {
"!type": "fn(name: todo, duration: todo) -> todo",
"!doc": "todo"
},
"setAnimationDirectionForward": {
"!type": "fn(name: todo) -> todo",
"!doc": "todo"
},
"getAnimationDuration": {
"!type": "fn(name: todo) -> todo",
"!doc": "todo"
},
"getAnimationTime": {
"!type": "fn(name: todo) -> todo",
"!doc": "todo"
},
"setAnimationDirectionBackward": {
"!type": "fn(name: todo) -> todo",
"!doc": "todo"
},
"setAnimationTime": {
"!type": "fn(name: todo, time: todo) -> todo",
"!doc": "todo"
},
"stopAnimation": {
"!type": "fn(name: todo) -> todo",
"!doc": "todo"
}
},
"!doc": "todo",
"!type": "fn(geometry: todo, material: todo)"
},
"AmbientLight": {
"!url": "http://threejs.org/docs/#Reference/lights/AmbientLight",
"prototype": {
......
/**
* @author alteredq / http://alteredqualia.com/
*/
THREE.MorphBlendMesh = function ( geometry, material ) {
THREE.Mesh.call( this, geometry, material );
this.animationsMap = {};
this.animationsList = [];
// prepare default animation
// (all frames played together in 1 second)
var numFrames = this.geometry.morphTargets.length;
var name = "__default";
var startFrame = 0;
var endFrame = numFrames - 1;
var fps = numFrames / 1;
this.createAnimation( name, startFrame, endFrame, fps );
this.setAnimationWeight( name, 1 );
}
THREE.MorphBlendMesh.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), {
constructor: THREE.MorphBlendMesh,
createAnimation: function ( name, start, end, fps ) {
var animation = {
start: start,
end: end,
length: end - start + 1,
fps: fps,
duration: ( end - start ) / fps,
lastFrame: 0,
currentFrame: 0,
active: false,
time: 0,
direction: 1,
weight: 1,
directionBackwards: false,
mirroredLoop: false
};
this.animationsMap[ name ] = animation;
this.animationsList.push( animation );
},
autoCreateAnimations: function ( fps ) {
var pattern = /([a-z]+)_?(\d+)/i;
var firstAnimation, frameRanges = {};
var geometry = this.geometry;
for ( var i = 0, il = geometry.morphTargets.length; i < il; i ++ ) {
var morph = geometry.morphTargets[ i ];
var chunks = morph.name.match( pattern );
if ( chunks && chunks.length > 1 ) {
var name = chunks[ 1 ];
if ( ! frameRanges[ name ] ) frameRanges[ name ] = { start: Infinity, end: - Infinity };
var range = frameRanges[ name ];
if ( i < range.start ) range.start = i;
if ( i > range.end ) range.end = i;
if ( ! firstAnimation ) firstAnimation = name;
}
}
for ( var name in frameRanges ) {
var range = frameRanges[ name ];
this.createAnimation( name, range.start, range.end, fps );
}
this.firstAnimation = firstAnimation;
},
setAnimationDirectionForward: function ( name ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.direction = 1;
animation.directionBackwards = false;
}
},
setAnimationDirectionBackward: function ( name ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.direction = - 1;
animation.directionBackwards = true;
}
},
setAnimationFPS: function ( name, fps ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.fps = fps;
animation.duration = ( animation.end - animation.start ) / animation.fps;
}
},
setAnimationDuration: function ( name, duration ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.duration = duration;
animation.fps = ( animation.end - animation.start ) / animation.duration;
}
},
setAnimationWeight: function ( name, weight ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.weight = weight;
}
},
setAnimationTime: function ( name, time ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.time = time;
}
},
getAnimationTime: function ( name ) {
var time = 0;
var animation = this.animationsMap[ name ];
if ( animation ) {
time = animation.time;
}
return time;
},
getAnimationDuration: function ( name ) {
var duration = - 1;
var animation = this.animationsMap[ name ];
if ( animation ) {
duration = animation.duration;
}
return duration;
},
playAnimation: function ( name ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.time = 0;
animation.active = true;
} else {
console.warn( "THREE.MorphBlendMesh: animation[" + name + "] undefined in .playAnimation()" );
}
},
stopAnimation: function ( name ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.active = false;
}
},
update: function ( delta ) {
for ( var i = 0, il = this.animationsList.length; i < il; i ++ ) {
var animation = this.animationsList[ i ];
if ( ! animation.active ) continue;
var frameTime = animation.duration / animation.length;
animation.time += animation.direction * delta;
if ( animation.mirroredLoop ) {
if ( animation.time > animation.duration || animation.time < 0 ) {
animation.direction *= - 1;
if ( animation.time > animation.duration ) {
animation.time = animation.duration;
animation.directionBackwards = true;
}
if ( animation.time < 0 ) {
animation.time = 0;
animation.directionBackwards = false;
}
}
} else {
animation.time = animation.time % animation.duration;
if ( animation.time < 0 ) animation.time += animation.duration;
}
var keyframe = animation.start + THREE.Math.clamp( Math.floor( animation.time / frameTime ), 0, animation.length - 1 );
var weight = animation.weight;
if ( keyframe !== animation.currentFrame ) {
this.morphTargetInfluences[ animation.lastFrame ] = 0;
this.morphTargetInfluences[ animation.currentFrame ] = 1 * weight;
this.morphTargetInfluences[ keyframe ] = 0;
animation.lastFrame = animation.currentFrame;
animation.currentFrame = keyframe;
}
var mix = ( animation.time % frameTime ) / frameTime;
if ( animation.directionBackwards ) mix = 1 - mix;
if ( animation.currentFrame !== animation.lastFrame ) {
this.morphTargetInfluences[ animation.currentFrame ] = mix * weight;
this.morphTargetInfluences[ animation.lastFrame ] = ( 1 - mix ) * weight;
} else {
this.morphTargetInfluences[ animation.currentFrame ] = weight;
}
}
}
} );
......@@ -41,6 +41,7 @@
<script src="js/controls/OrbitControls.js"></script>
<script src='js/loaders/MD2Loader.js'></script>
<script src='js/MorphBlendMesh.js'></script>
<script src='js/MD2CharacterComplex.js'></script>
<script src='js/Gyroscope.js'></script>
......
......@@ -120,7 +120,6 @@ export { Vector3 } from './math/Vector3.js';
export { Vector2 } from './math/Vector2.js';
export { Quaternion } from './math/Quaternion.js';
export { Color } from './math/Color.js';
export { MorphBlendMesh } from './extras/objects/MorphBlendMesh.js';
export { ImmediateRenderObject } from './extras/objects/ImmediateRenderObject.js';
export { VertexNormalsHelper } from './helpers/VertexNormalsHelper.js';
export { SpotLightHelper } from './helpers/SpotLightHelper.js';
......
import { Mesh } from '../../objects/Mesh';
import { _Math } from '../../math/Math';
/**
* @author alteredq / http://alteredqualia.com/
*/
function MorphBlendMesh( geometry, material ) {
Mesh.call( this, geometry, material );
this.animationsMap = {};
this.animationsList = [];
// prepare default animation
// (all frames played together in 1 second)
var numFrames = this.geometry.morphTargets.length;
var name = "__default";
var startFrame = 0;
var endFrame = numFrames - 1;
var fps = numFrames / 1;
this.createAnimation( name, startFrame, endFrame, fps );
this.setAnimationWeight( name, 1 );
}
MorphBlendMesh.prototype = Object.create( Mesh.prototype );
MorphBlendMesh.prototype.constructor = MorphBlendMesh;
MorphBlendMesh.prototype.createAnimation = function ( name, start, end, fps ) {
var animation = {
start: start,
end: end,
length: end - start + 1,
fps: fps,
duration: ( end - start ) / fps,
lastFrame: 0,
currentFrame: 0,
active: false,
time: 0,
direction: 1,
weight: 1,
directionBackwards: false,
mirroredLoop: false
};
this.animationsMap[ name ] = animation;
this.animationsList.push( animation );
};
MorphBlendMesh.prototype.autoCreateAnimations = function ( fps ) {
var pattern = /([a-z]+)_?(\d+)/i;
var firstAnimation, frameRanges = {};
var geometry = this.geometry;
for ( var i = 0, il = geometry.morphTargets.length; i < il; i ++ ) {
var morph = geometry.morphTargets[ i ];
var chunks = morph.name.match( pattern );
if ( chunks && chunks.length > 1 ) {
var name = chunks[ 1 ];
if ( ! frameRanges[ name ] ) frameRanges[ name ] = { start: Infinity, end: - Infinity };
var range = frameRanges[ name ];
if ( i < range.start ) range.start = i;
if ( i > range.end ) range.end = i;
if ( ! firstAnimation ) firstAnimation = name;
}
}
for ( var name in frameRanges ) {
var range = frameRanges[ name ];
this.createAnimation( name, range.start, range.end, fps );
}
this.firstAnimation = firstAnimation;
};
MorphBlendMesh.prototype.setAnimationDirectionForward = function ( name ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.direction = 1;
animation.directionBackwards = false;
}
};
MorphBlendMesh.prototype.setAnimationDirectionBackward = function ( name ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.direction = - 1;
animation.directionBackwards = true;
}
};
MorphBlendMesh.prototype.setAnimationFPS = function ( name, fps ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.fps = fps;
animation.duration = ( animation.end - animation.start ) / animation.fps;
}
};
MorphBlendMesh.prototype.setAnimationDuration = function ( name, duration ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.duration = duration;
animation.fps = ( animation.end - animation.start ) / animation.duration;
}
};
MorphBlendMesh.prototype.setAnimationWeight = function ( name, weight ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.weight = weight;
}
};
MorphBlendMesh.prototype.setAnimationTime = function ( name, time ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.time = time;
}
};
MorphBlendMesh.prototype.getAnimationTime = function ( name ) {
var time = 0;
var animation = this.animationsMap[ name ];
if ( animation ) {
time = animation.time;
}
return time;
};
MorphBlendMesh.prototype.getAnimationDuration = function ( name ) {
var duration = - 1;
var animation = this.animationsMap[ name ];
if ( animation ) {
duration = animation.duration;
}
return duration;
};
MorphBlendMesh.prototype.playAnimation = function ( name ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.time = 0;
animation.active = true;
} else {
console.warn( "THREE.MorphBlendMesh: animation[" + name + "] undefined in .playAnimation()" );
}
};
MorphBlendMesh.prototype.stopAnimation = function ( name ) {
var animation = this.animationsMap[ name ];
if ( animation ) {
animation.active = false;
}
};
MorphBlendMesh.prototype.update = function ( delta ) {
for ( var i = 0, il = this.animationsList.length; i < il; i ++ ) {
var animation = this.animationsList[ i ];
if ( ! animation.active ) continue;
var frameTime = animation.duration / animation.length;
animation.time += animation.direction * delta;
if ( animation.mirroredLoop ) {
if ( animation.time > animation.duration || animation.time < 0 ) {
animation.direction *= - 1;
if ( animation.time > animation.duration ) {
animation.time = animation.duration;
animation.directionBackwards = true;
}
if ( animation.time < 0 ) {
animation.time = 0;
animation.directionBackwards = false;
}
}
} else {
animation.time = animation.time % animation.duration;
if ( animation.time < 0 ) animation.time += animation.duration;
}
var keyframe = animation.start + _Math.clamp( Math.floor( animation.time / frameTime ), 0, animation.length - 1 );
var weight = animation.weight;
if ( keyframe !== animation.currentFrame ) {
this.morphTargetInfluences[ animation.lastFrame ] = 0;
this.morphTargetInfluences[ animation.currentFrame ] = 1 * weight;
this.morphTargetInfluences[ keyframe ] = 0;
animation.lastFrame = animation.currentFrame;
animation.currentFrame = keyframe;
}
var mix = ( animation.time % frameTime ) / frameTime;
if ( animation.directionBackwards ) mix = 1 - mix;
if ( animation.currentFrame !== animation.lastFrame ) {
this.morphTargetInfluences[ animation.currentFrame ] = mix * weight;
this.morphTargetInfluences[ animation.lastFrame ] = ( 1 - mix ) * weight;
} else {
this.morphTargetInfluences[ animation.currentFrame ] = weight;
}
}
};
export { MorphBlendMesh };
......@@ -89,7 +89,6 @@ export { SplineCurve } from '../src/extras/curves/SplineCurve.js';
//src/extras/objects
export { ImmediateRenderObject } from '../src/extras/objects/ImmediateRenderObject.js';
export { MorphBlendMesh } from '../src/extras/objects/MorphBlendMesh.js';
//src/geometries
......@@ -241,9 +240,3 @@ export { DataTexture } from '../src/textures/DataTexture.js';
export { DepthTexture } from '../src/textures/DepthTexture.js';
export { Texture } from '../src/textures/Texture.js';
export { VideoTexture } from '../src/textures/VideoTexture.js';
/**
* @author TristanVALCKE / https://github.com/TristanVALCKE
*/
//Todo
console.warn("Todo: Unit tests of MorphBlendMesh")
......@@ -25,7 +25,7 @@
<script src="src/polyfills.js"></script>
<script src="src/utils.js"></script>
<!-- /src/animation -->
<script src="src/animation/AnimationAction.js"></script>
<script src="src/animation/AnimationClip.js"></script>
......@@ -46,7 +46,7 @@
<script src="src/animation/tracks/StringKeyframeTrack.js"></script>
<script src="src/animation/tracks/VectorKeyframeTrack.js"></script>
<!-- /src/audio -->
<script src="src/audio/Audio.js"></script>
<script src="src/audio/AudioAnalyser.js"></script>
......@@ -54,7 +54,7 @@
<script src="src/audio/AudioListener.js"></script>
<script src="src/audio/PositionalAudio.js"></script>
<!-- /src/cameras -->
<script src="src/cameras/Camera.js"></script>
<script src="src/cameras/CubeCamera.js"></script>
......@@ -62,7 +62,7 @@
<script src="src/cameras/PerspectiveCamera.js"></script>
<script src="src/cameras/StereoCamera.js"></script>
<!-- /src/core -->
<script src="src/core/BufferAttribute.js"></script>
<script src="src/core/BufferGeometry.js"></script>
......@@ -110,7 +110,6 @@
<!-- /src/extras/objects -->
<script src="src/extras/objects/ImmediateRenderObject.js"></script>
<script src="src/extras/objects/MorphBlendMesh.js"></script>
<!-- /src/geometries -->
......@@ -139,7 +138,7 @@
<script src="src/geometries/TubeGeometry.tests.js"></script>
<script src="src/geometries/WireframeGeometry.tests.js"></script>
<!-- /src/helpers -->
<script src="src/helpers/ArrowHelper.js"></script>
<script src="src/helpers/AxisHelper.js"></script>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册