initial commit
BIN
assets/textures/fire-sprite-small.png
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
assets/textures/fire-sprite.png
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
assets/textures/fire.jpg
Normal file
After Width: | Height: | Size: 52 KiB |
BIN
assets/textures/grass-long.jpg
Normal file
After Width: | Height: | Size: 25 KiB |
BIN
assets/textures/grass.jpg
Normal file
After Width: | Height: | Size: 169 KiB |
BIN
assets/textures/grassdead.jpg
Normal file
After Width: | Height: | Size: 970 KiB |
BIN
assets/textures/lava.jpg
Normal file
After Width: | Height: | Size: 574 KiB |
BIN
assets/textures/metal.png
Normal file
After Width: | Height: | Size: 1.3 MiB |
62
index.html
Normal file
@ -0,0 +1,62 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Fire Ball</title>
|
||||
|
||||
<style>
|
||||
body, html {
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
overflow: hidden;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
#gui {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
color: white;
|
||||
z-index: 2;
|
||||
padding-left: 15px;
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
width: 200px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script src='lib/three.js'></script>
|
||||
<script src='lib/Tween.js'></script>
|
||||
<script src='lib/objects/Sky.js'></script>
|
||||
<script src='lib/whs.js'></script>
|
||||
<script src='lib/physics-module.js'></script>
|
||||
<script src='lib/ammo.js'></script>
|
||||
<script src='lib/mousetrap.js'></script>
|
||||
<script src='lib/moment.min.js'></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id='gui'>
|
||||
<p>
|
||||
Time: <span id='time'>00:00:000</span>
|
||||
</p>
|
||||
<p>
|
||||
Speed: <span id='speed'>000</span>
|
||||
</p>
|
||||
<p>
|
||||
Score: <span id='score'>000</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<script src='src/utils.js'></script>
|
||||
<script src='src/config.js'></script>
|
||||
<script src='src/gui.js'></script>
|
||||
<script src='src/objects.js'></script>
|
||||
<script src='src/world.js'></script>
|
||||
<script src='src/ball.js'></script>
|
||||
<script src='src/lights.js'></script>
|
||||
<script src='src/helpers.js'></script>
|
||||
<script src='src/scenes.js'></script>
|
||||
<script src='src/controls.js'></script>
|
||||
<script src='src/levels/0-basic.js'></script>
|
||||
<script src='src/main.js'></script>
|
||||
</body>
|
||||
</html>
|
1924
lib/Three.Legacy.js
Normal file
154
lib/Three.js
Normal file
@ -0,0 +1,154 @@
|
||||
import './polyfills.js';
|
||||
|
||||
export { WebGLRenderTargetCube } from './renderers/WebGLRenderTargetCube.js';
|
||||
export { WebGLRenderTarget } from './renderers/WebGLRenderTarget.js';
|
||||
export { WebGLRenderer } from './renderers/WebGLRenderer.js';
|
||||
// export { WebGL2Renderer } from './renderers/WebGL2Renderer.js';
|
||||
export { ShaderLib } from './renderers/shaders/ShaderLib.js';
|
||||
export { UniformsLib } from './renderers/shaders/UniformsLib.js';
|
||||
export { UniformsUtils } from './renderers/shaders/UniformsUtils.js';
|
||||
export { ShaderChunk } from './renderers/shaders/ShaderChunk.js';
|
||||
export { FogExp2 } from './scenes/FogExp2.js';
|
||||
export { Fog } from './scenes/Fog.js';
|
||||
export { Scene } from './scenes/Scene.js';
|
||||
export { Sprite } from './objects/Sprite.js';
|
||||
export { LOD } from './objects/LOD.js';
|
||||
export { SkinnedMesh } from './objects/SkinnedMesh.js';
|
||||
export { Skeleton } from './objects/Skeleton.js';
|
||||
export { Bone } from './objects/Bone.js';
|
||||
export { Mesh } from './objects/Mesh.js';
|
||||
export { LineSegments } from './objects/LineSegments.js';
|
||||
export { LineLoop } from './objects/LineLoop.js';
|
||||
export { Line } from './objects/Line.js';
|
||||
export { Points } from './objects/Points.js';
|
||||
export { Group } from './objects/Group.js';
|
||||
export { VideoTexture } from './textures/VideoTexture.js';
|
||||
export { DataTexture } from './textures/DataTexture.js';
|
||||
export { DataTexture3D } from './textures/DataTexture3D.js';
|
||||
export { CompressedTexture } from './textures/CompressedTexture.js';
|
||||
export { CubeTexture } from './textures/CubeTexture.js';
|
||||
export { CanvasTexture } from './textures/CanvasTexture.js';
|
||||
export { DepthTexture } from './textures/DepthTexture.js';
|
||||
export { Texture } from './textures/Texture.js';
|
||||
export * from './geometries/Geometries.js';
|
||||
export * from './materials/Materials.js';
|
||||
export { AnimationLoader } from './loaders/AnimationLoader.js';
|
||||
export { CompressedTextureLoader } from './loaders/CompressedTextureLoader.js';
|
||||
export { DataTextureLoader } from './loaders/DataTextureLoader.js';
|
||||
export { CubeTextureLoader } from './loaders/CubeTextureLoader.js';
|
||||
export { TextureLoader } from './loaders/TextureLoader.js';
|
||||
export { ObjectLoader } from './loaders/ObjectLoader.js';
|
||||
export { MaterialLoader } from './loaders/MaterialLoader.js';
|
||||
export { BufferGeometryLoader } from './loaders/BufferGeometryLoader.js';
|
||||
export { DefaultLoadingManager, LoadingManager } from './loaders/LoadingManager.js';
|
||||
export { JSONLoader } from './loaders/JSONLoader.js';
|
||||
export { ImageLoader } from './loaders/ImageLoader.js';
|
||||
export { ImageBitmapLoader } from './loaders/ImageBitmapLoader.js';
|
||||
export { FontLoader } from './loaders/FontLoader.js';
|
||||
export { FileLoader } from './loaders/FileLoader.js';
|
||||
export { Loader } from './loaders/Loader.js';
|
||||
export { LoaderUtils } from './loaders/LoaderUtils.js';
|
||||
export { Cache } from './loaders/Cache.js';
|
||||
export { AudioLoader } from './loaders/AudioLoader.js';
|
||||
export { SpotLightShadow } from './lights/SpotLightShadow.js';
|
||||
export { SpotLight } from './lights/SpotLight.js';
|
||||
export { PointLight } from './lights/PointLight.js';
|
||||
export { RectAreaLight } from './lights/RectAreaLight.js';
|
||||
export { HemisphereLight } from './lights/HemisphereLight.js';
|
||||
export { DirectionalLightShadow } from './lights/DirectionalLightShadow.js';
|
||||
export { DirectionalLight } from './lights/DirectionalLight.js';
|
||||
export { AmbientLight } from './lights/AmbientLight.js';
|
||||
export { LightShadow } from './lights/LightShadow.js';
|
||||
export { Light } from './lights/Light.js';
|
||||
export { StereoCamera } from './cameras/StereoCamera.js';
|
||||
export { PerspectiveCamera } from './cameras/PerspectiveCamera.js';
|
||||
export { OrthographicCamera } from './cameras/OrthographicCamera.js';
|
||||
export { CubeCamera } from './cameras/CubeCamera.js';
|
||||
export { ArrayCamera } from './cameras/ArrayCamera.js';
|
||||
export { Camera } from './cameras/Camera.js';
|
||||
export { AudioListener } from './audio/AudioListener.js';
|
||||
export { PositionalAudio } from './audio/PositionalAudio.js';
|
||||
export { AudioContext } from './audio/AudioContext.js';
|
||||
export { AudioAnalyser } from './audio/AudioAnalyser.js';
|
||||
export { Audio } from './audio/Audio.js';
|
||||
export { VectorKeyframeTrack } from './animation/tracks/VectorKeyframeTrack.js';
|
||||
export { StringKeyframeTrack } from './animation/tracks/StringKeyframeTrack.js';
|
||||
export { QuaternionKeyframeTrack } from './animation/tracks/QuaternionKeyframeTrack.js';
|
||||
export { NumberKeyframeTrack } from './animation/tracks/NumberKeyframeTrack.js';
|
||||
export { ColorKeyframeTrack } from './animation/tracks/ColorKeyframeTrack.js';
|
||||
export { BooleanKeyframeTrack } from './animation/tracks/BooleanKeyframeTrack.js';
|
||||
export { PropertyMixer } from './animation/PropertyMixer.js';
|
||||
export { PropertyBinding } from './animation/PropertyBinding.js';
|
||||
export { KeyframeTrack } from './animation/KeyframeTrack.js';
|
||||
export { AnimationUtils } from './animation/AnimationUtils.js';
|
||||
export { AnimationObjectGroup } from './animation/AnimationObjectGroup.js';
|
||||
export { AnimationMixer } from './animation/AnimationMixer.js';
|
||||
export { AnimationClip } from './animation/AnimationClip.js';
|
||||
export { Uniform } from './core/Uniform.js';
|
||||
export { InstancedBufferGeometry } from './core/InstancedBufferGeometry.js';
|
||||
export { BufferGeometry } from './core/BufferGeometry.js';
|
||||
export { Geometry } from './core/Geometry.js';
|
||||
export { InterleavedBufferAttribute } from './core/InterleavedBufferAttribute.js';
|
||||
export { InstancedInterleavedBuffer } from './core/InstancedInterleavedBuffer.js';
|
||||
export { InterleavedBuffer } from './core/InterleavedBuffer.js';
|
||||
export { InstancedBufferAttribute } from './core/InstancedBufferAttribute.js';
|
||||
export * from './core/BufferAttribute.js';
|
||||
export { Face3 } from './core/Face3.js';
|
||||
export { Object3D } from './core/Object3D.js';
|
||||
export { Raycaster } from './core/Raycaster.js';
|
||||
export { Layers } from './core/Layers.js';
|
||||
export { EventDispatcher } from './core/EventDispatcher.js';
|
||||
export { Clock } from './core/Clock.js';
|
||||
export { QuaternionLinearInterpolant } from './math/interpolants/QuaternionLinearInterpolant.js';
|
||||
export { LinearInterpolant } from './math/interpolants/LinearInterpolant.js';
|
||||
export { DiscreteInterpolant } from './math/interpolants/DiscreteInterpolant.js';
|
||||
export { CubicInterpolant } from './math/interpolants/CubicInterpolant.js';
|
||||
export { Interpolant } from './math/Interpolant.js';
|
||||
export { Triangle } from './math/Triangle.js';
|
||||
export { _Math as Math } from './math/Math.js';
|
||||
export { Spherical } from './math/Spherical.js';
|
||||
export { Cylindrical } from './math/Cylindrical.js';
|
||||
export { Plane } from './math/Plane.js';
|
||||
export { Frustum } from './math/Frustum.js';
|
||||
export { Sphere } from './math/Sphere.js';
|
||||
export { Ray } from './math/Ray.js';
|
||||
export { Matrix4 } from './math/Matrix4.js';
|
||||
export { Matrix3 } from './math/Matrix3.js';
|
||||
export { Box3 } from './math/Box3.js';
|
||||
export { Box2 } from './math/Box2.js';
|
||||
export { Line3 } from './math/Line3.js';
|
||||
export { Euler } from './math/Euler.js';
|
||||
export { Vector4 } from './math/Vector4.js';
|
||||
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 { ImmediateRenderObject } from './extras/objects/ImmediateRenderObject.js';
|
||||
export { VertexNormalsHelper } from './helpers/VertexNormalsHelper.js';
|
||||
export { SpotLightHelper } from './helpers/SpotLightHelper.js';
|
||||
export { SkeletonHelper } from './helpers/SkeletonHelper.js';
|
||||
export { PointLightHelper } from './helpers/PointLightHelper.js';
|
||||
export { RectAreaLightHelper } from './helpers/RectAreaLightHelper.js';
|
||||
export { HemisphereLightHelper } from './helpers/HemisphereLightHelper.js';
|
||||
export { GridHelper } from './helpers/GridHelper.js';
|
||||
export { PolarGridHelper } from './helpers/PolarGridHelper.js';
|
||||
export { FaceNormalsHelper } from './helpers/FaceNormalsHelper.js';
|
||||
export { DirectionalLightHelper } from './helpers/DirectionalLightHelper.js';
|
||||
export { CameraHelper } from './helpers/CameraHelper.js';
|
||||
export { BoxHelper } from './helpers/BoxHelper.js';
|
||||
export { Box3Helper } from './helpers/Box3Helper.js';
|
||||
export { PlaneHelper } from './helpers/PlaneHelper.js';
|
||||
export { ArrowHelper } from './helpers/ArrowHelper.js';
|
||||
export { AxesHelper } from './helpers/AxesHelper.js';
|
||||
export * from './extras/curves/Curves.js';
|
||||
export { Shape } from './extras/core/Shape.js';
|
||||
export { Path } from './extras/core/Path.js';
|
||||
export { ShapePath } from './extras/core/ShapePath.js';
|
||||
export { Font } from './extras/core/Font.js';
|
||||
export { CurvePath } from './extras/core/CurvePath.js';
|
||||
export { Curve } from './extras/core/Curve.js';
|
||||
export { ImageUtils } from './extras/ImageUtils.js';
|
||||
export { ShapeUtils } from './extras/ShapeUtils.js';
|
||||
export { WebGLUtils } from './renderers/webgl/WebGLUtils.js';
|
||||
export * from './constants.js';
|
||||
export * from './Three.Legacy.js';
|
917
lib/Tween.js
Normal file
@ -0,0 +1,917 @@
|
||||
/**
|
||||
* Tween.js - Licensed under the MIT license
|
||||
* https://github.com/tweenjs/tween.js
|
||||
* ----------------------------------------------
|
||||
*
|
||||
* See https://github.com/tweenjs/tween.js/graphs/contributors for the full list of contributors.
|
||||
* Thank you all, you're awesome!
|
||||
*/
|
||||
|
||||
|
||||
var _Group = function () {
|
||||
this._tweens = {};
|
||||
this._tweensAddedDuringUpdate = {};
|
||||
};
|
||||
|
||||
_Group.prototype = {
|
||||
getAll: function () {
|
||||
|
||||
return Object.keys(this._tweens).map(function (tweenId) {
|
||||
return this._tweens[tweenId];
|
||||
}.bind(this));
|
||||
|
||||
},
|
||||
|
||||
removeAll: function () {
|
||||
|
||||
this._tweens = {};
|
||||
|
||||
},
|
||||
|
||||
add: function (tween) {
|
||||
|
||||
this._tweens[tween.getId()] = tween;
|
||||
this._tweensAddedDuringUpdate[tween.getId()] = tween;
|
||||
|
||||
},
|
||||
|
||||
remove: function (tween) {
|
||||
|
||||
delete this._tweens[tween.getId()];
|
||||
delete this._tweensAddedDuringUpdate[tween.getId()];
|
||||
|
||||
},
|
||||
|
||||
update: function (time, preserve) {
|
||||
|
||||
var tweenIds = Object.keys(this._tweens);
|
||||
|
||||
if (tweenIds.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
time = time !== undefined ? time : TWEEN.now();
|
||||
|
||||
// Tweens are updated in "batches". If you add a new tween during an update, then the
|
||||
// new tween will be updated in the next batch.
|
||||
// If you remove a tween during an update, it may or may not be updated. However,
|
||||
// if the removed tween was added during the current batch, then it will not be updated.
|
||||
while (tweenIds.length > 0) {
|
||||
this._tweensAddedDuringUpdate = {};
|
||||
|
||||
for (var i = 0; i < tweenIds.length; i++) {
|
||||
|
||||
var tween = this._tweens[tweenIds[i]];
|
||||
|
||||
if (tween && tween.update(time) === false) {
|
||||
tween._isPlaying = false;
|
||||
|
||||
if (!preserve) {
|
||||
delete this._tweens[tweenIds[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tweenIds = Object.keys(this._tweensAddedDuringUpdate);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
var TWEEN = new _Group();
|
||||
|
||||
TWEEN.Group = _Group;
|
||||
TWEEN._nextId = 0;
|
||||
TWEEN.nextId = function () {
|
||||
return TWEEN._nextId++;
|
||||
};
|
||||
|
||||
|
||||
// Include a performance.now polyfill.
|
||||
// In node.js, use process.hrtime.
|
||||
if (typeof (window) === 'undefined' && typeof (process) !== 'undefined') {
|
||||
TWEEN.now = function () {
|
||||
var time = process.hrtime();
|
||||
|
||||
// Convert [seconds, nanoseconds] to milliseconds.
|
||||
return time[0] * 1000 + time[1] / 1000000;
|
||||
};
|
||||
}
|
||||
// In a browser, use window.performance.now if it is available.
|
||||
else if (typeof (window) !== 'undefined' &&
|
||||
window.performance !== undefined &&
|
||||
window.performance.now !== undefined) {
|
||||
// This must be bound, because directly assigning this function
|
||||
// leads to an invocation exception in Chrome.
|
||||
TWEEN.now = window.performance.now.bind(window.performance);
|
||||
}
|
||||
// Use Date.now if it is available.
|
||||
else if (Date.now !== undefined) {
|
||||
TWEEN.now = Date.now;
|
||||
}
|
||||
// Otherwise, use 'new Date().getTime()'.
|
||||
else {
|
||||
TWEEN.now = function () {
|
||||
return new Date().getTime();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
TWEEN.Tween = function (object, group) {
|
||||
this._object = object;
|
||||
this._valuesStart = {};
|
||||
this._valuesEnd = {};
|
||||
this._valuesStartRepeat = {};
|
||||
this._duration = 1000;
|
||||
this._repeat = 0;
|
||||
this._repeatDelayTime = undefined;
|
||||
this._yoyo = false;
|
||||
this._isPlaying = false;
|
||||
this._reversed = false;
|
||||
this._delayTime = 0;
|
||||
this._startTime = null;
|
||||
this._easingFunction = TWEEN.Easing.Linear.None;
|
||||
this._interpolationFunction = TWEEN.Interpolation.Linear;
|
||||
this._chainedTweens = [];
|
||||
this._onStartCallback = null;
|
||||
this._onStartCallbackFired = false;
|
||||
this._onUpdateCallback = null;
|
||||
this._onCompleteCallback = null;
|
||||
this._onStopCallback = null;
|
||||
this._group = group || TWEEN;
|
||||
this._id = TWEEN.nextId();
|
||||
|
||||
};
|
||||
|
||||
TWEEN.Tween.prototype = {
|
||||
getId: function getId() {
|
||||
return this._id;
|
||||
},
|
||||
|
||||
isPlaying: function isPlaying() {
|
||||
return this._isPlaying;
|
||||
},
|
||||
|
||||
to: function to(properties, duration) {
|
||||
|
||||
this._valuesEnd = properties;
|
||||
|
||||
if (duration !== undefined) {
|
||||
this._duration = duration;
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
start: function start(time) {
|
||||
|
||||
this._group.add(this);
|
||||
|
||||
this._isPlaying = true;
|
||||
|
||||
this._onStartCallbackFired = false;
|
||||
|
||||
this._startTime = time !== undefined ? typeof time === 'string' ? TWEEN.now() + parseFloat(time) : time : TWEEN.now();
|
||||
this._startTime += this._delayTime;
|
||||
|
||||
for (var property in this._valuesEnd) {
|
||||
|
||||
// Check if an Array was provided as property value
|
||||
if (this._valuesEnd[property] instanceof Array) {
|
||||
|
||||
if (this._valuesEnd[property].length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Create a local copy of the Array with the start value at the front
|
||||
this._valuesEnd[property] = [this._object[property]].concat(this._valuesEnd[property]);
|
||||
|
||||
}
|
||||
|
||||
// If `to()` specifies a property that doesn't exist in the source object,
|
||||
// we should not set that property in the object
|
||||
if (this._object[property] === undefined) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Save the starting value.
|
||||
this._valuesStart[property] = this._object[property];
|
||||
|
||||
if ((this._valuesStart[property] instanceof Array) === false) {
|
||||
this._valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings
|
||||
}
|
||||
|
||||
this._valuesStartRepeat[property] = this._valuesStart[property] || 0;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
stop: function stop() {
|
||||
|
||||
if (!this._isPlaying) {
|
||||
return this;
|
||||
}
|
||||
|
||||
this._group.remove(this);
|
||||
this._isPlaying = false;
|
||||
|
||||
if (this._onStopCallback !== null) {
|
||||
this._onStopCallback(this._object);
|
||||
}
|
||||
|
||||
this.stopChainedTweens();
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
end: function end() {
|
||||
|
||||
this.update(this._startTime + this._duration);
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
stopChainedTweens: function stopChainedTweens() {
|
||||
|
||||
for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
|
||||
this._chainedTweens[i].stop();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
group: function group(group) {
|
||||
this._group = group;
|
||||
return this;
|
||||
},
|
||||
|
||||
delay: function delay(amount) {
|
||||
|
||||
this._delayTime = amount;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
repeat: function repeat(times) {
|
||||
|
||||
this._repeat = times;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
repeatDelay: function repeatDelay(amount) {
|
||||
|
||||
this._repeatDelayTime = amount;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
yoyo: function yoyo(yy) {
|
||||
|
||||
this._yoyo = yy;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
easing: function easing(eas) {
|
||||
|
||||
this._easingFunction = eas;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
interpolation: function interpolation(inter) {
|
||||
|
||||
this._interpolationFunction = inter;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
chain: function chain() {
|
||||
|
||||
this._chainedTweens = arguments;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
onStart: function onStart(callback) {
|
||||
|
||||
this._onStartCallback = callback;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
onUpdate: function onUpdate(callback) {
|
||||
|
||||
this._onUpdateCallback = callback;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
onComplete: function onComplete(callback) {
|
||||
|
||||
this._onCompleteCallback = callback;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
onStop: function onStop(callback) {
|
||||
|
||||
this._onStopCallback = callback;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
update: function update(time) {
|
||||
|
||||
var property;
|
||||
var elapsed;
|
||||
var value;
|
||||
|
||||
if (time < this._startTime) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this._onStartCallbackFired === false) {
|
||||
|
||||
if (this._onStartCallback !== null) {
|
||||
this._onStartCallback(this._object);
|
||||
}
|
||||
|
||||
this._onStartCallbackFired = true;
|
||||
}
|
||||
|
||||
elapsed = (time - this._startTime) / this._duration;
|
||||
elapsed = (this._duration === 0 || elapsed > 1) ? 1 : elapsed;
|
||||
|
||||
value = this._easingFunction(elapsed);
|
||||
|
||||
for (property in this._valuesEnd) {
|
||||
|
||||
// Don't update properties that do not exist in the source object
|
||||
if (this._valuesStart[property] === undefined) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var start = this._valuesStart[property] || 0;
|
||||
var end = this._valuesEnd[property];
|
||||
|
||||
if (end instanceof Array) {
|
||||
|
||||
this._object[property] = this._interpolationFunction(end, value);
|
||||
|
||||
} else {
|
||||
|
||||
// Parses relative end values with start as base (e.g.: +10, -3)
|
||||
if (typeof (end) === 'string') {
|
||||
|
||||
if (end.charAt(0) === '+' || end.charAt(0) === '-') {
|
||||
end = start + parseFloat(end);
|
||||
} else {
|
||||
end = parseFloat(end);
|
||||
}
|
||||
}
|
||||
|
||||
// Protect against non numeric properties.
|
||||
if (typeof (end) === 'number') {
|
||||
this._object[property] = start + (end - start) * value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (this._onUpdateCallback !== null) {
|
||||
this._onUpdateCallback(this._object);
|
||||
}
|
||||
|
||||
if (elapsed === 1) {
|
||||
|
||||
if (this._repeat > 0) {
|
||||
|
||||
if (isFinite(this._repeat)) {
|
||||
this._repeat--;
|
||||
}
|
||||
|
||||
// Reassign starting values, restart by making startTime = now
|
||||
for (property in this._valuesStartRepeat) {
|
||||
|
||||
if (typeof (this._valuesEnd[property]) === 'string') {
|
||||
this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(this._valuesEnd[property]);
|
||||
}
|
||||
|
||||
if (this._yoyo) {
|
||||
var tmp = this._valuesStartRepeat[property];
|
||||
|
||||
this._valuesStartRepeat[property] = this._valuesEnd[property];
|
||||
this._valuesEnd[property] = tmp;
|
||||
}
|
||||
|
||||
this._valuesStart[property] = this._valuesStartRepeat[property];
|
||||
|
||||
}
|
||||
|
||||
if (this._yoyo) {
|
||||
this._reversed = !this._reversed;
|
||||
}
|
||||
|
||||
if (this._repeatDelayTime !== undefined) {
|
||||
this._startTime = time + this._repeatDelayTime;
|
||||
} else {
|
||||
this._startTime = time + this._delayTime;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
if (this._onCompleteCallback !== null) {
|
||||
|
||||
this._onCompleteCallback(this._object);
|
||||
}
|
||||
|
||||
for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
|
||||
// Make the chained tweens start exactly at the time they should,
|
||||
// even if the `update()` method was called way past the duration of the tween
|
||||
this._chainedTweens[i].start(this._startTime + this._duration);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
TWEEN.Easing = {
|
||||
|
||||
Linear: {
|
||||
|
||||
None: function (k) {
|
||||
|
||||
return k;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Quadratic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return k * (2 - k);
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * k * k;
|
||||
}
|
||||
|
||||
return - 0.5 * (--k * (k - 2) - 1);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Cubic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k * k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return --k * k * k + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * k * k * k;
|
||||
}
|
||||
|
||||
return 0.5 * ((k -= 2) * k * k + 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Quartic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k * k * k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return 1 - (--k * k * k * k);
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * k * k * k * k;
|
||||
}
|
||||
|
||||
return - 0.5 * ((k -= 2) * k * k * k - 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Quintic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k * k * k * k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return --k * k * k * k * k + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * k * k * k * k * k;
|
||||
}
|
||||
|
||||
return 0.5 * ((k -= 2) * k * k * k * k + 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Sinusoidal: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return 1 - Math.cos(k * Math.PI / 2);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return Math.sin(k * Math.PI / 2);
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
return 0.5 * (1 - Math.cos(Math.PI * k));
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Exponential: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k === 0 ? 0 : Math.pow(1024, k - 1);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return k === 1 ? 1 : 1 - Math.pow(2, - 10 * k);
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if (k === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (k === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * Math.pow(1024, k - 1);
|
||||
}
|
||||
|
||||
return 0.5 * (- Math.pow(2, - 10 * (k - 1)) + 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Circular: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return 1 - Math.sqrt(1 - k * k);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return Math.sqrt(1 - (--k * k));
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return - 0.5 * (Math.sqrt(1 - k * k) - 1);
|
||||
}
|
||||
|
||||
return 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Elastic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
if (k === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (k === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -Math.pow(2, 10 * (k - 1)) * Math.sin((k - 1.1) * 5 * Math.PI);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
if (k === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (k === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return Math.pow(2, -10 * k) * Math.sin((k - 0.1) * 5 * Math.PI) + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if (k === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (k === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
k *= 2;
|
||||
|
||||
if (k < 1) {
|
||||
return -0.5 * Math.pow(2, 10 * (k - 1)) * Math.sin((k - 1.1) * 5 * Math.PI);
|
||||
}
|
||||
|
||||
return 0.5 * Math.pow(2, -10 * (k - 1)) * Math.sin((k - 1.1) * 5 * Math.PI) + 1;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Back: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
var s = 1.70158;
|
||||
|
||||
return k * k * ((s + 1) * k - s);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
var s = 1.70158;
|
||||
|
||||
return --k * k * ((s + 1) * k + s) + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
var s = 1.70158 * 1.525;
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * (k * k * ((s + 1) * k - s));
|
||||
}
|
||||
|
||||
return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Bounce: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return 1 - TWEEN.Easing.Bounce.Out(1 - k);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
if (k < (1 / 2.75)) {
|
||||
return 7.5625 * k * k;
|
||||
} else if (k < (2 / 2.75)) {
|
||||
return 7.5625 * (k -= (1.5 / 2.75)) * k + 0.75;
|
||||
} else if (k < (2.5 / 2.75)) {
|
||||
return 7.5625 * (k -= (2.25 / 2.75)) * k + 0.9375;
|
||||
} else {
|
||||
return 7.5625 * (k -= (2.625 / 2.75)) * k + 0.984375;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if (k < 0.5) {
|
||||
return TWEEN.Easing.Bounce.In(k * 2) * 0.5;
|
||||
}
|
||||
|
||||
return TWEEN.Easing.Bounce.Out(k * 2 - 1) * 0.5 + 0.5;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
TWEEN.Interpolation = {
|
||||
|
||||
Linear: function (v, k) {
|
||||
|
||||
var m = v.length - 1;
|
||||
var f = m * k;
|
||||
var i = Math.floor(f);
|
||||
var fn = TWEEN.Interpolation.Utils.Linear;
|
||||
|
||||
if (k < 0) {
|
||||
return fn(v[0], v[1], f);
|
||||
}
|
||||
|
||||
if (k > 1) {
|
||||
return fn(v[m], v[m - 1], m - f);
|
||||
}
|
||||
|
||||
return fn(v[i], v[i + 1 > m ? m : i + 1], f - i);
|
||||
|
||||
},
|
||||
|
||||
Bezier: function (v, k) {
|
||||
|
||||
var b = 0;
|
||||
var n = v.length - 1;
|
||||
var pw = Math.pow;
|
||||
var bn = TWEEN.Interpolation.Utils.Bernstein;
|
||||
|
||||
for (var i = 0; i <= n; i++) {
|
||||
b += pw(1 - k, n - i) * pw(k, i) * v[i] * bn(n, i);
|
||||
}
|
||||
|
||||
return b;
|
||||
|
||||
},
|
||||
|
||||
CatmullRom: function (v, k) {
|
||||
|
||||
var m = v.length - 1;
|
||||
var f = m * k;
|
||||
var i = Math.floor(f);
|
||||
var fn = TWEEN.Interpolation.Utils.CatmullRom;
|
||||
|
||||
if (v[0] === v[m]) {
|
||||
|
||||
if (k < 0) {
|
||||
i = Math.floor(f = m * (1 + k));
|
||||
}
|
||||
|
||||
return fn(v[(i - 1 + m) % m], v[i], v[(i + 1) % m], v[(i + 2) % m], f - i);
|
||||
|
||||
} else {
|
||||
|
||||
if (k < 0) {
|
||||
return v[0] - (fn(v[0], v[0], v[1], v[1], -f) - v[0]);
|
||||
}
|
||||
|
||||
if (k > 1) {
|
||||
return v[m] - (fn(v[m], v[m], v[m - 1], v[m - 1], f - m) - v[m]);
|
||||
}
|
||||
|
||||
return fn(v[i ? i - 1 : 0], v[i], v[m < i + 1 ? m : i + 1], v[m < i + 2 ? m : i + 2], f - i);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Utils: {
|
||||
|
||||
Linear: function (p0, p1, t) {
|
||||
|
||||
return (p1 - p0) * t + p0;
|
||||
|
||||
},
|
||||
|
||||
Bernstein: function (n, i) {
|
||||
|
||||
var fc = TWEEN.Interpolation.Utils.Factorial;
|
||||
|
||||
return fc(n) / fc(i) / fc(n - i);
|
||||
|
||||
},
|
||||
|
||||
Factorial: (function () {
|
||||
|
||||
var a = [1];
|
||||
|
||||
return function (n) {
|
||||
|
||||
var s = 1;
|
||||
|
||||
if (a[n]) {
|
||||
return a[n];
|
||||
}
|
||||
|
||||
for (var i = n; i > 1; i--) {
|
||||
s *= i;
|
||||
}
|
||||
|
||||
a[n] = s;
|
||||
return s;
|
||||
|
||||
};
|
||||
|
||||
})(),
|
||||
|
||||
CatmullRom: function (p0, p1, p2, p3, t) {
|
||||
|
||||
var v0 = (p2 - p0) * 0.5;
|
||||
var v1 = (p3 - p1) * 0.5;
|
||||
var t2 = t * t;
|
||||
var t3 = t * t2;
|
||||
|
||||
return (2 * p1 - 2 * p2 + v0 + v1) * t3 + (- 3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// UMD (Universal Module Definition)
|
||||
(function (root) {
|
||||
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
|
||||
// AMD
|
||||
define([], function () {
|
||||
return TWEEN;
|
||||
});
|
||||
|
||||
} else if (typeof module !== 'undefined' && typeof exports === 'object') {
|
||||
|
||||
// Node.js
|
||||
module.exports = TWEEN;
|
||||
|
||||
} else if (root !== undefined) {
|
||||
|
||||
// Global variable
|
||||
root.TWEEN = TWEEN;
|
||||
|
||||
}
|
||||
|
||||
})(this);
|
33
lib/ammo.js
Normal file
31
lib/ammo.module.js
Normal file
672
lib/animation/AnimationAction.js
Normal file
@ -0,0 +1,672 @@
|
||||
import { WrapAroundEnding, ZeroCurvatureEnding, ZeroSlopeEnding, LoopPingPong, LoopOnce, LoopRepeat } from '../constants.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* Action provided by AnimationMixer for scheduling clip playback on specific
|
||||
* objects.
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*
|
||||
*/
|
||||
|
||||
function AnimationAction( mixer, clip, localRoot ) {
|
||||
|
||||
this._mixer = mixer;
|
||||
this._clip = clip;
|
||||
this._localRoot = localRoot || null;
|
||||
|
||||
var tracks = clip.tracks,
|
||||
nTracks = tracks.length,
|
||||
interpolants = new Array( nTracks );
|
||||
|
||||
var interpolantSettings = {
|
||||
endingStart: ZeroCurvatureEnding,
|
||||
endingEnd: ZeroCurvatureEnding
|
||||
};
|
||||
|
||||
for ( var i = 0; i !== nTracks; ++ i ) {
|
||||
|
||||
var interpolant = tracks[ i ].createInterpolant( null );
|
||||
interpolants[ i ] = interpolant;
|
||||
interpolant.settings = interpolantSettings;
|
||||
|
||||
}
|
||||
|
||||
this._interpolantSettings = interpolantSettings;
|
||||
|
||||
this._interpolants = interpolants; // bound by the mixer
|
||||
|
||||
// inside: PropertyMixer (managed by the mixer)
|
||||
this._propertyBindings = new Array( nTracks );
|
||||
|
||||
this._cacheIndex = null; // for the memory manager
|
||||
this._byClipCacheIndex = null; // for the memory manager
|
||||
|
||||
this._timeScaleInterpolant = null;
|
||||
this._weightInterpolant = null;
|
||||
|
||||
this.loop = LoopRepeat;
|
||||
this._loopCount = - 1;
|
||||
|
||||
// global mixer time when the action is to be started
|
||||
// it's set back to 'null' upon start of the action
|
||||
this._startTime = null;
|
||||
|
||||
// scaled local time of the action
|
||||
// gets clamped or wrapped to 0..clip.duration according to loop
|
||||
this.time = 0;
|
||||
|
||||
this.timeScale = 1;
|
||||
this._effectiveTimeScale = 1;
|
||||
|
||||
this.weight = 1;
|
||||
this._effectiveWeight = 1;
|
||||
|
||||
this.repetitions = Infinity; // no. of repetitions when looping
|
||||
|
||||
this.paused = false; // true -> zero effective time scale
|
||||
this.enabled = true; // false -> zero effective weight
|
||||
|
||||
this.clampWhenFinished = false; // keep feeding the last frame?
|
||||
|
||||
this.zeroSlopeAtStart = true; // for smooth interpolation w/o separate
|
||||
this.zeroSlopeAtEnd = true; // clips for start, loop and end
|
||||
|
||||
}
|
||||
|
||||
Object.assign( AnimationAction.prototype, {
|
||||
|
||||
// State & Scheduling
|
||||
|
||||
play: function () {
|
||||
|
||||
this._mixer._activateAction( this );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
stop: function () {
|
||||
|
||||
this._mixer._deactivateAction( this );
|
||||
|
||||
return this.reset();
|
||||
|
||||
},
|
||||
|
||||
reset: function () {
|
||||
|
||||
this.paused = false;
|
||||
this.enabled = true;
|
||||
|
||||
this.time = 0; // restart clip
|
||||
this._loopCount = - 1; // forget previous loops
|
||||
this._startTime = null; // forget scheduling
|
||||
|
||||
return this.stopFading().stopWarping();
|
||||
|
||||
},
|
||||
|
||||
isRunning: function () {
|
||||
|
||||
return this.enabled && ! this.paused && this.timeScale !== 0 &&
|
||||
this._startTime === null && this._mixer._isActiveAction( this );
|
||||
|
||||
},
|
||||
|
||||
// return true when play has been called
|
||||
isScheduled: function () {
|
||||
|
||||
return this._mixer._isActiveAction( this );
|
||||
|
||||
},
|
||||
|
||||
startAt: function ( time ) {
|
||||
|
||||
this._startTime = time;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setLoop: function ( mode, repetitions ) {
|
||||
|
||||
this.loop = mode;
|
||||
this.repetitions = repetitions;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
// Weight
|
||||
|
||||
// set the weight stopping any scheduled fading
|
||||
// although .enabled = false yields an effective weight of zero, this
|
||||
// method does *not* change .enabled, because it would be confusing
|
||||
setEffectiveWeight: function ( weight ) {
|
||||
|
||||
this.weight = weight;
|
||||
|
||||
// note: same logic as when updated at runtime
|
||||
this._effectiveWeight = this.enabled ? weight : 0;
|
||||
|
||||
return this.stopFading();
|
||||
|
||||
},
|
||||
|
||||
// return the weight considering fading and .enabled
|
||||
getEffectiveWeight: function () {
|
||||
|
||||
return this._effectiveWeight;
|
||||
|
||||
},
|
||||
|
||||
fadeIn: function ( duration ) {
|
||||
|
||||
return this._scheduleFading( duration, 0, 1 );
|
||||
|
||||
},
|
||||
|
||||
fadeOut: function ( duration ) {
|
||||
|
||||
return this._scheduleFading( duration, 1, 0 );
|
||||
|
||||
},
|
||||
|
||||
crossFadeFrom: function ( fadeOutAction, duration, warp ) {
|
||||
|
||||
fadeOutAction.fadeOut( duration );
|
||||
this.fadeIn( duration );
|
||||
|
||||
if ( warp ) {
|
||||
|
||||
var fadeInDuration = this._clip.duration,
|
||||
fadeOutDuration = fadeOutAction._clip.duration,
|
||||
|
||||
startEndRatio = fadeOutDuration / fadeInDuration,
|
||||
endStartRatio = fadeInDuration / fadeOutDuration;
|
||||
|
||||
fadeOutAction.warp( 1.0, startEndRatio, duration );
|
||||
this.warp( endStartRatio, 1.0, duration );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
crossFadeTo: function ( fadeInAction, duration, warp ) {
|
||||
|
||||
return fadeInAction.crossFadeFrom( this, duration, warp );
|
||||
|
||||
},
|
||||
|
||||
stopFading: function () {
|
||||
|
||||
var weightInterpolant = this._weightInterpolant;
|
||||
|
||||
if ( weightInterpolant !== null ) {
|
||||
|
||||
this._weightInterpolant = null;
|
||||
this._mixer._takeBackControlInterpolant( weightInterpolant );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
// Time Scale Control
|
||||
|
||||
// set the time scale stopping any scheduled warping
|
||||
// although .paused = true yields an effective time scale of zero, this
|
||||
// method does *not* change .paused, because it would be confusing
|
||||
setEffectiveTimeScale: function ( timeScale ) {
|
||||
|
||||
this.timeScale = timeScale;
|
||||
this._effectiveTimeScale = this.paused ? 0 : timeScale;
|
||||
|
||||
return this.stopWarping();
|
||||
|
||||
},
|
||||
|
||||
// return the time scale considering warping and .paused
|
||||
getEffectiveTimeScale: function () {
|
||||
|
||||
return this._effectiveTimeScale;
|
||||
|
||||
},
|
||||
|
||||
setDuration: function ( duration ) {
|
||||
|
||||
this.timeScale = this._clip.duration / duration;
|
||||
|
||||
return this.stopWarping();
|
||||
|
||||
},
|
||||
|
||||
syncWith: function ( action ) {
|
||||
|
||||
this.time = action.time;
|
||||
this.timeScale = action.timeScale;
|
||||
|
||||
return this.stopWarping();
|
||||
|
||||
},
|
||||
|
||||
halt: function ( duration ) {
|
||||
|
||||
return this.warp( this._effectiveTimeScale, 0, duration );
|
||||
|
||||
},
|
||||
|
||||
warp: function ( startTimeScale, endTimeScale, duration ) {
|
||||
|
||||
var mixer = this._mixer, now = mixer.time,
|
||||
interpolant = this._timeScaleInterpolant,
|
||||
|
||||
timeScale = this.timeScale;
|
||||
|
||||
if ( interpolant === null ) {
|
||||
|
||||
interpolant = mixer._lendControlInterpolant();
|
||||
this._timeScaleInterpolant = interpolant;
|
||||
|
||||
}
|
||||
|
||||
var times = interpolant.parameterPositions,
|
||||
values = interpolant.sampleValues;
|
||||
|
||||
times[ 0 ] = now;
|
||||
times[ 1 ] = now + duration;
|
||||
|
||||
values[ 0 ] = startTimeScale / timeScale;
|
||||
values[ 1 ] = endTimeScale / timeScale;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
stopWarping: function () {
|
||||
|
||||
var timeScaleInterpolant = this._timeScaleInterpolant;
|
||||
|
||||
if ( timeScaleInterpolant !== null ) {
|
||||
|
||||
this._timeScaleInterpolant = null;
|
||||
this._mixer._takeBackControlInterpolant( timeScaleInterpolant );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
// Object Accessors
|
||||
|
||||
getMixer: function () {
|
||||
|
||||
return this._mixer;
|
||||
|
||||
},
|
||||
|
||||
getClip: function () {
|
||||
|
||||
return this._clip;
|
||||
|
||||
},
|
||||
|
||||
getRoot: function () {
|
||||
|
||||
return this._localRoot || this._mixer._root;
|
||||
|
||||
},
|
||||
|
||||
// Interna
|
||||
|
||||
_update: function ( time, deltaTime, timeDirection, accuIndex ) {
|
||||
|
||||
// called by the mixer
|
||||
|
||||
if ( ! this.enabled ) {
|
||||
|
||||
// call ._updateWeight() to update ._effectiveWeight
|
||||
|
||||
this._updateWeight( time );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
var startTime = this._startTime;
|
||||
|
||||
if ( startTime !== null ) {
|
||||
|
||||
// check for scheduled start of action
|
||||
|
||||
var timeRunning = ( time - startTime ) * timeDirection;
|
||||
if ( timeRunning < 0 || timeDirection === 0 ) {
|
||||
|
||||
return; // yet to come / don't decide when delta = 0
|
||||
|
||||
}
|
||||
|
||||
// start
|
||||
|
||||
this._startTime = null; // unschedule
|
||||
deltaTime = timeDirection * timeRunning;
|
||||
|
||||
}
|
||||
|
||||
// apply time scale and advance time
|
||||
|
||||
deltaTime *= this._updateTimeScale( time );
|
||||
var clipTime = this._updateTime( deltaTime );
|
||||
|
||||
// note: _updateTime may disable the action resulting in
|
||||
// an effective weight of 0
|
||||
|
||||
var weight = this._updateWeight( time );
|
||||
|
||||
if ( weight > 0 ) {
|
||||
|
||||
var interpolants = this._interpolants;
|
||||
var propertyMixers = this._propertyBindings;
|
||||
|
||||
for ( var j = 0, m = interpolants.length; j !== m; ++ j ) {
|
||||
|
||||
interpolants[ j ].evaluate( clipTime );
|
||||
propertyMixers[ j ].accumulate( accuIndex, weight );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_updateWeight: function ( time ) {
|
||||
|
||||
var weight = 0;
|
||||
|
||||
if ( this.enabled ) {
|
||||
|
||||
weight = this.weight;
|
||||
var interpolant = this._weightInterpolant;
|
||||
|
||||
if ( interpolant !== null ) {
|
||||
|
||||
var interpolantValue = interpolant.evaluate( time )[ 0 ];
|
||||
|
||||
weight *= interpolantValue;
|
||||
|
||||
if ( time > interpolant.parameterPositions[ 1 ] ) {
|
||||
|
||||
this.stopFading();
|
||||
|
||||
if ( interpolantValue === 0 ) {
|
||||
|
||||
// faded out, disable
|
||||
this.enabled = false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this._effectiveWeight = weight;
|
||||
return weight;
|
||||
|
||||
},
|
||||
|
||||
_updateTimeScale: function ( time ) {
|
||||
|
||||
var timeScale = 0;
|
||||
|
||||
if ( ! this.paused ) {
|
||||
|
||||
timeScale = this.timeScale;
|
||||
|
||||
var interpolant = this._timeScaleInterpolant;
|
||||
|
||||
if ( interpolant !== null ) {
|
||||
|
||||
var interpolantValue = interpolant.evaluate( time )[ 0 ];
|
||||
|
||||
timeScale *= interpolantValue;
|
||||
|
||||
if ( time > interpolant.parameterPositions[ 1 ] ) {
|
||||
|
||||
this.stopWarping();
|
||||
|
||||
if ( timeScale === 0 ) {
|
||||
|
||||
// motion has halted, pause
|
||||
this.paused = true;
|
||||
|
||||
} else {
|
||||
|
||||
// warp done - apply final time scale
|
||||
this.timeScale = timeScale;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this._effectiveTimeScale = timeScale;
|
||||
return timeScale;
|
||||
|
||||
},
|
||||
|
||||
_updateTime: function ( deltaTime ) {
|
||||
|
||||
var time = this.time + deltaTime;
|
||||
var duration = this._clip.duration;
|
||||
var loop = this.loop;
|
||||
var loopCount = this._loopCount;
|
||||
|
||||
var pingPong = ( loop === LoopPingPong );
|
||||
|
||||
if ( deltaTime === 0 ) {
|
||||
|
||||
if ( loopCount === - 1 ) return time;
|
||||
|
||||
return ( pingPong && ( loopCount & 1 ) === 1 ) ? duration - time : time;
|
||||
|
||||
}
|
||||
|
||||
if ( loop === LoopOnce ) {
|
||||
|
||||
if ( loopCount === - 1 ) {
|
||||
|
||||
// just started
|
||||
|
||||
this._loopCount = 0;
|
||||
this._setEndings( true, true, false );
|
||||
|
||||
}
|
||||
|
||||
handle_stop: {
|
||||
|
||||
if ( time >= duration ) {
|
||||
|
||||
time = duration;
|
||||
|
||||
} else if ( time < 0 ) {
|
||||
|
||||
time = 0;
|
||||
|
||||
} else break handle_stop;
|
||||
|
||||
if ( this.clampWhenFinished ) this.paused = true;
|
||||
else this.enabled = false;
|
||||
|
||||
this._mixer.dispatchEvent( {
|
||||
type: 'finished', action: this,
|
||||
direction: deltaTime < 0 ? - 1 : 1
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
} else { // repetitive Repeat or PingPong
|
||||
|
||||
if ( loopCount === - 1 ) {
|
||||
|
||||
// just started
|
||||
|
||||
if ( deltaTime >= 0 ) {
|
||||
|
||||
loopCount = 0;
|
||||
|
||||
this._setEndings( true, this.repetitions === 0, pingPong );
|
||||
|
||||
} else {
|
||||
|
||||
// when looping in reverse direction, the initial
|
||||
// transition through zero counts as a repetition,
|
||||
// so leave loopCount at -1
|
||||
|
||||
this._setEndings( this.repetitions === 0, true, pingPong );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( time >= duration || time < 0 ) {
|
||||
|
||||
// wrap around
|
||||
|
||||
var loopDelta = Math.floor( time / duration ); // signed
|
||||
time -= duration * loopDelta;
|
||||
|
||||
loopCount += Math.abs( loopDelta );
|
||||
|
||||
var pending = this.repetitions - loopCount;
|
||||
|
||||
if ( pending <= 0 ) {
|
||||
|
||||
// have to stop (switch state, clamp time, fire event)
|
||||
|
||||
if ( this.clampWhenFinished ) this.paused = true;
|
||||
else this.enabled = false;
|
||||
|
||||
time = deltaTime > 0 ? duration : 0;
|
||||
|
||||
this._mixer.dispatchEvent( {
|
||||
type: 'finished', action: this,
|
||||
direction: deltaTime > 0 ? 1 : - 1
|
||||
} );
|
||||
|
||||
} else {
|
||||
|
||||
// keep running
|
||||
|
||||
if ( pending === 1 ) {
|
||||
|
||||
// entering the last round
|
||||
|
||||
var atStart = deltaTime < 0;
|
||||
this._setEndings( atStart, ! atStart, pingPong );
|
||||
|
||||
} else {
|
||||
|
||||
this._setEndings( false, false, pingPong );
|
||||
|
||||
}
|
||||
|
||||
this._loopCount = loopCount;
|
||||
|
||||
this._mixer.dispatchEvent( {
|
||||
type: 'loop', action: this, loopDelta: loopDelta
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( pingPong && ( loopCount & 1 ) === 1 ) {
|
||||
|
||||
// invert time for the "pong round"
|
||||
|
||||
this.time = time;
|
||||
return duration - time;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.time = time;
|
||||
return time;
|
||||
|
||||
},
|
||||
|
||||
_setEndings: function ( atStart, atEnd, pingPong ) {
|
||||
|
||||
var settings = this._interpolantSettings;
|
||||
|
||||
if ( pingPong ) {
|
||||
|
||||
settings.endingStart = ZeroSlopeEnding;
|
||||
settings.endingEnd = ZeroSlopeEnding;
|
||||
|
||||
} else {
|
||||
|
||||
// assuming for LoopOnce atStart == atEnd == true
|
||||
|
||||
if ( atStart ) {
|
||||
|
||||
settings.endingStart = this.zeroSlopeAtStart ? ZeroSlopeEnding : ZeroCurvatureEnding;
|
||||
|
||||
} else {
|
||||
|
||||
settings.endingStart = WrapAroundEnding;
|
||||
|
||||
}
|
||||
|
||||
if ( atEnd ) {
|
||||
|
||||
settings.endingEnd = this.zeroSlopeAtEnd ? ZeroSlopeEnding : ZeroCurvatureEnding;
|
||||
|
||||
} else {
|
||||
|
||||
settings.endingEnd = WrapAroundEnding;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_scheduleFading: function ( duration, weightNow, weightThen ) {
|
||||
|
||||
var mixer = this._mixer, now = mixer.time,
|
||||
interpolant = this._weightInterpolant;
|
||||
|
||||
if ( interpolant === null ) {
|
||||
|
||||
interpolant = mixer._lendControlInterpolant();
|
||||
this._weightInterpolant = interpolant;
|
||||
|
||||
}
|
||||
|
||||
var times = interpolant.parameterPositions,
|
||||
values = interpolant.sampleValues;
|
||||
|
||||
times[ 0 ] = now; values[ 0 ] = weightNow;
|
||||
times[ 1 ] = now + duration; values[ 1 ] = weightThen;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { AnimationAction };
|
453
lib/animation/AnimationClip.js
Normal file
@ -0,0 +1,453 @@
|
||||
import { AnimationUtils } from './AnimationUtils.js';
|
||||
import { KeyframeTrack } from './KeyframeTrack.js';
|
||||
import { BooleanKeyframeTrack } from './tracks/BooleanKeyframeTrack.js';
|
||||
import { ColorKeyframeTrack } from './tracks/ColorKeyframeTrack.js';
|
||||
import { NumberKeyframeTrack } from './tracks/NumberKeyframeTrack.js';
|
||||
import { QuaternionKeyframeTrack } from './tracks/QuaternionKeyframeTrack.js';
|
||||
import { StringKeyframeTrack } from './tracks/StringKeyframeTrack.js';
|
||||
import { VectorKeyframeTrack } from './tracks/VectorKeyframeTrack.js';
|
||||
import { _Math } from '../math/Math.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* Reusable set of Tracks that represent an animation.
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
*/
|
||||
|
||||
function AnimationClip( name, duration, tracks ) {
|
||||
|
||||
this.name = name;
|
||||
this.tracks = tracks;
|
||||
this.duration = ( duration !== undefined ) ? duration : - 1;
|
||||
|
||||
this.uuid = _Math.generateUUID();
|
||||
|
||||
// this means it should figure out its duration by scanning the tracks
|
||||
if ( this.duration < 0 ) {
|
||||
|
||||
this.resetDuration();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function getTrackTypeForValueTypeName( typeName ) {
|
||||
|
||||
switch ( typeName.toLowerCase() ) {
|
||||
|
||||
case 'scalar':
|
||||
case 'double':
|
||||
case 'float':
|
||||
case 'number':
|
||||
case 'integer':
|
||||
|
||||
return NumberKeyframeTrack;
|
||||
|
||||
case 'vector':
|
||||
case 'vector2':
|
||||
case 'vector3':
|
||||
case 'vector4':
|
||||
|
||||
return VectorKeyframeTrack;
|
||||
|
||||
case 'color':
|
||||
|
||||
return ColorKeyframeTrack;
|
||||
|
||||
case 'quaternion':
|
||||
|
||||
return QuaternionKeyframeTrack;
|
||||
|
||||
case 'bool':
|
||||
case 'boolean':
|
||||
|
||||
return BooleanKeyframeTrack;
|
||||
|
||||
case 'string':
|
||||
|
||||
return StringKeyframeTrack;
|
||||
|
||||
}
|
||||
|
||||
throw new Error( 'THREE.KeyframeTrack: Unsupported typeName: ' + typeName );
|
||||
|
||||
}
|
||||
|
||||
function parseKeyframeTrack( json ) {
|
||||
|
||||
if ( json.type === undefined ) {
|
||||
|
||||
throw new Error( 'THREE.KeyframeTrack: track type undefined, can not parse' );
|
||||
|
||||
}
|
||||
|
||||
var trackType = getTrackTypeForValueTypeName( json.type );
|
||||
|
||||
if ( json.times === undefined ) {
|
||||
|
||||
var times = [], values = [];
|
||||
|
||||
AnimationUtils.flattenJSON( json.keys, times, values, 'value' );
|
||||
|
||||
json.times = times;
|
||||
json.values = values;
|
||||
|
||||
}
|
||||
|
||||
// derived classes can define a static parse method
|
||||
if ( trackType.parse !== undefined ) {
|
||||
|
||||
return trackType.parse( json );
|
||||
|
||||
} else {
|
||||
|
||||
// by default, we assume a constructor compatible with the base
|
||||
return new trackType( json.name, json.times, json.values, json.interpolation );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Object.assign( AnimationClip, {
|
||||
|
||||
parse: function ( json ) {
|
||||
|
||||
var tracks = [],
|
||||
jsonTracks = json.tracks,
|
||||
frameTime = 1.0 / ( json.fps || 1.0 );
|
||||
|
||||
for ( var i = 0, n = jsonTracks.length; i !== n; ++ i ) {
|
||||
|
||||
tracks.push( parseKeyframeTrack( jsonTracks[ i ] ).scale( frameTime ) );
|
||||
|
||||
}
|
||||
|
||||
return new AnimationClip( json.name, json.duration, tracks );
|
||||
|
||||
},
|
||||
|
||||
toJSON: function ( clip ) {
|
||||
|
||||
var tracks = [],
|
||||
clipTracks = clip.tracks;
|
||||
|
||||
var json = {
|
||||
|
||||
'name': clip.name,
|
||||
'duration': clip.duration,
|
||||
'tracks': tracks,
|
||||
'uuid': clip.uuid
|
||||
|
||||
};
|
||||
|
||||
for ( var i = 0, n = clipTracks.length; i !== n; ++ i ) {
|
||||
|
||||
tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) );
|
||||
|
||||
}
|
||||
|
||||
return json;
|
||||
|
||||
},
|
||||
|
||||
CreateFromMorphTargetSequence: function ( name, morphTargetSequence, fps, noLoop ) {
|
||||
|
||||
var numMorphTargets = morphTargetSequence.length;
|
||||
var tracks = [];
|
||||
|
||||
for ( var i = 0; i < numMorphTargets; i ++ ) {
|
||||
|
||||
var times = [];
|
||||
var values = [];
|
||||
|
||||
times.push(
|
||||
( i + numMorphTargets - 1 ) % numMorphTargets,
|
||||
i,
|
||||
( i + 1 ) % numMorphTargets );
|
||||
|
||||
values.push( 0, 1, 0 );
|
||||
|
||||
var order = AnimationUtils.getKeyframeOrder( times );
|
||||
times = AnimationUtils.sortedArray( times, 1, order );
|
||||
values = AnimationUtils.sortedArray( values, 1, order );
|
||||
|
||||
// if there is a key at the first frame, duplicate it as the
|
||||
// last frame as well for perfect loop.
|
||||
if ( ! noLoop && times[ 0 ] === 0 ) {
|
||||
|
||||
times.push( numMorphTargets );
|
||||
values.push( values[ 0 ] );
|
||||
|
||||
}
|
||||
|
||||
tracks.push(
|
||||
new NumberKeyframeTrack(
|
||||
'.morphTargetInfluences[' + morphTargetSequence[ i ].name + ']',
|
||||
times, values
|
||||
).scale( 1.0 / fps ) );
|
||||
|
||||
}
|
||||
|
||||
return new AnimationClip( name, - 1, tracks );
|
||||
|
||||
},
|
||||
|
||||
findByName: function ( objectOrClipArray, name ) {
|
||||
|
||||
var clipArray = objectOrClipArray;
|
||||
|
||||
if ( ! Array.isArray( objectOrClipArray ) ) {
|
||||
|
||||
var o = objectOrClipArray;
|
||||
clipArray = o.geometry && o.geometry.animations || o.animations;
|
||||
|
||||
}
|
||||
|
||||
for ( var i = 0; i < clipArray.length; i ++ ) {
|
||||
|
||||
if ( clipArray[ i ].name === name ) {
|
||||
|
||||
return clipArray[ i ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
},
|
||||
|
||||
CreateClipsFromMorphTargetSequences: function ( morphTargets, fps, noLoop ) {
|
||||
|
||||
var animationToMorphTargets = {};
|
||||
|
||||
// tested with https://regex101.com/ on trick sequences
|
||||
// such flamingo_flyA_003, flamingo_run1_003, crdeath0059
|
||||
var pattern = /^([\w-]*?)([\d]+)$/;
|
||||
|
||||
// sort morph target names into animation groups based
|
||||
// patterns like Walk_001, Walk_002, Run_001, Run_002
|
||||
for ( var i = 0, il = morphTargets.length; i < il; i ++ ) {
|
||||
|
||||
var morphTarget = morphTargets[ i ];
|
||||
var parts = morphTarget.name.match( pattern );
|
||||
|
||||
if ( parts && parts.length > 1 ) {
|
||||
|
||||
var name = parts[ 1 ];
|
||||
|
||||
var animationMorphTargets = animationToMorphTargets[ name ];
|
||||
if ( ! animationMorphTargets ) {
|
||||
|
||||
animationToMorphTargets[ name ] = animationMorphTargets = [];
|
||||
|
||||
}
|
||||
|
||||
animationMorphTargets.push( morphTarget );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var clips = [];
|
||||
|
||||
for ( var name in animationToMorphTargets ) {
|
||||
|
||||
clips.push( AnimationClip.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) );
|
||||
|
||||
}
|
||||
|
||||
return clips;
|
||||
|
||||
},
|
||||
|
||||
// parse the animation.hierarchy format
|
||||
parseAnimation: function ( animation, bones ) {
|
||||
|
||||
if ( ! animation ) {
|
||||
|
||||
console.error( 'THREE.AnimationClip: No animation in JSONLoader data.' );
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
var addNonemptyTrack = function ( trackType, trackName, animationKeys, propertyName, destTracks ) {
|
||||
|
||||
// only return track if there are actually keys.
|
||||
if ( animationKeys.length !== 0 ) {
|
||||
|
||||
var times = [];
|
||||
var values = [];
|
||||
|
||||
AnimationUtils.flattenJSON( animationKeys, times, values, propertyName );
|
||||
|
||||
// empty keys are filtered out, so check again
|
||||
if ( times.length !== 0 ) {
|
||||
|
||||
destTracks.push( new trackType( trackName, times, values ) );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
var tracks = [];
|
||||
|
||||
var clipName = animation.name || 'default';
|
||||
// automatic length determination in AnimationClip.
|
||||
var duration = animation.length || - 1;
|
||||
var fps = animation.fps || 30;
|
||||
|
||||
var hierarchyTracks = animation.hierarchy || [];
|
||||
|
||||
for ( var h = 0; h < hierarchyTracks.length; h ++ ) {
|
||||
|
||||
var animationKeys = hierarchyTracks[ h ].keys;
|
||||
|
||||
// skip empty tracks
|
||||
if ( ! animationKeys || animationKeys.length === 0 ) continue;
|
||||
|
||||
// process morph targets
|
||||
if ( animationKeys[ 0 ].morphTargets ) {
|
||||
|
||||
// figure out all morph targets used in this track
|
||||
var morphTargetNames = {};
|
||||
|
||||
for ( var k = 0; k < animationKeys.length; k ++ ) {
|
||||
|
||||
if ( animationKeys[ k ].morphTargets ) {
|
||||
|
||||
for ( var m = 0; m < animationKeys[ k ].morphTargets.length; m ++ ) {
|
||||
|
||||
morphTargetNames[ animationKeys[ k ].morphTargets[ m ] ] = - 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// create a track for each morph target with all zero
|
||||
// morphTargetInfluences except for the keys in which
|
||||
// the morphTarget is named.
|
||||
for ( var morphTargetName in morphTargetNames ) {
|
||||
|
||||
var times = [];
|
||||
var values = [];
|
||||
|
||||
for ( var m = 0; m !== animationKeys[ k ].morphTargets.length; ++ m ) {
|
||||
|
||||
var animationKey = animationKeys[ k ];
|
||||
|
||||
times.push( animationKey.time );
|
||||
values.push( ( animationKey.morphTarget === morphTargetName ) ? 1 : 0 );
|
||||
|
||||
}
|
||||
|
||||
tracks.push( new NumberKeyframeTrack( '.morphTargetInfluence[' + morphTargetName + ']', times, values ) );
|
||||
|
||||
}
|
||||
|
||||
duration = morphTargetNames.length * ( fps || 1.0 );
|
||||
|
||||
} else {
|
||||
|
||||
// ...assume skeletal animation
|
||||
|
||||
var boneName = '.bones[' + bones[ h ].name + ']';
|
||||
|
||||
addNonemptyTrack(
|
||||
VectorKeyframeTrack, boneName + '.position',
|
||||
animationKeys, 'pos', tracks );
|
||||
|
||||
addNonemptyTrack(
|
||||
QuaternionKeyframeTrack, boneName + '.quaternion',
|
||||
animationKeys, 'rot', tracks );
|
||||
|
||||
addNonemptyTrack(
|
||||
VectorKeyframeTrack, boneName + '.scale',
|
||||
animationKeys, 'scl', tracks );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( tracks.length === 0 ) {
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
var clip = new AnimationClip( clipName, duration, tracks );
|
||||
|
||||
return clip;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
Object.assign( AnimationClip.prototype, {
|
||||
|
||||
resetDuration: function () {
|
||||
|
||||
var tracks = this.tracks, duration = 0;
|
||||
|
||||
for ( var i = 0, n = tracks.length; i !== n; ++ i ) {
|
||||
|
||||
var track = this.tracks[ i ];
|
||||
|
||||
duration = Math.max( duration, track.times[ track.times.length - 1 ] );
|
||||
|
||||
}
|
||||
|
||||
this.duration = duration;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
trim: function () {
|
||||
|
||||
for ( var i = 0; i < this.tracks.length; i ++ ) {
|
||||
|
||||
this.tracks[ i ].trim( 0, this.duration );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
validate: function () {
|
||||
|
||||
var valid = true;
|
||||
|
||||
for ( var i = 0; i < this.tracks.length; i ++ ) {
|
||||
|
||||
valid = valid && this.tracks[ i ].validate();
|
||||
|
||||
}
|
||||
|
||||
return valid;
|
||||
|
||||
},
|
||||
|
||||
optimize: function () {
|
||||
|
||||
for ( var i = 0; i < this.tracks.length; i ++ ) {
|
||||
|
||||
this.tracks[ i ].optimize();
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { AnimationClip };
|
761
lib/animation/AnimationMixer.js
Normal file
@ -0,0 +1,761 @@
|
||||
import { AnimationAction } from './AnimationAction.js';
|
||||
import { EventDispatcher } from '../core/EventDispatcher.js';
|
||||
import { LinearInterpolant } from '../math/interpolants/LinearInterpolant.js';
|
||||
import { PropertyBinding } from './PropertyBinding.js';
|
||||
import { PropertyMixer } from './PropertyMixer.js';
|
||||
import { AnimationClip } from './AnimationClip.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* Player for AnimationClips.
|
||||
*
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function AnimationMixer( root ) {
|
||||
|
||||
this._root = root;
|
||||
this._initMemoryManager();
|
||||
this._accuIndex = 0;
|
||||
|
||||
this.time = 0;
|
||||
|
||||
this.timeScale = 1.0;
|
||||
|
||||
}
|
||||
|
||||
AnimationMixer.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
|
||||
|
||||
constructor: AnimationMixer,
|
||||
|
||||
_bindAction: function ( action, prototypeAction ) {
|
||||
|
||||
var root = action._localRoot || this._root,
|
||||
tracks = action._clip.tracks,
|
||||
nTracks = tracks.length,
|
||||
bindings = action._propertyBindings,
|
||||
interpolants = action._interpolants,
|
||||
rootUuid = root.uuid,
|
||||
bindingsByRoot = this._bindingsByRootAndName,
|
||||
bindingsByName = bindingsByRoot[ rootUuid ];
|
||||
|
||||
if ( bindingsByName === undefined ) {
|
||||
|
||||
bindingsByName = {};
|
||||
bindingsByRoot[ rootUuid ] = bindingsByName;
|
||||
|
||||
}
|
||||
|
||||
for ( var i = 0; i !== nTracks; ++ i ) {
|
||||
|
||||
var track = tracks[ i ],
|
||||
trackName = track.name,
|
||||
binding = bindingsByName[ trackName ];
|
||||
|
||||
if ( binding !== undefined ) {
|
||||
|
||||
bindings[ i ] = binding;
|
||||
|
||||
} else {
|
||||
|
||||
binding = bindings[ i ];
|
||||
|
||||
if ( binding !== undefined ) {
|
||||
|
||||
// existing binding, make sure the cache knows
|
||||
|
||||
if ( binding._cacheIndex === null ) {
|
||||
|
||||
++ binding.referenceCount;
|
||||
this._addInactiveBinding( binding, rootUuid, trackName );
|
||||
|
||||
}
|
||||
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
var path = prototypeAction && prototypeAction.
|
||||
_propertyBindings[ i ].binding.parsedPath;
|
||||
|
||||
binding = new PropertyMixer(
|
||||
PropertyBinding.create( root, trackName, path ),
|
||||
track.ValueTypeName, track.getValueSize() );
|
||||
|
||||
++ binding.referenceCount;
|
||||
this._addInactiveBinding( binding, rootUuid, trackName );
|
||||
|
||||
bindings[ i ] = binding;
|
||||
|
||||
}
|
||||
|
||||
interpolants[ i ].resultBuffer = binding.buffer;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_activateAction: function ( action ) {
|
||||
|
||||
if ( ! this._isActiveAction( action ) ) {
|
||||
|
||||
if ( action._cacheIndex === null ) {
|
||||
|
||||
// this action has been forgotten by the cache, but the user
|
||||
// appears to be still using it -> rebind
|
||||
|
||||
var rootUuid = ( action._localRoot || this._root ).uuid,
|
||||
clipUuid = action._clip.uuid,
|
||||
actionsForClip = this._actionsByClip[ clipUuid ];
|
||||
|
||||
this._bindAction( action,
|
||||
actionsForClip && actionsForClip.knownActions[ 0 ] );
|
||||
|
||||
this._addInactiveAction( action, clipUuid, rootUuid );
|
||||
|
||||
}
|
||||
|
||||
var bindings = action._propertyBindings;
|
||||
|
||||
// increment reference counts / sort out state
|
||||
for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
|
||||
|
||||
var binding = bindings[ i ];
|
||||
|
||||
if ( binding.useCount ++ === 0 ) {
|
||||
|
||||
this._lendBinding( binding );
|
||||
binding.saveOriginalState();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this._lendAction( action );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_deactivateAction: function ( action ) {
|
||||
|
||||
if ( this._isActiveAction( action ) ) {
|
||||
|
||||
var bindings = action._propertyBindings;
|
||||
|
||||
// decrement reference counts / sort out state
|
||||
for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
|
||||
|
||||
var binding = bindings[ i ];
|
||||
|
||||
if ( -- binding.useCount === 0 ) {
|
||||
|
||||
binding.restoreOriginalState();
|
||||
this._takeBackBinding( binding );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this._takeBackAction( action );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
// Memory manager
|
||||
|
||||
_initMemoryManager: function () {
|
||||
|
||||
this._actions = []; // 'nActiveActions' followed by inactive ones
|
||||
this._nActiveActions = 0;
|
||||
|
||||
this._actionsByClip = {};
|
||||
// inside:
|
||||
// {
|
||||
// knownActions: Array< AnimationAction > - used as prototypes
|
||||
// actionByRoot: AnimationAction - lookup
|
||||
// }
|
||||
|
||||
|
||||
this._bindings = []; // 'nActiveBindings' followed by inactive ones
|
||||
this._nActiveBindings = 0;
|
||||
|
||||
this._bindingsByRootAndName = {}; // inside: Map< name, PropertyMixer >
|
||||
|
||||
|
||||
this._controlInterpolants = []; // same game as above
|
||||
this._nActiveControlInterpolants = 0;
|
||||
|
||||
var scope = this;
|
||||
|
||||
this.stats = {
|
||||
|
||||
actions: {
|
||||
get total() {
|
||||
|
||||
return scope._actions.length;
|
||||
|
||||
},
|
||||
get inUse() {
|
||||
|
||||
return scope._nActiveActions;
|
||||
|
||||
}
|
||||
},
|
||||
bindings: {
|
||||
get total() {
|
||||
|
||||
return scope._bindings.length;
|
||||
|
||||
},
|
||||
get inUse() {
|
||||
|
||||
return scope._nActiveBindings;
|
||||
|
||||
}
|
||||
},
|
||||
controlInterpolants: {
|
||||
get total() {
|
||||
|
||||
return scope._controlInterpolants.length;
|
||||
|
||||
},
|
||||
get inUse() {
|
||||
|
||||
return scope._nActiveControlInterpolants;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
},
|
||||
|
||||
// Memory management for AnimationAction objects
|
||||
|
||||
_isActiveAction: function ( action ) {
|
||||
|
||||
var index = action._cacheIndex;
|
||||
return index !== null && index < this._nActiveActions;
|
||||
|
||||
},
|
||||
|
||||
_addInactiveAction: function ( action, clipUuid, rootUuid ) {
|
||||
|
||||
var actions = this._actions,
|
||||
actionsByClip = this._actionsByClip,
|
||||
actionsForClip = actionsByClip[ clipUuid ];
|
||||
|
||||
if ( actionsForClip === undefined ) {
|
||||
|
||||
actionsForClip = {
|
||||
|
||||
knownActions: [ action ],
|
||||
actionByRoot: {}
|
||||
|
||||
};
|
||||
|
||||
action._byClipCacheIndex = 0;
|
||||
|
||||
actionsByClip[ clipUuid ] = actionsForClip;
|
||||
|
||||
} else {
|
||||
|
||||
var knownActions = actionsForClip.knownActions;
|
||||
|
||||
action._byClipCacheIndex = knownActions.length;
|
||||
knownActions.push( action );
|
||||
|
||||
}
|
||||
|
||||
action._cacheIndex = actions.length;
|
||||
actions.push( action );
|
||||
|
||||
actionsForClip.actionByRoot[ rootUuid ] = action;
|
||||
|
||||
},
|
||||
|
||||
_removeInactiveAction: function ( action ) {
|
||||
|
||||
var actions = this._actions,
|
||||
lastInactiveAction = actions[ actions.length - 1 ],
|
||||
cacheIndex = action._cacheIndex;
|
||||
|
||||
lastInactiveAction._cacheIndex = cacheIndex;
|
||||
actions[ cacheIndex ] = lastInactiveAction;
|
||||
actions.pop();
|
||||
|
||||
action._cacheIndex = null;
|
||||
|
||||
|
||||
var clipUuid = action._clip.uuid,
|
||||
actionsByClip = this._actionsByClip,
|
||||
actionsForClip = actionsByClip[ clipUuid ],
|
||||
knownActionsForClip = actionsForClip.knownActions,
|
||||
|
||||
lastKnownAction =
|
||||
knownActionsForClip[ knownActionsForClip.length - 1 ],
|
||||
|
||||
byClipCacheIndex = action._byClipCacheIndex;
|
||||
|
||||
lastKnownAction._byClipCacheIndex = byClipCacheIndex;
|
||||
knownActionsForClip[ byClipCacheIndex ] = lastKnownAction;
|
||||
knownActionsForClip.pop();
|
||||
|
||||
action._byClipCacheIndex = null;
|
||||
|
||||
|
||||
var actionByRoot = actionsForClip.actionByRoot,
|
||||
rootUuid = ( action._localRoot || this._root ).uuid;
|
||||
|
||||
delete actionByRoot[ rootUuid ];
|
||||
|
||||
if ( knownActionsForClip.length === 0 ) {
|
||||
|
||||
delete actionsByClip[ clipUuid ];
|
||||
|
||||
}
|
||||
|
||||
this._removeInactiveBindingsForAction( action );
|
||||
|
||||
},
|
||||
|
||||
_removeInactiveBindingsForAction: function ( action ) {
|
||||
|
||||
var bindings = action._propertyBindings;
|
||||
for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
|
||||
|
||||
var binding = bindings[ i ];
|
||||
|
||||
if ( -- binding.referenceCount === 0 ) {
|
||||
|
||||
this._removeInactiveBinding( binding );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_lendAction: function ( action ) {
|
||||
|
||||
// [ active actions | inactive actions ]
|
||||
// [ active actions >| inactive actions ]
|
||||
// s a
|
||||
// <-swap->
|
||||
// a s
|
||||
|
||||
var actions = this._actions,
|
||||
prevIndex = action._cacheIndex,
|
||||
|
||||
lastActiveIndex = this._nActiveActions ++,
|
||||
|
||||
firstInactiveAction = actions[ lastActiveIndex ];
|
||||
|
||||
action._cacheIndex = lastActiveIndex;
|
||||
actions[ lastActiveIndex ] = action;
|
||||
|
||||
firstInactiveAction._cacheIndex = prevIndex;
|
||||
actions[ prevIndex ] = firstInactiveAction;
|
||||
|
||||
},
|
||||
|
||||
_takeBackAction: function ( action ) {
|
||||
|
||||
// [ active actions | inactive actions ]
|
||||
// [ active actions |< inactive actions ]
|
||||
// a s
|
||||
// <-swap->
|
||||
// s a
|
||||
|
||||
var actions = this._actions,
|
||||
prevIndex = action._cacheIndex,
|
||||
|
||||
firstInactiveIndex = -- this._nActiveActions,
|
||||
|
||||
lastActiveAction = actions[ firstInactiveIndex ];
|
||||
|
||||
action._cacheIndex = firstInactiveIndex;
|
||||
actions[ firstInactiveIndex ] = action;
|
||||
|
||||
lastActiveAction._cacheIndex = prevIndex;
|
||||
actions[ prevIndex ] = lastActiveAction;
|
||||
|
||||
},
|
||||
|
||||
// Memory management for PropertyMixer objects
|
||||
|
||||
_addInactiveBinding: function ( binding, rootUuid, trackName ) {
|
||||
|
||||
var bindingsByRoot = this._bindingsByRootAndName,
|
||||
bindingByName = bindingsByRoot[ rootUuid ],
|
||||
|
||||
bindings = this._bindings;
|
||||
|
||||
if ( bindingByName === undefined ) {
|
||||
|
||||
bindingByName = {};
|
||||
bindingsByRoot[ rootUuid ] = bindingByName;
|
||||
|
||||
}
|
||||
|
||||
bindingByName[ trackName ] = binding;
|
||||
|
||||
binding._cacheIndex = bindings.length;
|
||||
bindings.push( binding );
|
||||
|
||||
},
|
||||
|
||||
_removeInactiveBinding: function ( binding ) {
|
||||
|
||||
var bindings = this._bindings,
|
||||
propBinding = binding.binding,
|
||||
rootUuid = propBinding.rootNode.uuid,
|
||||
trackName = propBinding.path,
|
||||
bindingsByRoot = this._bindingsByRootAndName,
|
||||
bindingByName = bindingsByRoot[ rootUuid ],
|
||||
|
||||
lastInactiveBinding = bindings[ bindings.length - 1 ],
|
||||
cacheIndex = binding._cacheIndex;
|
||||
|
||||
lastInactiveBinding._cacheIndex = cacheIndex;
|
||||
bindings[ cacheIndex ] = lastInactiveBinding;
|
||||
bindings.pop();
|
||||
|
||||
delete bindingByName[ trackName ];
|
||||
|
||||
remove_empty_map: {
|
||||
|
||||
for ( var _ in bindingByName ) break remove_empty_map; // eslint-disable-line no-unused-vars
|
||||
|
||||
delete bindingsByRoot[ rootUuid ];
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_lendBinding: function ( binding ) {
|
||||
|
||||
var bindings = this._bindings,
|
||||
prevIndex = binding._cacheIndex,
|
||||
|
||||
lastActiveIndex = this._nActiveBindings ++,
|
||||
|
||||
firstInactiveBinding = bindings[ lastActiveIndex ];
|
||||
|
||||
binding._cacheIndex = lastActiveIndex;
|
||||
bindings[ lastActiveIndex ] = binding;
|
||||
|
||||
firstInactiveBinding._cacheIndex = prevIndex;
|
||||
bindings[ prevIndex ] = firstInactiveBinding;
|
||||
|
||||
},
|
||||
|
||||
_takeBackBinding: function ( binding ) {
|
||||
|
||||
var bindings = this._bindings,
|
||||
prevIndex = binding._cacheIndex,
|
||||
|
||||
firstInactiveIndex = -- this._nActiveBindings,
|
||||
|
||||
lastActiveBinding = bindings[ firstInactiveIndex ];
|
||||
|
||||
binding._cacheIndex = firstInactiveIndex;
|
||||
bindings[ firstInactiveIndex ] = binding;
|
||||
|
||||
lastActiveBinding._cacheIndex = prevIndex;
|
||||
bindings[ prevIndex ] = lastActiveBinding;
|
||||
|
||||
},
|
||||
|
||||
|
||||
// Memory management of Interpolants for weight and time scale
|
||||
|
||||
_lendControlInterpolant: function () {
|
||||
|
||||
var interpolants = this._controlInterpolants,
|
||||
lastActiveIndex = this._nActiveControlInterpolants ++,
|
||||
interpolant = interpolants[ lastActiveIndex ];
|
||||
|
||||
if ( interpolant === undefined ) {
|
||||
|
||||
interpolant = new LinearInterpolant(
|
||||
new Float32Array( 2 ), new Float32Array( 2 ),
|
||||
1, this._controlInterpolantsResultBuffer );
|
||||
|
||||
interpolant.__cacheIndex = lastActiveIndex;
|
||||
interpolants[ lastActiveIndex ] = interpolant;
|
||||
|
||||
}
|
||||
|
||||
return interpolant;
|
||||
|
||||
},
|
||||
|
||||
_takeBackControlInterpolant: function ( interpolant ) {
|
||||
|
||||
var interpolants = this._controlInterpolants,
|
||||
prevIndex = interpolant.__cacheIndex,
|
||||
|
||||
firstInactiveIndex = -- this._nActiveControlInterpolants,
|
||||
|
||||
lastActiveInterpolant = interpolants[ firstInactiveIndex ];
|
||||
|
||||
interpolant.__cacheIndex = firstInactiveIndex;
|
||||
interpolants[ firstInactiveIndex ] = interpolant;
|
||||
|
||||
lastActiveInterpolant.__cacheIndex = prevIndex;
|
||||
interpolants[ prevIndex ] = lastActiveInterpolant;
|
||||
|
||||
},
|
||||
|
||||
_controlInterpolantsResultBuffer: new Float32Array( 1 ),
|
||||
|
||||
// return an action for a clip optionally using a custom root target
|
||||
// object (this method allocates a lot of dynamic memory in case a
|
||||
// previously unknown clip/root combination is specified)
|
||||
clipAction: function ( clip, optionalRoot ) {
|
||||
|
||||
var root = optionalRoot || this._root,
|
||||
rootUuid = root.uuid,
|
||||
|
||||
clipObject = typeof clip === 'string' ?
|
||||
AnimationClip.findByName( root, clip ) : clip,
|
||||
|
||||
clipUuid = clipObject !== null ? clipObject.uuid : clip,
|
||||
|
||||
actionsForClip = this._actionsByClip[ clipUuid ],
|
||||
prototypeAction = null;
|
||||
|
||||
if ( actionsForClip !== undefined ) {
|
||||
|
||||
var existingAction =
|
||||
actionsForClip.actionByRoot[ rootUuid ];
|
||||
|
||||
if ( existingAction !== undefined ) {
|
||||
|
||||
return existingAction;
|
||||
|
||||
}
|
||||
|
||||
// we know the clip, so we don't have to parse all
|
||||
// the bindings again but can just copy
|
||||
prototypeAction = actionsForClip.knownActions[ 0 ];
|
||||
|
||||
// also, take the clip from the prototype action
|
||||
if ( clipObject === null )
|
||||
clipObject = prototypeAction._clip;
|
||||
|
||||
}
|
||||
|
||||
// clip must be known when specified via string
|
||||
if ( clipObject === null ) return null;
|
||||
|
||||
// allocate all resources required to run it
|
||||
var newAction = new AnimationAction( this, clipObject, optionalRoot );
|
||||
|
||||
this._bindAction( newAction, prototypeAction );
|
||||
|
||||
// and make the action known to the memory manager
|
||||
this._addInactiveAction( newAction, clipUuid, rootUuid );
|
||||
|
||||
return newAction;
|
||||
|
||||
},
|
||||
|
||||
// get an existing action
|
||||
existingAction: function ( clip, optionalRoot ) {
|
||||
|
||||
var root = optionalRoot || this._root,
|
||||
rootUuid = root.uuid,
|
||||
|
||||
clipObject = typeof clip === 'string' ?
|
||||
AnimationClip.findByName( root, clip ) : clip,
|
||||
|
||||
clipUuid = clipObject ? clipObject.uuid : clip,
|
||||
|
||||
actionsForClip = this._actionsByClip[ clipUuid ];
|
||||
|
||||
if ( actionsForClip !== undefined ) {
|
||||
|
||||
return actionsForClip.actionByRoot[ rootUuid ] || null;
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
},
|
||||
|
||||
// deactivates all previously scheduled actions
|
||||
stopAllAction: function () {
|
||||
|
||||
var actions = this._actions,
|
||||
nActions = this._nActiveActions,
|
||||
bindings = this._bindings,
|
||||
nBindings = this._nActiveBindings;
|
||||
|
||||
this._nActiveActions = 0;
|
||||
this._nActiveBindings = 0;
|
||||
|
||||
for ( var i = 0; i !== nActions; ++ i ) {
|
||||
|
||||
actions[ i ].reset();
|
||||
|
||||
}
|
||||
|
||||
for ( var i = 0; i !== nBindings; ++ i ) {
|
||||
|
||||
bindings[ i ].useCount = 0;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
// advance the time and update apply the animation
|
||||
update: function ( deltaTime ) {
|
||||
|
||||
deltaTime *= this.timeScale;
|
||||
|
||||
var actions = this._actions,
|
||||
nActions = this._nActiveActions,
|
||||
|
||||
time = this.time += deltaTime,
|
||||
timeDirection = Math.sign( deltaTime ),
|
||||
|
||||
accuIndex = this._accuIndex ^= 1;
|
||||
|
||||
// run active actions
|
||||
|
||||
for ( var i = 0; i !== nActions; ++ i ) {
|
||||
|
||||
var action = actions[ i ];
|
||||
|
||||
action._update( time, deltaTime, timeDirection, accuIndex );
|
||||
|
||||
}
|
||||
|
||||
// update scene graph
|
||||
|
||||
var bindings = this._bindings,
|
||||
nBindings = this._nActiveBindings;
|
||||
|
||||
for ( var i = 0; i !== nBindings; ++ i ) {
|
||||
|
||||
bindings[ i ].apply( accuIndex );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
// return this mixer's root target object
|
||||
getRoot: function () {
|
||||
|
||||
return this._root;
|
||||
|
||||
},
|
||||
|
||||
// free all resources specific to a particular clip
|
||||
uncacheClip: function ( clip ) {
|
||||
|
||||
var actions = this._actions,
|
||||
clipUuid = clip.uuid,
|
||||
actionsByClip = this._actionsByClip,
|
||||
actionsForClip = actionsByClip[ clipUuid ];
|
||||
|
||||
if ( actionsForClip !== undefined ) {
|
||||
|
||||
// note: just calling _removeInactiveAction would mess up the
|
||||
// iteration state and also require updating the state we can
|
||||
// just throw away
|
||||
|
||||
var actionsToRemove = actionsForClip.knownActions;
|
||||
|
||||
for ( var i = 0, n = actionsToRemove.length; i !== n; ++ i ) {
|
||||
|
||||
var action = actionsToRemove[ i ];
|
||||
|
||||
this._deactivateAction( action );
|
||||
|
||||
var cacheIndex = action._cacheIndex,
|
||||
lastInactiveAction = actions[ actions.length - 1 ];
|
||||
|
||||
action._cacheIndex = null;
|
||||
action._byClipCacheIndex = null;
|
||||
|
||||
lastInactiveAction._cacheIndex = cacheIndex;
|
||||
actions[ cacheIndex ] = lastInactiveAction;
|
||||
actions.pop();
|
||||
|
||||
this._removeInactiveBindingsForAction( action );
|
||||
|
||||
}
|
||||
|
||||
delete actionsByClip[ clipUuid ];
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
// free all resources specific to a particular root target object
|
||||
uncacheRoot: function ( root ) {
|
||||
|
||||
var rootUuid = root.uuid,
|
||||
actionsByClip = this._actionsByClip;
|
||||
|
||||
for ( var clipUuid in actionsByClip ) {
|
||||
|
||||
var actionByRoot = actionsByClip[ clipUuid ].actionByRoot,
|
||||
action = actionByRoot[ rootUuid ];
|
||||
|
||||
if ( action !== undefined ) {
|
||||
|
||||
this._deactivateAction( action );
|
||||
this._removeInactiveAction( action );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var bindingsByRoot = this._bindingsByRootAndName,
|
||||
bindingByName = bindingsByRoot[ rootUuid ];
|
||||
|
||||
if ( bindingByName !== undefined ) {
|
||||
|
||||
for ( var trackName in bindingByName ) {
|
||||
|
||||
var binding = bindingByName[ trackName ];
|
||||
binding.restoreOriginalState();
|
||||
this._removeInactiveBinding( binding );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
// remove a targeted clip from the cache
|
||||
uncacheAction: function ( clip, optionalRoot ) {
|
||||
|
||||
var action = this.existingAction( clip, optionalRoot );
|
||||
|
||||
if ( action !== null ) {
|
||||
|
||||
this._deactivateAction( action );
|
||||
this._removeInactiveAction( action );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { AnimationMixer };
|
381
lib/animation/AnimationObjectGroup.js
Normal file
@ -0,0 +1,381 @@
|
||||
import { PropertyBinding } from './PropertyBinding.js';
|
||||
import { _Math } from '../math/Math.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* A group of objects that receives a shared animation state.
|
||||
*
|
||||
* Usage:
|
||||
*
|
||||
* - Add objects you would otherwise pass as 'root' to the
|
||||
* constructor or the .clipAction method of AnimationMixer.
|
||||
*
|
||||
* - Instead pass this object as 'root'.
|
||||
*
|
||||
* - You can also add and remove objects later when the mixer
|
||||
* is running.
|
||||
*
|
||||
* Note:
|
||||
*
|
||||
* Objects of this class appear as one object to the mixer,
|
||||
* so cache control of the individual objects must be done
|
||||
* on the group.
|
||||
*
|
||||
* Limitation:
|
||||
*
|
||||
* - The animated properties must be compatible among the
|
||||
* all objects in the group.
|
||||
*
|
||||
* - A single property can either be controlled through a
|
||||
* target group or directly, but not both.
|
||||
*
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function AnimationObjectGroup() {
|
||||
|
||||
this.uuid = _Math.generateUUID();
|
||||
|
||||
// cached objects followed by the active ones
|
||||
this._objects = Array.prototype.slice.call( arguments );
|
||||
|
||||
this.nCachedObjects_ = 0; // threshold
|
||||
// note: read by PropertyBinding.Composite
|
||||
|
||||
var indices = {};
|
||||
this._indicesByUUID = indices; // for bookkeeping
|
||||
|
||||
for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
|
||||
|
||||
indices[ arguments[ i ].uuid ] = i;
|
||||
|
||||
}
|
||||
|
||||
this._paths = []; // inside: string
|
||||
this._parsedPaths = []; // inside: { we don't care, here }
|
||||
this._bindings = []; // inside: Array< PropertyBinding >
|
||||
this._bindingsIndicesByPath = {}; // inside: indices in these arrays
|
||||
|
||||
var scope = this;
|
||||
|
||||
this.stats = {
|
||||
|
||||
objects: {
|
||||
get total() {
|
||||
|
||||
return scope._objects.length;
|
||||
|
||||
},
|
||||
get inUse() {
|
||||
|
||||
return this.total - scope.nCachedObjects_;
|
||||
|
||||
}
|
||||
},
|
||||
get bindingsPerObject() {
|
||||
|
||||
return scope._bindings.length;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Object.assign( AnimationObjectGroup.prototype, {
|
||||
|
||||
isAnimationObjectGroup: true,
|
||||
|
||||
add: function () {
|
||||
|
||||
var objects = this._objects,
|
||||
nObjects = objects.length,
|
||||
nCachedObjects = this.nCachedObjects_,
|
||||
indicesByUUID = this._indicesByUUID,
|
||||
paths = this._paths,
|
||||
parsedPaths = this._parsedPaths,
|
||||
bindings = this._bindings,
|
||||
nBindings = bindings.length,
|
||||
knownObject = undefined;
|
||||
|
||||
for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
|
||||
|
||||
var object = arguments[ i ],
|
||||
uuid = object.uuid,
|
||||
index = indicesByUUID[ uuid ];
|
||||
|
||||
if ( index === undefined ) {
|
||||
|
||||
// unknown object -> add it to the ACTIVE region
|
||||
|
||||
index = nObjects ++;
|
||||
indicesByUUID[ uuid ] = index;
|
||||
objects.push( object );
|
||||
|
||||
// accounting is done, now do the same for all bindings
|
||||
|
||||
for ( var j = 0, m = nBindings; j !== m; ++ j ) {
|
||||
|
||||
bindings[ j ].push( new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ) );
|
||||
|
||||
}
|
||||
|
||||
} else if ( index < nCachedObjects ) {
|
||||
|
||||
knownObject = objects[ index ];
|
||||
|
||||
// move existing object to the ACTIVE region
|
||||
|
||||
var firstActiveIndex = -- nCachedObjects,
|
||||
lastCachedObject = objects[ firstActiveIndex ];
|
||||
|
||||
indicesByUUID[ lastCachedObject.uuid ] = index;
|
||||
objects[ index ] = lastCachedObject;
|
||||
|
||||
indicesByUUID[ uuid ] = firstActiveIndex;
|
||||
objects[ firstActiveIndex ] = object;
|
||||
|
||||
// accounting is done, now do the same for all bindings
|
||||
|
||||
for ( var j = 0, m = nBindings; j !== m; ++ j ) {
|
||||
|
||||
var bindingsForPath = bindings[ j ],
|
||||
lastCached = bindingsForPath[ firstActiveIndex ],
|
||||
binding = bindingsForPath[ index ];
|
||||
|
||||
bindingsForPath[ index ] = lastCached;
|
||||
|
||||
if ( binding === undefined ) {
|
||||
|
||||
// since we do not bother to create new bindings
|
||||
// for objects that are cached, the binding may
|
||||
// or may not exist
|
||||
|
||||
binding = new PropertyBinding( object, paths[ j ], parsedPaths[ j ] );
|
||||
|
||||
}
|
||||
|
||||
bindingsForPath[ firstActiveIndex ] = binding;
|
||||
|
||||
}
|
||||
|
||||
} else if ( objects[ index ] !== knownObject ) {
|
||||
|
||||
console.error( 'THREE.AnimationObjectGroup: Different objects with the same UUID ' +
|
||||
'detected. Clean the caches or recreate your infrastructure when reloading scenes.' );
|
||||
|
||||
} // else the object is already where we want it to be
|
||||
|
||||
} // for arguments
|
||||
|
||||
this.nCachedObjects_ = nCachedObjects;
|
||||
|
||||
},
|
||||
|
||||
remove: function () {
|
||||
|
||||
var objects = this._objects,
|
||||
nCachedObjects = this.nCachedObjects_,
|
||||
indicesByUUID = this._indicesByUUID,
|
||||
bindings = this._bindings,
|
||||
nBindings = bindings.length;
|
||||
|
||||
for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
|
||||
|
||||
var object = arguments[ i ],
|
||||
uuid = object.uuid,
|
||||
index = indicesByUUID[ uuid ];
|
||||
|
||||
if ( index !== undefined && index >= nCachedObjects ) {
|
||||
|
||||
// move existing object into the CACHED region
|
||||
|
||||
var lastCachedIndex = nCachedObjects ++,
|
||||
firstActiveObject = objects[ lastCachedIndex ];
|
||||
|
||||
indicesByUUID[ firstActiveObject.uuid ] = index;
|
||||
objects[ index ] = firstActiveObject;
|
||||
|
||||
indicesByUUID[ uuid ] = lastCachedIndex;
|
||||
objects[ lastCachedIndex ] = object;
|
||||
|
||||
// accounting is done, now do the same for all bindings
|
||||
|
||||
for ( var j = 0, m = nBindings; j !== m; ++ j ) {
|
||||
|
||||
var bindingsForPath = bindings[ j ],
|
||||
firstActive = bindingsForPath[ lastCachedIndex ],
|
||||
binding = bindingsForPath[ index ];
|
||||
|
||||
bindingsForPath[ index ] = firstActive;
|
||||
bindingsForPath[ lastCachedIndex ] = binding;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // for arguments
|
||||
|
||||
this.nCachedObjects_ = nCachedObjects;
|
||||
|
||||
},
|
||||
|
||||
// remove & forget
|
||||
uncache: function () {
|
||||
|
||||
var objects = this._objects,
|
||||
nObjects = objects.length,
|
||||
nCachedObjects = this.nCachedObjects_,
|
||||
indicesByUUID = this._indicesByUUID,
|
||||
bindings = this._bindings,
|
||||
nBindings = bindings.length;
|
||||
|
||||
for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
|
||||
|
||||
var object = arguments[ i ],
|
||||
uuid = object.uuid,
|
||||
index = indicesByUUID[ uuid ];
|
||||
|
||||
if ( index !== undefined ) {
|
||||
|
||||
delete indicesByUUID[ uuid ];
|
||||
|
||||
if ( index < nCachedObjects ) {
|
||||
|
||||
// object is cached, shrink the CACHED region
|
||||
|
||||
var firstActiveIndex = -- nCachedObjects,
|
||||
lastCachedObject = objects[ firstActiveIndex ],
|
||||
lastIndex = -- nObjects,
|
||||
lastObject = objects[ lastIndex ];
|
||||
|
||||
// last cached object takes this object's place
|
||||
indicesByUUID[ lastCachedObject.uuid ] = index;
|
||||
objects[ index ] = lastCachedObject;
|
||||
|
||||
// last object goes to the activated slot and pop
|
||||
indicesByUUID[ lastObject.uuid ] = firstActiveIndex;
|
||||
objects[ firstActiveIndex ] = lastObject;
|
||||
objects.pop();
|
||||
|
||||
// accounting is done, now do the same for all bindings
|
||||
|
||||
for ( var j = 0, m = nBindings; j !== m; ++ j ) {
|
||||
|
||||
var bindingsForPath = bindings[ j ],
|
||||
lastCached = bindingsForPath[ firstActiveIndex ],
|
||||
last = bindingsForPath[ lastIndex ];
|
||||
|
||||
bindingsForPath[ index ] = lastCached;
|
||||
bindingsForPath[ firstActiveIndex ] = last;
|
||||
bindingsForPath.pop();
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// object is active, just swap with the last and pop
|
||||
|
||||
var lastIndex = -- nObjects,
|
||||
lastObject = objects[ lastIndex ];
|
||||
|
||||
indicesByUUID[ lastObject.uuid ] = index;
|
||||
objects[ index ] = lastObject;
|
||||
objects.pop();
|
||||
|
||||
// accounting is done, now do the same for all bindings
|
||||
|
||||
for ( var j = 0, m = nBindings; j !== m; ++ j ) {
|
||||
|
||||
var bindingsForPath = bindings[ j ];
|
||||
|
||||
bindingsForPath[ index ] = bindingsForPath[ lastIndex ];
|
||||
bindingsForPath.pop();
|
||||
|
||||
}
|
||||
|
||||
} // cached or active
|
||||
|
||||
} // if object is known
|
||||
|
||||
} // for arguments
|
||||
|
||||
this.nCachedObjects_ = nCachedObjects;
|
||||
|
||||
},
|
||||
|
||||
// Internal interface used by befriended PropertyBinding.Composite:
|
||||
|
||||
subscribe_: function ( path, parsedPath ) {
|
||||
|
||||
// returns an array of bindings for the given path that is changed
|
||||
// according to the contained objects in the group
|
||||
|
||||
var indicesByPath = this._bindingsIndicesByPath,
|
||||
index = indicesByPath[ path ],
|
||||
bindings = this._bindings;
|
||||
|
||||
if ( index !== undefined ) return bindings[ index ];
|
||||
|
||||
var paths = this._paths,
|
||||
parsedPaths = this._parsedPaths,
|
||||
objects = this._objects,
|
||||
nObjects = objects.length,
|
||||
nCachedObjects = this.nCachedObjects_,
|
||||
bindingsForPath = new Array( nObjects );
|
||||
|
||||
index = bindings.length;
|
||||
|
||||
indicesByPath[ path ] = index;
|
||||
|
||||
paths.push( path );
|
||||
parsedPaths.push( parsedPath );
|
||||
bindings.push( bindingsForPath );
|
||||
|
||||
for ( var i = nCachedObjects, n = objects.length; i !== n; ++ i ) {
|
||||
|
||||
var object = objects[ i ];
|
||||
bindingsForPath[ i ] = new PropertyBinding( object, path, parsedPath );
|
||||
|
||||
}
|
||||
|
||||
return bindingsForPath;
|
||||
|
||||
},
|
||||
|
||||
unsubscribe_: function ( path ) {
|
||||
|
||||
// tells the group to forget about a property path and no longer
|
||||
// update the array previously obtained with 'subscribe_'
|
||||
|
||||
var indicesByPath = this._bindingsIndicesByPath,
|
||||
index = indicesByPath[ path ];
|
||||
|
||||
if ( index !== undefined ) {
|
||||
|
||||
var paths = this._paths,
|
||||
parsedPaths = this._parsedPaths,
|
||||
bindings = this._bindings,
|
||||
lastBindingsIndex = bindings.length - 1,
|
||||
lastBindings = bindings[ lastBindingsIndex ],
|
||||
lastBindingsPath = path[ lastBindingsIndex ];
|
||||
|
||||
indicesByPath[ lastBindingsPath ] = index;
|
||||
|
||||
bindings[ index ] = lastBindings;
|
||||
bindings.pop();
|
||||
|
||||
parsedPaths[ index ] = parsedPaths[ lastBindingsIndex ];
|
||||
parsedPaths.pop();
|
||||
|
||||
paths[ index ] = paths[ lastBindingsIndex ];
|
||||
paths.pop();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { AnimationObjectGroup };
|
166
lib/animation/AnimationUtils.js
Normal file
@ -0,0 +1,166 @@
|
||||
/**
|
||||
* @author tschw
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
*/
|
||||
|
||||
var AnimationUtils = {
|
||||
|
||||
// same as Array.prototype.slice, but also works on typed arrays
|
||||
arraySlice: function ( array, from, to ) {
|
||||
|
||||
if ( AnimationUtils.isTypedArray( array ) ) {
|
||||
|
||||
// in ios9 array.subarray(from, undefined) will return empty array
|
||||
// but array.subarray(from) or array.subarray(from, len) is correct
|
||||
return new array.constructor( array.subarray( from, to !== undefined ? to : array.length ) );
|
||||
|
||||
}
|
||||
|
||||
return array.slice( from, to );
|
||||
|
||||
},
|
||||
|
||||
// converts an array to a specific type
|
||||
convertArray: function ( array, type, forceClone ) {
|
||||
|
||||
if ( ! array || // let 'undefined' and 'null' pass
|
||||
! forceClone && array.constructor === type ) return array;
|
||||
|
||||
if ( typeof type.BYTES_PER_ELEMENT === 'number' ) {
|
||||
|
||||
return new type( array ); // create typed array
|
||||
|
||||
}
|
||||
|
||||
return Array.prototype.slice.call( array ); // create Array
|
||||
|
||||
},
|
||||
|
||||
isTypedArray: function ( object ) {
|
||||
|
||||
return ArrayBuffer.isView( object ) &&
|
||||
! ( object instanceof DataView );
|
||||
|
||||
},
|
||||
|
||||
// returns an array by which times and values can be sorted
|
||||
getKeyframeOrder: function ( times ) {
|
||||
|
||||
function compareTime( i, j ) {
|
||||
|
||||
return times[ i ] - times[ j ];
|
||||
|
||||
}
|
||||
|
||||
var n = times.length;
|
||||
var result = new Array( n );
|
||||
for ( var i = 0; i !== n; ++ i ) result[ i ] = i;
|
||||
|
||||
result.sort( compareTime );
|
||||
|
||||
return result;
|
||||
|
||||
},
|
||||
|
||||
// uses the array previously returned by 'getKeyframeOrder' to sort data
|
||||
sortedArray: function ( values, stride, order ) {
|
||||
|
||||
var nValues = values.length;
|
||||
var result = new values.constructor( nValues );
|
||||
|
||||
for ( var i = 0, dstOffset = 0; dstOffset !== nValues; ++ i ) {
|
||||
|
||||
var srcOffset = order[ i ] * stride;
|
||||
|
||||
for ( var j = 0; j !== stride; ++ j ) {
|
||||
|
||||
result[ dstOffset ++ ] = values[ srcOffset + j ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
},
|
||||
|
||||
// function for parsing AOS keyframe formats
|
||||
flattenJSON: function ( jsonKeys, times, values, valuePropertyName ) {
|
||||
|
||||
var i = 1, key = jsonKeys[ 0 ];
|
||||
|
||||
while ( key !== undefined && key[ valuePropertyName ] === undefined ) {
|
||||
|
||||
key = jsonKeys[ i ++ ];
|
||||
|
||||
}
|
||||
|
||||
if ( key === undefined ) return; // no data
|
||||
|
||||
var value = key[ valuePropertyName ];
|
||||
if ( value === undefined ) return; // no data
|
||||
|
||||
if ( Array.isArray( value ) ) {
|
||||
|
||||
do {
|
||||
|
||||
value = key[ valuePropertyName ];
|
||||
|
||||
if ( value !== undefined ) {
|
||||
|
||||
times.push( key.time );
|
||||
values.push.apply( values, value ); // push all elements
|
||||
|
||||
}
|
||||
|
||||
key = jsonKeys[ i ++ ];
|
||||
|
||||
} while ( key !== undefined );
|
||||
|
||||
} else if ( value.toArray !== undefined ) {
|
||||
|
||||
// ...assume THREE.Math-ish
|
||||
|
||||
do {
|
||||
|
||||
value = key[ valuePropertyName ];
|
||||
|
||||
if ( value !== undefined ) {
|
||||
|
||||
times.push( key.time );
|
||||
value.toArray( values, values.length );
|
||||
|
||||
}
|
||||
|
||||
key = jsonKeys[ i ++ ];
|
||||
|
||||
} while ( key !== undefined );
|
||||
|
||||
} else {
|
||||
|
||||
// otherwise push as-is
|
||||
|
||||
do {
|
||||
|
||||
value = key[ valuePropertyName ];
|
||||
|
||||
if ( value !== undefined ) {
|
||||
|
||||
times.push( key.time );
|
||||
values.push( value );
|
||||
|
||||
}
|
||||
|
||||
key = jsonKeys[ i ++ ];
|
||||
|
||||
} while ( key !== undefined );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { AnimationUtils };
|
454
lib/animation/KeyframeTrack.js
Normal file
@ -0,0 +1,454 @@
|
||||
import {
|
||||
InterpolateLinear,
|
||||
InterpolateSmooth,
|
||||
InterpolateDiscrete
|
||||
} from '../constants.js';
|
||||
import { CubicInterpolant } from '../math/interpolants/CubicInterpolant.js';
|
||||
import { LinearInterpolant } from '../math/interpolants/LinearInterpolant.js';
|
||||
import { DiscreteInterpolant } from '../math/interpolants/DiscreteInterpolant.js';
|
||||
import { AnimationUtils } from './AnimationUtils.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* A timed sequence of keyframes for a specific property.
|
||||
*
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function KeyframeTrack( name, times, values, interpolation ) {
|
||||
|
||||
if ( name === undefined ) throw new Error( 'THREE.KeyframeTrack: track name is undefined' );
|
||||
if ( times === undefined || times.length === 0 ) throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name );
|
||||
|
||||
this.name = name;
|
||||
|
||||
this.times = AnimationUtils.convertArray( times, this.TimeBufferType );
|
||||
this.values = AnimationUtils.convertArray( values, this.ValueBufferType );
|
||||
|
||||
this.setInterpolation( interpolation || this.DefaultInterpolation );
|
||||
|
||||
}
|
||||
|
||||
// Static methods
|
||||
|
||||
Object.assign( KeyframeTrack, {
|
||||
|
||||
// Serialization (in static context, because of constructor invocation
|
||||
// and automatic invocation of .toJSON):
|
||||
|
||||
toJSON: function ( track ) {
|
||||
|
||||
var trackType = track.constructor;
|
||||
|
||||
var json;
|
||||
|
||||
// derived classes can define a static toJSON method
|
||||
if ( trackType.toJSON !== undefined ) {
|
||||
|
||||
json = trackType.toJSON( track );
|
||||
|
||||
} else {
|
||||
|
||||
// by default, we assume the data can be serialized as-is
|
||||
json = {
|
||||
|
||||
'name': track.name,
|
||||
'times': AnimationUtils.convertArray( track.times, Array ),
|
||||
'values': AnimationUtils.convertArray( track.values, Array )
|
||||
|
||||
};
|
||||
|
||||
var interpolation = track.getInterpolation();
|
||||
|
||||
if ( interpolation !== track.DefaultInterpolation ) {
|
||||
|
||||
json.interpolation = interpolation;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
json.type = track.ValueTypeName; // mandatory
|
||||
|
||||
return json;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
Object.assign( KeyframeTrack.prototype, {
|
||||
|
||||
constructor: KeyframeTrack,
|
||||
|
||||
TimeBufferType: Float32Array,
|
||||
|
||||
ValueBufferType: Float32Array,
|
||||
|
||||
DefaultInterpolation: InterpolateLinear,
|
||||
|
||||
InterpolantFactoryMethodDiscrete: function ( result ) {
|
||||
|
||||
return new DiscreteInterpolant( this.times, this.values, this.getValueSize(), result );
|
||||
|
||||
},
|
||||
|
||||
InterpolantFactoryMethodLinear: function ( result ) {
|
||||
|
||||
return new LinearInterpolant( this.times, this.values, this.getValueSize(), result );
|
||||
|
||||
},
|
||||
|
||||
InterpolantFactoryMethodSmooth: function ( result ) {
|
||||
|
||||
return new CubicInterpolant( this.times, this.values, this.getValueSize(), result );
|
||||
|
||||
},
|
||||
|
||||
setInterpolation: function ( interpolation ) {
|
||||
|
||||
var factoryMethod;
|
||||
|
||||
switch ( interpolation ) {
|
||||
|
||||
case InterpolateDiscrete:
|
||||
|
||||
factoryMethod = this.InterpolantFactoryMethodDiscrete;
|
||||
|
||||
break;
|
||||
|
||||
case InterpolateLinear:
|
||||
|
||||
factoryMethod = this.InterpolantFactoryMethodLinear;
|
||||
|
||||
break;
|
||||
|
||||
case InterpolateSmooth:
|
||||
|
||||
factoryMethod = this.InterpolantFactoryMethodSmooth;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if ( factoryMethod === undefined ) {
|
||||
|
||||
var message = "unsupported interpolation for " +
|
||||
this.ValueTypeName + " keyframe track named " + this.name;
|
||||
|
||||
if ( this.createInterpolant === undefined ) {
|
||||
|
||||
// fall back to default, unless the default itself is messed up
|
||||
if ( interpolation !== this.DefaultInterpolation ) {
|
||||
|
||||
this.setInterpolation( this.DefaultInterpolation );
|
||||
|
||||
} else {
|
||||
|
||||
throw new Error( message ); // fatal, in this case
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
console.warn( 'THREE.KeyframeTrack:', message );
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
this.createInterpolant = factoryMethod;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getInterpolation: function () {
|
||||
|
||||
switch ( this.createInterpolant ) {
|
||||
|
||||
case this.InterpolantFactoryMethodDiscrete:
|
||||
|
||||
return InterpolateDiscrete;
|
||||
|
||||
case this.InterpolantFactoryMethodLinear:
|
||||
|
||||
return InterpolateLinear;
|
||||
|
||||
case this.InterpolantFactoryMethodSmooth:
|
||||
|
||||
return InterpolateSmooth;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
getValueSize: function () {
|
||||
|
||||
return this.values.length / this.times.length;
|
||||
|
||||
},
|
||||
|
||||
// move all keyframes either forwards or backwards in time
|
||||
shift: function ( timeOffset ) {
|
||||
|
||||
if ( timeOffset !== 0.0 ) {
|
||||
|
||||
var times = this.times;
|
||||
|
||||
for ( var i = 0, n = times.length; i !== n; ++ i ) {
|
||||
|
||||
times[ i ] += timeOffset;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
// scale all keyframe times by a factor (useful for frame <-> seconds conversions)
|
||||
scale: function ( timeScale ) {
|
||||
|
||||
if ( timeScale !== 1.0 ) {
|
||||
|
||||
var times = this.times;
|
||||
|
||||
for ( var i = 0, n = times.length; i !== n; ++ i ) {
|
||||
|
||||
times[ i ] *= timeScale;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
// removes keyframes before and after animation without changing any values within the range [startTime, endTime].
|
||||
// IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values
|
||||
trim: function ( startTime, endTime ) {
|
||||
|
||||
var times = this.times,
|
||||
nKeys = times.length,
|
||||
from = 0,
|
||||
to = nKeys - 1;
|
||||
|
||||
while ( from !== nKeys && times[ from ] < startTime ) {
|
||||
|
||||
++ from;
|
||||
|
||||
}
|
||||
|
||||
while ( to !== - 1 && times[ to ] > endTime ) {
|
||||
|
||||
-- to;
|
||||
|
||||
}
|
||||
|
||||
++ to; // inclusive -> exclusive bound
|
||||
|
||||
if ( from !== 0 || to !== nKeys ) {
|
||||
|
||||
// empty tracks are forbidden, so keep at least one keyframe
|
||||
if ( from >= to ) to = Math.max( to, 1 ), from = to - 1;
|
||||
|
||||
var stride = this.getValueSize();
|
||||
this.times = AnimationUtils.arraySlice( times, from, to );
|
||||
this.values = AnimationUtils.arraySlice( this.values, from * stride, to * stride );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
// ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
|
||||
validate: function () {
|
||||
|
||||
var valid = true;
|
||||
|
||||
var valueSize = this.getValueSize();
|
||||
if ( valueSize - Math.floor( valueSize ) !== 0 ) {
|
||||
|
||||
console.error( 'THREE.KeyframeTrack: Invalid value size in track.', this );
|
||||
valid = false;
|
||||
|
||||
}
|
||||
|
||||
var times = this.times,
|
||||
values = this.values,
|
||||
|
||||
nKeys = times.length;
|
||||
|
||||
if ( nKeys === 0 ) {
|
||||
|
||||
console.error( 'THREE.KeyframeTrack: Track is empty.', this );
|
||||
valid = false;
|
||||
|
||||
}
|
||||
|
||||
var prevTime = null;
|
||||
|
||||
for ( var i = 0; i !== nKeys; i ++ ) {
|
||||
|
||||
var currTime = times[ i ];
|
||||
|
||||
if ( typeof currTime === 'number' && isNaN( currTime ) ) {
|
||||
|
||||
console.error( 'THREE.KeyframeTrack: Time is not a valid number.', this, i, currTime );
|
||||
valid = false;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if ( prevTime !== null && prevTime > currTime ) {
|
||||
|
||||
console.error( 'THREE.KeyframeTrack: Out of order keys.', this, i, currTime, prevTime );
|
||||
valid = false;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
prevTime = currTime;
|
||||
|
||||
}
|
||||
|
||||
if ( values !== undefined ) {
|
||||
|
||||
if ( AnimationUtils.isTypedArray( values ) ) {
|
||||
|
||||
for ( var i = 0, n = values.length; i !== n; ++ i ) {
|
||||
|
||||
var value = values[ i ];
|
||||
|
||||
if ( isNaN( value ) ) {
|
||||
|
||||
console.error( 'THREE.KeyframeTrack: Value is not a valid number.', this, i, value );
|
||||
valid = false;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return valid;
|
||||
|
||||
},
|
||||
|
||||
// removes equivalent sequential keys as common in morph target sequences
|
||||
// (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0)
|
||||
optimize: function () {
|
||||
|
||||
var times = this.times,
|
||||
values = this.values,
|
||||
stride = this.getValueSize(),
|
||||
|
||||
smoothInterpolation = this.getInterpolation() === InterpolateSmooth,
|
||||
|
||||
writeIndex = 1,
|
||||
lastIndex = times.length - 1;
|
||||
|
||||
for ( var i = 1; i < lastIndex; ++ i ) {
|
||||
|
||||
var keep = false;
|
||||
|
||||
var time = times[ i ];
|
||||
var timeNext = times[ i + 1 ];
|
||||
|
||||
// remove adjacent keyframes scheduled at the same time
|
||||
|
||||
if ( time !== timeNext && ( i !== 1 || time !== time[ 0 ] ) ) {
|
||||
|
||||
if ( ! smoothInterpolation ) {
|
||||
|
||||
// remove unnecessary keyframes same as their neighbors
|
||||
|
||||
var offset = i * stride,
|
||||
offsetP = offset - stride,
|
||||
offsetN = offset + stride;
|
||||
|
||||
for ( var j = 0; j !== stride; ++ j ) {
|
||||
|
||||
var value = values[ offset + j ];
|
||||
|
||||
if ( value !== values[ offsetP + j ] ||
|
||||
value !== values[ offsetN + j ] ) {
|
||||
|
||||
keep = true;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
keep = true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// in-place compaction
|
||||
|
||||
if ( keep ) {
|
||||
|
||||
if ( i !== writeIndex ) {
|
||||
|
||||
times[ writeIndex ] = times[ i ];
|
||||
|
||||
var readOffset = i * stride,
|
||||
writeOffset = writeIndex * stride;
|
||||
|
||||
for ( var j = 0; j !== stride; ++ j ) {
|
||||
|
||||
values[ writeOffset + j ] = values[ readOffset + j ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
++ writeIndex;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// flush last keyframe (compaction looks ahead)
|
||||
|
||||
if ( lastIndex > 0 ) {
|
||||
|
||||
times[ writeIndex ] = times[ lastIndex ];
|
||||
|
||||
for ( var readOffset = lastIndex * stride, writeOffset = writeIndex * stride, j = 0; j !== stride; ++ j ) {
|
||||
|
||||
values[ writeOffset + j ] = values[ readOffset + j ];
|
||||
|
||||
}
|
||||
|
||||
++ writeIndex;
|
||||
|
||||
}
|
||||
|
||||
if ( writeIndex !== times.length ) {
|
||||
|
||||
this.times = AnimationUtils.arraySlice( times, 0, writeIndex );
|
||||
this.values = AnimationUtils.arraySlice( values, 0, writeIndex * stride );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
export { KeyframeTrack };
|
726
lib/animation/PropertyBinding.js
Normal file
@ -0,0 +1,726 @@
|
||||
/**
|
||||
*
|
||||
* A reference to a real property in the scene graph.
|
||||
*
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
// Characters [].:/ are reserved for track binding syntax.
|
||||
var RESERVED_CHARS_RE = '\\[\\]\\.:\\/';
|
||||
|
||||
function Composite( targetGroup, path, optionalParsedPath ) {
|
||||
|
||||
var parsedPath = optionalParsedPath || PropertyBinding.parseTrackName( path );
|
||||
|
||||
this._targetGroup = targetGroup;
|
||||
this._bindings = targetGroup.subscribe_( path, parsedPath );
|
||||
|
||||
}
|
||||
|
||||
Object.assign( Composite.prototype, {
|
||||
|
||||
getValue: function ( array, offset ) {
|
||||
|
||||
this.bind(); // bind all binding
|
||||
|
||||
var firstValidIndex = this._targetGroup.nCachedObjects_,
|
||||
binding = this._bindings[ firstValidIndex ];
|
||||
|
||||
// and only call .getValue on the first
|
||||
if ( binding !== undefined ) binding.getValue( array, offset );
|
||||
|
||||
},
|
||||
|
||||
setValue: function ( array, offset ) {
|
||||
|
||||
var bindings = this._bindings;
|
||||
|
||||
for ( var i = this._targetGroup.nCachedObjects_,
|
||||
n = bindings.length; i !== n; ++ i ) {
|
||||
|
||||
bindings[ i ].setValue( array, offset );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
bind: function () {
|
||||
|
||||
var bindings = this._bindings;
|
||||
|
||||
for ( var i = this._targetGroup.nCachedObjects_,
|
||||
n = bindings.length; i !== n; ++ i ) {
|
||||
|
||||
bindings[ i ].bind();
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
unbind: function () {
|
||||
|
||||
var bindings = this._bindings;
|
||||
|
||||
for ( var i = this._targetGroup.nCachedObjects_,
|
||||
n = bindings.length; i !== n; ++ i ) {
|
||||
|
||||
bindings[ i ].unbind();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
function PropertyBinding( rootNode, path, parsedPath ) {
|
||||
|
||||
this.path = path;
|
||||
this.parsedPath = parsedPath || PropertyBinding.parseTrackName( path );
|
||||
|
||||
this.node = PropertyBinding.findNode( rootNode, this.parsedPath.nodeName ) || rootNode;
|
||||
|
||||
this.rootNode = rootNode;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( PropertyBinding, {
|
||||
|
||||
Composite: Composite,
|
||||
|
||||
create: function ( root, path, parsedPath ) {
|
||||
|
||||
if ( ! ( root && root.isAnimationObjectGroup ) ) {
|
||||
|
||||
return new PropertyBinding( root, path, parsedPath );
|
||||
|
||||
} else {
|
||||
|
||||
return new PropertyBinding.Composite( root, path, parsedPath );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Replaces spaces with underscores and removes unsupported characters from
|
||||
* node names, to ensure compatibility with parseTrackName().
|
||||
*
|
||||
* @param {string} name Node name to be sanitized.
|
||||
* @return {string}
|
||||
*/
|
||||
sanitizeNodeName: ( function () {
|
||||
|
||||
var reservedRe = new RegExp( '[' + RESERVED_CHARS_RE + ']', 'g' );
|
||||
|
||||
return function sanitizeNodeName( name ) {
|
||||
|
||||
return name.replace( /\s/g, '_' ).replace( reservedRe, '' );
|
||||
|
||||
};
|
||||
|
||||
}() ),
|
||||
|
||||
parseTrackName: function () {
|
||||
|
||||
// Attempts to allow node names from any language. ES5's `\w` regexp matches
|
||||
// only latin characters, and the unicode \p{L} is not yet supported. So
|
||||
// instead, we exclude reserved characters and match everything else.
|
||||
var wordChar = '[^' + RESERVED_CHARS_RE + ']';
|
||||
var wordCharOrDot = '[^' + RESERVED_CHARS_RE.replace( '\\.', '' ) + ']';
|
||||
|
||||
// Parent directories, delimited by '/' or ':'. Currently unused, but must
|
||||
// be matched to parse the rest of the track name.
|
||||
var directoryRe = /((?:WC+[\/:])*)/.source.replace( 'WC', wordChar );
|
||||
|
||||
// Target node. May contain word characters (a-zA-Z0-9_) and '.' or '-'.
|
||||
var nodeRe = /(WCOD+)?/.source.replace( 'WCOD', wordCharOrDot );
|
||||
|
||||
// Object on target node, and accessor. May not contain reserved
|
||||
// characters. Accessor may contain any character except closing bracket.
|
||||
var objectRe = /(?:\.(WC+)(?:\[(.+)\])?)?/.source.replace( 'WC', wordChar );
|
||||
|
||||
// Property and accessor. May not contain reserved characters. Accessor may
|
||||
// contain any non-bracket characters.
|
||||
var propertyRe = /\.(WC+)(?:\[(.+)\])?/.source.replace( 'WC', wordChar );
|
||||
|
||||
var trackRe = new RegExp( ''
|
||||
+ '^'
|
||||
+ directoryRe
|
||||
+ nodeRe
|
||||
+ objectRe
|
||||
+ propertyRe
|
||||
+ '$'
|
||||
);
|
||||
|
||||
var supportedObjectNames = [ 'material', 'materials', 'bones' ];
|
||||
|
||||
return function parseTrackName( trackName ) {
|
||||
|
||||
var matches = trackRe.exec( trackName );
|
||||
|
||||
if ( ! matches ) {
|
||||
|
||||
throw new Error( 'PropertyBinding: Cannot parse trackName: ' + trackName );
|
||||
|
||||
}
|
||||
|
||||
var results = {
|
||||
// directoryName: matches[ 1 ], // (tschw) currently unused
|
||||
nodeName: matches[ 2 ],
|
||||
objectName: matches[ 3 ],
|
||||
objectIndex: matches[ 4 ],
|
||||
propertyName: matches[ 5 ], // required
|
||||
propertyIndex: matches[ 6 ]
|
||||
};
|
||||
|
||||
var lastDot = results.nodeName && results.nodeName.lastIndexOf( '.' );
|
||||
|
||||
if ( lastDot !== undefined && lastDot !== - 1 ) {
|
||||
|
||||
var objectName = results.nodeName.substring( lastDot + 1 );
|
||||
|
||||
// Object names must be checked against a whitelist. Otherwise, there
|
||||
// is no way to parse 'foo.bar.baz': 'baz' must be a property, but
|
||||
// 'bar' could be the objectName, or part of a nodeName (which can
|
||||
// include '.' characters).
|
||||
if ( supportedObjectNames.indexOf( objectName ) !== - 1 ) {
|
||||
|
||||
results.nodeName = results.nodeName.substring( 0, lastDot );
|
||||
results.objectName = objectName;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( results.propertyName === null || results.propertyName.length === 0 ) {
|
||||
|
||||
throw new Error( 'PropertyBinding: can not parse propertyName from trackName: ' + trackName );
|
||||
|
||||
}
|
||||
|
||||
return results;
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
findNode: function ( root, nodeName ) {
|
||||
|
||||
if ( ! nodeName || nodeName === "" || nodeName === "root" || nodeName === "." || nodeName === - 1 || nodeName === root.name || nodeName === root.uuid ) {
|
||||
|
||||
return root;
|
||||
|
||||
}
|
||||
|
||||
// search into skeleton bones.
|
||||
if ( root.skeleton ) {
|
||||
|
||||
var bone = root.skeleton.getBoneByName( nodeName );
|
||||
|
||||
if ( bone !== undefined ) {
|
||||
|
||||
return bone;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// search into node subtree.
|
||||
if ( root.children ) {
|
||||
|
||||
var searchNodeSubtree = function ( children ) {
|
||||
|
||||
for ( var i = 0; i < children.length; i ++ ) {
|
||||
|
||||
var childNode = children[ i ];
|
||||
|
||||
if ( childNode.name === nodeName || childNode.uuid === nodeName ) {
|
||||
|
||||
return childNode;
|
||||
|
||||
}
|
||||
|
||||
var result = searchNodeSubtree( childNode.children );
|
||||
|
||||
if ( result ) return result;
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
};
|
||||
|
||||
var subTreeNode = searchNodeSubtree( root.children );
|
||||
|
||||
if ( subTreeNode ) {
|
||||
|
||||
return subTreeNode;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
Object.assign( PropertyBinding.prototype, { // prototype, continued
|
||||
|
||||
// these are used to "bind" a nonexistent property
|
||||
_getValue_unavailable: function () {},
|
||||
_setValue_unavailable: function () {},
|
||||
|
||||
BindingType: {
|
||||
Direct: 0,
|
||||
EntireArray: 1,
|
||||
ArrayElement: 2,
|
||||
HasFromToArray: 3
|
||||
},
|
||||
|
||||
Versioning: {
|
||||
None: 0,
|
||||
NeedsUpdate: 1,
|
||||
MatrixWorldNeedsUpdate: 2
|
||||
},
|
||||
|
||||
GetterByBindingType: [
|
||||
|
||||
function getValue_direct( buffer, offset ) {
|
||||
|
||||
buffer[ offset ] = this.node[ this.propertyName ];
|
||||
|
||||
},
|
||||
|
||||
function getValue_array( buffer, offset ) {
|
||||
|
||||
var source = this.resolvedProperty;
|
||||
|
||||
for ( var i = 0, n = source.length; i !== n; ++ i ) {
|
||||
|
||||
buffer[ offset ++ ] = source[ i ];
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
function getValue_arrayElement( buffer, offset ) {
|
||||
|
||||
buffer[ offset ] = this.resolvedProperty[ this.propertyIndex ];
|
||||
|
||||
},
|
||||
|
||||
function getValue_toArray( buffer, offset ) {
|
||||
|
||||
this.resolvedProperty.toArray( buffer, offset );
|
||||
|
||||
}
|
||||
|
||||
],
|
||||
|
||||
SetterByBindingTypeAndVersioning: [
|
||||
|
||||
[
|
||||
// Direct
|
||||
|
||||
function setValue_direct( buffer, offset ) {
|
||||
|
||||
this.targetObject[ this.propertyName ] = buffer[ offset ];
|
||||
|
||||
},
|
||||
|
||||
function setValue_direct_setNeedsUpdate( buffer, offset ) {
|
||||
|
||||
this.targetObject[ this.propertyName ] = buffer[ offset ];
|
||||
this.targetObject.needsUpdate = true;
|
||||
|
||||
},
|
||||
|
||||
function setValue_direct_setMatrixWorldNeedsUpdate( buffer, offset ) {
|
||||
|
||||
this.targetObject[ this.propertyName ] = buffer[ offset ];
|
||||
this.targetObject.matrixWorldNeedsUpdate = true;
|
||||
|
||||
}
|
||||
|
||||
], [
|
||||
|
||||
// EntireArray
|
||||
|
||||
function setValue_array( buffer, offset ) {
|
||||
|
||||
var dest = this.resolvedProperty;
|
||||
|
||||
for ( var i = 0, n = dest.length; i !== n; ++ i ) {
|
||||
|
||||
dest[ i ] = buffer[ offset ++ ];
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
function setValue_array_setNeedsUpdate( buffer, offset ) {
|
||||
|
||||
var dest = this.resolvedProperty;
|
||||
|
||||
for ( var i = 0, n = dest.length; i !== n; ++ i ) {
|
||||
|
||||
dest[ i ] = buffer[ offset ++ ];
|
||||
|
||||
}
|
||||
|
||||
this.targetObject.needsUpdate = true;
|
||||
|
||||
},
|
||||
|
||||
function setValue_array_setMatrixWorldNeedsUpdate( buffer, offset ) {
|
||||
|
||||
var dest = this.resolvedProperty;
|
||||
|
||||
for ( var i = 0, n = dest.length; i !== n; ++ i ) {
|
||||
|
||||
dest[ i ] = buffer[ offset ++ ];
|
||||
|
||||
}
|
||||
|
||||
this.targetObject.matrixWorldNeedsUpdate = true;
|
||||
|
||||
}
|
||||
|
||||
], [
|
||||
|
||||
// ArrayElement
|
||||
|
||||
function setValue_arrayElement( buffer, offset ) {
|
||||
|
||||
this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
|
||||
|
||||
},
|
||||
|
||||
function setValue_arrayElement_setNeedsUpdate( buffer, offset ) {
|
||||
|
||||
this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
|
||||
this.targetObject.needsUpdate = true;
|
||||
|
||||
},
|
||||
|
||||
function setValue_arrayElement_setMatrixWorldNeedsUpdate( buffer, offset ) {
|
||||
|
||||
this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
|
||||
this.targetObject.matrixWorldNeedsUpdate = true;
|
||||
|
||||
}
|
||||
|
||||
], [
|
||||
|
||||
// HasToFromArray
|
||||
|
||||
function setValue_fromArray( buffer, offset ) {
|
||||
|
||||
this.resolvedProperty.fromArray( buffer, offset );
|
||||
|
||||
},
|
||||
|
||||
function setValue_fromArray_setNeedsUpdate( buffer, offset ) {
|
||||
|
||||
this.resolvedProperty.fromArray( buffer, offset );
|
||||
this.targetObject.needsUpdate = true;
|
||||
|
||||
},
|
||||
|
||||
function setValue_fromArray_setMatrixWorldNeedsUpdate( buffer, offset ) {
|
||||
|
||||
this.resolvedProperty.fromArray( buffer, offset );
|
||||
this.targetObject.matrixWorldNeedsUpdate = true;
|
||||
|
||||
}
|
||||
|
||||
]
|
||||
|
||||
],
|
||||
|
||||
getValue: function getValue_unbound( targetArray, offset ) {
|
||||
|
||||
this.bind();
|
||||
this.getValue( targetArray, offset );
|
||||
|
||||
// Note: This class uses a State pattern on a per-method basis:
|
||||
// 'bind' sets 'this.getValue' / 'setValue' and shadows the
|
||||
// prototype version of these methods with one that represents
|
||||
// the bound state. When the property is not found, the methods
|
||||
// become no-ops.
|
||||
|
||||
},
|
||||
|
||||
setValue: function getValue_unbound( sourceArray, offset ) {
|
||||
|
||||
this.bind();
|
||||
this.setValue( sourceArray, offset );
|
||||
|
||||
},
|
||||
|
||||
// create getter / setter pair for a property in the scene graph
|
||||
bind: function () {
|
||||
|
||||
var targetObject = this.node,
|
||||
parsedPath = this.parsedPath,
|
||||
|
||||
objectName = parsedPath.objectName,
|
||||
propertyName = parsedPath.propertyName,
|
||||
propertyIndex = parsedPath.propertyIndex;
|
||||
|
||||
if ( ! targetObject ) {
|
||||
|
||||
targetObject = PropertyBinding.findNode( this.rootNode, parsedPath.nodeName ) || this.rootNode;
|
||||
|
||||
this.node = targetObject;
|
||||
|
||||
}
|
||||
|
||||
// set fail state so we can just 'return' on error
|
||||
this.getValue = this._getValue_unavailable;
|
||||
this.setValue = this._setValue_unavailable;
|
||||
|
||||
// ensure there is a value node
|
||||
if ( ! targetObject ) {
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Trying to update node for track: ' + this.path + ' but it wasn\'t found.' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if ( objectName ) {
|
||||
|
||||
var objectIndex = parsedPath.objectIndex;
|
||||
|
||||
// special cases were we need to reach deeper into the hierarchy to get the face materials....
|
||||
switch ( objectName ) {
|
||||
|
||||
case 'materials':
|
||||
|
||||
if ( ! targetObject.material ) {
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if ( ! targetObject.material.materials ) {
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Can not bind to material.materials as node.material does not have a materials array.', this );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
targetObject = targetObject.material.materials;
|
||||
|
||||
break;
|
||||
|
||||
case 'bones':
|
||||
|
||||
if ( ! targetObject.skeleton ) {
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Can not bind to bones as node does not have a skeleton.', this );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
// potential future optimization: skip this if propertyIndex is already an integer
|
||||
// and convert the integer string to a true integer.
|
||||
|
||||
targetObject = targetObject.skeleton.bones;
|
||||
|
||||
// support resolving morphTarget names into indices.
|
||||
for ( var i = 0; i < targetObject.length; i ++ ) {
|
||||
|
||||
if ( targetObject[ i ].name === objectIndex ) {
|
||||
|
||||
objectIndex = i;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
if ( targetObject[ objectName ] === undefined ) {
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Can not bind to objectName of node undefined.', this );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
targetObject = targetObject[ objectName ];
|
||||
|
||||
}
|
||||
|
||||
|
||||
if ( objectIndex !== undefined ) {
|
||||
|
||||
if ( targetObject[ objectIndex ] === undefined ) {
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Trying to bind to objectIndex of objectName, but is undefined.', this, targetObject );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
targetObject = targetObject[ objectIndex ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// resolve property
|
||||
var nodeProperty = targetObject[ propertyName ];
|
||||
|
||||
if ( nodeProperty === undefined ) {
|
||||
|
||||
var nodeName = parsedPath.nodeName;
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Trying to update property for track: ' + nodeName +
|
||||
'.' + propertyName + ' but it wasn\'t found.', targetObject );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
// determine versioning scheme
|
||||
var versioning = this.Versioning.None;
|
||||
|
||||
this.targetObject = targetObject;
|
||||
|
||||
if ( targetObject.needsUpdate !== undefined ) { // material
|
||||
|
||||
versioning = this.Versioning.NeedsUpdate;
|
||||
|
||||
} else if ( targetObject.matrixWorldNeedsUpdate !== undefined ) { // node transform
|
||||
|
||||
versioning = this.Versioning.MatrixWorldNeedsUpdate;
|
||||
|
||||
}
|
||||
|
||||
// determine how the property gets bound
|
||||
var bindingType = this.BindingType.Direct;
|
||||
|
||||
if ( propertyIndex !== undefined ) {
|
||||
|
||||
// access a sub element of the property array (only primitives are supported right now)
|
||||
|
||||
if ( propertyName === "morphTargetInfluences" ) {
|
||||
|
||||
// potential optimization, skip this if propertyIndex is already an integer, and convert the integer string to a true integer.
|
||||
|
||||
// support resolving morphTarget names into indices.
|
||||
if ( ! targetObject.geometry ) {
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.', this );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if ( targetObject.geometry.isBufferGeometry ) {
|
||||
|
||||
if ( ! targetObject.geometry.morphAttributes ) {
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphAttributes.', this );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
for ( var i = 0; i < this.node.geometry.morphAttributes.position.length; i ++ ) {
|
||||
|
||||
if ( targetObject.geometry.morphAttributes.position[ i ].name === propertyIndex ) {
|
||||
|
||||
propertyIndex = i;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
if ( ! targetObject.geometry.morphTargets ) {
|
||||
|
||||
console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphTargets.', this );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
for ( var i = 0; i < this.node.geometry.morphTargets.length; i ++ ) {
|
||||
|
||||
if ( targetObject.geometry.morphTargets[ i ].name === propertyIndex ) {
|
||||
|
||||
propertyIndex = i;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bindingType = this.BindingType.ArrayElement;
|
||||
|
||||
this.resolvedProperty = nodeProperty;
|
||||
this.propertyIndex = propertyIndex;
|
||||
|
||||
} else if ( nodeProperty.fromArray !== undefined && nodeProperty.toArray !== undefined ) {
|
||||
|
||||
// must use copy for Object3D.Euler/Quaternion
|
||||
|
||||
bindingType = this.BindingType.HasFromToArray;
|
||||
|
||||
this.resolvedProperty = nodeProperty;
|
||||
|
||||
} else if ( Array.isArray( nodeProperty ) ) {
|
||||
|
||||
bindingType = this.BindingType.EntireArray;
|
||||
|
||||
this.resolvedProperty = nodeProperty;
|
||||
|
||||
} else {
|
||||
|
||||
this.propertyName = propertyName;
|
||||
|
||||
}
|
||||
|
||||
// select getter / setter
|
||||
this.getValue = this.GetterByBindingType[ bindingType ];
|
||||
this.setValue = this.SetterByBindingTypeAndVersioning[ bindingType ][ versioning ];
|
||||
|
||||
},
|
||||
|
||||
unbind: function () {
|
||||
|
||||
this.node = null;
|
||||
|
||||
// back to the prototype version of getValue / setValue
|
||||
// note: avoiding to mutate the shape of 'this' via 'delete'
|
||||
this.getValue = this._getValue_unbound;
|
||||
this.setValue = this._setValue_unbound;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
//!\ DECLARE ALIAS AFTER assign prototype !
|
||||
Object.assign( PropertyBinding.prototype, {
|
||||
|
||||
// initial state of these methods that calls 'bind'
|
||||
_getValue_unbound: PropertyBinding.prototype.getValue,
|
||||
_setValue_unbound: PropertyBinding.prototype.setValue,
|
||||
|
||||
} );
|
||||
|
||||
export { PropertyBinding };
|
209
lib/animation/PropertyMixer.js
Normal file
@ -0,0 +1,209 @@
|
||||
import { Quaternion } from '../math/Quaternion.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* Buffered scene graph property that allows weighted accumulation.
|
||||
*
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function PropertyMixer( binding, typeName, valueSize ) {
|
||||
|
||||
this.binding = binding;
|
||||
this.valueSize = valueSize;
|
||||
|
||||
var bufferType = Float64Array,
|
||||
mixFunction;
|
||||
|
||||
switch ( typeName ) {
|
||||
|
||||
case 'quaternion':
|
||||
mixFunction = this._slerp;
|
||||
break;
|
||||
|
||||
case 'string':
|
||||
case 'bool':
|
||||
bufferType = Array;
|
||||
mixFunction = this._select;
|
||||
break;
|
||||
|
||||
default:
|
||||
mixFunction = this._lerp;
|
||||
|
||||
}
|
||||
|
||||
this.buffer = new bufferType( valueSize * 4 );
|
||||
// layout: [ incoming | accu0 | accu1 | orig ]
|
||||
//
|
||||
// interpolators can use .buffer as their .result
|
||||
// the data then goes to 'incoming'
|
||||
//
|
||||
// 'accu0' and 'accu1' are used frame-interleaved for
|
||||
// the cumulative result and are compared to detect
|
||||
// changes
|
||||
//
|
||||
// 'orig' stores the original state of the property
|
||||
|
||||
this._mixBufferRegion = mixFunction;
|
||||
|
||||
this.cumulativeWeight = 0;
|
||||
|
||||
this.useCount = 0;
|
||||
this.referenceCount = 0;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( PropertyMixer.prototype, {
|
||||
|
||||
// accumulate data in the 'incoming' region into 'accu<i>'
|
||||
accumulate: function ( accuIndex, weight ) {
|
||||
|
||||
// note: happily accumulating nothing when weight = 0, the caller knows
|
||||
// the weight and shouldn't have made the call in the first place
|
||||
|
||||
var buffer = this.buffer,
|
||||
stride = this.valueSize,
|
||||
offset = accuIndex * stride + stride,
|
||||
|
||||
currentWeight = this.cumulativeWeight;
|
||||
|
||||
if ( currentWeight === 0 ) {
|
||||
|
||||
// accuN := incoming * weight
|
||||
|
||||
for ( var i = 0; i !== stride; ++ i ) {
|
||||
|
||||
buffer[ offset + i ] = buffer[ i ];
|
||||
|
||||
}
|
||||
|
||||
currentWeight = weight;
|
||||
|
||||
} else {
|
||||
|
||||
// accuN := accuN + incoming * weight
|
||||
|
||||
currentWeight += weight;
|
||||
var mix = weight / currentWeight;
|
||||
this._mixBufferRegion( buffer, offset, 0, mix, stride );
|
||||
|
||||
}
|
||||
|
||||
this.cumulativeWeight = currentWeight;
|
||||
|
||||
},
|
||||
|
||||
// apply the state of 'accu<i>' to the binding when accus differ
|
||||
apply: function ( accuIndex ) {
|
||||
|
||||
var stride = this.valueSize,
|
||||
buffer = this.buffer,
|
||||
offset = accuIndex * stride + stride,
|
||||
|
||||
weight = this.cumulativeWeight,
|
||||
|
||||
binding = this.binding;
|
||||
|
||||
this.cumulativeWeight = 0;
|
||||
|
||||
if ( weight < 1 ) {
|
||||
|
||||
// accuN := accuN + original * ( 1 - cumulativeWeight )
|
||||
|
||||
var originalValueOffset = stride * 3;
|
||||
|
||||
this._mixBufferRegion(
|
||||
buffer, offset, originalValueOffset, 1 - weight, stride );
|
||||
|
||||
}
|
||||
|
||||
for ( var i = stride, e = stride + stride; i !== e; ++ i ) {
|
||||
|
||||
if ( buffer[ i ] !== buffer[ i + stride ] ) {
|
||||
|
||||
// value has changed -> update scene graph
|
||||
|
||||
binding.setValue( buffer, offset );
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
// remember the state of the bound property and copy it to both accus
|
||||
saveOriginalState: function () {
|
||||
|
||||
var binding = this.binding;
|
||||
|
||||
var buffer = this.buffer,
|
||||
stride = this.valueSize,
|
||||
|
||||
originalValueOffset = stride * 3;
|
||||
|
||||
binding.getValue( buffer, originalValueOffset );
|
||||
|
||||
// accu[0..1] := orig -- initially detect changes against the original
|
||||
for ( var i = stride, e = originalValueOffset; i !== e; ++ i ) {
|
||||
|
||||
buffer[ i ] = buffer[ originalValueOffset + ( i % stride ) ];
|
||||
|
||||
}
|
||||
|
||||
this.cumulativeWeight = 0;
|
||||
|
||||
},
|
||||
|
||||
// apply the state previously taken via 'saveOriginalState' to the binding
|
||||
restoreOriginalState: function () {
|
||||
|
||||
var originalValueOffset = this.valueSize * 3;
|
||||
this.binding.setValue( this.buffer, originalValueOffset );
|
||||
|
||||
},
|
||||
|
||||
|
||||
// mix functions
|
||||
|
||||
_select: function ( buffer, dstOffset, srcOffset, t, stride ) {
|
||||
|
||||
if ( t >= 0.5 ) {
|
||||
|
||||
for ( var i = 0; i !== stride; ++ i ) {
|
||||
|
||||
buffer[ dstOffset + i ] = buffer[ srcOffset + i ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_slerp: function ( buffer, dstOffset, srcOffset, t ) {
|
||||
|
||||
Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, srcOffset, t );
|
||||
|
||||
},
|
||||
|
||||
_lerp: function ( buffer, dstOffset, srcOffset, t, stride ) {
|
||||
|
||||
var s = 1 - t;
|
||||
|
||||
for ( var i = 0; i !== stride; ++ i ) {
|
||||
|
||||
var j = dstOffset + i;
|
||||
|
||||
buffer[ j ] = buffer[ j ] * s + buffer[ srcOffset + i ] * t;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { PropertyMixer };
|
38
lib/animation/tracks/BooleanKeyframeTrack.js
Normal file
@ -0,0 +1,38 @@
|
||||
import { InterpolateDiscrete } from '../../constants.js';
|
||||
import { KeyframeTrack } from '../KeyframeTrack.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* A Track of Boolean keyframe values.
|
||||
*
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function BooleanKeyframeTrack( name, times, values ) {
|
||||
|
||||
KeyframeTrack.call( this, name, times, values );
|
||||
|
||||
}
|
||||
|
||||
BooleanKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
|
||||
|
||||
constructor: BooleanKeyframeTrack,
|
||||
|
||||
ValueTypeName: 'bool',
|
||||
ValueBufferType: Array,
|
||||
|
||||
DefaultInterpolation: InterpolateDiscrete,
|
||||
|
||||
InterpolantFactoryMethodLinear: undefined,
|
||||
InterpolantFactoryMethodSmooth: undefined
|
||||
|
||||
// Note: Actually this track could have a optimized / compressed
|
||||
// representation of a single value and a custom interpolant that
|
||||
// computes "firstValue ^ isOdd( index )".
|
||||
|
||||
} );
|
||||
|
||||
export { BooleanKeyframeTrack };
|
34
lib/animation/tracks/ColorKeyframeTrack.js
Normal file
@ -0,0 +1,34 @@
|
||||
import { KeyframeTrack } from '../KeyframeTrack.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* A Track of keyframe values that represent color.
|
||||
*
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function ColorKeyframeTrack( name, times, values, interpolation ) {
|
||||
|
||||
KeyframeTrack.call( this, name, times, values, interpolation );
|
||||
|
||||
}
|
||||
|
||||
ColorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
|
||||
|
||||
constructor: ColorKeyframeTrack,
|
||||
|
||||
ValueTypeName: 'color'
|
||||
|
||||
// ValueBufferType is inherited
|
||||
|
||||
// DefaultInterpolation is inherited
|
||||
|
||||
// Note: Very basic implementation and nothing special yet.
|
||||
// However, this is the place for color space parameterization.
|
||||
|
||||
} );
|
||||
|
||||
export { ColorKeyframeTrack };
|
30
lib/animation/tracks/NumberKeyframeTrack.js
Normal file
@ -0,0 +1,30 @@
|
||||
import { KeyframeTrack } from '../KeyframeTrack.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* A Track of numeric keyframe values.
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function NumberKeyframeTrack( name, times, values, interpolation ) {
|
||||
|
||||
KeyframeTrack.call( this, name, times, values, interpolation );
|
||||
|
||||
}
|
||||
|
||||
NumberKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
|
||||
|
||||
constructor: NumberKeyframeTrack,
|
||||
|
||||
ValueTypeName: 'number'
|
||||
|
||||
// ValueBufferType is inherited
|
||||
|
||||
// DefaultInterpolation is inherited
|
||||
|
||||
} );
|
||||
|
||||
export { NumberKeyframeTrack };
|
40
lib/animation/tracks/QuaternionKeyframeTrack.js
Normal file
@ -0,0 +1,40 @@
|
||||
import { InterpolateLinear } from '../../constants.js';
|
||||
import { KeyframeTrack } from '../KeyframeTrack.js';
|
||||
import { QuaternionLinearInterpolant } from '../../math/interpolants/QuaternionLinearInterpolant.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* A Track of quaternion keyframe values.
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function QuaternionKeyframeTrack( name, times, values, interpolation ) {
|
||||
|
||||
KeyframeTrack.call( this, name, times, values, interpolation );
|
||||
|
||||
}
|
||||
|
||||
QuaternionKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
|
||||
|
||||
constructor: QuaternionKeyframeTrack,
|
||||
|
||||
ValueTypeName: 'quaternion',
|
||||
|
||||
// ValueBufferType is inherited
|
||||
|
||||
DefaultInterpolation: InterpolateLinear,
|
||||
|
||||
InterpolantFactoryMethodLinear: function ( result ) {
|
||||
|
||||
return new QuaternionLinearInterpolant( this.times, this.values, this.getValueSize(), result );
|
||||
|
||||
},
|
||||
|
||||
InterpolantFactoryMethodSmooth: undefined // not yet implemented
|
||||
|
||||
} );
|
||||
|
||||
export { QuaternionKeyframeTrack };
|
35
lib/animation/tracks/StringKeyframeTrack.js
Normal file
@ -0,0 +1,35 @@
|
||||
import { InterpolateDiscrete } from '../../constants.js';
|
||||
import { KeyframeTrack } from '../KeyframeTrack.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* A Track that interpolates Strings
|
||||
*
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function StringKeyframeTrack( name, times, values, interpolation ) {
|
||||
|
||||
KeyframeTrack.call( this, name, times, values, interpolation );
|
||||
|
||||
}
|
||||
|
||||
StringKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
|
||||
|
||||
constructor: StringKeyframeTrack,
|
||||
|
||||
ValueTypeName: 'string',
|
||||
ValueBufferType: Array,
|
||||
|
||||
DefaultInterpolation: InterpolateDiscrete,
|
||||
|
||||
InterpolantFactoryMethodLinear: undefined,
|
||||
|
||||
InterpolantFactoryMethodSmooth: undefined
|
||||
|
||||
} );
|
||||
|
||||
export { StringKeyframeTrack };
|
31
lib/animation/tracks/VectorKeyframeTrack.js
Normal file
@ -0,0 +1,31 @@
|
||||
import { KeyframeTrack } from '../KeyframeTrack.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* A Track of vectored keyframe values.
|
||||
*
|
||||
*
|
||||
* @author Ben Houston / http://clara.io/
|
||||
* @author David Sarno / http://lighthaus.us/
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function VectorKeyframeTrack( name, times, values, interpolation ) {
|
||||
|
||||
KeyframeTrack.call( this, name, times, values, interpolation );
|
||||
|
||||
}
|
||||
|
||||
VectorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
|
||||
|
||||
constructor: VectorKeyframeTrack,
|
||||
|
||||
ValueTypeName: 'vector'
|
||||
|
||||
// ValueBufferType is inherited
|
||||
|
||||
// DefaultInterpolation is inherited
|
||||
|
||||
} );
|
||||
|
||||
export { VectorKeyframeTrack };
|
319
lib/audio/Audio.js
Normal file
@ -0,0 +1,319 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author Reece Aaron Lecrivain / http://reecenotes.com/
|
||||
*/
|
||||
|
||||
import { Object3D } from '../core/Object3D.js';
|
||||
|
||||
function Audio( listener ) {
|
||||
|
||||
Object3D.call( this );
|
||||
|
||||
this.type = 'Audio';
|
||||
|
||||
this.context = listener.context;
|
||||
|
||||
this.gain = this.context.createGain();
|
||||
this.gain.connect( listener.getInput() );
|
||||
|
||||
this.autoplay = false;
|
||||
|
||||
this.buffer = null;
|
||||
this.loop = false;
|
||||
this.startTime = 0;
|
||||
this.offset = 0;
|
||||
this.playbackRate = 1;
|
||||
this.isPlaying = false;
|
||||
this.hasPlaybackControl = true;
|
||||
this.sourceType = 'empty';
|
||||
|
||||
this.filters = [];
|
||||
|
||||
}
|
||||
|
||||
Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
|
||||
|
||||
constructor: Audio,
|
||||
|
||||
getOutput: function () {
|
||||
|
||||
return this.gain;
|
||||
|
||||
},
|
||||
|
||||
setNodeSource: function ( audioNode ) {
|
||||
|
||||
this.hasPlaybackControl = false;
|
||||
this.sourceType = 'audioNode';
|
||||
this.source = audioNode;
|
||||
this.connect();
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setMediaElementSource: function ( mediaElement ) {
|
||||
|
||||
this.hasPlaybackControl = false;
|
||||
this.sourceType = 'mediaNode';
|
||||
this.source = this.context.createMediaElementSource( mediaElement );
|
||||
this.connect();
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setBuffer: function ( audioBuffer ) {
|
||||
|
||||
this.buffer = audioBuffer;
|
||||
this.sourceType = 'buffer';
|
||||
|
||||
if ( this.autoplay ) this.play();
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
play: function () {
|
||||
|
||||
if ( this.isPlaying === true ) {
|
||||
|
||||
console.warn( 'THREE.Audio: Audio is already playing.' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if ( this.hasPlaybackControl === false ) {
|
||||
|
||||
console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
var source = this.context.createBufferSource();
|
||||
|
||||
source.buffer = this.buffer;
|
||||
source.loop = this.loop;
|
||||
source.onended = this.onEnded.bind( this );
|
||||
source.playbackRate.setValueAtTime( this.playbackRate, this.startTime );
|
||||
this.startTime = this.context.currentTime;
|
||||
source.start( this.startTime, this.offset );
|
||||
|
||||
this.isPlaying = true;
|
||||
|
||||
this.source = source;
|
||||
|
||||
return this.connect();
|
||||
|
||||
},
|
||||
|
||||
pause: function () {
|
||||
|
||||
if ( this.hasPlaybackControl === false ) {
|
||||
|
||||
console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if ( this.isPlaying === true ) {
|
||||
|
||||
this.source.stop();
|
||||
this.source.onended = null;
|
||||
this.offset += ( this.context.currentTime - this.startTime ) * this.playbackRate;
|
||||
this.isPlaying = false;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
stop: function () {
|
||||
|
||||
if ( this.hasPlaybackControl === false ) {
|
||||
|
||||
console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
this.source.stop();
|
||||
this.source.onended = null;
|
||||
this.offset = 0;
|
||||
this.isPlaying = false;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
connect: function () {
|
||||
|
||||
if ( this.filters.length > 0 ) {
|
||||
|
||||
this.source.connect( this.filters[ 0 ] );
|
||||
|
||||
for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
|
||||
|
||||
this.filters[ i - 1 ].connect( this.filters[ i ] );
|
||||
|
||||
}
|
||||
|
||||
this.filters[ this.filters.length - 1 ].connect( this.getOutput() );
|
||||
|
||||
} else {
|
||||
|
||||
this.source.connect( this.getOutput() );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
disconnect: function () {
|
||||
|
||||
if ( this.filters.length > 0 ) {
|
||||
|
||||
this.source.disconnect( this.filters[ 0 ] );
|
||||
|
||||
for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
|
||||
|
||||
this.filters[ i - 1 ].disconnect( this.filters[ i ] );
|
||||
|
||||
}
|
||||
|
||||
this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() );
|
||||
|
||||
} else {
|
||||
|
||||
this.source.disconnect( this.getOutput() );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getFilters: function () {
|
||||
|
||||
return this.filters;
|
||||
|
||||
},
|
||||
|
||||
setFilters: function ( value ) {
|
||||
|
||||
if ( ! value ) value = [];
|
||||
|
||||
if ( this.isPlaying === true ) {
|
||||
|
||||
this.disconnect();
|
||||
this.filters = value;
|
||||
this.connect();
|
||||
|
||||
} else {
|
||||
|
||||
this.filters = value;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getFilter: function () {
|
||||
|
||||
return this.getFilters()[ 0 ];
|
||||
|
||||
},
|
||||
|
||||
setFilter: function ( filter ) {
|
||||
|
||||
return this.setFilters( filter ? [ filter ] : [] );
|
||||
|
||||
},
|
||||
|
||||
setPlaybackRate: function ( value ) {
|
||||
|
||||
if ( this.hasPlaybackControl === false ) {
|
||||
|
||||
console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
this.playbackRate = value;
|
||||
|
||||
if ( this.isPlaying === true ) {
|
||||
|
||||
this.source.playbackRate.setValueAtTime( this.playbackRate, this.context.currentTime );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getPlaybackRate: function () {
|
||||
|
||||
return this.playbackRate;
|
||||
|
||||
},
|
||||
|
||||
onEnded: function () {
|
||||
|
||||
this.isPlaying = false;
|
||||
|
||||
},
|
||||
|
||||
getLoop: function () {
|
||||
|
||||
if ( this.hasPlaybackControl === false ) {
|
||||
|
||||
console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
return this.loop;
|
||||
|
||||
},
|
||||
|
||||
setLoop: function ( value ) {
|
||||
|
||||
if ( this.hasPlaybackControl === false ) {
|
||||
|
||||
console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
this.loop = value;
|
||||
|
||||
if ( this.isPlaying === true ) {
|
||||
|
||||
this.source.loop = this.loop;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getVolume: function () {
|
||||
|
||||
return this.gain.gain.value;
|
||||
|
||||
},
|
||||
|
||||
setVolume: function ( value ) {
|
||||
|
||||
this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
export { Audio };
|
42
lib/audio/AudioAnalyser.js
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
function AudioAnalyser( audio, fftSize ) {
|
||||
|
||||
this.analyser = audio.context.createAnalyser();
|
||||
this.analyser.fftSize = fftSize !== undefined ? fftSize : 2048;
|
||||
|
||||
this.data = new Uint8Array( this.analyser.frequencyBinCount );
|
||||
|
||||
audio.getOutput().connect( this.analyser );
|
||||
|
||||
}
|
||||
|
||||
Object.assign( AudioAnalyser.prototype, {
|
||||
|
||||
getFrequencyData: function () {
|
||||
|
||||
this.analyser.getByteFrequencyData( this.data );
|
||||
|
||||
return this.data;
|
||||
|
||||
},
|
||||
|
||||
getAverageFrequency: function () {
|
||||
|
||||
var value = 0, data = this.getFrequencyData();
|
||||
|
||||
for ( var i = 0; i < data.length; i ++ ) {
|
||||
|
||||
value += data[ i ];
|
||||
|
||||
}
|
||||
|
||||
return value / data.length;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
export { AudioAnalyser };
|
29
lib/audio/AudioContext.js
Normal file
@ -0,0 +1,29 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
var context;
|
||||
|
||||
var AudioContext = {
|
||||
|
||||
getContext: function () {
|
||||
|
||||
if ( context === undefined ) {
|
||||
|
||||
context = new ( window.AudioContext || window.webkitAudioContext )();
|
||||
|
||||
}
|
||||
|
||||
return context;
|
||||
|
||||
},
|
||||
|
||||
setContext: function ( value ) {
|
||||
|
||||
context = value;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export { AudioContext };
|
135
lib/audio/AudioListener.js
Normal file
@ -0,0 +1,135 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { Quaternion } from '../math/Quaternion.js';
|
||||
import { Object3D } from '../core/Object3D.js';
|
||||
import { AudioContext } from './AudioContext.js';
|
||||
|
||||
function AudioListener() {
|
||||
|
||||
Object3D.call( this );
|
||||
|
||||
this.type = 'AudioListener';
|
||||
|
||||
this.context = AudioContext.getContext();
|
||||
|
||||
this.gain = this.context.createGain();
|
||||
this.gain.connect( this.context.destination );
|
||||
|
||||
this.filter = null;
|
||||
|
||||
}
|
||||
|
||||
AudioListener.prototype = Object.assign( Object.create( Object3D.prototype ), {
|
||||
|
||||
constructor: AudioListener,
|
||||
|
||||
getInput: function () {
|
||||
|
||||
return this.gain;
|
||||
|
||||
},
|
||||
|
||||
removeFilter: function ( ) {
|
||||
|
||||
if ( this.filter !== null ) {
|
||||
|
||||
this.gain.disconnect( this.filter );
|
||||
this.filter.disconnect( this.context.destination );
|
||||
this.gain.connect( this.context.destination );
|
||||
this.filter = null;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getFilter: function () {
|
||||
|
||||
return this.filter;
|
||||
|
||||
},
|
||||
|
||||
setFilter: function ( value ) {
|
||||
|
||||
if ( this.filter !== null ) {
|
||||
|
||||
this.gain.disconnect( this.filter );
|
||||
this.filter.disconnect( this.context.destination );
|
||||
|
||||
} else {
|
||||
|
||||
this.gain.disconnect( this.context.destination );
|
||||
|
||||
}
|
||||
|
||||
this.filter = value;
|
||||
this.gain.connect( this.filter );
|
||||
this.filter.connect( this.context.destination );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getMasterVolume: function () {
|
||||
|
||||
return this.gain.gain.value;
|
||||
|
||||
},
|
||||
|
||||
setMasterVolume: function ( value ) {
|
||||
|
||||
this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
updateMatrixWorld: ( function () {
|
||||
|
||||
var position = new Vector3();
|
||||
var quaternion = new Quaternion();
|
||||
var scale = new Vector3();
|
||||
|
||||
var orientation = new Vector3();
|
||||
|
||||
return function updateMatrixWorld( force ) {
|
||||
|
||||
Object3D.prototype.updateMatrixWorld.call( this, force );
|
||||
|
||||
var listener = this.context.listener;
|
||||
var up = this.up;
|
||||
|
||||
this.matrixWorld.decompose( position, quaternion, scale );
|
||||
|
||||
orientation.set( 0, 0, - 1 ).applyQuaternion( quaternion );
|
||||
|
||||
if ( listener.positionX ) {
|
||||
|
||||
listener.positionX.setValueAtTime( position.x, this.context.currentTime );
|
||||
listener.positionY.setValueAtTime( position.y, this.context.currentTime );
|
||||
listener.positionZ.setValueAtTime( position.z, this.context.currentTime );
|
||||
listener.forwardX.setValueAtTime( orientation.x, this.context.currentTime );
|
||||
listener.forwardY.setValueAtTime( orientation.y, this.context.currentTime );
|
||||
listener.forwardZ.setValueAtTime( orientation.z, this.context.currentTime );
|
||||
listener.upX.setValueAtTime( up.x, this.context.currentTime );
|
||||
listener.upY.setValueAtTime( up.y, this.context.currentTime );
|
||||
listener.upZ.setValueAtTime( up.z, this.context.currentTime );
|
||||
|
||||
} else {
|
||||
|
||||
listener.setPosition( position.x, position.y, position.z );
|
||||
listener.setOrientation( orientation.x, orientation.y, orientation.z, up.x, up.y, up.z );
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} )()
|
||||
|
||||
} );
|
||||
|
||||
export { AudioListener };
|
122
lib/audio/PositionalAudio.js
Normal file
@ -0,0 +1,122 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { Quaternion } from '../math/Quaternion.js';
|
||||
import { Audio } from './Audio.js';
|
||||
import { Object3D } from '../core/Object3D.js';
|
||||
|
||||
function PositionalAudio( listener ) {
|
||||
|
||||
Audio.call( this, listener );
|
||||
|
||||
this.panner = this.context.createPanner();
|
||||
this.panner.connect( this.gain );
|
||||
|
||||
}
|
||||
|
||||
PositionalAudio.prototype = Object.assign( Object.create( Audio.prototype ), {
|
||||
|
||||
constructor: PositionalAudio,
|
||||
|
||||
getOutput: function () {
|
||||
|
||||
return this.panner;
|
||||
|
||||
},
|
||||
|
||||
getRefDistance: function () {
|
||||
|
||||
return this.panner.refDistance;
|
||||
|
||||
},
|
||||
|
||||
setRefDistance: function ( value ) {
|
||||
|
||||
this.panner.refDistance = value;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getRolloffFactor: function () {
|
||||
|
||||
return this.panner.rolloffFactor;
|
||||
|
||||
},
|
||||
|
||||
setRolloffFactor: function ( value ) {
|
||||
|
||||
this.panner.rolloffFactor = value;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getDistanceModel: function () {
|
||||
|
||||
return this.panner.distanceModel;
|
||||
|
||||
},
|
||||
|
||||
setDistanceModel: function ( value ) {
|
||||
|
||||
this.panner.distanceModel = value;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getMaxDistance: function () {
|
||||
|
||||
return this.panner.maxDistance;
|
||||
|
||||
},
|
||||
|
||||
setMaxDistance: function ( value ) {
|
||||
|
||||
this.panner.maxDistance = value;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setDirectionalCone: function ( coneInnerAngle, coneOuterAngle, coneOuterGain ) {
|
||||
|
||||
this.panner.coneInnerAngle = coneInnerAngle;
|
||||
this.panner.coneOuterAngle = coneOuterAngle;
|
||||
this.panner.coneOuterGain = coneOuterGain;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
updateMatrixWorld: ( function () {
|
||||
|
||||
var position = new Vector3();
|
||||
var quaternion = new Quaternion();
|
||||
var scale = new Vector3();
|
||||
|
||||
var orientation = new Vector3();
|
||||
|
||||
return function updateMatrixWorld( force ) {
|
||||
|
||||
Object3D.prototype.updateMatrixWorld.call( this, force );
|
||||
|
||||
var panner = this.panner;
|
||||
this.matrixWorld.decompose( position, quaternion, scale );
|
||||
|
||||
orientation.set( 0, 0, 1 ).applyQuaternion( quaternion );
|
||||
|
||||
panner.setPosition( position.x, position.y, position.z );
|
||||
panner.setOrientation( orientation.x, orientation.y, orientation.z );
|
||||
|
||||
};
|
||||
|
||||
} )()
|
||||
|
||||
|
||||
} );
|
||||
|
||||
export { PositionalAudio };
|
24
lib/cameras/ArrayCamera.js
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
import { PerspectiveCamera } from './PerspectiveCamera.js';
|
||||
|
||||
function ArrayCamera( array ) {
|
||||
|
||||
PerspectiveCamera.call( this );
|
||||
|
||||
this.cameras = array || [];
|
||||
|
||||
}
|
||||
|
||||
ArrayCamera.prototype = Object.assign( Object.create( PerspectiveCamera.prototype ), {
|
||||
|
||||
constructor: ArrayCamera,
|
||||
|
||||
isArrayCamera: true
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { ArrayCamera };
|
76
lib/cameras/Camera.js
Normal file
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author mikael emtinger / http://gomo.se/
|
||||
* @author WestLangley / http://github.com/WestLangley
|
||||
*/
|
||||
|
||||
import { Matrix4 } from '../math/Matrix4.js';
|
||||
import { Object3D } from '../core/Object3D.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
|
||||
function Camera() {
|
||||
|
||||
Object3D.call( this );
|
||||
|
||||
this.type = 'Camera';
|
||||
|
||||
this.matrixWorldInverse = new Matrix4();
|
||||
|
||||
this.projectionMatrix = new Matrix4();
|
||||
this.projectionMatrixInverse = new Matrix4();
|
||||
|
||||
}
|
||||
|
||||
Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {
|
||||
|
||||
constructor: Camera,
|
||||
|
||||
isCamera: true,
|
||||
|
||||
copy: function ( source, recursive ) {
|
||||
|
||||
Object3D.prototype.copy.call( this, source, recursive );
|
||||
|
||||
this.matrixWorldInverse.copy( source.matrixWorldInverse );
|
||||
|
||||
this.projectionMatrix.copy( source.projectionMatrix );
|
||||
this.projectionMatrixInverse.copy( source.projectionMatrixInverse );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getWorldDirection: function ( target ) {
|
||||
|
||||
if ( target === undefined ) {
|
||||
|
||||
console.warn( 'THREE.Camera: .getWorldDirection() target is now required' );
|
||||
target = new Vector3();
|
||||
|
||||
}
|
||||
|
||||
this.updateMatrixWorld( true );
|
||||
|
||||
var e = this.matrixWorld.elements;
|
||||
|
||||
return target.set( - e[ 8 ], - e[ 9 ], - e[ 10 ] ).normalize();
|
||||
|
||||
},
|
||||
|
||||
updateMatrixWorld: function ( force ) {
|
||||
|
||||
Object3D.prototype.updateMatrixWorld.call( this, force );
|
||||
|
||||
this.matrixWorldInverse.getInverse( this.matrixWorld );
|
||||
|
||||
},
|
||||
|
||||
clone: function () {
|
||||
|
||||
return new this.constructor().copy( this );
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
export { Camera };
|
113
lib/cameras/CubeCamera.js
Normal file
@ -0,0 +1,113 @@
|
||||
import { Object3D } from '../core/Object3D.js';
|
||||
import { WebGLRenderTargetCube } from '../renderers/WebGLRenderTargetCube.js';
|
||||
import { LinearFilter, RGBFormat } from '../constants.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { PerspectiveCamera } from './PerspectiveCamera.js';
|
||||
|
||||
/**
|
||||
* Camera for rendering cube maps
|
||||
* - renders scene into axis-aligned cube
|
||||
*
|
||||
* @author alteredq / http://alteredqualia.com/
|
||||
*/
|
||||
|
||||
function CubeCamera( near, far, cubeResolution, options ) {
|
||||
|
||||
Object3D.call( this );
|
||||
|
||||
this.type = 'CubeCamera';
|
||||
|
||||
var fov = 90, aspect = 1;
|
||||
|
||||
var cameraPX = new PerspectiveCamera( fov, aspect, near, far );
|
||||
cameraPX.up.set( 0, - 1, 0 );
|
||||
cameraPX.lookAt( new Vector3( 1, 0, 0 ) );
|
||||
this.add( cameraPX );
|
||||
|
||||
var cameraNX = new PerspectiveCamera( fov, aspect, near, far );
|
||||
cameraNX.up.set( 0, - 1, 0 );
|
||||
cameraNX.lookAt( new Vector3( - 1, 0, 0 ) );
|
||||
this.add( cameraNX );
|
||||
|
||||
var cameraPY = new PerspectiveCamera( fov, aspect, near, far );
|
||||
cameraPY.up.set( 0, 0, 1 );
|
||||
cameraPY.lookAt( new Vector3( 0, 1, 0 ) );
|
||||
this.add( cameraPY );
|
||||
|
||||
var cameraNY = new PerspectiveCamera( fov, aspect, near, far );
|
||||
cameraNY.up.set( 0, 0, - 1 );
|
||||
cameraNY.lookAt( new Vector3( 0, - 1, 0 ) );
|
||||
this.add( cameraNY );
|
||||
|
||||
var cameraPZ = new PerspectiveCamera( fov, aspect, near, far );
|
||||
cameraPZ.up.set( 0, - 1, 0 );
|
||||
cameraPZ.lookAt( new Vector3( 0, 0, 1 ) );
|
||||
this.add( cameraPZ );
|
||||
|
||||
var cameraNZ = new PerspectiveCamera( fov, aspect, near, far );
|
||||
cameraNZ.up.set( 0, - 1, 0 );
|
||||
cameraNZ.lookAt( new Vector3( 0, 0, - 1 ) );
|
||||
this.add( cameraNZ );
|
||||
|
||||
options = options || { format: RGBFormat, magFilter: LinearFilter, minFilter: LinearFilter };
|
||||
|
||||
this.renderTarget = new WebGLRenderTargetCube( cubeResolution, cubeResolution, options );
|
||||
this.renderTarget.texture.name = "CubeCamera";
|
||||
|
||||
this.update = function ( renderer, scene ) {
|
||||
|
||||
if ( this.parent === null ) this.updateMatrixWorld();
|
||||
|
||||
var renderTarget = this.renderTarget;
|
||||
var generateMipmaps = renderTarget.texture.generateMipmaps;
|
||||
|
||||
renderTarget.texture.generateMipmaps = false;
|
||||
|
||||
renderTarget.activeCubeFace = 0;
|
||||
renderer.render( scene, cameraPX, renderTarget );
|
||||
|
||||
renderTarget.activeCubeFace = 1;
|
||||
renderer.render( scene, cameraNX, renderTarget );
|
||||
|
||||
renderTarget.activeCubeFace = 2;
|
||||
renderer.render( scene, cameraPY, renderTarget );
|
||||
|
||||
renderTarget.activeCubeFace = 3;
|
||||
renderer.render( scene, cameraNY, renderTarget );
|
||||
|
||||
renderTarget.activeCubeFace = 4;
|
||||
renderer.render( scene, cameraPZ, renderTarget );
|
||||
|
||||
renderTarget.texture.generateMipmaps = generateMipmaps;
|
||||
|
||||
renderTarget.activeCubeFace = 5;
|
||||
renderer.render( scene, cameraNZ, renderTarget );
|
||||
|
||||
renderer.setRenderTarget( null );
|
||||
|
||||
};
|
||||
|
||||
this.clear = function ( renderer, color, depth, stencil ) {
|
||||
|
||||
var renderTarget = this.renderTarget;
|
||||
|
||||
for ( var i = 0; i < 6; i ++ ) {
|
||||
|
||||
renderTarget.activeCubeFace = i;
|
||||
renderer.setRenderTarget( renderTarget );
|
||||
|
||||
renderer.clear( color, depth, stencil );
|
||||
|
||||
}
|
||||
|
||||
renderer.setRenderTarget( null );
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
CubeCamera.prototype = Object.create( Object3D.prototype );
|
||||
CubeCamera.prototype.constructor = CubeCamera;
|
||||
|
||||
|
||||
export { CubeCamera };
|
147
lib/cameras/OrthographicCamera.js
Executable file
@ -0,0 +1,147 @@
|
||||
import { Camera } from './Camera.js';
|
||||
import { Object3D } from '../core/Object3D.js';
|
||||
|
||||
/**
|
||||
* @author alteredq / http://alteredqualia.com/
|
||||
* @author arose / http://github.com/arose
|
||||
*/
|
||||
|
||||
function OrthographicCamera( left, right, top, bottom, near, far ) {
|
||||
|
||||
Camera.call( this );
|
||||
|
||||
this.type = 'OrthographicCamera';
|
||||
|
||||
this.zoom = 1;
|
||||
this.view = null;
|
||||
|
||||
this.left = ( left !== undefined ) ? left : - 1;
|
||||
this.right = ( right !== undefined ) ? right : 1;
|
||||
this.top = ( top !== undefined ) ? top : 1;
|
||||
this.bottom = ( bottom !== undefined ) ? bottom : - 1;
|
||||
|
||||
this.near = ( near !== undefined ) ? near : 0.1;
|
||||
this.far = ( far !== undefined ) ? far : 2000;
|
||||
|
||||
this.updateProjectionMatrix();
|
||||
|
||||
}
|
||||
|
||||
OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
|
||||
|
||||
constructor: OrthographicCamera,
|
||||
|
||||
isOrthographicCamera: true,
|
||||
|
||||
copy: function ( source, recursive ) {
|
||||
|
||||
Camera.prototype.copy.call( this, source, recursive );
|
||||
|
||||
this.left = source.left;
|
||||
this.right = source.right;
|
||||
this.top = source.top;
|
||||
this.bottom = source.bottom;
|
||||
this.near = source.near;
|
||||
this.far = source.far;
|
||||
|
||||
this.zoom = source.zoom;
|
||||
this.view = source.view === null ? null : Object.assign( {}, source.view );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
|
||||
|
||||
if ( this.view === null ) {
|
||||
|
||||
this.view = {
|
||||
enabled: true,
|
||||
fullWidth: 1,
|
||||
fullHeight: 1,
|
||||
offsetX: 0,
|
||||
offsetY: 0,
|
||||
width: 1,
|
||||
height: 1
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
this.view.enabled = true;
|
||||
this.view.fullWidth = fullWidth;
|
||||
this.view.fullHeight = fullHeight;
|
||||
this.view.offsetX = x;
|
||||
this.view.offsetY = y;
|
||||
this.view.width = width;
|
||||
this.view.height = height;
|
||||
|
||||
this.updateProjectionMatrix();
|
||||
|
||||
},
|
||||
|
||||
clearViewOffset: function () {
|
||||
|
||||
if ( this.view !== null ) {
|
||||
|
||||
this.view.enabled = false;
|
||||
|
||||
}
|
||||
|
||||
this.updateProjectionMatrix();
|
||||
|
||||
},
|
||||
|
||||
updateProjectionMatrix: function () {
|
||||
|
||||
var dx = ( this.right - this.left ) / ( 2 * this.zoom );
|
||||
var dy = ( this.top - this.bottom ) / ( 2 * this.zoom );
|
||||
var cx = ( this.right + this.left ) / 2;
|
||||
var cy = ( this.top + this.bottom ) / 2;
|
||||
|
||||
var left = cx - dx;
|
||||
var right = cx + dx;
|
||||
var top = cy + dy;
|
||||
var bottom = cy - dy;
|
||||
|
||||
if ( this.view !== null && this.view.enabled ) {
|
||||
|
||||
var zoomW = this.zoom / ( this.view.width / this.view.fullWidth );
|
||||
var zoomH = this.zoom / ( this.view.height / this.view.fullHeight );
|
||||
var scaleW = ( this.right - this.left ) / this.view.width;
|
||||
var scaleH = ( this.top - this.bottom ) / this.view.height;
|
||||
|
||||
left += scaleW * ( this.view.offsetX / zoomW );
|
||||
right = left + scaleW * ( this.view.width / zoomW );
|
||||
top -= scaleH * ( this.view.offsetY / zoomH );
|
||||
bottom = top - scaleH * ( this.view.height / zoomH );
|
||||
|
||||
}
|
||||
|
||||
this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far );
|
||||
|
||||
this.projectionMatrixInverse.getInverse( this.projectionMatrix );
|
||||
|
||||
},
|
||||
|
||||
toJSON: function ( meta ) {
|
||||
|
||||
var data = Object3D.prototype.toJSON.call( this, meta );
|
||||
|
||||
data.object.zoom = this.zoom;
|
||||
data.object.left = this.left;
|
||||
data.object.right = this.right;
|
||||
data.object.top = this.top;
|
||||
data.object.bottom = this.bottom;
|
||||
data.object.near = this.near;
|
||||
data.object.far = this.far;
|
||||
|
||||
if ( this.view !== null ) data.object.view = Object.assign( {}, this.view );
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { OrthographicCamera };
|
244
lib/cameras/PerspectiveCamera.js
Executable file
@ -0,0 +1,244 @@
|
||||
import { Camera } from './Camera.js';
|
||||
import { Object3D } from '../core/Object3D.js';
|
||||
import { _Math } from '../math/Math.js';
|
||||
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author greggman / http://games.greggman.com/
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
* @author tschw
|
||||
*/
|
||||
|
||||
function PerspectiveCamera( fov, aspect, near, far ) {
|
||||
|
||||
Camera.call( this );
|
||||
|
||||
this.type = 'PerspectiveCamera';
|
||||
|
||||
this.fov = fov !== undefined ? fov : 50;
|
||||
this.zoom = 1;
|
||||
|
||||
this.near = near !== undefined ? near : 0.1;
|
||||
this.far = far !== undefined ? far : 2000;
|
||||
this.focus = 10;
|
||||
|
||||
this.aspect = aspect !== undefined ? aspect : 1;
|
||||
this.view = null;
|
||||
|
||||
this.filmGauge = 35; // width of the film (default in millimeters)
|
||||
this.filmOffset = 0; // horizontal film offset (same unit as gauge)
|
||||
|
||||
this.updateProjectionMatrix();
|
||||
|
||||
}
|
||||
|
||||
PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
|
||||
|
||||
constructor: PerspectiveCamera,
|
||||
|
||||
isPerspectiveCamera: true,
|
||||
|
||||
copy: function ( source, recursive ) {
|
||||
|
||||
Camera.prototype.copy.call( this, source, recursive );
|
||||
|
||||
this.fov = source.fov;
|
||||
this.zoom = source.zoom;
|
||||
|
||||
this.near = source.near;
|
||||
this.far = source.far;
|
||||
this.focus = source.focus;
|
||||
|
||||
this.aspect = source.aspect;
|
||||
this.view = source.view === null ? null : Object.assign( {}, source.view );
|
||||
|
||||
this.filmGauge = source.filmGauge;
|
||||
this.filmOffset = source.filmOffset;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the FOV by focal length in respect to the current .filmGauge.
|
||||
*
|
||||
* The default film gauge is 35, so that the focal length can be specified for
|
||||
* a 35mm (full frame) camera.
|
||||
*
|
||||
* Values for focal length and film gauge must have the same unit.
|
||||
*/
|
||||
setFocalLength: function ( focalLength ) {
|
||||
|
||||
// see http://www.bobatkins.com/photography/technical/field_of_view.html
|
||||
var vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;
|
||||
|
||||
this.fov = _Math.RAD2DEG * 2 * Math.atan( vExtentSlope );
|
||||
this.updateProjectionMatrix();
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Calculates the focal length from the current .fov and .filmGauge.
|
||||
*/
|
||||
getFocalLength: function () {
|
||||
|
||||
var vExtentSlope = Math.tan( _Math.DEG2RAD * 0.5 * this.fov );
|
||||
|
||||
return 0.5 * this.getFilmHeight() / vExtentSlope;
|
||||
|
||||
},
|
||||
|
||||
getEffectiveFOV: function () {
|
||||
|
||||
return _Math.RAD2DEG * 2 * Math.atan(
|
||||
Math.tan( _Math.DEG2RAD * 0.5 * this.fov ) / this.zoom );
|
||||
|
||||
},
|
||||
|
||||
getFilmWidth: function () {
|
||||
|
||||
// film not completely covered in portrait format (aspect < 1)
|
||||
return this.filmGauge * Math.min( this.aspect, 1 );
|
||||
|
||||
},
|
||||
|
||||
getFilmHeight: function () {
|
||||
|
||||
// film not completely covered in landscape format (aspect > 1)
|
||||
return this.filmGauge / Math.max( this.aspect, 1 );
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets an offset in a larger frustum. This is useful for multi-window or
|
||||
* multi-monitor/multi-machine setups.
|
||||
*
|
||||
* For example, if you have 3x2 monitors and each monitor is 1920x1080 and
|
||||
* the monitors are in grid like this
|
||||
*
|
||||
* +---+---+---+
|
||||
* | A | B | C |
|
||||
* +---+---+---+
|
||||
* | D | E | F |
|
||||
* +---+---+---+
|
||||
*
|
||||
* then for each monitor you would call it like this
|
||||
*
|
||||
* var w = 1920;
|
||||
* var h = 1080;
|
||||
* var fullWidth = w * 3;
|
||||
* var fullHeight = h * 2;
|
||||
*
|
||||
* --A--
|
||||
* camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
|
||||
* --B--
|
||||
* camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
|
||||
* --C--
|
||||
* camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
|
||||
* --D--
|
||||
* camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
|
||||
* --E--
|
||||
* camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
|
||||
* --F--
|
||||
* camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
|
||||
*
|
||||
* Note there is no reason monitors have to be the same size or in a grid.
|
||||
*/
|
||||
setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
|
||||
|
||||
this.aspect = fullWidth / fullHeight;
|
||||
|
||||
if ( this.view === null ) {
|
||||
|
||||
this.view = {
|
||||
enabled: true,
|
||||
fullWidth: 1,
|
||||
fullHeight: 1,
|
||||
offsetX: 0,
|
||||
offsetY: 0,
|
||||
width: 1,
|
||||
height: 1
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
this.view.enabled = true;
|
||||
this.view.fullWidth = fullWidth;
|
||||
this.view.fullHeight = fullHeight;
|
||||
this.view.offsetX = x;
|
||||
this.view.offsetY = y;
|
||||
this.view.width = width;
|
||||
this.view.height = height;
|
||||
|
||||
this.updateProjectionMatrix();
|
||||
|
||||
},
|
||||
|
||||
clearViewOffset: function () {
|
||||
|
||||
if ( this.view !== null ) {
|
||||
|
||||
this.view.enabled = false;
|
||||
|
||||
}
|
||||
|
||||
this.updateProjectionMatrix();
|
||||
|
||||
},
|
||||
|
||||
updateProjectionMatrix: function () {
|
||||
|
||||
var near = this.near,
|
||||
top = near * Math.tan( _Math.DEG2RAD * 0.5 * this.fov ) / this.zoom,
|
||||
height = 2 * top,
|
||||
width = this.aspect * height,
|
||||
left = - 0.5 * width,
|
||||
view = this.view;
|
||||
|
||||
if ( this.view !== null && this.view.enabled ) {
|
||||
|
||||
var fullWidth = view.fullWidth,
|
||||
fullHeight = view.fullHeight;
|
||||
|
||||
left += view.offsetX * width / fullWidth;
|
||||
top -= view.offsetY * height / fullHeight;
|
||||
width *= view.width / fullWidth;
|
||||
height *= view.height / fullHeight;
|
||||
|
||||
}
|
||||
|
||||
var skew = this.filmOffset;
|
||||
if ( skew !== 0 ) left += near * skew / this.getFilmWidth();
|
||||
|
||||
this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far );
|
||||
|
||||
this.projectionMatrixInverse.getInverse( this.projectionMatrix );
|
||||
|
||||
},
|
||||
|
||||
toJSON: function ( meta ) {
|
||||
|
||||
var data = Object3D.prototype.toJSON.call( this, meta );
|
||||
|
||||
data.object.fov = this.fov;
|
||||
data.object.zoom = this.zoom;
|
||||
|
||||
data.object.near = this.near;
|
||||
data.object.far = this.far;
|
||||
data.object.focus = this.focus;
|
||||
|
||||
data.object.aspect = this.aspect;
|
||||
|
||||
if ( this.view !== null ) data.object.view = Object.assign( {}, this.view );
|
||||
|
||||
data.object.filmGauge = this.filmGauge;
|
||||
data.object.filmOffset = this.filmOffset;
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { PerspectiveCamera };
|
98
lib/cameras/StereoCamera.js
Normal file
@ -0,0 +1,98 @@
|
||||
import { Matrix4 } from '../math/Matrix4.js';
|
||||
import { _Math } from '../math/Math.js';
|
||||
import { PerspectiveCamera } from './PerspectiveCamera.js';
|
||||
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
function StereoCamera() {
|
||||
|
||||
this.type = 'StereoCamera';
|
||||
|
||||
this.aspect = 1;
|
||||
|
||||
this.eyeSep = 0.064;
|
||||
|
||||
this.cameraL = new PerspectiveCamera();
|
||||
this.cameraL.layers.enable( 1 );
|
||||
this.cameraL.matrixAutoUpdate = false;
|
||||
|
||||
this.cameraR = new PerspectiveCamera();
|
||||
this.cameraR.layers.enable( 2 );
|
||||
this.cameraR.matrixAutoUpdate = false;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( StereoCamera.prototype, {
|
||||
|
||||
update: ( function () {
|
||||
|
||||
var instance, focus, fov, aspect, near, far, zoom, eyeSep;
|
||||
|
||||
var eyeRight = new Matrix4();
|
||||
var eyeLeft = new Matrix4();
|
||||
|
||||
return function update( camera ) {
|
||||
|
||||
var needsUpdate = instance !== this || focus !== camera.focus || fov !== camera.fov ||
|
||||
aspect !== camera.aspect * this.aspect || near !== camera.near ||
|
||||
far !== camera.far || zoom !== camera.zoom || eyeSep !== this.eyeSep;
|
||||
|
||||
if ( needsUpdate ) {
|
||||
|
||||
instance = this;
|
||||
focus = camera.focus;
|
||||
fov = camera.fov;
|
||||
aspect = camera.aspect * this.aspect;
|
||||
near = camera.near;
|
||||
far = camera.far;
|
||||
zoom = camera.zoom;
|
||||
|
||||
// Off-axis stereoscopic effect based on
|
||||
// http://paulbourke.net/stereographics/stereorender/
|
||||
|
||||
var projectionMatrix = camera.projectionMatrix.clone();
|
||||
eyeSep = this.eyeSep / 2;
|
||||
var eyeSepOnProjection = eyeSep * near / focus;
|
||||
var ymax = ( near * Math.tan( _Math.DEG2RAD * fov * 0.5 ) ) / zoom;
|
||||
var xmin, xmax;
|
||||
|
||||
// translate xOffset
|
||||
|
||||
eyeLeft.elements[ 12 ] = - eyeSep;
|
||||
eyeRight.elements[ 12 ] = eyeSep;
|
||||
|
||||
// for left eye
|
||||
|
||||
xmin = - ymax * aspect + eyeSepOnProjection;
|
||||
xmax = ymax * aspect + eyeSepOnProjection;
|
||||
|
||||
projectionMatrix.elements[ 0 ] = 2 * near / ( xmax - xmin );
|
||||
projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
|
||||
|
||||
this.cameraL.projectionMatrix.copy( projectionMatrix );
|
||||
|
||||
// for right eye
|
||||
|
||||
xmin = - ymax * aspect - eyeSepOnProjection;
|
||||
xmax = ymax * aspect - eyeSepOnProjection;
|
||||
|
||||
projectionMatrix.elements[ 0 ] = 2 * near / ( xmax - xmin );
|
||||
projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
|
||||
|
||||
this.cameraR.projectionMatrix.copy( projectionMatrix );
|
||||
|
||||
}
|
||||
|
||||
this.cameraL.matrixWorld.copy( camera.matrixWorld ).multiply( eyeLeft );
|
||||
this.cameraR.matrixWorld.copy( camera.matrixWorld ).multiply( eyeRight );
|
||||
|
||||
};
|
||||
|
||||
} )()
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { StereoCamera };
|
142
lib/constants.js
Normal file
@ -0,0 +1,142 @@
|
||||
export var REVISION = '98dev';
|
||||
export var MOUSE = { LEFT: 0, MIDDLE: 1, RIGHT: 2 };
|
||||
export var CullFaceNone = 0;
|
||||
export var CullFaceBack = 1;
|
||||
export var CullFaceFront = 2;
|
||||
export var CullFaceFrontBack = 3;
|
||||
export var FrontFaceDirectionCW = 0;
|
||||
export var FrontFaceDirectionCCW = 1;
|
||||
export var BasicShadowMap = 0;
|
||||
export var PCFShadowMap = 1;
|
||||
export var PCFSoftShadowMap = 2;
|
||||
export var FrontSide = 0;
|
||||
export var BackSide = 1;
|
||||
export var DoubleSide = 2;
|
||||
export var FlatShading = 1;
|
||||
export var SmoothShading = 2;
|
||||
export var NoColors = 0;
|
||||
export var FaceColors = 1;
|
||||
export var VertexColors = 2;
|
||||
export var NoBlending = 0;
|
||||
export var NormalBlending = 1;
|
||||
export var AdditiveBlending = 2;
|
||||
export var SubtractiveBlending = 3;
|
||||
export var MultiplyBlending = 4;
|
||||
export var CustomBlending = 5;
|
||||
export var AddEquation = 100;
|
||||
export var SubtractEquation = 101;
|
||||
export var ReverseSubtractEquation = 102;
|
||||
export var MinEquation = 103;
|
||||
export var MaxEquation = 104;
|
||||
export var ZeroFactor = 200;
|
||||
export var OneFactor = 201;
|
||||
export var SrcColorFactor = 202;
|
||||
export var OneMinusSrcColorFactor = 203;
|
||||
export var SrcAlphaFactor = 204;
|
||||
export var OneMinusSrcAlphaFactor = 205;
|
||||
export var DstAlphaFactor = 206;
|
||||
export var OneMinusDstAlphaFactor = 207;
|
||||
export var DstColorFactor = 208;
|
||||
export var OneMinusDstColorFactor = 209;
|
||||
export var SrcAlphaSaturateFactor = 210;
|
||||
export var NeverDepth = 0;
|
||||
export var AlwaysDepth = 1;
|
||||
export var LessDepth = 2;
|
||||
export var LessEqualDepth = 3;
|
||||
export var EqualDepth = 4;
|
||||
export var GreaterEqualDepth = 5;
|
||||
export var GreaterDepth = 6;
|
||||
export var NotEqualDepth = 7;
|
||||
export var MultiplyOperation = 0;
|
||||
export var MixOperation = 1;
|
||||
export var AddOperation = 2;
|
||||
export var NoToneMapping = 0;
|
||||
export var LinearToneMapping = 1;
|
||||
export var ReinhardToneMapping = 2;
|
||||
export var Uncharted2ToneMapping = 3;
|
||||
export var CineonToneMapping = 4;
|
||||
export var UVMapping = 300;
|
||||
export var CubeReflectionMapping = 301;
|
||||
export var CubeRefractionMapping = 302;
|
||||
export var EquirectangularReflectionMapping = 303;
|
||||
export var EquirectangularRefractionMapping = 304;
|
||||
export var SphericalReflectionMapping = 305;
|
||||
export var CubeUVReflectionMapping = 306;
|
||||
export var CubeUVRefractionMapping = 307;
|
||||
export var RepeatWrapping = 1000;
|
||||
export var ClampToEdgeWrapping = 1001;
|
||||
export var MirroredRepeatWrapping = 1002;
|
||||
export var NearestFilter = 1003;
|
||||
export var NearestMipMapNearestFilter = 1004;
|
||||
export var NearestMipMapLinearFilter = 1005;
|
||||
export var LinearFilter = 1006;
|
||||
export var LinearMipMapNearestFilter = 1007;
|
||||
export var LinearMipMapLinearFilter = 1008;
|
||||
export var UnsignedByteType = 1009;
|
||||
export var ByteType = 1010;
|
||||
export var ShortType = 1011;
|
||||
export var UnsignedShortType = 1012;
|
||||
export var IntType = 1013;
|
||||
export var UnsignedIntType = 1014;
|
||||
export var FloatType = 1015;
|
||||
export var HalfFloatType = 1016;
|
||||
export var UnsignedShort4444Type = 1017;
|
||||
export var UnsignedShort5551Type = 1018;
|
||||
export var UnsignedShort565Type = 1019;
|
||||
export var UnsignedInt248Type = 1020;
|
||||
export var AlphaFormat = 1021;
|
||||
export var RGBFormat = 1022;
|
||||
export var RGBAFormat = 1023;
|
||||
export var LuminanceFormat = 1024;
|
||||
export var LuminanceAlphaFormat = 1025;
|
||||
export var RGBEFormat = RGBAFormat;
|
||||
export var DepthFormat = 1026;
|
||||
export var DepthStencilFormat = 1027;
|
||||
export var RedFormat = 1028;
|
||||
export var RGB_S3TC_DXT1_Format = 33776;
|
||||
export var RGBA_S3TC_DXT1_Format = 33777;
|
||||
export var RGBA_S3TC_DXT3_Format = 33778;
|
||||
export var RGBA_S3TC_DXT5_Format = 33779;
|
||||
export var RGB_PVRTC_4BPPV1_Format = 35840;
|
||||
export var RGB_PVRTC_2BPPV1_Format = 35841;
|
||||
export var RGBA_PVRTC_4BPPV1_Format = 35842;
|
||||
export var RGBA_PVRTC_2BPPV1_Format = 35843;
|
||||
export var RGB_ETC1_Format = 36196;
|
||||
export var RGBA_ASTC_4x4_Format = 37808;
|
||||
export var RGBA_ASTC_5x4_Format = 37809;
|
||||
export var RGBA_ASTC_5x5_Format = 37810;
|
||||
export var RGBA_ASTC_6x5_Format = 37811;
|
||||
export var RGBA_ASTC_6x6_Format = 37812;
|
||||
export var RGBA_ASTC_8x5_Format = 37813;
|
||||
export var RGBA_ASTC_8x6_Format = 37814;
|
||||
export var RGBA_ASTC_8x8_Format = 37815;
|
||||
export var RGBA_ASTC_10x5_Format = 37816;
|
||||
export var RGBA_ASTC_10x6_Format = 37817;
|
||||
export var RGBA_ASTC_10x8_Format = 37818;
|
||||
export var RGBA_ASTC_10x10_Format = 37819;
|
||||
export var RGBA_ASTC_12x10_Format = 37820;
|
||||
export var RGBA_ASTC_12x12_Format = 37821;
|
||||
export var LoopOnce = 2200;
|
||||
export var LoopRepeat = 2201;
|
||||
export var LoopPingPong = 2202;
|
||||
export var InterpolateDiscrete = 2300;
|
||||
export var InterpolateLinear = 2301;
|
||||
export var InterpolateSmooth = 2302;
|
||||
export var ZeroCurvatureEnding = 2400;
|
||||
export var ZeroSlopeEnding = 2401;
|
||||
export var WrapAroundEnding = 2402;
|
||||
export var TrianglesDrawMode = 0;
|
||||
export var TriangleStripDrawMode = 1;
|
||||
export var TriangleFanDrawMode = 2;
|
||||
export var LinearEncoding = 3000;
|
||||
export var sRGBEncoding = 3001;
|
||||
export var GammaEncoding = 3007;
|
||||
export var RGBEEncoding = 3002;
|
||||
export var LogLuvEncoding = 3003;
|
||||
export var RGBM7Encoding = 3004;
|
||||
export var RGBM16Encoding = 3005;
|
||||
export var RGBDEncoding = 3006;
|
||||
export var BasicDepthPacking = 3200;
|
||||
export var RGBADepthPacking = 3201;
|
||||
export var TangentSpaceNormalMap = 0;
|
||||
export var ObjectSpaceNormalMap = 1;
|
430
lib/core/BufferAttribute.js
Normal file
@ -0,0 +1,430 @@
|
||||
import { Vector4 } from '../math/Vector4.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { Vector2 } from '../math/Vector2.js';
|
||||
import { Color } from '../math/Color.js';
|
||||
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
function BufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
if ( Array.isArray( array ) ) {
|
||||
|
||||
throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
|
||||
|
||||
}
|
||||
|
||||
this.name = '';
|
||||
|
||||
this.array = array;
|
||||
this.itemSize = itemSize;
|
||||
this.count = array !== undefined ? array.length / itemSize : 0;
|
||||
this.normalized = normalized === true;
|
||||
|
||||
this.dynamic = false;
|
||||
this.updateRange = { offset: 0, count: - 1 };
|
||||
|
||||
this.version = 0;
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperty( BufferAttribute.prototype, 'needsUpdate', {
|
||||
|
||||
set: function ( value ) {
|
||||
|
||||
if ( value === true ) this.version ++;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
Object.assign( BufferAttribute.prototype, {
|
||||
|
||||
isBufferAttribute: true,
|
||||
|
||||
onUploadCallback: function () {},
|
||||
|
||||
setArray: function ( array ) {
|
||||
|
||||
if ( Array.isArray( array ) ) {
|
||||
|
||||
throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
|
||||
|
||||
}
|
||||
|
||||
this.count = array !== undefined ? array.length / this.itemSize : 0;
|
||||
this.array = array;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setDynamic: function ( value ) {
|
||||
|
||||
this.dynamic = value;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
this.name = source.name;
|
||||
this.array = new source.array.constructor( source.array );
|
||||
this.itemSize = source.itemSize;
|
||||
this.count = source.count;
|
||||
this.normalized = source.normalized;
|
||||
|
||||
this.dynamic = source.dynamic;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
copyAt: function ( index1, attribute, index2 ) {
|
||||
|
||||
index1 *= this.itemSize;
|
||||
index2 *= attribute.itemSize;
|
||||
|
||||
for ( var i = 0, l = this.itemSize; i < l; i ++ ) {
|
||||
|
||||
this.array[ index1 + i ] = attribute.array[ index2 + i ];
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
copyArray: function ( array ) {
|
||||
|
||||
this.array.set( array );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
copyColorsArray: function ( colors ) {
|
||||
|
||||
var array = this.array, offset = 0;
|
||||
|
||||
for ( var i = 0, l = colors.length; i < l; i ++ ) {
|
||||
|
||||
var color = colors[ i ];
|
||||
|
||||
if ( color === undefined ) {
|
||||
|
||||
console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i );
|
||||
color = new Color();
|
||||
|
||||
}
|
||||
|
||||
array[ offset ++ ] = color.r;
|
||||
array[ offset ++ ] = color.g;
|
||||
array[ offset ++ ] = color.b;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
copyVector2sArray: function ( vectors ) {
|
||||
|
||||
var array = this.array, offset = 0;
|
||||
|
||||
for ( var i = 0, l = vectors.length; i < l; i ++ ) {
|
||||
|
||||
var vector = vectors[ i ];
|
||||
|
||||
if ( vector === undefined ) {
|
||||
|
||||
console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i );
|
||||
vector = new Vector2();
|
||||
|
||||
}
|
||||
|
||||
array[ offset ++ ] = vector.x;
|
||||
array[ offset ++ ] = vector.y;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
copyVector3sArray: function ( vectors ) {
|
||||
|
||||
var array = this.array, offset = 0;
|
||||
|
||||
for ( var i = 0, l = vectors.length; i < l; i ++ ) {
|
||||
|
||||
var vector = vectors[ i ];
|
||||
|
||||
if ( vector === undefined ) {
|
||||
|
||||
console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i );
|
||||
vector = new Vector3();
|
||||
|
||||
}
|
||||
|
||||
array[ offset ++ ] = vector.x;
|
||||
array[ offset ++ ] = vector.y;
|
||||
array[ offset ++ ] = vector.z;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
copyVector4sArray: function ( vectors ) {
|
||||
|
||||
var array = this.array, offset = 0;
|
||||
|
||||
for ( var i = 0, l = vectors.length; i < l; i ++ ) {
|
||||
|
||||
var vector = vectors[ i ];
|
||||
|
||||
if ( vector === undefined ) {
|
||||
|
||||
console.warn( 'THREE.BufferAttribute.copyVector4sArray(): vector is undefined', i );
|
||||
vector = new Vector4();
|
||||
|
||||
}
|
||||
|
||||
array[ offset ++ ] = vector.x;
|
||||
array[ offset ++ ] = vector.y;
|
||||
array[ offset ++ ] = vector.z;
|
||||
array[ offset ++ ] = vector.w;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
set: function ( value, offset ) {
|
||||
|
||||
if ( offset === undefined ) offset = 0;
|
||||
|
||||
this.array.set( value, offset );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getX: function ( index ) {
|
||||
|
||||
return this.array[ index * this.itemSize ];
|
||||
|
||||
},
|
||||
|
||||
setX: function ( index, x ) {
|
||||
|
||||
this.array[ index * this.itemSize ] = x;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getY: function ( index ) {
|
||||
|
||||
return this.array[ index * this.itemSize + 1 ];
|
||||
|
||||
},
|
||||
|
||||
setY: function ( index, y ) {
|
||||
|
||||
this.array[ index * this.itemSize + 1 ] = y;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getZ: function ( index ) {
|
||||
|
||||
return this.array[ index * this.itemSize + 2 ];
|
||||
|
||||
},
|
||||
|
||||
setZ: function ( index, z ) {
|
||||
|
||||
this.array[ index * this.itemSize + 2 ] = z;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getW: function ( index ) {
|
||||
|
||||
return this.array[ index * this.itemSize + 3 ];
|
||||
|
||||
},
|
||||
|
||||
setW: function ( index, w ) {
|
||||
|
||||
this.array[ index * this.itemSize + 3 ] = w;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setXY: function ( index, x, y ) {
|
||||
|
||||
index *= this.itemSize;
|
||||
|
||||
this.array[ index + 0 ] = x;
|
||||
this.array[ index + 1 ] = y;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setXYZ: function ( index, x, y, z ) {
|
||||
|
||||
index *= this.itemSize;
|
||||
|
||||
this.array[ index + 0 ] = x;
|
||||
this.array[ index + 1 ] = y;
|
||||
this.array[ index + 2 ] = z;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setXYZW: function ( index, x, y, z, w ) {
|
||||
|
||||
index *= this.itemSize;
|
||||
|
||||
this.array[ index + 0 ] = x;
|
||||
this.array[ index + 1 ] = y;
|
||||
this.array[ index + 2 ] = z;
|
||||
this.array[ index + 3 ] = w;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
onUpload: function ( callback ) {
|
||||
|
||||
this.onUploadCallback = callback;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
clone: function () {
|
||||
|
||||
return new this.constructor( this.array, this.itemSize ).copy( this );
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
//
|
||||
|
||||
function Int8BufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
BufferAttribute.call( this, new Int8Array( array ), itemSize, normalized );
|
||||
|
||||
}
|
||||
|
||||
Int8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
|
||||
Int8BufferAttribute.prototype.constructor = Int8BufferAttribute;
|
||||
|
||||
|
||||
function Uint8BufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
BufferAttribute.call( this, new Uint8Array( array ), itemSize, normalized );
|
||||
|
||||
}
|
||||
|
||||
Uint8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
|
||||
Uint8BufferAttribute.prototype.constructor = Uint8BufferAttribute;
|
||||
|
||||
|
||||
function Uint8ClampedBufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
BufferAttribute.call( this, new Uint8ClampedArray( array ), itemSize, normalized );
|
||||
|
||||
}
|
||||
|
||||
Uint8ClampedBufferAttribute.prototype = Object.create( BufferAttribute.prototype );
|
||||
Uint8ClampedBufferAttribute.prototype.constructor = Uint8ClampedBufferAttribute;
|
||||
|
||||
|
||||
function Int16BufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
BufferAttribute.call( this, new Int16Array( array ), itemSize, normalized );
|
||||
|
||||
}
|
||||
|
||||
Int16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
|
||||
Int16BufferAttribute.prototype.constructor = Int16BufferAttribute;
|
||||
|
||||
|
||||
function Uint16BufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized );
|
||||
|
||||
}
|
||||
|
||||
Uint16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
|
||||
Uint16BufferAttribute.prototype.constructor = Uint16BufferAttribute;
|
||||
|
||||
|
||||
function Int32BufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
BufferAttribute.call( this, new Int32Array( array ), itemSize, normalized );
|
||||
|
||||
}
|
||||
|
||||
Int32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
|
||||
Int32BufferAttribute.prototype.constructor = Int32BufferAttribute;
|
||||
|
||||
|
||||
function Uint32BufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
BufferAttribute.call( this, new Uint32Array( array ), itemSize, normalized );
|
||||
|
||||
}
|
||||
|
||||
Uint32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
|
||||
Uint32BufferAttribute.prototype.constructor = Uint32BufferAttribute;
|
||||
|
||||
|
||||
function Float32BufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
BufferAttribute.call( this, new Float32Array( array ), itemSize, normalized );
|
||||
|
||||
}
|
||||
|
||||
Float32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
|
||||
Float32BufferAttribute.prototype.constructor = Float32BufferAttribute;
|
||||
|
||||
|
||||
function Float64BufferAttribute( array, itemSize, normalized ) {
|
||||
|
||||
BufferAttribute.call( this, new Float64Array( array ), itemSize, normalized );
|
||||
|
||||
}
|
||||
|
||||
Float64BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
|
||||
Float64BufferAttribute.prototype.constructor = Float64BufferAttribute;
|
||||
|
||||
//
|
||||
|
||||
export {
|
||||
Float64BufferAttribute,
|
||||
Float32BufferAttribute,
|
||||
Uint32BufferAttribute,
|
||||
Int32BufferAttribute,
|
||||
Uint16BufferAttribute,
|
||||
Int16BufferAttribute,
|
||||
Uint8ClampedBufferAttribute,
|
||||
Uint8BufferAttribute,
|
||||
Int8BufferAttribute,
|
||||
BufferAttribute
|
||||
};
|
1129
lib/core/BufferGeometry.js
Normal file
73
lib/core/Clock.js
Normal file
@ -0,0 +1,73 @@
|
||||
/**
|
||||
* @author alteredq / http://alteredqualia.com/
|
||||
*/
|
||||
|
||||
function Clock( autoStart ) {
|
||||
|
||||
this.autoStart = ( autoStart !== undefined ) ? autoStart : true;
|
||||
|
||||
this.startTime = 0;
|
||||
this.oldTime = 0;
|
||||
this.elapsedTime = 0;
|
||||
|
||||
this.running = false;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( Clock.prototype, {
|
||||
|
||||
start: function () {
|
||||
|
||||
this.startTime = ( typeof performance === 'undefined' ? Date : performance ).now(); // see #10732
|
||||
|
||||
this.oldTime = this.startTime;
|
||||
this.elapsedTime = 0;
|
||||
this.running = true;
|
||||
|
||||
},
|
||||
|
||||
stop: function () {
|
||||
|
||||
this.getElapsedTime();
|
||||
this.running = false;
|
||||
this.autoStart = false;
|
||||
|
||||
},
|
||||
|
||||
getElapsedTime: function () {
|
||||
|
||||
this.getDelta();
|
||||
return this.elapsedTime;
|
||||
|
||||
},
|
||||
|
||||
getDelta: function () {
|
||||
|
||||
var diff = 0;
|
||||
|
||||
if ( this.autoStart && ! this.running ) {
|
||||
|
||||
this.start();
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
if ( this.running ) {
|
||||
|
||||
var newTime = ( typeof performance === 'undefined' ? Date : performance ).now();
|
||||
|
||||
diff = ( newTime - this.oldTime ) / 1000;
|
||||
this.oldTime = newTime;
|
||||
|
||||
this.elapsedTime += diff;
|
||||
|
||||
}
|
||||
|
||||
return diff;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { Clock };
|
274
lib/core/DirectGeometry.js
Normal file
@ -0,0 +1,274 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
import { Vector2 } from '../math/Vector2.js';
|
||||
|
||||
function DirectGeometry() {
|
||||
|
||||
this.vertices = [];
|
||||
this.normals = [];
|
||||
this.colors = [];
|
||||
this.uvs = [];
|
||||
this.uvs2 = [];
|
||||
|
||||
this.groups = [];
|
||||
|
||||
this.morphTargets = {};
|
||||
|
||||
this.skinWeights = [];
|
||||
this.skinIndices = [];
|
||||
|
||||
// this.lineDistances = [];
|
||||
|
||||
this.boundingBox = null;
|
||||
this.boundingSphere = null;
|
||||
|
||||
// update flags
|
||||
|
||||
this.verticesNeedUpdate = false;
|
||||
this.normalsNeedUpdate = false;
|
||||
this.colorsNeedUpdate = false;
|
||||
this.uvsNeedUpdate = false;
|
||||
this.groupsNeedUpdate = false;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( DirectGeometry.prototype, {
|
||||
|
||||
computeGroups: function ( geometry ) {
|
||||
|
||||
var group;
|
||||
var groups = [];
|
||||
var materialIndex = undefined;
|
||||
|
||||
var faces = geometry.faces;
|
||||
|
||||
for ( var i = 0; i < faces.length; i ++ ) {
|
||||
|
||||
var face = faces[ i ];
|
||||
|
||||
// materials
|
||||
|
||||
if ( face.materialIndex !== materialIndex ) {
|
||||
|
||||
materialIndex = face.materialIndex;
|
||||
|
||||
if ( group !== undefined ) {
|
||||
|
||||
group.count = ( i * 3 ) - group.start;
|
||||
groups.push( group );
|
||||
|
||||
}
|
||||
|
||||
group = {
|
||||
start: i * 3,
|
||||
materialIndex: materialIndex
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( group !== undefined ) {
|
||||
|
||||
group.count = ( i * 3 ) - group.start;
|
||||
groups.push( group );
|
||||
|
||||
}
|
||||
|
||||
this.groups = groups;
|
||||
|
||||
},
|
||||
|
||||
fromGeometry: function ( geometry ) {
|
||||
|
||||
var faces = geometry.faces;
|
||||
var vertices = geometry.vertices;
|
||||
var faceVertexUvs = geometry.faceVertexUvs;
|
||||
|
||||
var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
|
||||
var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
|
||||
|
||||
// morphs
|
||||
|
||||
var morphTargets = geometry.morphTargets;
|
||||
var morphTargetsLength = morphTargets.length;
|
||||
|
||||
var morphTargetsPosition;
|
||||
|
||||
if ( morphTargetsLength > 0 ) {
|
||||
|
||||
morphTargetsPosition = [];
|
||||
|
||||
for ( var i = 0; i < morphTargetsLength; i ++ ) {
|
||||
|
||||
morphTargetsPosition[ i ] = {
|
||||
name: morphTargets[ i ].name,
|
||||
data: []
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
this.morphTargets.position = morphTargetsPosition;
|
||||
|
||||
}
|
||||
|
||||
var morphNormals = geometry.morphNormals;
|
||||
var morphNormalsLength = morphNormals.length;
|
||||
|
||||
var morphTargetsNormal;
|
||||
|
||||
if ( morphNormalsLength > 0 ) {
|
||||
|
||||
morphTargetsNormal = [];
|
||||
|
||||
for ( var i = 0; i < morphNormalsLength; i ++ ) {
|
||||
|
||||
morphTargetsNormal[ i ] = {
|
||||
name: morphNormals[ i ].name,
|
||||
data: []
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
this.morphTargets.normal = morphTargetsNormal;
|
||||
|
||||
}
|
||||
|
||||
// skins
|
||||
|
||||
var skinIndices = geometry.skinIndices;
|
||||
var skinWeights = geometry.skinWeights;
|
||||
|
||||
var hasSkinIndices = skinIndices.length === vertices.length;
|
||||
var hasSkinWeights = skinWeights.length === vertices.length;
|
||||
|
||||
//
|
||||
|
||||
if ( vertices.length > 0 && faces.length === 0 ) {
|
||||
|
||||
console.error( 'THREE.DirectGeometry: Faceless geometries are not supported.' );
|
||||
|
||||
}
|
||||
|
||||
for ( var i = 0; i < faces.length; i ++ ) {
|
||||
|
||||
var face = faces[ i ];
|
||||
|
||||
this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
|
||||
|
||||
var vertexNormals = face.vertexNormals;
|
||||
|
||||
if ( vertexNormals.length === 3 ) {
|
||||
|
||||
this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
|
||||
|
||||
} else {
|
||||
|
||||
var normal = face.normal;
|
||||
|
||||
this.normals.push( normal, normal, normal );
|
||||
|
||||
}
|
||||
|
||||
var vertexColors = face.vertexColors;
|
||||
|
||||
if ( vertexColors.length === 3 ) {
|
||||
|
||||
this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
|
||||
|
||||
} else {
|
||||
|
||||
var color = face.color;
|
||||
|
||||
this.colors.push( color, color, color );
|
||||
|
||||
}
|
||||
|
||||
if ( hasFaceVertexUv === true ) {
|
||||
|
||||
var vertexUvs = faceVertexUvs[ 0 ][ i ];
|
||||
|
||||
if ( vertexUvs !== undefined ) {
|
||||
|
||||
this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
|
||||
|
||||
} else {
|
||||
|
||||
console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i );
|
||||
|
||||
this.uvs.push( new Vector2(), new Vector2(), new Vector2() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( hasFaceVertexUv2 === true ) {
|
||||
|
||||
var vertexUvs = faceVertexUvs[ 1 ][ i ];
|
||||
|
||||
if ( vertexUvs !== undefined ) {
|
||||
|
||||
this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
|
||||
|
||||
} else {
|
||||
|
||||
console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i );
|
||||
|
||||
this.uvs2.push( new Vector2(), new Vector2(), new Vector2() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// morphs
|
||||
|
||||
for ( var j = 0; j < morphTargetsLength; j ++ ) {
|
||||
|
||||
var morphTarget = morphTargets[ j ].vertices;
|
||||
|
||||
morphTargetsPosition[ j ].data.push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
|
||||
|
||||
}
|
||||
|
||||
for ( var j = 0; j < morphNormalsLength; j ++ ) {
|
||||
|
||||
var morphNormal = morphNormals[ j ].vertexNormals[ i ];
|
||||
|
||||
morphTargetsNormal[ j ].data.push( morphNormal.a, morphNormal.b, morphNormal.c );
|
||||
|
||||
}
|
||||
|
||||
// skins
|
||||
|
||||
if ( hasSkinIndices ) {
|
||||
|
||||
this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
|
||||
|
||||
}
|
||||
|
||||
if ( hasSkinWeights ) {
|
||||
|
||||
this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.computeGroups( geometry );
|
||||
|
||||
this.verticesNeedUpdate = geometry.verticesNeedUpdate;
|
||||
this.normalsNeedUpdate = geometry.normalsNeedUpdate;
|
||||
this.colorsNeedUpdate = geometry.colorsNeedUpdate;
|
||||
this.uvsNeedUpdate = geometry.uvsNeedUpdate;
|
||||
this.groupsNeedUpdate = geometry.groupsNeedUpdate;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { DirectGeometry };
|
86
lib/core/EventDispatcher.js
Normal file
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* https://github.com/mrdoob/eventdispatcher.js/
|
||||
*/
|
||||
|
||||
function EventDispatcher() {}
|
||||
|
||||
Object.assign( EventDispatcher.prototype, {
|
||||
|
||||
addEventListener: function ( type, listener ) {
|
||||
|
||||
if ( this._listeners === undefined ) this._listeners = {};
|
||||
|
||||
var listeners = this._listeners;
|
||||
|
||||
if ( listeners[ type ] === undefined ) {
|
||||
|
||||
listeners[ type ] = [];
|
||||
|
||||
}
|
||||
|
||||
if ( listeners[ type ].indexOf( listener ) === - 1 ) {
|
||||
|
||||
listeners[ type ].push( listener );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
hasEventListener: function ( type, listener ) {
|
||||
|
||||
if ( this._listeners === undefined ) return false;
|
||||
|
||||
var listeners = this._listeners;
|
||||
|
||||
return listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1;
|
||||
|
||||
},
|
||||
|
||||
removeEventListener: function ( type, listener ) {
|
||||
|
||||
if ( this._listeners === undefined ) return;
|
||||
|
||||
var listeners = this._listeners;
|
||||
var listenerArray = listeners[ type ];
|
||||
|
||||
if ( listenerArray !== undefined ) {
|
||||
|
||||
var index = listenerArray.indexOf( listener );
|
||||
|
||||
if ( index !== - 1 ) {
|
||||
|
||||
listenerArray.splice( index, 1 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
dispatchEvent: function ( event ) {
|
||||
|
||||
if ( this._listeners === undefined ) return;
|
||||
|
||||
var listeners = this._listeners;
|
||||
var listenerArray = listeners[ event.type ];
|
||||
|
||||
if ( listenerArray !== undefined ) {
|
||||
|
||||
event.target = this;
|
||||
|
||||
var array = listenerArray.slice( 0 );
|
||||
|
||||
for ( var i = 0, l = array.length; i < l; i ++ ) {
|
||||
|
||||
array[ i ].call( this, event );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { EventDispatcher };
|
63
lib/core/Face3.js
Normal file
@ -0,0 +1,63 @@
|
||||
import { Color } from '../math/Color.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author alteredq / http://alteredqualia.com/
|
||||
*/
|
||||
|
||||
function Face3( a, b, c, normal, color, materialIndex ) {
|
||||
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
|
||||
this.normal = ( normal && normal.isVector3 ) ? normal : new Vector3();
|
||||
this.vertexNormals = Array.isArray( normal ) ? normal : [];
|
||||
|
||||
this.color = ( color && color.isColor ) ? color : new Color();
|
||||
this.vertexColors = Array.isArray( color ) ? color : [];
|
||||
|
||||
this.materialIndex = materialIndex !== undefined ? materialIndex : 0;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( Face3.prototype, {
|
||||
|
||||
clone: function () {
|
||||
|
||||
return new this.constructor().copy( this );
|
||||
|
||||
},
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
this.a = source.a;
|
||||
this.b = source.b;
|
||||
this.c = source.c;
|
||||
|
||||
this.normal.copy( source.normal );
|
||||
this.color.copy( source.color );
|
||||
|
||||
this.materialIndex = source.materialIndex;
|
||||
|
||||
for ( var i = 0, il = source.vertexNormals.length; i < il; i ++ ) {
|
||||
|
||||
this.vertexNormals[ i ] = source.vertexNormals[ i ].clone();
|
||||
|
||||
}
|
||||
|
||||
for ( var i = 0, il = source.vertexColors.length; i < il; i ++ ) {
|
||||
|
||||
this.vertexColors[ i ] = source.vertexColors[ i ].clone();
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { Face3 };
|
1435
lib/core/Geometry.js
Normal file
45
lib/core/InstancedBufferAttribute.js
Normal file
@ -0,0 +1,45 @@
|
||||
import { BufferAttribute } from './BufferAttribute.js';
|
||||
|
||||
/**
|
||||
* @author benaadams / https://twitter.com/ben_a_adams
|
||||
*/
|
||||
|
||||
function InstancedBufferAttribute( array, itemSize, normalized, meshPerAttribute ) {
|
||||
|
||||
if ( typeof ( normalized ) === 'number' ) {
|
||||
|
||||
meshPerAttribute = normalized;
|
||||
|
||||
normalized = false;
|
||||
|
||||
console.error( 'THREE.InstancedBufferAttribute: The constructor now expects normalized as the third argument.' );
|
||||
|
||||
}
|
||||
|
||||
BufferAttribute.call( this, array, itemSize, normalized );
|
||||
|
||||
this.meshPerAttribute = meshPerAttribute || 1;
|
||||
|
||||
}
|
||||
|
||||
InstancedBufferAttribute.prototype = Object.assign( Object.create( BufferAttribute.prototype ), {
|
||||
|
||||
constructor: InstancedBufferAttribute,
|
||||
|
||||
isInstancedBufferAttribute: true,
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
BufferAttribute.prototype.copy.call( this, source );
|
||||
|
||||
this.meshPerAttribute = source.meshPerAttribute;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
|
||||
export { InstancedBufferAttribute };
|
40
lib/core/InstancedBufferGeometry.js
Normal file
@ -0,0 +1,40 @@
|
||||
import { BufferGeometry } from './BufferGeometry.js';
|
||||
|
||||
/**
|
||||
* @author benaadams / https://twitter.com/ben_a_adams
|
||||
*/
|
||||
|
||||
function InstancedBufferGeometry() {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'InstancedBufferGeometry';
|
||||
this.maxInstancedCount = undefined;
|
||||
|
||||
}
|
||||
|
||||
InstancedBufferGeometry.prototype = Object.assign( Object.create( BufferGeometry.prototype ), {
|
||||
|
||||
constructor: InstancedBufferGeometry,
|
||||
|
||||
isInstancedBufferGeometry: true,
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
BufferGeometry.prototype.copy.call( this, source );
|
||||
|
||||
this.maxInstancedCount = source.maxInstancedCount;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
clone: function () {
|
||||
|
||||
return new this.constructor().copy( this );
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
export { InstancedBufferGeometry };
|
33
lib/core/InstancedInterleavedBuffer.js
Normal file
@ -0,0 +1,33 @@
|
||||
import { InterleavedBuffer } from './InterleavedBuffer.js';
|
||||
|
||||
/**
|
||||
* @author benaadams / https://twitter.com/ben_a_adams
|
||||
*/
|
||||
|
||||
function InstancedInterleavedBuffer( array, stride, meshPerAttribute ) {
|
||||
|
||||
InterleavedBuffer.call( this, array, stride );
|
||||
|
||||
this.meshPerAttribute = meshPerAttribute || 1;
|
||||
|
||||
}
|
||||
|
||||
InstancedInterleavedBuffer.prototype = Object.assign( Object.create( InterleavedBuffer.prototype ), {
|
||||
|
||||
constructor: InstancedInterleavedBuffer,
|
||||
|
||||
isInstancedInterleavedBuffer: true,
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
InterleavedBuffer.prototype.copy.call( this, source );
|
||||
|
||||
this.meshPerAttribute = source.meshPerAttribute;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
export { InstancedInterleavedBuffer };
|
111
lib/core/InterleavedBuffer.js
Normal file
@ -0,0 +1,111 @@
|
||||
|
||||
/**
|
||||
* @author benaadams / https://twitter.com/ben_a_adams
|
||||
*/
|
||||
|
||||
function InterleavedBuffer( array, stride ) {
|
||||
|
||||
this.array = array;
|
||||
this.stride = stride;
|
||||
this.count = array !== undefined ? array.length / stride : 0;
|
||||
|
||||
this.dynamic = false;
|
||||
this.updateRange = { offset: 0, count: - 1 };
|
||||
|
||||
this.version = 0;
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperty( InterleavedBuffer.prototype, 'needsUpdate', {
|
||||
|
||||
set: function ( value ) {
|
||||
|
||||
if ( value === true ) this.version ++;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
Object.assign( InterleavedBuffer.prototype, {
|
||||
|
||||
isInterleavedBuffer: true,
|
||||
|
||||
onUploadCallback: function () {},
|
||||
|
||||
setArray: function ( array ) {
|
||||
|
||||
if ( Array.isArray( array ) ) {
|
||||
|
||||
throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
|
||||
|
||||
}
|
||||
|
||||
this.count = array !== undefined ? array.length / this.stride : 0;
|
||||
this.array = array;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setDynamic: function ( value ) {
|
||||
|
||||
this.dynamic = value;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
this.array = new source.array.constructor( source.array );
|
||||
this.count = source.count;
|
||||
this.stride = source.stride;
|
||||
this.dynamic = source.dynamic;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
copyAt: function ( index1, attribute, index2 ) {
|
||||
|
||||
index1 *= this.stride;
|
||||
index2 *= attribute.stride;
|
||||
|
||||
for ( var i = 0, l = this.stride; i < l; i ++ ) {
|
||||
|
||||
this.array[ index1 + i ] = attribute.array[ index2 + i ];
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
set: function ( value, offset ) {
|
||||
|
||||
if ( offset === undefined ) offset = 0;
|
||||
|
||||
this.array.set( value, offset );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
clone: function () {
|
||||
|
||||
return new this.constructor().copy( this );
|
||||
|
||||
},
|
||||
|
||||
onUpload: function ( callback ) {
|
||||
|
||||
this.onUploadCallback = callback;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { InterleavedBuffer };
|
139
lib/core/InterleavedBufferAttribute.js
Normal file
@ -0,0 +1,139 @@
|
||||
|
||||
/**
|
||||
* @author benaadams / https://twitter.com/ben_a_adams
|
||||
*/
|
||||
|
||||
function InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, normalized ) {
|
||||
|
||||
this.data = interleavedBuffer;
|
||||
this.itemSize = itemSize;
|
||||
this.offset = offset;
|
||||
|
||||
this.normalized = normalized === true;
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperties( InterleavedBufferAttribute.prototype, {
|
||||
|
||||
count: {
|
||||
|
||||
get: function () {
|
||||
|
||||
return this.data.count;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
array: {
|
||||
|
||||
get: function () {
|
||||
|
||||
return this.data.array;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
Object.assign( InterleavedBufferAttribute.prototype, {
|
||||
|
||||
isInterleavedBufferAttribute: true,
|
||||
|
||||
setX: function ( index, x ) {
|
||||
|
||||
this.data.array[ index * this.data.stride + this.offset ] = x;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setY: function ( index, y ) {
|
||||
|
||||
this.data.array[ index * this.data.stride + this.offset + 1 ] = y;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setZ: function ( index, z ) {
|
||||
|
||||
this.data.array[ index * this.data.stride + this.offset + 2 ] = z;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setW: function ( index, w ) {
|
||||
|
||||
this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getX: function ( index ) {
|
||||
|
||||
return this.data.array[ index * this.data.stride + this.offset ];
|
||||
|
||||
},
|
||||
|
||||
getY: function ( index ) {
|
||||
|
||||
return this.data.array[ index * this.data.stride + this.offset + 1 ];
|
||||
|
||||
},
|
||||
|
||||
getZ: function ( index ) {
|
||||
|
||||
return this.data.array[ index * this.data.stride + this.offset + 2 ];
|
||||
|
||||
},
|
||||
|
||||
getW: function ( index ) {
|
||||
|
||||
return this.data.array[ index * this.data.stride + this.offset + 3 ];
|
||||
|
||||
},
|
||||
|
||||
setXY: function ( index, x, y ) {
|
||||
|
||||
index = index * this.data.stride + this.offset;
|
||||
|
||||
this.data.array[ index + 0 ] = x;
|
||||
this.data.array[ index + 1 ] = y;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setXYZ: function ( index, x, y, z ) {
|
||||
|
||||
index = index * this.data.stride + this.offset;
|
||||
|
||||
this.data.array[ index + 0 ] = x;
|
||||
this.data.array[ index + 1 ] = y;
|
||||
this.data.array[ index + 2 ] = z;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setXYZW: function ( index, x, y, z, w ) {
|
||||
|
||||
index = index * this.data.stride + this.offset;
|
||||
|
||||
this.data.array[ index + 0 ] = x;
|
||||
this.data.array[ index + 1 ] = y;
|
||||
this.data.array[ index + 2 ] = z;
|
||||
this.data.array[ index + 3 ] = w;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { InterleavedBufferAttribute };
|
46
lib/core/Layers.js
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
function Layers() {
|
||||
|
||||
this.mask = 1 | 0;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( Layers.prototype, {
|
||||
|
||||
set: function ( channel ) {
|
||||
|
||||
this.mask = 1 << channel | 0;
|
||||
|
||||
},
|
||||
|
||||
enable: function ( channel ) {
|
||||
|
||||
this.mask |= 1 << channel | 0;
|
||||
|
||||
},
|
||||
|
||||
toggle: function ( channel ) {
|
||||
|
||||
this.mask ^= 1 << channel | 0;
|
||||
|
||||
},
|
||||
|
||||
disable: function ( channel ) {
|
||||
|
||||
this.mask &= ~ ( 1 << channel | 0 );
|
||||
|
||||
},
|
||||
|
||||
test: function ( layers ) {
|
||||
|
||||
return ( this.mask & layers.mask ) !== 0;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { Layers };
|
893
lib/core/Object3D.js
Normal file
@ -0,0 +1,893 @@
|
||||
import { Quaternion } from '../math/Quaternion.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { Matrix4 } from '../math/Matrix4.js';
|
||||
import { EventDispatcher } from './EventDispatcher.js';
|
||||
import { Euler } from '../math/Euler.js';
|
||||
import { Layers } from './Layers.js';
|
||||
import { Matrix3 } from '../math/Matrix3.js';
|
||||
import { _Math } from '../math/Math.js';
|
||||
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author mikael emtinger / http://gomo.se/
|
||||
* @author alteredq / http://alteredqualia.com/
|
||||
* @author WestLangley / http://github.com/WestLangley
|
||||
* @author elephantatwork / www.elephantatwork.ch
|
||||
*/
|
||||
|
||||
var object3DId = 0;
|
||||
|
||||
function Object3D() {
|
||||
|
||||
Object.defineProperty( this, 'id', { value: object3DId ++ } );
|
||||
|
||||
this.uuid = _Math.generateUUID();
|
||||
|
||||
this.name = '';
|
||||
this.type = 'Object3D';
|
||||
|
||||
this.parent = null;
|
||||
this.children = [];
|
||||
|
||||
this.up = Object3D.DefaultUp.clone();
|
||||
|
||||
var position = new Vector3();
|
||||
var rotation = new Euler();
|
||||
var quaternion = new Quaternion();
|
||||
var scale = new Vector3( 1, 1, 1 );
|
||||
|
||||
function onRotationChange() {
|
||||
|
||||
quaternion.setFromEuler( rotation, false );
|
||||
|
||||
}
|
||||
|
||||
function onQuaternionChange() {
|
||||
|
||||
rotation.setFromQuaternion( quaternion, undefined, false );
|
||||
|
||||
}
|
||||
|
||||
rotation.onChange( onRotationChange );
|
||||
quaternion.onChange( onQuaternionChange );
|
||||
|
||||
Object.defineProperties( this, {
|
||||
position: {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
value: position
|
||||
},
|
||||
rotation: {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
value: rotation
|
||||
},
|
||||
quaternion: {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
value: quaternion
|
||||
},
|
||||
scale: {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
value: scale
|
||||
},
|
||||
modelViewMatrix: {
|
||||
value: new Matrix4()
|
||||
},
|
||||
normalMatrix: {
|
||||
value: new Matrix3()
|
||||
}
|
||||
} );
|
||||
|
||||
this.matrix = new Matrix4();
|
||||
this.matrixWorld = new Matrix4();
|
||||
|
||||
this.matrixAutoUpdate = Object3D.DefaultMatrixAutoUpdate;
|
||||
this.matrixWorldNeedsUpdate = false;
|
||||
|
||||
this.layers = new Layers();
|
||||
this.visible = true;
|
||||
|
||||
this.castShadow = false;
|
||||
this.receiveShadow = false;
|
||||
|
||||
this.frustumCulled = true;
|
||||
this.renderOrder = 0;
|
||||
|
||||
this.userData = {};
|
||||
|
||||
}
|
||||
|
||||
Object3D.DefaultUp = new Vector3( 0, 1, 0 );
|
||||
Object3D.DefaultMatrixAutoUpdate = true;
|
||||
|
||||
Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
|
||||
|
||||
constructor: Object3D,
|
||||
|
||||
isObject3D: true,
|
||||
|
||||
onBeforeRender: function () {},
|
||||
onAfterRender: function () {},
|
||||
|
||||
applyMatrix: function ( matrix ) {
|
||||
|
||||
this.matrix.multiplyMatrices( matrix, this.matrix );
|
||||
|
||||
this.matrix.decompose( this.position, this.quaternion, this.scale );
|
||||
|
||||
},
|
||||
|
||||
applyQuaternion: function ( q ) {
|
||||
|
||||
this.quaternion.premultiply( q );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setRotationFromAxisAngle: function ( axis, angle ) {
|
||||
|
||||
// assumes axis is normalized
|
||||
|
||||
this.quaternion.setFromAxisAngle( axis, angle );
|
||||
|
||||
},
|
||||
|
||||
setRotationFromEuler: function ( euler ) {
|
||||
|
||||
this.quaternion.setFromEuler( euler, true );
|
||||
|
||||
},
|
||||
|
||||
setRotationFromMatrix: function ( m ) {
|
||||
|
||||
// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
|
||||
|
||||
this.quaternion.setFromRotationMatrix( m );
|
||||
|
||||
},
|
||||
|
||||
setRotationFromQuaternion: function ( q ) {
|
||||
|
||||
// assumes q is normalized
|
||||
|
||||
this.quaternion.copy( q );
|
||||
|
||||
},
|
||||
|
||||
rotateOnAxis: function () {
|
||||
|
||||
// rotate object on axis in object space
|
||||
// axis is assumed to be normalized
|
||||
|
||||
var q1 = new Quaternion();
|
||||
|
||||
return function rotateOnAxis( axis, angle ) {
|
||||
|
||||
q1.setFromAxisAngle( axis, angle );
|
||||
|
||||
this.quaternion.multiply( q1 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
rotateOnWorldAxis: function () {
|
||||
|
||||
// rotate object on axis in world space
|
||||
// axis is assumed to be normalized
|
||||
// method assumes no rotated parent
|
||||
|
||||
var q1 = new Quaternion();
|
||||
|
||||
return function rotateOnWorldAxis( axis, angle ) {
|
||||
|
||||
q1.setFromAxisAngle( axis, angle );
|
||||
|
||||
this.quaternion.premultiply( q1 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
rotateX: function () {
|
||||
|
||||
var v1 = new Vector3( 1, 0, 0 );
|
||||
|
||||
return function rotateX( angle ) {
|
||||
|
||||
return this.rotateOnAxis( v1, angle );
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
rotateY: function () {
|
||||
|
||||
var v1 = new Vector3( 0, 1, 0 );
|
||||
|
||||
return function rotateY( angle ) {
|
||||
|
||||
return this.rotateOnAxis( v1, angle );
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
rotateZ: function () {
|
||||
|
||||
var v1 = new Vector3( 0, 0, 1 );
|
||||
|
||||
return function rotateZ( angle ) {
|
||||
|
||||
return this.rotateOnAxis( v1, angle );
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
translateOnAxis: function () {
|
||||
|
||||
// translate object by distance along axis in object space
|
||||
// axis is assumed to be normalized
|
||||
|
||||
var v1 = new Vector3();
|
||||
|
||||
return function translateOnAxis( axis, distance ) {
|
||||
|
||||
v1.copy( axis ).applyQuaternion( this.quaternion );
|
||||
|
||||
this.position.add( v1.multiplyScalar( distance ) );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
translateX: function () {
|
||||
|
||||
var v1 = new Vector3( 1, 0, 0 );
|
||||
|
||||
return function translateX( distance ) {
|
||||
|
||||
return this.translateOnAxis( v1, distance );
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
translateY: function () {
|
||||
|
||||
var v1 = new Vector3( 0, 1, 0 );
|
||||
|
||||
return function translateY( distance ) {
|
||||
|
||||
return this.translateOnAxis( v1, distance );
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
translateZ: function () {
|
||||
|
||||
var v1 = new Vector3( 0, 0, 1 );
|
||||
|
||||
return function translateZ( distance ) {
|
||||
|
||||
return this.translateOnAxis( v1, distance );
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
localToWorld: function ( vector ) {
|
||||
|
||||
return vector.applyMatrix4( this.matrixWorld );
|
||||
|
||||
},
|
||||
|
||||
worldToLocal: function () {
|
||||
|
||||
var m1 = new Matrix4();
|
||||
|
||||
return function worldToLocal( vector ) {
|
||||
|
||||
return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) );
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
lookAt: function () {
|
||||
|
||||
// This method does not support objects having non-uniformly-scaled parent(s)
|
||||
|
||||
var q1 = new Quaternion();
|
||||
var m1 = new Matrix4();
|
||||
var target = new Vector3();
|
||||
var position = new Vector3();
|
||||
|
||||
return function lookAt( x, y, z ) {
|
||||
|
||||
if ( x.isVector3 ) {
|
||||
|
||||
target.copy( x );
|
||||
|
||||
} else {
|
||||
|
||||
target.set( x, y, z );
|
||||
|
||||
}
|
||||
|
||||
var parent = this.parent;
|
||||
|
||||
this.updateWorldMatrix( true, false );
|
||||
|
||||
position.setFromMatrixPosition( this.matrixWorld );
|
||||
|
||||
if ( this.isCamera ) {
|
||||
|
||||
m1.lookAt( position, target, this.up );
|
||||
|
||||
} else {
|
||||
|
||||
m1.lookAt( target, position, this.up );
|
||||
|
||||
}
|
||||
|
||||
this.quaternion.setFromRotationMatrix( m1 );
|
||||
|
||||
if ( parent ) {
|
||||
|
||||
m1.extractRotation( parent.matrixWorld );
|
||||
q1.setFromRotationMatrix( m1 );
|
||||
this.quaternion.premultiply( q1.inverse() );
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
add: function ( object ) {
|
||||
|
||||
if ( arguments.length > 1 ) {
|
||||
|
||||
for ( var i = 0; i < arguments.length; i ++ ) {
|
||||
|
||||
this.add( arguments[ i ] );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
if ( object === this ) {
|
||||
|
||||
console.error( "THREE.Object3D.add: object can't be added as a child of itself.", object );
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
if ( ( object && object.isObject3D ) ) {
|
||||
|
||||
if ( object.parent !== null ) {
|
||||
|
||||
object.parent.remove( object );
|
||||
|
||||
}
|
||||
|
||||
object.parent = this;
|
||||
object.dispatchEvent( { type: 'added' } );
|
||||
|
||||
this.children.push( object );
|
||||
|
||||
} else {
|
||||
|
||||
console.error( "THREE.Object3D.add: object not an instance of THREE.Object3D.", object );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
remove: function ( object ) {
|
||||
|
||||
if ( arguments.length > 1 ) {
|
||||
|
||||
for ( var i = 0; i < arguments.length; i ++ ) {
|
||||
|
||||
this.remove( arguments[ i ] );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
var index = this.children.indexOf( object );
|
||||
|
||||
if ( index !== - 1 ) {
|
||||
|
||||
object.parent = null;
|
||||
|
||||
object.dispatchEvent( { type: 'removed' } );
|
||||
|
||||
this.children.splice( index, 1 );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
getObjectById: function ( id ) {
|
||||
|
||||
return this.getObjectByProperty( 'id', id );
|
||||
|
||||
},
|
||||
|
||||
getObjectByName: function ( name ) {
|
||||
|
||||
return this.getObjectByProperty( 'name', name );
|
||||
|
||||
},
|
||||
|
||||
getObjectByProperty: function ( name, value ) {
|
||||
|
||||
if ( this[ name ] === value ) return this;
|
||||
|
||||
for ( var i = 0, l = this.children.length; i < l; i ++ ) {
|
||||
|
||||
var child = this.children[ i ];
|
||||
var object = child.getObjectByProperty( name, value );
|
||||
|
||||
if ( object !== undefined ) {
|
||||
|
||||
return object;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return undefined;
|
||||
|
||||
},
|
||||
|
||||
getWorldPosition: function ( target ) {
|
||||
|
||||
if ( target === undefined ) {
|
||||
|
||||
console.warn( 'THREE.Object3D: .getWorldPosition() target is now required' );
|
||||
target = new Vector3();
|
||||
|
||||
}
|
||||
|
||||
this.updateMatrixWorld( true );
|
||||
|
||||
return target.setFromMatrixPosition( this.matrixWorld );
|
||||
|
||||
},
|
||||
|
||||
getWorldQuaternion: function () {
|
||||
|
||||
var position = new Vector3();
|
||||
var scale = new Vector3();
|
||||
|
||||
return function getWorldQuaternion( target ) {
|
||||
|
||||
if ( target === undefined ) {
|
||||
|
||||
console.warn( 'THREE.Object3D: .getWorldQuaternion() target is now required' );
|
||||
target = new Quaternion();
|
||||
|
||||
}
|
||||
|
||||
this.updateMatrixWorld( true );
|
||||
|
||||
this.matrixWorld.decompose( position, target, scale );
|
||||
|
||||
return target;
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
getWorldScale: function () {
|
||||
|
||||
var position = new Vector3();
|
||||
var quaternion = new Quaternion();
|
||||
|
||||
return function getWorldScale( target ) {
|
||||
|
||||
if ( target === undefined ) {
|
||||
|
||||
console.warn( 'THREE.Object3D: .getWorldScale() target is now required' );
|
||||
target = new Vector3();
|
||||
|
||||
}
|
||||
|
||||
this.updateMatrixWorld( true );
|
||||
|
||||
this.matrixWorld.decompose( position, quaternion, target );
|
||||
|
||||
return target;
|
||||
|
||||
};
|
||||
|
||||
}(),
|
||||
|
||||
getWorldDirection: function ( target ) {
|
||||
|
||||
if ( target === undefined ) {
|
||||
|
||||
console.warn( 'THREE.Object3D: .getWorldDirection() target is now required' );
|
||||
target = new Vector3();
|
||||
|
||||
}
|
||||
|
||||
this.updateMatrixWorld( true );
|
||||
|
||||
var e = this.matrixWorld.elements;
|
||||
|
||||
return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize();
|
||||
|
||||
},
|
||||
|
||||
raycast: function () {},
|
||||
|
||||
traverse: function ( callback ) {
|
||||
|
||||
callback( this );
|
||||
|
||||
var children = this.children;
|
||||
|
||||
for ( var i = 0, l = children.length; i < l; i ++ ) {
|
||||
|
||||
children[ i ].traverse( callback );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
traverseVisible: function ( callback ) {
|
||||
|
||||
if ( this.visible === false ) return;
|
||||
|
||||
callback( this );
|
||||
|
||||
var children = this.children;
|
||||
|
||||
for ( var i = 0, l = children.length; i < l; i ++ ) {
|
||||
|
||||
children[ i ].traverseVisible( callback );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
traverseAncestors: function ( callback ) {
|
||||
|
||||
var parent = this.parent;
|
||||
|
||||
if ( parent !== null ) {
|
||||
|
||||
callback( parent );
|
||||
|
||||
parent.traverseAncestors( callback );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
updateMatrix: function () {
|
||||
|
||||
this.matrix.compose( this.position, this.quaternion, this.scale );
|
||||
|
||||
this.matrixWorldNeedsUpdate = true;
|
||||
|
||||
},
|
||||
|
||||
updateMatrixWorld: function ( force ) {
|
||||
|
||||
if ( this.matrixAutoUpdate ) this.updateMatrix();
|
||||
|
||||
if ( this.matrixWorldNeedsUpdate || force ) {
|
||||
|
||||
if ( this.parent === null ) {
|
||||
|
||||
this.matrixWorld.copy( this.matrix );
|
||||
|
||||
} else {
|
||||
|
||||
this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
|
||||
|
||||
}
|
||||
|
||||
this.matrixWorldNeedsUpdate = false;
|
||||
|
||||
force = true;
|
||||
|
||||
}
|
||||
|
||||
// update children
|
||||
|
||||
var children = this.children;
|
||||
|
||||
for ( var i = 0, l = children.length; i < l; i ++ ) {
|
||||
|
||||
children[ i ].updateMatrixWorld( force );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
updateWorldMatrix: function ( updateParents, updateChildren ) {
|
||||
|
||||
var parent = this.parent;
|
||||
|
||||
if ( updateParents === true && parent !== null ) {
|
||||
|
||||
parent.updateWorldMatrix( true, false );
|
||||
|
||||
}
|
||||
|
||||
if ( this.matrixAutoUpdate ) this.updateMatrix();
|
||||
|
||||
if ( this.parent === null ) {
|
||||
|
||||
this.matrixWorld.copy( this.matrix );
|
||||
|
||||
} else {
|
||||
|
||||
this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
|
||||
|
||||
}
|
||||
|
||||
// update children
|
||||
|
||||
if ( updateChildren === true ) {
|
||||
|
||||
var children = this.children;
|
||||
|
||||
for ( var i = 0, l = children.length; i < l; i ++ ) {
|
||||
|
||||
children[ i ].updateWorldMatrix( false, true );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
toJSON: function ( meta ) {
|
||||
|
||||
// meta is a string when called from JSON.stringify
|
||||
var isRootObject = ( meta === undefined || typeof meta === 'string' );
|
||||
|
||||
var output = {};
|
||||
|
||||
// meta is a hash used to collect geometries, materials.
|
||||
// not providing it implies that this is the root object
|
||||
// being serialized.
|
||||
if ( isRootObject ) {
|
||||
|
||||
// initialize meta obj
|
||||
meta = {
|
||||
geometries: {},
|
||||
materials: {},
|
||||
textures: {},
|
||||
images: {},
|
||||
shapes: {}
|
||||
};
|
||||
|
||||
output.metadata = {
|
||||
version: 4.5,
|
||||
type: 'Object',
|
||||
generator: 'Object3D.toJSON'
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
// standard Object3D serialization
|
||||
|
||||
var object = {};
|
||||
|
||||
object.uuid = this.uuid;
|
||||
object.type = this.type;
|
||||
|
||||
if ( this.name !== '' ) object.name = this.name;
|
||||
if ( this.castShadow === true ) object.castShadow = true;
|
||||
if ( this.receiveShadow === true ) object.receiveShadow = true;
|
||||
if ( this.visible === false ) object.visible = false;
|
||||
if ( this.frustumCulled === false ) object.frustumCulled = false;
|
||||
if ( this.renderOrder !== 0 ) object.renderOrder = this.renderOrder;
|
||||
if ( JSON.stringify( this.userData ) !== '{}' ) object.userData = this.userData;
|
||||
|
||||
object.layers = this.layers.mask;
|
||||
object.matrix = this.matrix.toArray();
|
||||
|
||||
if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
|
||||
|
||||
//
|
||||
|
||||
function serialize( library, element ) {
|
||||
|
||||
if ( library[ element.uuid ] === undefined ) {
|
||||
|
||||
library[ element.uuid ] = element.toJSON( meta );
|
||||
|
||||
}
|
||||
|
||||
return element.uuid;
|
||||
|
||||
}
|
||||
|
||||
if ( this.isMesh || this.isLine || this.isPoints ) {
|
||||
|
||||
object.geometry = serialize( meta.geometries, this.geometry );
|
||||
|
||||
var parameters = this.geometry.parameters;
|
||||
|
||||
if ( parameters !== undefined && parameters.shapes !== undefined ) {
|
||||
|
||||
var shapes = parameters.shapes;
|
||||
|
||||
if ( Array.isArray( shapes ) ) {
|
||||
|
||||
for ( var i = 0, l = shapes.length; i < l; i ++ ) {
|
||||
|
||||
var shape = shapes[ i ];
|
||||
|
||||
serialize( meta.shapes, shape );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
serialize( meta.shapes, shapes );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( this.material !== undefined ) {
|
||||
|
||||
if ( Array.isArray( this.material ) ) {
|
||||
|
||||
var uuids = [];
|
||||
|
||||
for ( var i = 0, l = this.material.length; i < l; i ++ ) {
|
||||
|
||||
uuids.push( serialize( meta.materials, this.material[ i ] ) );
|
||||
|
||||
}
|
||||
|
||||
object.material = uuids;
|
||||
|
||||
} else {
|
||||
|
||||
object.material = serialize( meta.materials, this.material );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
if ( this.children.length > 0 ) {
|
||||
|
||||
object.children = [];
|
||||
|
||||
for ( var i = 0; i < this.children.length; i ++ ) {
|
||||
|
||||
object.children.push( this.children[ i ].toJSON( meta ).object );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( isRootObject ) {
|
||||
|
||||
var geometries = extractFromCache( meta.geometries );
|
||||
var materials = extractFromCache( meta.materials );
|
||||
var textures = extractFromCache( meta.textures );
|
||||
var images = extractFromCache( meta.images );
|
||||
var shapes = extractFromCache( meta.shapes );
|
||||
|
||||
if ( geometries.length > 0 ) output.geometries = geometries;
|
||||
if ( materials.length > 0 ) output.materials = materials;
|
||||
if ( textures.length > 0 ) output.textures = textures;
|
||||
if ( images.length > 0 ) output.images = images;
|
||||
if ( shapes.length > 0 ) output.shapes = shapes;
|
||||
|
||||
}
|
||||
|
||||
output.object = object;
|
||||
|
||||
return output;
|
||||
|
||||
// extract data from the cache hash
|
||||
// remove metadata on each item
|
||||
// and return as array
|
||||
function extractFromCache( cache ) {
|
||||
|
||||
var values = [];
|
||||
for ( var key in cache ) {
|
||||
|
||||
var data = cache[ key ];
|
||||
delete data.metadata;
|
||||
values.push( data );
|
||||
|
||||
}
|
||||
return values;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
clone: function ( recursive ) {
|
||||
|
||||
return new this.constructor().copy( this, recursive );
|
||||
|
||||
},
|
||||
|
||||
copy: function ( source, recursive ) {
|
||||
|
||||
if ( recursive === undefined ) recursive = true;
|
||||
|
||||
this.name = source.name;
|
||||
|
||||
this.up.copy( source.up );
|
||||
|
||||
this.position.copy( source.position );
|
||||
this.quaternion.copy( source.quaternion );
|
||||
this.scale.copy( source.scale );
|
||||
|
||||
this.matrix.copy( source.matrix );
|
||||
this.matrixWorld.copy( source.matrixWorld );
|
||||
|
||||
this.matrixAutoUpdate = source.matrixAutoUpdate;
|
||||
this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
|
||||
|
||||
this.layers.mask = source.layers.mask;
|
||||
this.visible = source.visible;
|
||||
|
||||
this.castShadow = source.castShadow;
|
||||
this.receiveShadow = source.receiveShadow;
|
||||
|
||||
this.frustumCulled = source.frustumCulled;
|
||||
this.renderOrder = source.renderOrder;
|
||||
|
||||
this.userData = JSON.parse( JSON.stringify( source.userData ) );
|
||||
|
||||
if ( recursive === true ) {
|
||||
|
||||
for ( var i = 0; i < source.children.length; i ++ ) {
|
||||
|
||||
var child = source.children[ i ];
|
||||
this.add( child.clone() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { Object3D };
|
134
lib/core/Raycaster.js
Normal file
@ -0,0 +1,134 @@
|
||||
import { Ray } from '../math/Ray.js';
|
||||
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author bhouston / http://clara.io/
|
||||
* @author stephomi / http://stephaneginier.com/
|
||||
*/
|
||||
|
||||
function Raycaster( origin, direction, near, far ) {
|
||||
|
||||
this.ray = new Ray( origin, direction );
|
||||
// direction is assumed to be normalized (for accurate distance calculations)
|
||||
|
||||
this.near = near || 0;
|
||||
this.far = far || Infinity;
|
||||
|
||||
this.params = {
|
||||
Mesh: {},
|
||||
Line: {},
|
||||
LOD: {},
|
||||
Points: { threshold: 1 },
|
||||
Sprite: {}
|
||||
};
|
||||
|
||||
Object.defineProperties( this.params, {
|
||||
PointCloud: {
|
||||
get: function () {
|
||||
|
||||
console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' );
|
||||
return this.Points;
|
||||
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
function ascSort( a, b ) {
|
||||
|
||||
return a.distance - b.distance;
|
||||
|
||||
}
|
||||
|
||||
function intersectObject( object, raycaster, intersects, recursive ) {
|
||||
|
||||
if ( object.visible === false ) return;
|
||||
|
||||
object.raycast( raycaster, intersects );
|
||||
|
||||
if ( recursive === true ) {
|
||||
|
||||
var children = object.children;
|
||||
|
||||
for ( var i = 0, l = children.length; i < l; i ++ ) {
|
||||
|
||||
intersectObject( children[ i ], raycaster, intersects, true );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Object.assign( Raycaster.prototype, {
|
||||
|
||||
linePrecision: 1,
|
||||
|
||||
set: function ( origin, direction ) {
|
||||
|
||||
// direction is assumed to be normalized (for accurate distance calculations)
|
||||
|
||||
this.ray.set( origin, direction );
|
||||
|
||||
},
|
||||
|
||||
setFromCamera: function ( coords, camera ) {
|
||||
|
||||
if ( ( camera && camera.isPerspectiveCamera ) ) {
|
||||
|
||||
this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
|
||||
this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
|
||||
|
||||
} else if ( ( camera && camera.isOrthographicCamera ) ) {
|
||||
|
||||
this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
|
||||
this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
|
||||
|
||||
} else {
|
||||
|
||||
console.error( 'THREE.Raycaster: Unsupported camera type.' );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
intersectObject: function ( object, recursive, optionalTarget ) {
|
||||
|
||||
var intersects = optionalTarget || [];
|
||||
|
||||
intersectObject( object, this, intersects, recursive );
|
||||
|
||||
intersects.sort( ascSort );
|
||||
|
||||
return intersects;
|
||||
|
||||
},
|
||||
|
||||
intersectObjects: function ( objects, recursive, optionalTarget ) {
|
||||
|
||||
var intersects = optionalTarget || [];
|
||||
|
||||
if ( Array.isArray( objects ) === false ) {
|
||||
|
||||
console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
|
||||
return intersects;
|
||||
|
||||
}
|
||||
|
||||
for ( var i = 0, l = objects.length; i < l; i ++ ) {
|
||||
|
||||
intersectObject( objects[ i ], this, intersects, recursive );
|
||||
|
||||
}
|
||||
|
||||
intersects.sort( ascSort );
|
||||
|
||||
return intersects;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { Raycaster };
|
24
lib/core/Uniform.js
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
function Uniform( value ) {
|
||||
|
||||
if ( typeof value === 'string' ) {
|
||||
|
||||
console.warn( 'THREE.Uniform: Type parameter is no longer needed.' );
|
||||
value = arguments[ 1 ];
|
||||
|
||||
}
|
||||
|
||||
this.value = value;
|
||||
|
||||
}
|
||||
|
||||
Uniform.prototype.clone = function () {
|
||||
|
||||
return new Uniform( this.value.clone === undefined ? this.value : this.value.clone() );
|
||||
|
||||
};
|
||||
|
||||
export { Uniform };
|
810
lib/extras/Earcut.js
Normal file
@ -0,0 +1,810 @@
|
||||
/**
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
* Port from https://github.com/mapbox/earcut (v2.1.2)
|
||||
*/
|
||||
|
||||
var Earcut = {
|
||||
|
||||
triangulate: function ( data, holeIndices, dim ) {
|
||||
|
||||
dim = dim || 2;
|
||||
|
||||
var hasHoles = holeIndices && holeIndices.length,
|
||||
outerLen = hasHoles ? holeIndices[ 0 ] * dim : data.length,
|
||||
outerNode = linkedList( data, 0, outerLen, dim, true ),
|
||||
triangles = [];
|
||||
|
||||
if ( ! outerNode ) return triangles;
|
||||
|
||||
var minX, minY, maxX, maxY, x, y, invSize;
|
||||
|
||||
if ( hasHoles ) outerNode = eliminateHoles( data, holeIndices, outerNode, dim );
|
||||
|
||||
// if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
|
||||
|
||||
if ( data.length > 80 * dim ) {
|
||||
|
||||
minX = maxX = data[ 0 ];
|
||||
minY = maxY = data[ 1 ];
|
||||
|
||||
for ( var i = dim; i < outerLen; i += dim ) {
|
||||
|
||||
x = data[ i ];
|
||||
y = data[ i + 1 ];
|
||||
if ( x < minX ) minX = x;
|
||||
if ( y < minY ) minY = y;
|
||||
if ( x > maxX ) maxX = x;
|
||||
if ( y > maxY ) maxY = y;
|
||||
|
||||
}
|
||||
|
||||
// minX, minY and invSize are later used to transform coords into integers for z-order calculation
|
||||
|
||||
invSize = Math.max( maxX - minX, maxY - minY );
|
||||
invSize = invSize !== 0 ? 1 / invSize : 0;
|
||||
|
||||
}
|
||||
|
||||
earcutLinked( outerNode, triangles, dim, minX, minY, invSize );
|
||||
|
||||
return triangles;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// create a circular doubly linked list from polygon points in the specified winding order
|
||||
|
||||
function linkedList( data, start, end, dim, clockwise ) {
|
||||
|
||||
var i, last;
|
||||
|
||||
if ( clockwise === ( signedArea( data, start, end, dim ) > 0 ) ) {
|
||||
|
||||
for ( i = start; i < end; i += dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last );
|
||||
|
||||
} else {
|
||||
|
||||
for ( i = end - dim; i >= start; i -= dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last );
|
||||
|
||||
}
|
||||
|
||||
if ( last && equals( last, last.next ) ) {
|
||||
|
||||
removeNode( last );
|
||||
last = last.next;
|
||||
|
||||
}
|
||||
|
||||
return last;
|
||||
|
||||
}
|
||||
|
||||
// eliminate colinear or duplicate points
|
||||
|
||||
function filterPoints( start, end ) {
|
||||
|
||||
if ( ! start ) return start;
|
||||
if ( ! end ) end = start;
|
||||
|
||||
var p = start, again;
|
||||
|
||||
do {
|
||||
|
||||
again = false;
|
||||
|
||||
if ( ! p.steiner && ( equals( p, p.next ) || area( p.prev, p, p.next ) === 0 ) ) {
|
||||
|
||||
removeNode( p );
|
||||
p = end = p.prev;
|
||||
if ( p === p.next ) break;
|
||||
again = true;
|
||||
|
||||
} else {
|
||||
|
||||
p = p.next;
|
||||
|
||||
}
|
||||
|
||||
} while ( again || p !== end );
|
||||
|
||||
return end;
|
||||
|
||||
}
|
||||
|
||||
// main ear slicing loop which triangulates a polygon (given as a linked list)
|
||||
|
||||
function earcutLinked( ear, triangles, dim, minX, minY, invSize, pass ) {
|
||||
|
||||
if ( ! ear ) return;
|
||||
|
||||
// interlink polygon nodes in z-order
|
||||
|
||||
if ( ! pass && invSize ) indexCurve( ear, minX, minY, invSize );
|
||||
|
||||
var stop = ear, prev, next;
|
||||
|
||||
// iterate through ears, slicing them one by one
|
||||
|
||||
while ( ear.prev !== ear.next ) {
|
||||
|
||||
prev = ear.prev;
|
||||
next = ear.next;
|
||||
|
||||
if ( invSize ? isEarHashed( ear, minX, minY, invSize ) : isEar( ear ) ) {
|
||||
|
||||
// cut off the triangle
|
||||
triangles.push( prev.i / dim );
|
||||
triangles.push( ear.i / dim );
|
||||
triangles.push( next.i / dim );
|
||||
|
||||
removeNode( ear );
|
||||
|
||||
// skipping the next vertice leads to less sliver triangles
|
||||
ear = next.next;
|
||||
stop = next.next;
|
||||
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
ear = next;
|
||||
|
||||
// if we looped through the whole remaining polygon and can't find any more ears
|
||||
|
||||
if ( ear === stop ) {
|
||||
|
||||
// try filtering points and slicing again
|
||||
|
||||
if ( ! pass ) {
|
||||
|
||||
earcutLinked( filterPoints( ear ), triangles, dim, minX, minY, invSize, 1 );
|
||||
|
||||
// if this didn't work, try curing all small self-intersections locally
|
||||
|
||||
} else if ( pass === 1 ) {
|
||||
|
||||
ear = cureLocalIntersections( ear, triangles, dim );
|
||||
earcutLinked( ear, triangles, dim, minX, minY, invSize, 2 );
|
||||
|
||||
// as a last resort, try splitting the remaining polygon into two
|
||||
|
||||
} else if ( pass === 2 ) {
|
||||
|
||||
splitEarcut( ear, triangles, dim, minX, minY, invSize );
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// check whether a polygon node forms a valid ear with adjacent nodes
|
||||
|
||||
function isEar( ear ) {
|
||||
|
||||
var a = ear.prev,
|
||||
b = ear,
|
||||
c = ear.next;
|
||||
|
||||
if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear
|
||||
|
||||
// now make sure we don't have other points inside the potential ear
|
||||
var p = ear.next.next;
|
||||
|
||||
while ( p !== ear.prev ) {
|
||||
|
||||
if ( pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
p = p.next;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
function isEarHashed( ear, minX, minY, invSize ) {
|
||||
|
||||
var a = ear.prev,
|
||||
b = ear,
|
||||
c = ear.next;
|
||||
|
||||
if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear
|
||||
|
||||
// triangle bbox; min & max are calculated like this for speed
|
||||
|
||||
var minTX = a.x < b.x ? ( a.x < c.x ? a.x : c.x ) : ( b.x < c.x ? b.x : c.x ),
|
||||
minTY = a.y < b.y ? ( a.y < c.y ? a.y : c.y ) : ( b.y < c.y ? b.y : c.y ),
|
||||
maxTX = a.x > b.x ? ( a.x > c.x ? a.x : c.x ) : ( b.x > c.x ? b.x : c.x ),
|
||||
maxTY = a.y > b.y ? ( a.y > c.y ? a.y : c.y ) : ( b.y > c.y ? b.y : c.y );
|
||||
|
||||
// z-order range for the current triangle bbox;
|
||||
|
||||
var minZ = zOrder( minTX, minTY, minX, minY, invSize ),
|
||||
maxZ = zOrder( maxTX, maxTY, minX, minY, invSize );
|
||||
|
||||
// first look for points inside the triangle in increasing z-order
|
||||
|
||||
var p = ear.nextZ;
|
||||
|
||||
while ( p && p.z <= maxZ ) {
|
||||
|
||||
if ( p !== ear.prev && p !== ear.next &&
|
||||
pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) &&
|
||||
area( p.prev, p, p.next ) >= 0 ) return false;
|
||||
p = p.nextZ;
|
||||
|
||||
}
|
||||
|
||||
// then look for points in decreasing z-order
|
||||
|
||||
p = ear.prevZ;
|
||||
|
||||
while ( p && p.z >= minZ ) {
|
||||
|
||||
if ( p !== ear.prev && p !== ear.next &&
|
||||
pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) &&
|
||||
area( p.prev, p, p.next ) >= 0 ) return false;
|
||||
|
||||
p = p.prevZ;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
// go through all polygon nodes and cure small local self-intersections
|
||||
|
||||
function cureLocalIntersections( start, triangles, dim ) {
|
||||
|
||||
var p = start;
|
||||
|
||||
do {
|
||||
|
||||
var a = p.prev, b = p.next.next;
|
||||
|
||||
if ( ! equals( a, b ) && intersects( a, p, p.next, b ) && locallyInside( a, b ) && locallyInside( b, a ) ) {
|
||||
|
||||
triangles.push( a.i / dim );
|
||||
triangles.push( p.i / dim );
|
||||
triangles.push( b.i / dim );
|
||||
|
||||
// remove two nodes involved
|
||||
|
||||
removeNode( p );
|
||||
removeNode( p.next );
|
||||
|
||||
p = start = b;
|
||||
|
||||
}
|
||||
|
||||
p = p.next;
|
||||
|
||||
} while ( p !== start );
|
||||
|
||||
return p;
|
||||
|
||||
}
|
||||
|
||||
// try splitting polygon into two and triangulate them independently
|
||||
|
||||
function splitEarcut( start, triangles, dim, minX, minY, invSize ) {
|
||||
|
||||
// look for a valid diagonal that divides the polygon into two
|
||||
|
||||
var a = start;
|
||||
|
||||
do {
|
||||
|
||||
var b = a.next.next;
|
||||
|
||||
while ( b !== a.prev ) {
|
||||
|
||||
if ( a.i !== b.i && isValidDiagonal( a, b ) ) {
|
||||
|
||||
// split the polygon in two by the diagonal
|
||||
|
||||
var c = splitPolygon( a, b );
|
||||
|
||||
// filter colinear points around the cuts
|
||||
|
||||
a = filterPoints( a, a.next );
|
||||
c = filterPoints( c, c.next );
|
||||
|
||||
// run earcut on each half
|
||||
|
||||
earcutLinked( a, triangles, dim, minX, minY, invSize );
|
||||
earcutLinked( c, triangles, dim, minX, minY, invSize );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
b = b.next;
|
||||
|
||||
}
|
||||
|
||||
a = a.next;
|
||||
|
||||
} while ( a !== start );
|
||||
|
||||
}
|
||||
|
||||
// link every hole into the outer loop, producing a single-ring polygon without holes
|
||||
|
||||
function eliminateHoles( data, holeIndices, outerNode, dim ) {
|
||||
|
||||
var queue = [], i, len, start, end, list;
|
||||
|
||||
for ( i = 0, len = holeIndices.length; i < len; i ++ ) {
|
||||
|
||||
start = holeIndices[ i ] * dim;
|
||||
end = i < len - 1 ? holeIndices[ i + 1 ] * dim : data.length;
|
||||
list = linkedList( data, start, end, dim, false );
|
||||
if ( list === list.next ) list.steiner = true;
|
||||
queue.push( getLeftmost( list ) );
|
||||
|
||||
}
|
||||
|
||||
queue.sort( compareX );
|
||||
|
||||
// process holes from left to right
|
||||
|
||||
for ( i = 0; i < queue.length; i ++ ) {
|
||||
|
||||
eliminateHole( queue[ i ], outerNode );
|
||||
outerNode = filterPoints( outerNode, outerNode.next );
|
||||
|
||||
}
|
||||
|
||||
return outerNode;
|
||||
|
||||
}
|
||||
|
||||
function compareX( a, b ) {
|
||||
|
||||
return a.x - b.x;
|
||||
|
||||
}
|
||||
|
||||
// find a bridge between vertices that connects hole with an outer ring and and link it
|
||||
|
||||
function eliminateHole( hole, outerNode ) {
|
||||
|
||||
outerNode = findHoleBridge( hole, outerNode );
|
||||
|
||||
if ( outerNode ) {
|
||||
|
||||
var b = splitPolygon( outerNode, hole );
|
||||
|
||||
filterPoints( b, b.next );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// David Eberly's algorithm for finding a bridge between hole and outer polygon
|
||||
|
||||
function findHoleBridge( hole, outerNode ) {
|
||||
|
||||
var p = outerNode,
|
||||
hx = hole.x,
|
||||
hy = hole.y,
|
||||
qx = - Infinity,
|
||||
m;
|
||||
|
||||
// find a segment intersected by a ray from the hole's leftmost point to the left;
|
||||
// segment's endpoint with lesser x will be potential connection point
|
||||
|
||||
do {
|
||||
|
||||
if ( hy <= p.y && hy >= p.next.y && p.next.y !== p.y ) {
|
||||
|
||||
var x = p.x + ( hy - p.y ) * ( p.next.x - p.x ) / ( p.next.y - p.y );
|
||||
|
||||
if ( x <= hx && x > qx ) {
|
||||
|
||||
qx = x;
|
||||
|
||||
if ( x === hx ) {
|
||||
|
||||
if ( hy === p.y ) return p;
|
||||
if ( hy === p.next.y ) return p.next;
|
||||
|
||||
}
|
||||
|
||||
m = p.x < p.next.x ? p : p.next;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
p = p.next;
|
||||
|
||||
} while ( p !== outerNode );
|
||||
|
||||
if ( ! m ) return null;
|
||||
|
||||
if ( hx === qx ) return m.prev; // hole touches outer segment; pick lower endpoint
|
||||
|
||||
// look for points inside the triangle of hole point, segment intersection and endpoint;
|
||||
// if there are no points found, we have a valid connection;
|
||||
// otherwise choose the point of the minimum angle with the ray as connection point
|
||||
|
||||
var stop = m,
|
||||
mx = m.x,
|
||||
my = m.y,
|
||||
tanMin = Infinity,
|
||||
tan;
|
||||
|
||||
p = m.next;
|
||||
|
||||
while ( p !== stop ) {
|
||||
|
||||
if ( hx >= p.x && p.x >= mx && hx !== p.x &&
|
||||
pointInTriangle( hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y ) ) {
|
||||
|
||||
tan = Math.abs( hy - p.y ) / ( hx - p.x ); // tangential
|
||||
|
||||
if ( ( tan < tanMin || ( tan === tanMin && p.x > m.x ) ) && locallyInside( p, hole ) ) {
|
||||
|
||||
m = p;
|
||||
tanMin = tan;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
p = p.next;
|
||||
|
||||
}
|
||||
|
||||
return m;
|
||||
|
||||
}
|
||||
|
||||
// interlink polygon nodes in z-order
|
||||
|
||||
function indexCurve( start, minX, minY, invSize ) {
|
||||
|
||||
var p = start;
|
||||
|
||||
do {
|
||||
|
||||
if ( p.z === null ) p.z = zOrder( p.x, p.y, minX, minY, invSize );
|
||||
p.prevZ = p.prev;
|
||||
p.nextZ = p.next;
|
||||
p = p.next;
|
||||
|
||||
} while ( p !== start );
|
||||
|
||||
p.prevZ.nextZ = null;
|
||||
p.prevZ = null;
|
||||
|
||||
sortLinked( p );
|
||||
|
||||
}
|
||||
|
||||
// Simon Tatham's linked list merge sort algorithm
|
||||
// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
|
||||
|
||||
function sortLinked( list ) {
|
||||
|
||||
var i, p, q, e, tail, numMerges, pSize, qSize, inSize = 1;
|
||||
|
||||
do {
|
||||
|
||||
p = list;
|
||||
list = null;
|
||||
tail = null;
|
||||
numMerges = 0;
|
||||
|
||||
while ( p ) {
|
||||
|
||||
numMerges ++;
|
||||
q = p;
|
||||
pSize = 0;
|
||||
|
||||
for ( i = 0; i < inSize; i ++ ) {
|
||||
|
||||
pSize ++;
|
||||
q = q.nextZ;
|
||||
if ( ! q ) break;
|
||||
|
||||
}
|
||||
|
||||
qSize = inSize;
|
||||
|
||||
while ( pSize > 0 || ( qSize > 0 && q ) ) {
|
||||
|
||||
if ( pSize !== 0 && ( qSize === 0 || ! q || p.z <= q.z ) ) {
|
||||
|
||||
e = p;
|
||||
p = p.nextZ;
|
||||
pSize --;
|
||||
|
||||
} else {
|
||||
|
||||
e = q;
|
||||
q = q.nextZ;
|
||||
qSize --;
|
||||
|
||||
}
|
||||
|
||||
if ( tail ) tail.nextZ = e;
|
||||
else list = e;
|
||||
|
||||
e.prevZ = tail;
|
||||
tail = e;
|
||||
|
||||
}
|
||||
|
||||
p = q;
|
||||
|
||||
}
|
||||
|
||||
tail.nextZ = null;
|
||||
inSize *= 2;
|
||||
|
||||
} while ( numMerges > 1 );
|
||||
|
||||
return list;
|
||||
|
||||
}
|
||||
|
||||
// z-order of a point given coords and inverse of the longer side of data bbox
|
||||
|
||||
function zOrder( x, y, minX, minY, invSize ) {
|
||||
|
||||
// coords are transformed into non-negative 15-bit integer range
|
||||
|
||||
x = 32767 * ( x - minX ) * invSize;
|
||||
y = 32767 * ( y - minY ) * invSize;
|
||||
|
||||
x = ( x | ( x << 8 ) ) & 0x00FF00FF;
|
||||
x = ( x | ( x << 4 ) ) & 0x0F0F0F0F;
|
||||
x = ( x | ( x << 2 ) ) & 0x33333333;
|
||||
x = ( x | ( x << 1 ) ) & 0x55555555;
|
||||
|
||||
y = ( y | ( y << 8 ) ) & 0x00FF00FF;
|
||||
y = ( y | ( y << 4 ) ) & 0x0F0F0F0F;
|
||||
y = ( y | ( y << 2 ) ) & 0x33333333;
|
||||
y = ( y | ( y << 1 ) ) & 0x55555555;
|
||||
|
||||
return x | ( y << 1 );
|
||||
|
||||
}
|
||||
|
||||
// find the leftmost node of a polygon ring
|
||||
|
||||
function getLeftmost( start ) {
|
||||
|
||||
var p = start, leftmost = start;
|
||||
|
||||
do {
|
||||
|
||||
if ( p.x < leftmost.x ) leftmost = p;
|
||||
p = p.next;
|
||||
|
||||
} while ( p !== start );
|
||||
|
||||
return leftmost;
|
||||
|
||||
}
|
||||
|
||||
// check if a point lies within a convex triangle
|
||||
|
||||
function pointInTriangle( ax, ay, bx, by, cx, cy, px, py ) {
|
||||
|
||||
return ( cx - px ) * ( ay - py ) - ( ax - px ) * ( cy - py ) >= 0 &&
|
||||
( ax - px ) * ( by - py ) - ( bx - px ) * ( ay - py ) >= 0 &&
|
||||
( bx - px ) * ( cy - py ) - ( cx - px ) * ( by - py ) >= 0;
|
||||
|
||||
}
|
||||
|
||||
// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
|
||||
|
||||
function isValidDiagonal( a, b ) {
|
||||
|
||||
return a.next.i !== b.i && a.prev.i !== b.i && ! intersectsPolygon( a, b ) &&
|
||||
locallyInside( a, b ) && locallyInside( b, a ) && middleInside( a, b );
|
||||
|
||||
}
|
||||
|
||||
// signed area of a triangle
|
||||
|
||||
function area( p, q, r ) {
|
||||
|
||||
return ( q.y - p.y ) * ( r.x - q.x ) - ( q.x - p.x ) * ( r.y - q.y );
|
||||
|
||||
}
|
||||
|
||||
// check if two points are equal
|
||||
|
||||
function equals( p1, p2 ) {
|
||||
|
||||
return p1.x === p2.x && p1.y === p2.y;
|
||||
|
||||
}
|
||||
|
||||
// check if two segments intersect
|
||||
|
||||
function intersects( p1, q1, p2, q2 ) {
|
||||
|
||||
if ( ( equals( p1, q1 ) && equals( p2, q2 ) ) ||
|
||||
( equals( p1, q2 ) && equals( p2, q1 ) ) ) return true;
|
||||
|
||||
return area( p1, q1, p2 ) > 0 !== area( p1, q1, q2 ) > 0 &&
|
||||
area( p2, q2, p1 ) > 0 !== area( p2, q2, q1 ) > 0;
|
||||
|
||||
}
|
||||
|
||||
// check if a polygon diagonal intersects any polygon segments
|
||||
|
||||
function intersectsPolygon( a, b ) {
|
||||
|
||||
var p = a;
|
||||
|
||||
do {
|
||||
|
||||
if ( p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
|
||||
intersects( p, p.next, a, b ) ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
p = p.next;
|
||||
|
||||
} while ( p !== a );
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
// check if a polygon diagonal is locally inside the polygon
|
||||
|
||||
function locallyInside( a, b ) {
|
||||
|
||||
return area( a.prev, a, a.next ) < 0 ?
|
||||
area( a, b, a.next ) >= 0 && area( a, a.prev, b ) >= 0 :
|
||||
area( a, b, a.prev ) < 0 || area( a, a.next, b ) < 0;
|
||||
|
||||
}
|
||||
|
||||
// check if the middle point of a polygon diagonal is inside the polygon
|
||||
|
||||
function middleInside( a, b ) {
|
||||
|
||||
var p = a,
|
||||
inside = false,
|
||||
px = ( a.x + b.x ) / 2,
|
||||
py = ( a.y + b.y ) / 2;
|
||||
|
||||
do {
|
||||
|
||||
if ( ( ( p.y > py ) !== ( p.next.y > py ) ) && p.next.y !== p.y &&
|
||||
( px < ( p.next.x - p.x ) * ( py - p.y ) / ( p.next.y - p.y ) + p.x ) ) {
|
||||
|
||||
inside = ! inside;
|
||||
|
||||
}
|
||||
|
||||
p = p.next;
|
||||
|
||||
} while ( p !== a );
|
||||
|
||||
return inside;
|
||||
|
||||
}
|
||||
|
||||
// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
|
||||
// if one belongs to the outer ring and another to a hole, it merges it into a single ring
|
||||
|
||||
function splitPolygon( a, b ) {
|
||||
|
||||
var a2 = new Node( a.i, a.x, a.y ),
|
||||
b2 = new Node( b.i, b.x, b.y ),
|
||||
an = a.next,
|
||||
bp = b.prev;
|
||||
|
||||
a.next = b;
|
||||
b.prev = a;
|
||||
|
||||
a2.next = an;
|
||||
an.prev = a2;
|
||||
|
||||
b2.next = a2;
|
||||
a2.prev = b2;
|
||||
|
||||
bp.next = b2;
|
||||
b2.prev = bp;
|
||||
|
||||
return b2;
|
||||
|
||||
}
|
||||
|
||||
// create a node and optionally link it with previous one (in a circular doubly linked list)
|
||||
|
||||
function insertNode( i, x, y, last ) {
|
||||
|
||||
var p = new Node( i, x, y );
|
||||
|
||||
if ( ! last ) {
|
||||
|
||||
p.prev = p;
|
||||
p.next = p;
|
||||
|
||||
} else {
|
||||
|
||||
p.next = last.next;
|
||||
p.prev = last;
|
||||
last.next.prev = p;
|
||||
last.next = p;
|
||||
|
||||
}
|
||||
|
||||
return p;
|
||||
|
||||
}
|
||||
|
||||
function removeNode( p ) {
|
||||
|
||||
p.next.prev = p.prev;
|
||||
p.prev.next = p.next;
|
||||
|
||||
if ( p.prevZ ) p.prevZ.nextZ = p.nextZ;
|
||||
if ( p.nextZ ) p.nextZ.prevZ = p.prevZ;
|
||||
|
||||
}
|
||||
|
||||
function Node( i, x, y ) {
|
||||
|
||||
// vertice index in coordinates array
|
||||
this.i = i;
|
||||
|
||||
// vertex coordinates
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
// previous and next vertice nodes in a polygon ring
|
||||
this.prev = null;
|
||||
this.next = null;
|
||||
|
||||
// z-order curve value
|
||||
this.z = null;
|
||||
|
||||
// previous and next nodes in z-order
|
||||
this.prevZ = null;
|
||||
this.nextZ = null;
|
||||
|
||||
// indicates whether this is a steiner point
|
||||
this.steiner = false;
|
||||
|
||||
}
|
||||
|
||||
function signedArea( data, start, end, dim ) {
|
||||
|
||||
var sum = 0;
|
||||
|
||||
for ( var i = start, j = end - dim; i < end; i += dim ) {
|
||||
|
||||
sum += ( data[ j ] - data[ i ] ) * ( data[ i + 1 ] + data[ j + 1 ] );
|
||||
j = i;
|
||||
|
||||
}
|
||||
|
||||
return sum;
|
||||
|
||||
}
|
||||
|
||||
export { Earcut };
|
55
lib/extras/ImageUtils.js
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author alteredq / http://alteredqualia.com/
|
||||
* @author szimek / https://github.com/szimek/
|
||||
*/
|
||||
|
||||
var ImageUtils = {
|
||||
|
||||
getDataURL: function ( image ) {
|
||||
|
||||
var canvas;
|
||||
|
||||
if ( typeof HTMLCanvasElement == 'undefined' ) {
|
||||
|
||||
return image.src;
|
||||
|
||||
} else if ( image instanceof HTMLCanvasElement ) {
|
||||
|
||||
canvas = image;
|
||||
|
||||
} else {
|
||||
|
||||
canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
|
||||
canvas.width = image.width;
|
||||
canvas.height = image.height;
|
||||
|
||||
var context = canvas.getContext( '2d' );
|
||||
|
||||
if ( image instanceof ImageData ) {
|
||||
|
||||
context.putImageData( image, 0, 0 );
|
||||
|
||||
} else {
|
||||
|
||||
context.drawImage( image, 0, 0, image.width, image.height );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( canvas.width > 2048 || canvas.height > 2048 ) {
|
||||
|
||||
return canvas.toDataURL( 'image/jpeg', 0.6 );
|
||||
|
||||
} else {
|
||||
|
||||
return canvas.toDataURL( 'image/png' );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export { ImageUtils };
|
96
lib/extras/ShapeUtils.js
Normal file
@ -0,0 +1,96 @@
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
*/
|
||||
|
||||
import { Earcut } from './Earcut.js';
|
||||
|
||||
var ShapeUtils = {
|
||||
|
||||
// calculate area of the contour polygon
|
||||
|
||||
area: function ( contour ) {
|
||||
|
||||
var n = contour.length;
|
||||
var a = 0.0;
|
||||
|
||||
for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
|
||||
|
||||
a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
|
||||
|
||||
}
|
||||
|
||||
return a * 0.5;
|
||||
|
||||
},
|
||||
|
||||
isClockWise: function ( pts ) {
|
||||
|
||||
return ShapeUtils.area( pts ) < 0;
|
||||
|
||||
},
|
||||
|
||||
triangulateShape: function ( contour, holes ) {
|
||||
|
||||
var vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ]
|
||||
var holeIndices = []; // array of hole indices
|
||||
var faces = []; // final array of vertex indices like [ [ a,b,d ], [ b,c,d ] ]
|
||||
|
||||
removeDupEndPts( contour );
|
||||
addContour( vertices, contour );
|
||||
|
||||
//
|
||||
|
||||
var holeIndex = contour.length;
|
||||
|
||||
holes.forEach( removeDupEndPts );
|
||||
|
||||
for ( var i = 0; i < holes.length; i ++ ) {
|
||||
|
||||
holeIndices.push( holeIndex );
|
||||
holeIndex += holes[ i ].length;
|
||||
addContour( vertices, holes[ i ] );
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
var triangles = Earcut.triangulate( vertices, holeIndices );
|
||||
|
||||
//
|
||||
|
||||
for ( var i = 0; i < triangles.length; i += 3 ) {
|
||||
|
||||
faces.push( triangles.slice( i, i + 3 ) );
|
||||
|
||||
}
|
||||
|
||||
return faces;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
function removeDupEndPts( points ) {
|
||||
|
||||
var l = points.length;
|
||||
|
||||
if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) {
|
||||
|
||||
points.pop();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function addContour( vertices, contour ) {
|
||||
|
||||
for ( var i = 0; i < contour.length; i ++ ) {
|
||||
|
||||
vertices.push( contour[ i ].x );
|
||||
vertices.push( contour[ i ].y );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export { ShapeUtils };
|
425
lib/extras/core/Curve.js
Normal file
@ -0,0 +1,425 @@
|
||||
import { _Math } from '../../math/Math.js';
|
||||
import { Vector3 } from '../../math/Vector3.js';
|
||||
import { Matrix4 } from '../../math/Matrix4.js';
|
||||
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
* Extensible curve object
|
||||
*
|
||||
* Some common of curve methods:
|
||||
* .getPoint( t, optionalTarget ), .getTangent( t )
|
||||
* .getPointAt( u, optionalTarget ), .getTangentAt( u )
|
||||
* .getPoints(), .getSpacedPoints()
|
||||
* .getLength()
|
||||
* .updateArcLengths()
|
||||
*
|
||||
* This following curves inherit from THREE.Curve:
|
||||
*
|
||||
* -- 2D curves --
|
||||
* THREE.ArcCurve
|
||||
* THREE.CubicBezierCurve
|
||||
* THREE.EllipseCurve
|
||||
* THREE.LineCurve
|
||||
* THREE.QuadraticBezierCurve
|
||||
* THREE.SplineCurve
|
||||
*
|
||||
* -- 3D curves --
|
||||
* THREE.CatmullRomCurve3
|
||||
* THREE.CubicBezierCurve3
|
||||
* THREE.LineCurve3
|
||||
* THREE.QuadraticBezierCurve3
|
||||
*
|
||||
* A series of curves can be represented as a THREE.CurvePath.
|
||||
*
|
||||
**/
|
||||
|
||||
/**************************************************************
|
||||
* Abstract Curve base class
|
||||
**************************************************************/
|
||||
|
||||
function Curve() {
|
||||
|
||||
this.type = 'Curve';
|
||||
|
||||
this.arcLengthDivisions = 200;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( Curve.prototype, {
|
||||
|
||||
// Virtual base class method to overwrite and implement in subclasses
|
||||
// - t [0 .. 1]
|
||||
|
||||
getPoint: function ( /* t, optionalTarget */ ) {
|
||||
|
||||
console.warn( 'THREE.Curve: .getPoint() not implemented.' );
|
||||
return null;
|
||||
|
||||
},
|
||||
|
||||
// Get point at relative position in curve according to arc length
|
||||
// - u [0 .. 1]
|
||||
|
||||
getPointAt: function ( u, optionalTarget ) {
|
||||
|
||||
var t = this.getUtoTmapping( u );
|
||||
return this.getPoint( t, optionalTarget );
|
||||
|
||||
},
|
||||
|
||||
// Get sequence of points using getPoint( t )
|
||||
|
||||
getPoints: function ( divisions ) {
|
||||
|
||||
if ( divisions === undefined ) divisions = 5;
|
||||
|
||||
var points = [];
|
||||
|
||||
for ( var d = 0; d <= divisions; d ++ ) {
|
||||
|
||||
points.push( this.getPoint( d / divisions ) );
|
||||
|
||||
}
|
||||
|
||||
return points;
|
||||
|
||||
},
|
||||
|
||||
// Get sequence of points using getPointAt( u )
|
||||
|
||||
getSpacedPoints: function ( divisions ) {
|
||||
|
||||
if ( divisions === undefined ) divisions = 5;
|
||||
|
||||
var points = [];
|
||||
|
||||
for ( var d = 0; d <= divisions; d ++ ) {
|
||||
|
||||
points.push( this.getPointAt( d / divisions ) );
|
||||
|
||||
}
|
||||
|
||||
return points;
|
||||
|
||||
},
|
||||
|
||||
// Get total curve arc length
|
||||
|
||||
getLength: function () {
|
||||
|
||||
var lengths = this.getLengths();
|
||||
return lengths[ lengths.length - 1 ];
|
||||
|
||||
},
|
||||
|
||||
// Get list of cumulative segment lengths
|
||||
|
||||
getLengths: function ( divisions ) {
|
||||
|
||||
if ( divisions === undefined ) divisions = this.arcLengthDivisions;
|
||||
|
||||
if ( this.cacheArcLengths &&
|
||||
( this.cacheArcLengths.length === divisions + 1 ) &&
|
||||
! this.needsUpdate ) {
|
||||
|
||||
return this.cacheArcLengths;
|
||||
|
||||
}
|
||||
|
||||
this.needsUpdate = false;
|
||||
|
||||
var cache = [];
|
||||
var current, last = this.getPoint( 0 );
|
||||
var p, sum = 0;
|
||||
|
||||
cache.push( 0 );
|
||||
|
||||
for ( p = 1; p <= divisions; p ++ ) {
|
||||
|
||||
current = this.getPoint( p / divisions );
|
||||
sum += current.distanceTo( last );
|
||||
cache.push( sum );
|
||||
last = current;
|
||||
|
||||
}
|
||||
|
||||
this.cacheArcLengths = cache;
|
||||
|
||||
return cache; // { sums: cache, sum: sum }; Sum is in the last element.
|
||||
|
||||
},
|
||||
|
||||
updateArcLengths: function () {
|
||||
|
||||
this.needsUpdate = true;
|
||||
this.getLengths();
|
||||
|
||||
},
|
||||
|
||||
// Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant
|
||||
|
||||
getUtoTmapping: function ( u, distance ) {
|
||||
|
||||
var arcLengths = this.getLengths();
|
||||
|
||||
var i = 0, il = arcLengths.length;
|
||||
|
||||
var targetArcLength; // The targeted u distance value to get
|
||||
|
||||
if ( distance ) {
|
||||
|
||||
targetArcLength = distance;
|
||||
|
||||
} else {
|
||||
|
||||
targetArcLength = u * arcLengths[ il - 1 ];
|
||||
|
||||
}
|
||||
|
||||
// binary search for the index with largest value smaller than target u distance
|
||||
|
||||
var low = 0, high = il - 1, comparison;
|
||||
|
||||
while ( low <= high ) {
|
||||
|
||||
i = Math.floor( low + ( high - low ) / 2 ); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats
|
||||
|
||||
comparison = arcLengths[ i ] - targetArcLength;
|
||||
|
||||
if ( comparison < 0 ) {
|
||||
|
||||
low = i + 1;
|
||||
|
||||
} else if ( comparison > 0 ) {
|
||||
|
||||
high = i - 1;
|
||||
|
||||
} else {
|
||||
|
||||
high = i;
|
||||
break;
|
||||
|
||||
// DONE
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
i = high;
|
||||
|
||||
if ( arcLengths[ i ] === targetArcLength ) {
|
||||
|
||||
return i / ( il - 1 );
|
||||
|
||||
}
|
||||
|
||||
// we could get finer grain at lengths, or use simple interpolation between two points
|
||||
|
||||
var lengthBefore = arcLengths[ i ];
|
||||
var lengthAfter = arcLengths[ i + 1 ];
|
||||
|
||||
var segmentLength = lengthAfter - lengthBefore;
|
||||
|
||||
// determine where we are between the 'before' and 'after' points
|
||||
|
||||
var segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength;
|
||||
|
||||
// add that fractional amount to t
|
||||
|
||||
var t = ( i + segmentFraction ) / ( il - 1 );
|
||||
|
||||
return t;
|
||||
|
||||
},
|
||||
|
||||
// Returns a unit vector tangent at t
|
||||
// In case any sub curve does not implement its tangent derivation,
|
||||
// 2 points a small delta apart will be used to find its gradient
|
||||
// which seems to give a reasonable approximation
|
||||
|
||||
getTangent: function ( t ) {
|
||||
|
||||
var delta = 0.0001;
|
||||
var t1 = t - delta;
|
||||
var t2 = t + delta;
|
||||
|
||||
// Capping in case of danger
|
||||
|
||||
if ( t1 < 0 ) t1 = 0;
|
||||
if ( t2 > 1 ) t2 = 1;
|
||||
|
||||
var pt1 = this.getPoint( t1 );
|
||||
var pt2 = this.getPoint( t2 );
|
||||
|
||||
var vec = pt2.clone().sub( pt1 );
|
||||
return vec.normalize();
|
||||
|
||||
},
|
||||
|
||||
getTangentAt: function ( u ) {
|
||||
|
||||
var t = this.getUtoTmapping( u );
|
||||
return this.getTangent( t );
|
||||
|
||||
},
|
||||
|
||||
computeFrenetFrames: function ( segments, closed ) {
|
||||
|
||||
// see http://www.cs.indiana.edu/pub/techreports/TR425.pdf
|
||||
|
||||
var normal = new Vector3();
|
||||
|
||||
var tangents = [];
|
||||
var normals = [];
|
||||
var binormals = [];
|
||||
|
||||
var vec = new Vector3();
|
||||
var mat = new Matrix4();
|
||||
|
||||
var i, u, theta;
|
||||
|
||||
// compute the tangent vectors for each segment on the curve
|
||||
|
||||
for ( i = 0; i <= segments; i ++ ) {
|
||||
|
||||
u = i / segments;
|
||||
|
||||
tangents[ i ] = this.getTangentAt( u );
|
||||
tangents[ i ].normalize();
|
||||
|
||||
}
|
||||
|
||||
// select an initial normal vector perpendicular to the first tangent vector,
|
||||
// and in the direction of the minimum tangent xyz component
|
||||
|
||||
normals[ 0 ] = new Vector3();
|
||||
binormals[ 0 ] = new Vector3();
|
||||
var min = Number.MAX_VALUE;
|
||||
var tx = Math.abs( tangents[ 0 ].x );
|
||||
var ty = Math.abs( tangents[ 0 ].y );
|
||||
var tz = Math.abs( tangents[ 0 ].z );
|
||||
|
||||
if ( tx <= min ) {
|
||||
|
||||
min = tx;
|
||||
normal.set( 1, 0, 0 );
|
||||
|
||||
}
|
||||
|
||||
if ( ty <= min ) {
|
||||
|
||||
min = ty;
|
||||
normal.set( 0, 1, 0 );
|
||||
|
||||
}
|
||||
|
||||
if ( tz <= min ) {
|
||||
|
||||
normal.set( 0, 0, 1 );
|
||||
|
||||
}
|
||||
|
||||
vec.crossVectors( tangents[ 0 ], normal ).normalize();
|
||||
|
||||
normals[ 0 ].crossVectors( tangents[ 0 ], vec );
|
||||
binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] );
|
||||
|
||||
|
||||
// compute the slowly-varying normal and binormal vectors for each segment on the curve
|
||||
|
||||
for ( i = 1; i <= segments; i ++ ) {
|
||||
|
||||
normals[ i ] = normals[ i - 1 ].clone();
|
||||
|
||||
binormals[ i ] = binormals[ i - 1 ].clone();
|
||||
|
||||
vec.crossVectors( tangents[ i - 1 ], tangents[ i ] );
|
||||
|
||||
if ( vec.length() > Number.EPSILON ) {
|
||||
|
||||
vec.normalize();
|
||||
|
||||
theta = Math.acos( _Math.clamp( tangents[ i - 1 ].dot( tangents[ i ] ), - 1, 1 ) ); // clamp for floating pt errors
|
||||
|
||||
normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) );
|
||||
|
||||
}
|
||||
|
||||
binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
|
||||
|
||||
}
|
||||
|
||||
// if the curve is closed, postprocess the vectors so the first and last normal vectors are the same
|
||||
|
||||
if ( closed === true ) {
|
||||
|
||||
theta = Math.acos( _Math.clamp( normals[ 0 ].dot( normals[ segments ] ), - 1, 1 ) );
|
||||
theta /= segments;
|
||||
|
||||
if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ segments ] ) ) > 0 ) {
|
||||
|
||||
theta = - theta;
|
||||
|
||||
}
|
||||
|
||||
for ( i = 1; i <= segments; i ++ ) {
|
||||
|
||||
// twist a little...
|
||||
normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) );
|
||||
binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return {
|
||||
tangents: tangents,
|
||||
normals: normals,
|
||||
binormals: binormals
|
||||
};
|
||||
|
||||
},
|
||||
|
||||
clone: function () {
|
||||
|
||||
return new this.constructor().copy( this );
|
||||
|
||||
},
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
this.arcLengthDivisions = source.arcLengthDivisions;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
toJSON: function () {
|
||||
|
||||
var data = {
|
||||
metadata: {
|
||||
version: 4.5,
|
||||
type: 'Curve',
|
||||
generator: 'Curve.toJSON'
|
||||
}
|
||||
};
|
||||
|
||||
data.arcLengthDivisions = this.arcLengthDivisions;
|
||||
data.type = this.type;
|
||||
|
||||
return data;
|
||||
|
||||
},
|
||||
|
||||
fromJSON: function ( json ) {
|
||||
|
||||
this.arcLengthDivisions = json.arcLengthDivisions;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { Curve };
|
261
lib/extras/core/CurvePath.js
Normal file
@ -0,0 +1,261 @@
|
||||
import { Curve } from './Curve.js';
|
||||
import * as Curves from '../curves/Curves.js';
|
||||
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
*
|
||||
**/
|
||||
|
||||
/**************************************************************
|
||||
* Curved Path - a curve path is simply a array of connected
|
||||
* curves, but retains the api of a curve
|
||||
**************************************************************/
|
||||
|
||||
function CurvePath() {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'CurvePath';
|
||||
|
||||
this.curves = [];
|
||||
this.autoClose = false; // Automatically closes the path
|
||||
|
||||
}
|
||||
|
||||
CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {
|
||||
|
||||
constructor: CurvePath,
|
||||
|
||||
add: function ( curve ) {
|
||||
|
||||
this.curves.push( curve );
|
||||
|
||||
},
|
||||
|
||||
closePath: function () {
|
||||
|
||||
// Add a line curve if start and end of lines are not connected
|
||||
var startPoint = this.curves[ 0 ].getPoint( 0 );
|
||||
var endPoint = this.curves[ this.curves.length - 1 ].getPoint( 1 );
|
||||
|
||||
if ( ! startPoint.equals( endPoint ) ) {
|
||||
|
||||
this.curves.push( new Curves[ 'LineCurve' ]( endPoint, startPoint ) );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
// To get accurate point with reference to
|
||||
// entire path distance at time t,
|
||||
// following has to be done:
|
||||
|
||||
// 1. Length of each sub path have to be known
|
||||
// 2. Locate and identify type of curve
|
||||
// 3. Get t for the curve
|
||||
// 4. Return curve.getPointAt(t')
|
||||
|
||||
getPoint: function ( t ) {
|
||||
|
||||
var d = t * this.getLength();
|
||||
var curveLengths = this.getCurveLengths();
|
||||
var i = 0;
|
||||
|
||||
// To think about boundaries points.
|
||||
|
||||
while ( i < curveLengths.length ) {
|
||||
|
||||
if ( curveLengths[ i ] >= d ) {
|
||||
|
||||
var diff = curveLengths[ i ] - d;
|
||||
var curve = this.curves[ i ];
|
||||
|
||||
var segmentLength = curve.getLength();
|
||||
var u = segmentLength === 0 ? 0 : 1 - diff / segmentLength;
|
||||
|
||||
return curve.getPointAt( u );
|
||||
|
||||
}
|
||||
|
||||
i ++;
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
// loop where sum != 0, sum > d , sum+1 <d
|
||||
|
||||
},
|
||||
|
||||
// We cannot use the default THREE.Curve getPoint() with getLength() because in
|
||||
// THREE.Curve, getLength() depends on getPoint() but in THREE.CurvePath
|
||||
// getPoint() depends on getLength
|
||||
|
||||
getLength: function () {
|
||||
|
||||
var lens = this.getCurveLengths();
|
||||
return lens[ lens.length - 1 ];
|
||||
|
||||
},
|
||||
|
||||
// cacheLengths must be recalculated.
|
||||
updateArcLengths: function () {
|
||||
|
||||
this.needsUpdate = true;
|
||||
this.cacheLengths = null;
|
||||
this.getCurveLengths();
|
||||
|
||||
},
|
||||
|
||||
// Compute lengths and cache them
|
||||
// We cannot overwrite getLengths() because UtoT mapping uses it.
|
||||
|
||||
getCurveLengths: function () {
|
||||
|
||||
// We use cache values if curves and cache array are same length
|
||||
|
||||
if ( this.cacheLengths && this.cacheLengths.length === this.curves.length ) {
|
||||
|
||||
return this.cacheLengths;
|
||||
|
||||
}
|
||||
|
||||
// Get length of sub-curve
|
||||
// Push sums into cached array
|
||||
|
||||
var lengths = [], sums = 0;
|
||||
|
||||
for ( var i = 0, l = this.curves.length; i < l; i ++ ) {
|
||||
|
||||
sums += this.curves[ i ].getLength();
|
||||
lengths.push( sums );
|
||||
|
||||
}
|
||||
|
||||
this.cacheLengths = lengths;
|
||||
|
||||
return lengths;
|
||||
|
||||
},
|
||||
|
||||
getSpacedPoints: function ( divisions ) {
|
||||
|
||||
if ( divisions === undefined ) divisions = 40;
|
||||
|
||||
var points = [];
|
||||
|
||||
for ( var i = 0; i <= divisions; i ++ ) {
|
||||
|
||||
points.push( this.getPoint( i / divisions ) );
|
||||
|
||||
}
|
||||
|
||||
if ( this.autoClose ) {
|
||||
|
||||
points.push( points[ 0 ] );
|
||||
|
||||
}
|
||||
|
||||
return points;
|
||||
|
||||
},
|
||||
|
||||
getPoints: function ( divisions ) {
|
||||
|
||||
divisions = divisions || 12;
|
||||
|
||||
var points = [], last;
|
||||
|
||||
for ( var i = 0, curves = this.curves; i < curves.length; i ++ ) {
|
||||
|
||||
var curve = curves[ i ];
|
||||
var resolution = ( curve && curve.isEllipseCurve ) ? divisions * 2
|
||||
: ( curve && ( curve.isLineCurve || curve.isLineCurve3 ) ) ? 1
|
||||
: ( curve && curve.isSplineCurve ) ? divisions * curve.points.length
|
||||
: divisions;
|
||||
|
||||
var pts = curve.getPoints( resolution );
|
||||
|
||||
for ( var j = 0; j < pts.length; j ++ ) {
|
||||
|
||||
var point = pts[ j ];
|
||||
|
||||
if ( last && last.equals( point ) ) continue; // ensures no consecutive points are duplicates
|
||||
|
||||
points.push( point );
|
||||
last = point;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( this.autoClose && points.length > 1 && ! points[ points.length - 1 ].equals( points[ 0 ] ) ) {
|
||||
|
||||
points.push( points[ 0 ] );
|
||||
|
||||
}
|
||||
|
||||
return points;
|
||||
|
||||
},
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.curves = [];
|
||||
|
||||
for ( var i = 0, l = source.curves.length; i < l; i ++ ) {
|
||||
|
||||
var curve = source.curves[ i ];
|
||||
|
||||
this.curves.push( curve.clone() );
|
||||
|
||||
}
|
||||
|
||||
this.autoClose = source.autoClose;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
toJSON: function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.autoClose = this.autoClose;
|
||||
data.curves = [];
|
||||
|
||||
for ( var i = 0, l = this.curves.length; i < l; i ++ ) {
|
||||
|
||||
var curve = this.curves[ i ];
|
||||
data.curves.push( curve.toJSON() );
|
||||
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
},
|
||||
|
||||
fromJSON: function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.autoClose = json.autoClose;
|
||||
this.curves = [];
|
||||
|
||||
for ( var i = 0, l = json.curves.length; i < l; i ++ ) {
|
||||
|
||||
var curve = json.curves[ i ];
|
||||
this.curves.push( new Curves[ curve.type ]().fromJSON( curve ) );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { CurvePath };
|
145
lib/extras/core/Font.js
Normal file
@ -0,0 +1,145 @@
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
*/
|
||||
|
||||
import { ShapePath } from './ShapePath.js';
|
||||
|
||||
|
||||
function Font( data ) {
|
||||
|
||||
this.type = 'Font';
|
||||
|
||||
this.data = data;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( Font.prototype, {
|
||||
|
||||
isFont: true,
|
||||
|
||||
generateShapes: function ( text, size ) {
|
||||
|
||||
if ( size === undefined ) size = 100;
|
||||
|
||||
var shapes = [];
|
||||
var paths = createPaths( text, size, this.data );
|
||||
|
||||
for ( var p = 0, pl = paths.length; p < pl; p ++ ) {
|
||||
|
||||
Array.prototype.push.apply( shapes, paths[ p ].toShapes() );
|
||||
|
||||
}
|
||||
|
||||
return shapes;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
function createPaths( text, size, data ) {
|
||||
|
||||
var chars = Array.from ? Array.from( text ) : String( text ).split( '' ); // see #13988
|
||||
var scale = size / data.resolution;
|
||||
var line_height = ( data.boundingBox.yMax - data.boundingBox.yMin + data.underlineThickness ) * scale;
|
||||
|
||||
var paths = [];
|
||||
|
||||
var offsetX = 0, offsetY = 0;
|
||||
|
||||
for ( var i = 0; i < chars.length; i ++ ) {
|
||||
|
||||
var char = chars[ i ];
|
||||
|
||||
if ( char === '\n' ) {
|
||||
|
||||
offsetX = 0;
|
||||
offsetY -= line_height;
|
||||
|
||||
} else {
|
||||
|
||||
var ret = createPath( char, scale, offsetX, offsetY, data );
|
||||
offsetX += ret.offsetX;
|
||||
paths.push( ret.path );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return paths;
|
||||
|
||||
}
|
||||
|
||||
function createPath( char, scale, offsetX, offsetY, data ) {
|
||||
|
||||
var glyph = data.glyphs[ char ] || data.glyphs[ '?' ];
|
||||
|
||||
if ( ! glyph ) return;
|
||||
|
||||
var path = new ShapePath();
|
||||
|
||||
var x, y, cpx, cpy, cpx1, cpy1, cpx2, cpy2;
|
||||
|
||||
if ( glyph.o ) {
|
||||
|
||||
var outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) );
|
||||
|
||||
for ( var i = 0, l = outline.length; i < l; ) {
|
||||
|
||||
var action = outline[ i ++ ];
|
||||
|
||||
switch ( action ) {
|
||||
|
||||
case 'm': // moveTo
|
||||
|
||||
x = outline[ i ++ ] * scale + offsetX;
|
||||
y = outline[ i ++ ] * scale + offsetY;
|
||||
|
||||
path.moveTo( x, y );
|
||||
|
||||
break;
|
||||
|
||||
case 'l': // lineTo
|
||||
|
||||
x = outline[ i ++ ] * scale + offsetX;
|
||||
y = outline[ i ++ ] * scale + offsetY;
|
||||
|
||||
path.lineTo( x, y );
|
||||
|
||||
break;
|
||||
|
||||
case 'q': // quadraticCurveTo
|
||||
|
||||
cpx = outline[ i ++ ] * scale + offsetX;
|
||||
cpy = outline[ i ++ ] * scale + offsetY;
|
||||
cpx1 = outline[ i ++ ] * scale + offsetX;
|
||||
cpy1 = outline[ i ++ ] * scale + offsetY;
|
||||
|
||||
path.quadraticCurveTo( cpx1, cpy1, cpx, cpy );
|
||||
|
||||
break;
|
||||
|
||||
case 'b': // bezierCurveTo
|
||||
|
||||
cpx = outline[ i ++ ] * scale + offsetX;
|
||||
cpy = outline[ i ++ ] * scale + offsetY;
|
||||
cpx1 = outline[ i ++ ] * scale + offsetX;
|
||||
cpy1 = outline[ i ++ ] * scale + offsetY;
|
||||
cpx2 = outline[ i ++ ] * scale + offsetX;
|
||||
cpy2 = outline[ i ++ ] * scale + offsetY;
|
||||
|
||||
path.bezierCurveTo( cpx1, cpy1, cpx2, cpy2, cpx, cpy );
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return { offsetX: glyph.ha * scale, path: path };
|
||||
|
||||
}
|
||||
|
||||
export { Font };
|
81
lib/extras/core/Interpolations.js
Normal file
@ -0,0 +1,81 @@
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
*
|
||||
* Bezier Curves formulas obtained from
|
||||
* http://en.wikipedia.org/wiki/Bézier_curve
|
||||
*/
|
||||
|
||||
function CatmullRom( t, p0, p1, p2, p3 ) {
|
||||
|
||||
var v0 = ( p2 - p0 ) * 0.5;
|
||||
var v1 = ( p3 - p1 ) * 0.5;
|
||||
var t2 = t * t;
|
||||
var t3 = t * t2;
|
||||
return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1;
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
function QuadraticBezierP0( t, p ) {
|
||||
|
||||
var k = 1 - t;
|
||||
return k * k * p;
|
||||
|
||||
}
|
||||
|
||||
function QuadraticBezierP1( t, p ) {
|
||||
|
||||
return 2 * ( 1 - t ) * t * p;
|
||||
|
||||
}
|
||||
|
||||
function QuadraticBezierP2( t, p ) {
|
||||
|
||||
return t * t * p;
|
||||
|
||||
}
|
||||
|
||||
function QuadraticBezier( t, p0, p1, p2 ) {
|
||||
|
||||
return QuadraticBezierP0( t, p0 ) + QuadraticBezierP1( t, p1 ) +
|
||||
QuadraticBezierP2( t, p2 );
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
function CubicBezierP0( t, p ) {
|
||||
|
||||
var k = 1 - t;
|
||||
return k * k * k * p;
|
||||
|
||||
}
|
||||
|
||||
function CubicBezierP1( t, p ) {
|
||||
|
||||
var k = 1 - t;
|
||||
return 3 * k * k * t * p;
|
||||
|
||||
}
|
||||
|
||||
function CubicBezierP2( t, p ) {
|
||||
|
||||
return 3 * ( 1 - t ) * t * t * p;
|
||||
|
||||
}
|
||||
|
||||
function CubicBezierP3( t, p ) {
|
||||
|
||||
return t * t * t * p;
|
||||
|
||||
}
|
||||
|
||||
function CubicBezier( t, p0, p1, p2, p3 ) {
|
||||
|
||||
return CubicBezierP0( t, p0 ) + CubicBezierP1( t, p1 ) + CubicBezierP2( t, p2 ) +
|
||||
CubicBezierP3( t, p3 );
|
||||
|
||||
}
|
||||
|
||||
export { CatmullRom, QuadraticBezier, CubicBezier };
|
183
lib/extras/core/Path.js
Normal file
@ -0,0 +1,183 @@
|
||||
import { Vector2 } from '../../math/Vector2.js';
|
||||
import { CurvePath } from './CurvePath.js';
|
||||
import { EllipseCurve } from '../curves/EllipseCurve.js';
|
||||
import { SplineCurve } from '../curves/SplineCurve.js';
|
||||
import { CubicBezierCurve } from '../curves/CubicBezierCurve.js';
|
||||
import { QuadraticBezierCurve } from '../curves/QuadraticBezierCurve.js';
|
||||
import { LineCurve } from '../curves/LineCurve.js';
|
||||
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
* Creates free form 2d path using series of points, lines or curves.
|
||||
**/
|
||||
|
||||
function Path( points ) {
|
||||
|
||||
CurvePath.call( this );
|
||||
|
||||
this.type = 'Path';
|
||||
|
||||
this.currentPoint = new Vector2();
|
||||
|
||||
if ( points ) {
|
||||
|
||||
this.setFromPoints( points );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {
|
||||
|
||||
constructor: Path,
|
||||
|
||||
setFromPoints: function ( points ) {
|
||||
|
||||
this.moveTo( points[ 0 ].x, points[ 0 ].y );
|
||||
|
||||
for ( var i = 1, l = points.length; i < l; i ++ ) {
|
||||
|
||||
this.lineTo( points[ i ].x, points[ i ].y );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
moveTo: function ( x, y ) {
|
||||
|
||||
this.currentPoint.set( x, y ); // TODO consider referencing vectors instead of copying?
|
||||
|
||||
},
|
||||
|
||||
lineTo: function ( x, y ) {
|
||||
|
||||
var curve = new LineCurve( this.currentPoint.clone(), new Vector2( x, y ) );
|
||||
this.curves.push( curve );
|
||||
|
||||
this.currentPoint.set( x, y );
|
||||
|
||||
},
|
||||
|
||||
quadraticCurveTo: function ( aCPx, aCPy, aX, aY ) {
|
||||
|
||||
var curve = new QuadraticBezierCurve(
|
||||
this.currentPoint.clone(),
|
||||
new Vector2( aCPx, aCPy ),
|
||||
new Vector2( aX, aY )
|
||||
);
|
||||
|
||||
this.curves.push( curve );
|
||||
|
||||
this.currentPoint.set( aX, aY );
|
||||
|
||||
},
|
||||
|
||||
bezierCurveTo: function ( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
|
||||
|
||||
var curve = new CubicBezierCurve(
|
||||
this.currentPoint.clone(),
|
||||
new Vector2( aCP1x, aCP1y ),
|
||||
new Vector2( aCP2x, aCP2y ),
|
||||
new Vector2( aX, aY )
|
||||
);
|
||||
|
||||
this.curves.push( curve );
|
||||
|
||||
this.currentPoint.set( aX, aY );
|
||||
|
||||
},
|
||||
|
||||
splineThru: function ( pts /*Array of Vector*/ ) {
|
||||
|
||||
var npts = [ this.currentPoint.clone() ].concat( pts );
|
||||
|
||||
var curve = new SplineCurve( npts );
|
||||
this.curves.push( curve );
|
||||
|
||||
this.currentPoint.copy( pts[ pts.length - 1 ] );
|
||||
|
||||
},
|
||||
|
||||
arc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
|
||||
|
||||
var x0 = this.currentPoint.x;
|
||||
var y0 = this.currentPoint.y;
|
||||
|
||||
this.absarc( aX + x0, aY + y0, aRadius,
|
||||
aStartAngle, aEndAngle, aClockwise );
|
||||
|
||||
},
|
||||
|
||||
absarc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
|
||||
|
||||
this.absellipse( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
|
||||
|
||||
},
|
||||
|
||||
ellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
|
||||
|
||||
var x0 = this.currentPoint.x;
|
||||
var y0 = this.currentPoint.y;
|
||||
|
||||
this.absellipse( aX + x0, aY + y0, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
|
||||
|
||||
},
|
||||
|
||||
absellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
|
||||
|
||||
var curve = new EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
|
||||
|
||||
if ( this.curves.length > 0 ) {
|
||||
|
||||
// if a previous curve is present, attempt to join
|
||||
var firstPoint = curve.getPoint( 0 );
|
||||
|
||||
if ( ! firstPoint.equals( this.currentPoint ) ) {
|
||||
|
||||
this.lineTo( firstPoint.x, firstPoint.y );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.curves.push( curve );
|
||||
|
||||
var lastPoint = curve.getPoint( 1 );
|
||||
this.currentPoint.copy( lastPoint );
|
||||
|
||||
},
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
CurvePath.prototype.copy.call( this, source );
|
||||
|
||||
this.currentPoint.copy( source.currentPoint );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
toJSON: function () {
|
||||
|
||||
var data = CurvePath.prototype.toJSON.call( this );
|
||||
|
||||
data.currentPoint = this.currentPoint.toArray();
|
||||
|
||||
return data;
|
||||
|
||||
},
|
||||
|
||||
fromJSON: function ( json ) {
|
||||
|
||||
CurvePath.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.currentPoint.fromArray( json.currentPoint );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { Path };
|
115
lib/extras/core/Shape.js
Normal file
@ -0,0 +1,115 @@
|
||||
import { Path } from './Path.js';
|
||||
import { _Math } from '../../math/Math.js';
|
||||
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
* Defines a 2d shape plane using paths.
|
||||
**/
|
||||
|
||||
// STEP 1 Create a path.
|
||||
// STEP 2 Turn path into shape.
|
||||
// STEP 3 ExtrudeGeometry takes in Shape/Shapes
|
||||
// STEP 3a - Extract points from each shape, turn to vertices
|
||||
// STEP 3b - Triangulate each shape, add faces.
|
||||
|
||||
function Shape( points ) {
|
||||
|
||||
Path.call( this, points );
|
||||
|
||||
this.uuid = _Math.generateUUID();
|
||||
|
||||
this.type = 'Shape';
|
||||
|
||||
this.holes = [];
|
||||
|
||||
}
|
||||
|
||||
Shape.prototype = Object.assign( Object.create( Path.prototype ), {
|
||||
|
||||
constructor: Shape,
|
||||
|
||||
getPointsHoles: function ( divisions ) {
|
||||
|
||||
var holesPts = [];
|
||||
|
||||
for ( var i = 0, l = this.holes.length; i < l; i ++ ) {
|
||||
|
||||
holesPts[ i ] = this.holes[ i ].getPoints( divisions );
|
||||
|
||||
}
|
||||
|
||||
return holesPts;
|
||||
|
||||
},
|
||||
|
||||
// get points of shape and holes (keypoints based on segments parameter)
|
||||
|
||||
extractPoints: function ( divisions ) {
|
||||
|
||||
return {
|
||||
|
||||
shape: this.getPoints( divisions ),
|
||||
holes: this.getPointsHoles( divisions )
|
||||
|
||||
};
|
||||
|
||||
},
|
||||
|
||||
copy: function ( source ) {
|
||||
|
||||
Path.prototype.copy.call( this, source );
|
||||
|
||||
this.holes = [];
|
||||
|
||||
for ( var i = 0, l = source.holes.length; i < l; i ++ ) {
|
||||
|
||||
var hole = source.holes[ i ];
|
||||
|
||||
this.holes.push( hole.clone() );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
toJSON: function () {
|
||||
|
||||
var data = Path.prototype.toJSON.call( this );
|
||||
|
||||
data.uuid = this.uuid;
|
||||
data.holes = [];
|
||||
|
||||
for ( var i = 0, l = this.holes.length; i < l; i ++ ) {
|
||||
|
||||
var hole = this.holes[ i ];
|
||||
data.holes.push( hole.toJSON() );
|
||||
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
},
|
||||
|
||||
fromJSON: function ( json ) {
|
||||
|
||||
Path.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.uuid = json.uuid;
|
||||
this.holes = [];
|
||||
|
||||
for ( var i = 0, l = json.holes.length; i < l; i ++ ) {
|
||||
|
||||
var hole = json.holes[ i ];
|
||||
this.holes.push( new Path().fromJSON( hole ) );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { Shape };
|
286
lib/extras/core/ShapePath.js
Normal file
@ -0,0 +1,286 @@
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
* minimal class for proxing functions to Path. Replaces old "extractSubpaths()"
|
||||
**/
|
||||
|
||||
import { Color } from '../../math/Color.js';
|
||||
import { Path } from './Path.js';
|
||||
import { Shape } from './Shape.js';
|
||||
import { ShapeUtils } from '../ShapeUtils.js';
|
||||
|
||||
function ShapePath() {
|
||||
|
||||
this.type = 'ShapePath';
|
||||
|
||||
this.color = new Color();
|
||||
|
||||
this.subPaths = [];
|
||||
this.currentPath = null;
|
||||
|
||||
}
|
||||
|
||||
Object.assign( ShapePath.prototype, {
|
||||
|
||||
moveTo: function ( x, y ) {
|
||||
|
||||
this.currentPath = new Path();
|
||||
this.subPaths.push( this.currentPath );
|
||||
this.currentPath.moveTo( x, y );
|
||||
|
||||
},
|
||||
|
||||
lineTo: function ( x, y ) {
|
||||
|
||||
this.currentPath.lineTo( x, y );
|
||||
|
||||
},
|
||||
|
||||
quadraticCurveTo: function ( aCPx, aCPy, aX, aY ) {
|
||||
|
||||
this.currentPath.quadraticCurveTo( aCPx, aCPy, aX, aY );
|
||||
|
||||
},
|
||||
|
||||
bezierCurveTo: function ( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
|
||||
|
||||
this.currentPath.bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY );
|
||||
|
||||
},
|
||||
|
||||
splineThru: function ( pts ) {
|
||||
|
||||
this.currentPath.splineThru( pts );
|
||||
|
||||
},
|
||||
|
||||
toShapes: function ( isCCW, noHoles ) {
|
||||
|
||||
function toShapesNoHoles( inSubpaths ) {
|
||||
|
||||
var shapes = [];
|
||||
|
||||
for ( var i = 0, l = inSubpaths.length; i < l; i ++ ) {
|
||||
|
||||
var tmpPath = inSubpaths[ i ];
|
||||
|
||||
var tmpShape = new Shape();
|
||||
tmpShape.curves = tmpPath.curves;
|
||||
|
||||
shapes.push( tmpShape );
|
||||
|
||||
}
|
||||
|
||||
return shapes;
|
||||
|
||||
}
|
||||
|
||||
function isPointInsidePolygon( inPt, inPolygon ) {
|
||||
|
||||
var polyLen = inPolygon.length;
|
||||
|
||||
// inPt on polygon contour => immediate success or
|
||||
// toggling of inside/outside at every single! intersection point of an edge
|
||||
// with the horizontal line through inPt, left of inPt
|
||||
// not counting lowerY endpoints of edges and whole edges on that line
|
||||
var inside = false;
|
||||
for ( var p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) {
|
||||
|
||||
var edgeLowPt = inPolygon[ p ];
|
||||
var edgeHighPt = inPolygon[ q ];
|
||||
|
||||
var edgeDx = edgeHighPt.x - edgeLowPt.x;
|
||||
var edgeDy = edgeHighPt.y - edgeLowPt.y;
|
||||
|
||||
if ( Math.abs( edgeDy ) > Number.EPSILON ) {
|
||||
|
||||
// not parallel
|
||||
if ( edgeDy < 0 ) {
|
||||
|
||||
edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx;
|
||||
edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy;
|
||||
|
||||
}
|
||||
if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue;
|
||||
|
||||
if ( inPt.y === edgeLowPt.y ) {
|
||||
|
||||
if ( inPt.x === edgeLowPt.x ) return true; // inPt is on contour ?
|
||||
// continue; // no intersection or edgeLowPt => doesn't count !!!
|
||||
|
||||
} else {
|
||||
|
||||
var perpEdge = edgeDy * ( inPt.x - edgeLowPt.x ) - edgeDx * ( inPt.y - edgeLowPt.y );
|
||||
if ( perpEdge === 0 ) return true; // inPt is on contour ?
|
||||
if ( perpEdge < 0 ) continue;
|
||||
inside = ! inside; // true intersection left of inPt
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// parallel or collinear
|
||||
if ( inPt.y !== edgeLowPt.y ) continue; // parallel
|
||||
// edge lies on the same horizontal line as inPt
|
||||
if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) ||
|
||||
( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour !
|
||||
// continue;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return inside;
|
||||
|
||||
}
|
||||
|
||||
var isClockWise = ShapeUtils.isClockWise;
|
||||
|
||||
var subPaths = this.subPaths;
|
||||
if ( subPaths.length === 0 ) return [];
|
||||
|
||||
if ( noHoles === true ) return toShapesNoHoles( subPaths );
|
||||
|
||||
|
||||
var solid, tmpPath, tmpShape, shapes = [];
|
||||
|
||||
if ( subPaths.length === 1 ) {
|
||||
|
||||
tmpPath = subPaths[ 0 ];
|
||||
tmpShape = new Shape();
|
||||
tmpShape.curves = tmpPath.curves;
|
||||
shapes.push( tmpShape );
|
||||
return shapes;
|
||||
|
||||
}
|
||||
|
||||
var holesFirst = ! isClockWise( subPaths[ 0 ].getPoints() );
|
||||
holesFirst = isCCW ? ! holesFirst : holesFirst;
|
||||
|
||||
// console.log("Holes first", holesFirst);
|
||||
|
||||
var betterShapeHoles = [];
|
||||
var newShapes = [];
|
||||
var newShapeHoles = [];
|
||||
var mainIdx = 0;
|
||||
var tmpPoints;
|
||||
|
||||
newShapes[ mainIdx ] = undefined;
|
||||
newShapeHoles[ mainIdx ] = [];
|
||||
|
||||
for ( var i = 0, l = subPaths.length; i < l; i ++ ) {
|
||||
|
||||
tmpPath = subPaths[ i ];
|
||||
tmpPoints = tmpPath.getPoints();
|
||||
solid = isClockWise( tmpPoints );
|
||||
solid = isCCW ? ! solid : solid;
|
||||
|
||||
if ( solid ) {
|
||||
|
||||
if ( ( ! holesFirst ) && ( newShapes[ mainIdx ] ) ) mainIdx ++;
|
||||
|
||||
newShapes[ mainIdx ] = { s: new Shape(), p: tmpPoints };
|
||||
newShapes[ mainIdx ].s.curves = tmpPath.curves;
|
||||
|
||||
if ( holesFirst ) mainIdx ++;
|
||||
newShapeHoles[ mainIdx ] = [];
|
||||
|
||||
//console.log('cw', i);
|
||||
|
||||
} else {
|
||||
|
||||
newShapeHoles[ mainIdx ].push( { h: tmpPath, p: tmpPoints[ 0 ] } );
|
||||
|
||||
//console.log('ccw', i);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// only Holes? -> probably all Shapes with wrong orientation
|
||||
if ( ! newShapes[ 0 ] ) return toShapesNoHoles( subPaths );
|
||||
|
||||
|
||||
if ( newShapes.length > 1 ) {
|
||||
|
||||
var ambiguous = false;
|
||||
var toChange = [];
|
||||
|
||||
for ( var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
|
||||
|
||||
betterShapeHoles[ sIdx ] = [];
|
||||
|
||||
}
|
||||
|
||||
for ( var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
|
||||
|
||||
var sho = newShapeHoles[ sIdx ];
|
||||
|
||||
for ( var hIdx = 0; hIdx < sho.length; hIdx ++ ) {
|
||||
|
||||
var ho = sho[ hIdx ];
|
||||
var hole_unassigned = true;
|
||||
|
||||
for ( var s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) {
|
||||
|
||||
if ( isPointInsidePolygon( ho.p, newShapes[ s2Idx ].p ) ) {
|
||||
|
||||
if ( sIdx !== s2Idx ) toChange.push( { froms: sIdx, tos: s2Idx, hole: hIdx } );
|
||||
if ( hole_unassigned ) {
|
||||
|
||||
hole_unassigned = false;
|
||||
betterShapeHoles[ s2Idx ].push( ho );
|
||||
|
||||
} else {
|
||||
|
||||
ambiguous = true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if ( hole_unassigned ) {
|
||||
|
||||
betterShapeHoles[ sIdx ].push( ho );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
// console.log("ambiguous: ", ambiguous);
|
||||
if ( toChange.length > 0 ) {
|
||||
|
||||
// console.log("to change: ", toChange);
|
||||
if ( ! ambiguous ) newShapeHoles = betterShapeHoles;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var tmpHoles;
|
||||
|
||||
for ( var i = 0, il = newShapes.length; i < il; i ++ ) {
|
||||
|
||||
tmpShape = newShapes[ i ].s;
|
||||
shapes.push( tmpShape );
|
||||
tmpHoles = newShapeHoles[ i ];
|
||||
|
||||
for ( var j = 0, jl = tmpHoles.length; j < jl; j ++ ) {
|
||||
|
||||
tmpShape.holes.push( tmpHoles[ j ].h );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//console.log("shape", shapes);
|
||||
|
||||
return shapes;
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
|
||||
export { ShapePath };
|
18
lib/extras/curves/ArcCurve.js
Normal file
@ -0,0 +1,18 @@
|
||||
import { EllipseCurve } from './EllipseCurve.js';
|
||||
|
||||
|
||||
function ArcCurve( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
|
||||
|
||||
EllipseCurve.call( this, aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
|
||||
|
||||
this.type = 'ArcCurve';
|
||||
|
||||
}
|
||||
|
||||
ArcCurve.prototype = Object.create( EllipseCurve.prototype );
|
||||
ArcCurve.prototype.constructor = ArcCurve;
|
||||
|
||||
ArcCurve.prototype.isArcCurve = true;
|
||||
|
||||
|
||||
export { ArcCurve };
|
255
lib/extras/curves/CatmullRomCurve3.js
Normal file
@ -0,0 +1,255 @@
|
||||
import { Vector3 } from '../../math/Vector3.js';
|
||||
import { Curve } from '../core/Curve.js';
|
||||
|
||||
/**
|
||||
* @author zz85 https://github.com/zz85
|
||||
*
|
||||
* Centripetal CatmullRom Curve - which is useful for avoiding
|
||||
* cusps and self-intersections in non-uniform catmull rom curves.
|
||||
* http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf
|
||||
*
|
||||
* curve.type accepts centripetal(default), chordal and catmullrom
|
||||
* curve.tension is used for catmullrom which defaults to 0.5
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
Based on an optimized c++ solution in
|
||||
- http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/
|
||||
- http://ideone.com/NoEbVM
|
||||
|
||||
This CubicPoly class could be used for reusing some variables and calculations,
|
||||
but for three.js curve use, it could be possible inlined and flatten into a single function call
|
||||
which can be placed in CurveUtils.
|
||||
*/
|
||||
|
||||
function CubicPoly() {
|
||||
|
||||
var c0 = 0, c1 = 0, c2 = 0, c3 = 0;
|
||||
|
||||
/*
|
||||
* Compute coefficients for a cubic polynomial
|
||||
* p(s) = c0 + c1*s + c2*s^2 + c3*s^3
|
||||
* such that
|
||||
* p(0) = x0, p(1) = x1
|
||||
* and
|
||||
* p'(0) = t0, p'(1) = t1.
|
||||
*/
|
||||
function init( x0, x1, t0, t1 ) {
|
||||
|
||||
c0 = x0;
|
||||
c1 = t0;
|
||||
c2 = - 3 * x0 + 3 * x1 - 2 * t0 - t1;
|
||||
c3 = 2 * x0 - 2 * x1 + t0 + t1;
|
||||
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
initCatmullRom: function ( x0, x1, x2, x3, tension ) {
|
||||
|
||||
init( x1, x2, tension * ( x2 - x0 ), tension * ( x3 - x1 ) );
|
||||
|
||||
},
|
||||
|
||||
initNonuniformCatmullRom: function ( x0, x1, x2, x3, dt0, dt1, dt2 ) {
|
||||
|
||||
// compute tangents when parameterized in [t1,t2]
|
||||
var t1 = ( x1 - x0 ) / dt0 - ( x2 - x0 ) / ( dt0 + dt1 ) + ( x2 - x1 ) / dt1;
|
||||
var t2 = ( x2 - x1 ) / dt1 - ( x3 - x1 ) / ( dt1 + dt2 ) + ( x3 - x2 ) / dt2;
|
||||
|
||||
// rescale tangents for parametrization in [0,1]
|
||||
t1 *= dt1;
|
||||
t2 *= dt1;
|
||||
|
||||
init( x1, x2, t1, t2 );
|
||||
|
||||
},
|
||||
|
||||
calc: function ( t ) {
|
||||
|
||||
var t2 = t * t;
|
||||
var t3 = t2 * t;
|
||||
return c0 + c1 * t + c2 * t2 + c3 * t3;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
var tmp = new Vector3();
|
||||
var px = new CubicPoly(), py = new CubicPoly(), pz = new CubicPoly();
|
||||
|
||||
function CatmullRomCurve3( points, closed, curveType, tension ) {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'CatmullRomCurve3';
|
||||
|
||||
this.points = points || [];
|
||||
this.closed = closed || false;
|
||||
this.curveType = curveType || 'centripetal';
|
||||
this.tension = tension || 0.5;
|
||||
|
||||
}
|
||||
|
||||
CatmullRomCurve3.prototype = Object.create( Curve.prototype );
|
||||
CatmullRomCurve3.prototype.constructor = CatmullRomCurve3;
|
||||
|
||||
CatmullRomCurve3.prototype.isCatmullRomCurve3 = true;
|
||||
|
||||
CatmullRomCurve3.prototype.getPoint = function ( t, optionalTarget ) {
|
||||
|
||||
var point = optionalTarget || new Vector3();
|
||||
|
||||
var points = this.points;
|
||||
var l = points.length;
|
||||
|
||||
var p = ( l - ( this.closed ? 0 : 1 ) ) * t;
|
||||
var intPoint = Math.floor( p );
|
||||
var weight = p - intPoint;
|
||||
|
||||
if ( this.closed ) {
|
||||
|
||||
intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / l ) + 1 ) * l;
|
||||
|
||||
} else if ( weight === 0 && intPoint === l - 1 ) {
|
||||
|
||||
intPoint = l - 2;
|
||||
weight = 1;
|
||||
|
||||
}
|
||||
|
||||
var p0, p1, p2, p3; // 4 points
|
||||
|
||||
if ( this.closed || intPoint > 0 ) {
|
||||
|
||||
p0 = points[ ( intPoint - 1 ) % l ];
|
||||
|
||||
} else {
|
||||
|
||||
// extrapolate first point
|
||||
tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
|
||||
p0 = tmp;
|
||||
|
||||
}
|
||||
|
||||
p1 = points[ intPoint % l ];
|
||||
p2 = points[ ( intPoint + 1 ) % l ];
|
||||
|
||||
if ( this.closed || intPoint + 2 < l ) {
|
||||
|
||||
p3 = points[ ( intPoint + 2 ) % l ];
|
||||
|
||||
} else {
|
||||
|
||||
// extrapolate last point
|
||||
tmp.subVectors( points[ l - 1 ], points[ l - 2 ] ).add( points[ l - 1 ] );
|
||||
p3 = tmp;
|
||||
|
||||
}
|
||||
|
||||
if ( this.curveType === 'centripetal' || this.curveType === 'chordal' ) {
|
||||
|
||||
// init Centripetal / Chordal Catmull-Rom
|
||||
var pow = this.curveType === 'chordal' ? 0.5 : 0.25;
|
||||
var dt0 = Math.pow( p0.distanceToSquared( p1 ), pow );
|
||||
var dt1 = Math.pow( p1.distanceToSquared( p2 ), pow );
|
||||
var dt2 = Math.pow( p2.distanceToSquared( p3 ), pow );
|
||||
|
||||
// safety check for repeated points
|
||||
if ( dt1 < 1e-4 ) dt1 = 1.0;
|
||||
if ( dt0 < 1e-4 ) dt0 = dt1;
|
||||
if ( dt2 < 1e-4 ) dt2 = dt1;
|
||||
|
||||
px.initNonuniformCatmullRom( p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2 );
|
||||
py.initNonuniformCatmullRom( p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2 );
|
||||
pz.initNonuniformCatmullRom( p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2 );
|
||||
|
||||
} else if ( this.curveType === 'catmullrom' ) {
|
||||
|
||||
px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, this.tension );
|
||||
py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, this.tension );
|
||||
pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, this.tension );
|
||||
|
||||
}
|
||||
|
||||
point.set(
|
||||
px.calc( weight ),
|
||||
py.calc( weight ),
|
||||
pz.calc( weight )
|
||||
);
|
||||
|
||||
return point;
|
||||
|
||||
};
|
||||
|
||||
CatmullRomCurve3.prototype.copy = function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.points = [];
|
||||
|
||||
for ( var i = 0, l = source.points.length; i < l; i ++ ) {
|
||||
|
||||
var point = source.points[ i ];
|
||||
|
||||
this.points.push( point.clone() );
|
||||
|
||||
}
|
||||
|
||||
this.closed = source.closed;
|
||||
this.curveType = source.curveType;
|
||||
this.tension = source.tension;
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
CatmullRomCurve3.prototype.toJSON = function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.points = [];
|
||||
|
||||
for ( var i = 0, l = this.points.length; i < l; i ++ ) {
|
||||
|
||||
var point = this.points[ i ];
|
||||
data.points.push( point.toArray() );
|
||||
|
||||
}
|
||||
|
||||
data.closed = this.closed;
|
||||
data.curveType = this.curveType;
|
||||
data.tension = this.tension;
|
||||
|
||||
return data;
|
||||
|
||||
};
|
||||
|
||||
CatmullRomCurve3.prototype.fromJSON = function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.points = [];
|
||||
|
||||
for ( var i = 0, l = json.points.length; i < l; i ++ ) {
|
||||
|
||||
var point = json.points[ i ];
|
||||
this.points.push( new Vector3().fromArray( point ) );
|
||||
|
||||
}
|
||||
|
||||
this.closed = json.closed;
|
||||
this.curveType = json.curveType;
|
||||
this.tension = json.tension;
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { CatmullRomCurve3 };
|
79
lib/extras/curves/CubicBezierCurve.js
Normal file
@ -0,0 +1,79 @@
|
||||
import { Curve } from '../core/Curve.js';
|
||||
import { CubicBezier } from '../core/Interpolations.js';
|
||||
import { Vector2 } from '../../math/Vector2.js';
|
||||
|
||||
|
||||
function CubicBezierCurve( v0, v1, v2, v3 ) {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'CubicBezierCurve';
|
||||
|
||||
this.v0 = v0 || new Vector2();
|
||||
this.v1 = v1 || new Vector2();
|
||||
this.v2 = v2 || new Vector2();
|
||||
this.v3 = v3 || new Vector2();
|
||||
|
||||
}
|
||||
|
||||
CubicBezierCurve.prototype = Object.create( Curve.prototype );
|
||||
CubicBezierCurve.prototype.constructor = CubicBezierCurve;
|
||||
|
||||
CubicBezierCurve.prototype.isCubicBezierCurve = true;
|
||||
|
||||
CubicBezierCurve.prototype.getPoint = function ( t, optionalTarget ) {
|
||||
|
||||
var point = optionalTarget || new Vector2();
|
||||
|
||||
var v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
|
||||
|
||||
point.set(
|
||||
CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
|
||||
CubicBezier( t, v0.y, v1.y, v2.y, v3.y )
|
||||
);
|
||||
|
||||
return point;
|
||||
|
||||
};
|
||||
|
||||
CubicBezierCurve.prototype.copy = function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.v0.copy( source.v0 );
|
||||
this.v1.copy( source.v1 );
|
||||
this.v2.copy( source.v2 );
|
||||
this.v3.copy( source.v3 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
CubicBezierCurve.prototype.toJSON = function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.v0 = this.v0.toArray();
|
||||
data.v1 = this.v1.toArray();
|
||||
data.v2 = this.v2.toArray();
|
||||
data.v3 = this.v3.toArray();
|
||||
|
||||
return data;
|
||||
|
||||
};
|
||||
|
||||
CubicBezierCurve.prototype.fromJSON = function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.v0.fromArray( json.v0 );
|
||||
this.v1.fromArray( json.v1 );
|
||||
this.v2.fromArray( json.v2 );
|
||||
this.v3.fromArray( json.v3 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { CubicBezierCurve };
|
80
lib/extras/curves/CubicBezierCurve3.js
Normal file
@ -0,0 +1,80 @@
|
||||
import { Curve } from '../core/Curve.js';
|
||||
import { CubicBezier } from '../core/Interpolations.js';
|
||||
import { Vector3 } from '../../math/Vector3.js';
|
||||
|
||||
|
||||
function CubicBezierCurve3( v0, v1, v2, v3 ) {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'CubicBezierCurve3';
|
||||
|
||||
this.v0 = v0 || new Vector3();
|
||||
this.v1 = v1 || new Vector3();
|
||||
this.v2 = v2 || new Vector3();
|
||||
this.v3 = v3 || new Vector3();
|
||||
|
||||
}
|
||||
|
||||
CubicBezierCurve3.prototype = Object.create( Curve.prototype );
|
||||
CubicBezierCurve3.prototype.constructor = CubicBezierCurve3;
|
||||
|
||||
CubicBezierCurve3.prototype.isCubicBezierCurve3 = true;
|
||||
|
||||
CubicBezierCurve3.prototype.getPoint = function ( t, optionalTarget ) {
|
||||
|
||||
var point = optionalTarget || new Vector3();
|
||||
|
||||
var v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
|
||||
|
||||
point.set(
|
||||
CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
|
||||
CubicBezier( t, v0.y, v1.y, v2.y, v3.y ),
|
||||
CubicBezier( t, v0.z, v1.z, v2.z, v3.z )
|
||||
);
|
||||
|
||||
return point;
|
||||
|
||||
};
|
||||
|
||||
CubicBezierCurve3.prototype.copy = function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.v0.copy( source.v0 );
|
||||
this.v1.copy( source.v1 );
|
||||
this.v2.copy( source.v2 );
|
||||
this.v3.copy( source.v3 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
CubicBezierCurve3.prototype.toJSON = function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.v0 = this.v0.toArray();
|
||||
data.v1 = this.v1.toArray();
|
||||
data.v2 = this.v2.toArray();
|
||||
data.v3 = this.v3.toArray();
|
||||
|
||||
return data;
|
||||
|
||||
};
|
||||
|
||||
CubicBezierCurve3.prototype.fromJSON = function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.v0.fromArray( json.v0 );
|
||||
this.v1.fromArray( json.v1 );
|
||||
this.v2.fromArray( json.v2 );
|
||||
this.v3.fromArray( json.v3 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { CubicBezierCurve3 };
|
10
lib/extras/curves/Curves.js
Normal file
@ -0,0 +1,10 @@
|
||||
export { ArcCurve } from './ArcCurve.js';
|
||||
export { CatmullRomCurve3 } from './CatmullRomCurve3.js';
|
||||
export { CubicBezierCurve } from './CubicBezierCurve.js';
|
||||
export { CubicBezierCurve3 } from './CubicBezierCurve3.js';
|
||||
export { EllipseCurve } from './EllipseCurve.js';
|
||||
export { LineCurve } from './LineCurve.js';
|
||||
export { LineCurve3 } from './LineCurve3.js';
|
||||
export { QuadraticBezierCurve } from './QuadraticBezierCurve.js';
|
||||
export { QuadraticBezierCurve3 } from './QuadraticBezierCurve3.js';
|
||||
export { SplineCurve } from './SplineCurve.js';
|
158
lib/extras/curves/EllipseCurve.js
Normal file
@ -0,0 +1,158 @@
|
||||
import { Curve } from '../core/Curve.js';
|
||||
import { Vector2 } from '../../math/Vector2.js';
|
||||
|
||||
|
||||
function EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'EllipseCurve';
|
||||
|
||||
this.aX = aX || 0;
|
||||
this.aY = aY || 0;
|
||||
|
||||
this.xRadius = xRadius || 1;
|
||||
this.yRadius = yRadius || 1;
|
||||
|
||||
this.aStartAngle = aStartAngle || 0;
|
||||
this.aEndAngle = aEndAngle || 2 * Math.PI;
|
||||
|
||||
this.aClockwise = aClockwise || false;
|
||||
|
||||
this.aRotation = aRotation || 0;
|
||||
|
||||
}
|
||||
|
||||
EllipseCurve.prototype = Object.create( Curve.prototype );
|
||||
EllipseCurve.prototype.constructor = EllipseCurve;
|
||||
|
||||
EllipseCurve.prototype.isEllipseCurve = true;
|
||||
|
||||
EllipseCurve.prototype.getPoint = function ( t, optionalTarget ) {
|
||||
|
||||
var point = optionalTarget || new Vector2();
|
||||
|
||||
var twoPi = Math.PI * 2;
|
||||
var deltaAngle = this.aEndAngle - this.aStartAngle;
|
||||
var samePoints = Math.abs( deltaAngle ) < Number.EPSILON;
|
||||
|
||||
// ensures that deltaAngle is 0 .. 2 PI
|
||||
while ( deltaAngle < 0 ) deltaAngle += twoPi;
|
||||
while ( deltaAngle > twoPi ) deltaAngle -= twoPi;
|
||||
|
||||
if ( deltaAngle < Number.EPSILON ) {
|
||||
|
||||
if ( samePoints ) {
|
||||
|
||||
deltaAngle = 0;
|
||||
|
||||
} else {
|
||||
|
||||
deltaAngle = twoPi;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( this.aClockwise === true && ! samePoints ) {
|
||||
|
||||
if ( deltaAngle === twoPi ) {
|
||||
|
||||
deltaAngle = - twoPi;
|
||||
|
||||
} else {
|
||||
|
||||
deltaAngle = deltaAngle - twoPi;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var angle = this.aStartAngle + t * deltaAngle;
|
||||
var x = this.aX + this.xRadius * Math.cos( angle );
|
||||
var y = this.aY + this.yRadius * Math.sin( angle );
|
||||
|
||||
if ( this.aRotation !== 0 ) {
|
||||
|
||||
var cos = Math.cos( this.aRotation );
|
||||
var sin = Math.sin( this.aRotation );
|
||||
|
||||
var tx = x - this.aX;
|
||||
var ty = y - this.aY;
|
||||
|
||||
// Rotate the point about the center of the ellipse.
|
||||
x = tx * cos - ty * sin + this.aX;
|
||||
y = tx * sin + ty * cos + this.aY;
|
||||
|
||||
}
|
||||
|
||||
return point.set( x, y );
|
||||
|
||||
};
|
||||
|
||||
EllipseCurve.prototype.copy = function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.aX = source.aX;
|
||||
this.aY = source.aY;
|
||||
|
||||
this.xRadius = source.xRadius;
|
||||
this.yRadius = source.yRadius;
|
||||
|
||||
this.aStartAngle = source.aStartAngle;
|
||||
this.aEndAngle = source.aEndAngle;
|
||||
|
||||
this.aClockwise = source.aClockwise;
|
||||
|
||||
this.aRotation = source.aRotation;
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
EllipseCurve.prototype.toJSON = function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.aX = this.aX;
|
||||
data.aY = this.aY;
|
||||
|
||||
data.xRadius = this.xRadius;
|
||||
data.yRadius = this.yRadius;
|
||||
|
||||
data.aStartAngle = this.aStartAngle;
|
||||
data.aEndAngle = this.aEndAngle;
|
||||
|
||||
data.aClockwise = this.aClockwise;
|
||||
|
||||
data.aRotation = this.aRotation;
|
||||
|
||||
return data;
|
||||
|
||||
};
|
||||
|
||||
EllipseCurve.prototype.fromJSON = function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.aX = json.aX;
|
||||
this.aY = json.aY;
|
||||
|
||||
this.xRadius = json.xRadius;
|
||||
this.yRadius = json.yRadius;
|
||||
|
||||
this.aStartAngle = json.aStartAngle;
|
||||
this.aEndAngle = json.aEndAngle;
|
||||
|
||||
this.aClockwise = json.aClockwise;
|
||||
|
||||
this.aRotation = json.aRotation;
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { EllipseCurve };
|
90
lib/extras/curves/LineCurve.js
Normal file
@ -0,0 +1,90 @@
|
||||
import { Vector2 } from '../../math/Vector2.js';
|
||||
import { Curve } from '../core/Curve.js';
|
||||
|
||||
|
||||
function LineCurve( v1, v2 ) {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'LineCurve';
|
||||
|
||||
this.v1 = v1 || new Vector2();
|
||||
this.v2 = v2 || new Vector2();
|
||||
|
||||
}
|
||||
|
||||
LineCurve.prototype = Object.create( Curve.prototype );
|
||||
LineCurve.prototype.constructor = LineCurve;
|
||||
|
||||
LineCurve.prototype.isLineCurve = true;
|
||||
|
||||
LineCurve.prototype.getPoint = function ( t, optionalTarget ) {
|
||||
|
||||
var point = optionalTarget || new Vector2();
|
||||
|
||||
if ( t === 1 ) {
|
||||
|
||||
point.copy( this.v2 );
|
||||
|
||||
} else {
|
||||
|
||||
point.copy( this.v2 ).sub( this.v1 );
|
||||
point.multiplyScalar( t ).add( this.v1 );
|
||||
|
||||
}
|
||||
|
||||
return point;
|
||||
|
||||
};
|
||||
|
||||
// Line curve is linear, so we can overwrite default getPointAt
|
||||
|
||||
LineCurve.prototype.getPointAt = function ( u, optionalTarget ) {
|
||||
|
||||
return this.getPoint( u, optionalTarget );
|
||||
|
||||
};
|
||||
|
||||
LineCurve.prototype.getTangent = function ( /* t */ ) {
|
||||
|
||||
var tangent = this.v2.clone().sub( this.v1 );
|
||||
|
||||
return tangent.normalize();
|
||||
|
||||
};
|
||||
|
||||
LineCurve.prototype.copy = function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.v1.copy( source.v1 );
|
||||
this.v2.copy( source.v2 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
LineCurve.prototype.toJSON = function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.v1 = this.v1.toArray();
|
||||
data.v2 = this.v2.toArray();
|
||||
|
||||
return data;
|
||||
|
||||
};
|
||||
|
||||
LineCurve.prototype.fromJSON = function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.v1.fromArray( json.v1 );
|
||||
this.v2.fromArray( json.v2 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { LineCurve };
|
82
lib/extras/curves/LineCurve3.js
Normal file
@ -0,0 +1,82 @@
|
||||
import { Vector3 } from '../../math/Vector3.js';
|
||||
import { Curve } from '../core/Curve.js';
|
||||
|
||||
|
||||
function LineCurve3( v1, v2 ) {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'LineCurve3';
|
||||
|
||||
this.v1 = v1 || new Vector3();
|
||||
this.v2 = v2 || new Vector3();
|
||||
|
||||
}
|
||||
|
||||
LineCurve3.prototype = Object.create( Curve.prototype );
|
||||
LineCurve3.prototype.constructor = LineCurve3;
|
||||
|
||||
LineCurve3.prototype.isLineCurve3 = true;
|
||||
|
||||
LineCurve3.prototype.getPoint = function ( t, optionalTarget ) {
|
||||
|
||||
var point = optionalTarget || new Vector3();
|
||||
|
||||
if ( t === 1 ) {
|
||||
|
||||
point.copy( this.v2 );
|
||||
|
||||
} else {
|
||||
|
||||
point.copy( this.v2 ).sub( this.v1 );
|
||||
point.multiplyScalar( t ).add( this.v1 );
|
||||
|
||||
}
|
||||
|
||||
return point;
|
||||
|
||||
};
|
||||
|
||||
// Line curve is linear, so we can overwrite default getPointAt
|
||||
|
||||
LineCurve3.prototype.getPointAt = function ( u, optionalTarget ) {
|
||||
|
||||
return this.getPoint( u, optionalTarget );
|
||||
|
||||
};
|
||||
|
||||
LineCurve3.prototype.copy = function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.v1.copy( source.v1 );
|
||||
this.v2.copy( source.v2 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
LineCurve3.prototype.toJSON = function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.v1 = this.v1.toArray();
|
||||
data.v2 = this.v2.toArray();
|
||||
|
||||
return data;
|
||||
|
||||
};
|
||||
|
||||
LineCurve3.prototype.fromJSON = function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.v1.fromArray( json.v1 );
|
||||
this.v2.fromArray( json.v2 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { LineCurve3 };
|
75
lib/extras/curves/QuadraticBezierCurve.js
Normal file
@ -0,0 +1,75 @@
|
||||
import { Curve } from '../core/Curve.js';
|
||||
import { QuadraticBezier } from '../core/Interpolations.js';
|
||||
import { Vector2 } from '../../math/Vector2.js';
|
||||
|
||||
|
||||
function QuadraticBezierCurve( v0, v1, v2 ) {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'QuadraticBezierCurve';
|
||||
|
||||
this.v0 = v0 || new Vector2();
|
||||
this.v1 = v1 || new Vector2();
|
||||
this.v2 = v2 || new Vector2();
|
||||
|
||||
}
|
||||
|
||||
QuadraticBezierCurve.prototype = Object.create( Curve.prototype );
|
||||
QuadraticBezierCurve.prototype.constructor = QuadraticBezierCurve;
|
||||
|
||||
QuadraticBezierCurve.prototype.isQuadraticBezierCurve = true;
|
||||
|
||||
QuadraticBezierCurve.prototype.getPoint = function ( t, optionalTarget ) {
|
||||
|
||||
var point = optionalTarget || new Vector2();
|
||||
|
||||
var v0 = this.v0, v1 = this.v1, v2 = this.v2;
|
||||
|
||||
point.set(
|
||||
QuadraticBezier( t, v0.x, v1.x, v2.x ),
|
||||
QuadraticBezier( t, v0.y, v1.y, v2.y )
|
||||
);
|
||||
|
||||
return point;
|
||||
|
||||
};
|
||||
|
||||
QuadraticBezierCurve.prototype.copy = function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.v0.copy( source.v0 );
|
||||
this.v1.copy( source.v1 );
|
||||
this.v2.copy( source.v2 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
QuadraticBezierCurve.prototype.toJSON = function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.v0 = this.v0.toArray();
|
||||
data.v1 = this.v1.toArray();
|
||||
data.v2 = this.v2.toArray();
|
||||
|
||||
return data;
|
||||
|
||||
};
|
||||
|
||||
QuadraticBezierCurve.prototype.fromJSON = function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.v0.fromArray( json.v0 );
|
||||
this.v1.fromArray( json.v1 );
|
||||
this.v2.fromArray( json.v2 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { QuadraticBezierCurve };
|
76
lib/extras/curves/QuadraticBezierCurve3.js
Normal file
@ -0,0 +1,76 @@
|
||||
import { Curve } from '../core/Curve.js';
|
||||
import { QuadraticBezier } from '../core/Interpolations.js';
|
||||
import { Vector3 } from '../../math/Vector3.js';
|
||||
|
||||
|
||||
function QuadraticBezierCurve3( v0, v1, v2 ) {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'QuadraticBezierCurve3';
|
||||
|
||||
this.v0 = v0 || new Vector3();
|
||||
this.v1 = v1 || new Vector3();
|
||||
this.v2 = v2 || new Vector3();
|
||||
|
||||
}
|
||||
|
||||
QuadraticBezierCurve3.prototype = Object.create( Curve.prototype );
|
||||
QuadraticBezierCurve3.prototype.constructor = QuadraticBezierCurve3;
|
||||
|
||||
QuadraticBezierCurve3.prototype.isQuadraticBezierCurve3 = true;
|
||||
|
||||
QuadraticBezierCurve3.prototype.getPoint = function ( t, optionalTarget ) {
|
||||
|
||||
var point = optionalTarget || new Vector3();
|
||||
|
||||
var v0 = this.v0, v1 = this.v1, v2 = this.v2;
|
||||
|
||||
point.set(
|
||||
QuadraticBezier( t, v0.x, v1.x, v2.x ),
|
||||
QuadraticBezier( t, v0.y, v1.y, v2.y ),
|
||||
QuadraticBezier( t, v0.z, v1.z, v2.z )
|
||||
);
|
||||
|
||||
return point;
|
||||
|
||||
};
|
||||
|
||||
QuadraticBezierCurve3.prototype.copy = function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.v0.copy( source.v0 );
|
||||
this.v1.copy( source.v1 );
|
||||
this.v2.copy( source.v2 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
QuadraticBezierCurve3.prototype.toJSON = function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.v0 = this.v0.toArray();
|
||||
data.v1 = this.v1.toArray();
|
||||
data.v2 = this.v2.toArray();
|
||||
|
||||
return data;
|
||||
|
||||
};
|
||||
|
||||
QuadraticBezierCurve3.prototype.fromJSON = function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.v0.fromArray( json.v0 );
|
||||
this.v1.fromArray( json.v1 );
|
||||
this.v2.fromArray( json.v2 );
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { QuadraticBezierCurve3 };
|
98
lib/extras/curves/SplineCurve.js
Normal file
@ -0,0 +1,98 @@
|
||||
import { Curve } from '../core/Curve.js';
|
||||
import { CatmullRom } from '../core/Interpolations.js';
|
||||
import { Vector2 } from '../../math/Vector2.js';
|
||||
|
||||
|
||||
function SplineCurve( points /* array of Vector2 */ ) {
|
||||
|
||||
Curve.call( this );
|
||||
|
||||
this.type = 'SplineCurve';
|
||||
|
||||
this.points = points || [];
|
||||
|
||||
}
|
||||
|
||||
SplineCurve.prototype = Object.create( Curve.prototype );
|
||||
SplineCurve.prototype.constructor = SplineCurve;
|
||||
|
||||
SplineCurve.prototype.isSplineCurve = true;
|
||||
|
||||
SplineCurve.prototype.getPoint = function ( t, optionalTarget ) {
|
||||
|
||||
var point = optionalTarget || new Vector2();
|
||||
|
||||
var points = this.points;
|
||||
var p = ( points.length - 1 ) * t;
|
||||
|
||||
var intPoint = Math.floor( p );
|
||||
var weight = p - intPoint;
|
||||
|
||||
var p0 = points[ intPoint === 0 ? intPoint : intPoint - 1 ];
|
||||
var p1 = points[ intPoint ];
|
||||
var p2 = points[ intPoint > points.length - 2 ? points.length - 1 : intPoint + 1 ];
|
||||
var p3 = points[ intPoint > points.length - 3 ? points.length - 1 : intPoint + 2 ];
|
||||
|
||||
point.set(
|
||||
CatmullRom( weight, p0.x, p1.x, p2.x, p3.x ),
|
||||
CatmullRom( weight, p0.y, p1.y, p2.y, p3.y )
|
||||
);
|
||||
|
||||
return point;
|
||||
|
||||
};
|
||||
|
||||
SplineCurve.prototype.copy = function ( source ) {
|
||||
|
||||
Curve.prototype.copy.call( this, source );
|
||||
|
||||
this.points = [];
|
||||
|
||||
for ( var i = 0, l = source.points.length; i < l; i ++ ) {
|
||||
|
||||
var point = source.points[ i ];
|
||||
|
||||
this.points.push( point.clone() );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
SplineCurve.prototype.toJSON = function () {
|
||||
|
||||
var data = Curve.prototype.toJSON.call( this );
|
||||
|
||||
data.points = [];
|
||||
|
||||
for ( var i = 0, l = this.points.length; i < l; i ++ ) {
|
||||
|
||||
var point = this.points[ i ];
|
||||
data.points.push( point.toArray() );
|
||||
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
};
|
||||
|
||||
SplineCurve.prototype.fromJSON = function ( json ) {
|
||||
|
||||
Curve.prototype.fromJSON.call( this, json );
|
||||
|
||||
this.points = [];
|
||||
|
||||
for ( var i = 0, l = json.points.length; i < l; i ++ ) {
|
||||
|
||||
var point = json.points[ i ];
|
||||
this.points.push( new Vector2().fromArray( point ) );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
export { SplineCurve };
|
22
lib/extras/objects/ImmediateRenderObject.js
Normal file
@ -0,0 +1,22 @@
|
||||
import { Object3D } from '../../core/Object3D.js';
|
||||
|
||||
/**
|
||||
* @author alteredq / http://alteredqualia.com/
|
||||
*/
|
||||
|
||||
function ImmediateRenderObject( material ) {
|
||||
|
||||
Object3D.call( this );
|
||||
|
||||
this.material = material;
|
||||
this.render = function ( /* renderCallback */ ) {};
|
||||
|
||||
}
|
||||
|
||||
ImmediateRenderObject.prototype = Object.create( Object3D.prototype );
|
||||
ImmediateRenderObject.prototype.constructor = ImmediateRenderObject;
|
||||
|
||||
ImmediateRenderObject.prototype.isImmediateRenderObject = true;
|
||||
|
||||
|
||||
export { ImmediateRenderObject };
|
203
lib/geometries/BoxGeometry.js
Normal file
@ -0,0 +1,203 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
|
||||
// BoxGeometry
|
||||
|
||||
function BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'BoxGeometry';
|
||||
|
||||
this.parameters = {
|
||||
width: width,
|
||||
height: height,
|
||||
depth: depth,
|
||||
widthSegments: widthSegments,
|
||||
heightSegments: heightSegments,
|
||||
depthSegments: depthSegments
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
BoxGeometry.prototype = Object.create( Geometry.prototype );
|
||||
BoxGeometry.prototype.constructor = BoxGeometry;
|
||||
|
||||
// BoxBufferGeometry
|
||||
|
||||
function BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'BoxBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
width: width,
|
||||
height: height,
|
||||
depth: depth,
|
||||
widthSegments: widthSegments,
|
||||
heightSegments: heightSegments,
|
||||
depthSegments: depthSegments
|
||||
};
|
||||
|
||||
var scope = this;
|
||||
|
||||
width = width || 1;
|
||||
height = height || 1;
|
||||
depth = depth || 1;
|
||||
|
||||
// segments
|
||||
|
||||
widthSegments = Math.floor( widthSegments ) || 1;
|
||||
heightSegments = Math.floor( heightSegments ) || 1;
|
||||
depthSegments = Math.floor( depthSegments ) || 1;
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
// helper variables
|
||||
|
||||
var numberOfVertices = 0;
|
||||
var groupStart = 0;
|
||||
|
||||
// build each side of the box geometry
|
||||
|
||||
buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); // px
|
||||
buildPlane( 'z', 'y', 'x', 1, - 1, depth, height, - width, depthSegments, heightSegments, 1 ); // nx
|
||||
buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); // py
|
||||
buildPlane( 'x', 'z', 'y', 1, - 1, width, depth, - height, widthSegments, depthSegments, 3 ); // ny
|
||||
buildPlane( 'x', 'y', 'z', 1, - 1, width, height, depth, widthSegments, heightSegments, 4 ); // pz
|
||||
buildPlane( 'x', 'y', 'z', - 1, - 1, width, height, - depth, widthSegments, heightSegments, 5 ); // nz
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) {
|
||||
|
||||
var segmentWidth = width / gridX;
|
||||
var segmentHeight = height / gridY;
|
||||
|
||||
var widthHalf = width / 2;
|
||||
var heightHalf = height / 2;
|
||||
var depthHalf = depth / 2;
|
||||
|
||||
var gridX1 = gridX + 1;
|
||||
var gridY1 = gridY + 1;
|
||||
|
||||
var vertexCounter = 0;
|
||||
var groupCount = 0;
|
||||
|
||||
var ix, iy;
|
||||
|
||||
var vector = new Vector3();
|
||||
|
||||
// generate vertices, normals and uvs
|
||||
|
||||
for ( iy = 0; iy < gridY1; iy ++ ) {
|
||||
|
||||
var y = iy * segmentHeight - heightHalf;
|
||||
|
||||
for ( ix = 0; ix < gridX1; ix ++ ) {
|
||||
|
||||
var x = ix * segmentWidth - widthHalf;
|
||||
|
||||
// set values to correct vector component
|
||||
|
||||
vector[ u ] = x * udir;
|
||||
vector[ v ] = y * vdir;
|
||||
vector[ w ] = depthHalf;
|
||||
|
||||
// now apply vector to vertex buffer
|
||||
|
||||
vertices.push( vector.x, vector.y, vector.z );
|
||||
|
||||
// set values to correct vector component
|
||||
|
||||
vector[ u ] = 0;
|
||||
vector[ v ] = 0;
|
||||
vector[ w ] = depth > 0 ? 1 : - 1;
|
||||
|
||||
// now apply vector to normal buffer
|
||||
|
||||
normals.push( vector.x, vector.y, vector.z );
|
||||
|
||||
// uvs
|
||||
|
||||
uvs.push( ix / gridX );
|
||||
uvs.push( 1 - ( iy / gridY ) );
|
||||
|
||||
// counters
|
||||
|
||||
vertexCounter += 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// indices
|
||||
|
||||
// 1. you need three indices to draw a single face
|
||||
// 2. a single segment consists of two faces
|
||||
// 3. so we need to generate six (2*3) indices per segment
|
||||
|
||||
for ( iy = 0; iy < gridY; iy ++ ) {
|
||||
|
||||
for ( ix = 0; ix < gridX; ix ++ ) {
|
||||
|
||||
var a = numberOfVertices + ix + gridX1 * iy;
|
||||
var b = numberOfVertices + ix + gridX1 * ( iy + 1 );
|
||||
var c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 );
|
||||
var d = numberOfVertices + ( ix + 1 ) + gridX1 * iy;
|
||||
|
||||
// faces
|
||||
|
||||
indices.push( a, b, d );
|
||||
indices.push( b, c, d );
|
||||
|
||||
// increase counter
|
||||
|
||||
groupCount += 6;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// add a group to the geometry. this will ensure multi material support
|
||||
|
||||
scope.addGroup( groupStart, groupCount, materialIndex );
|
||||
|
||||
// calculate new start value for groups
|
||||
|
||||
groupStart += groupCount;
|
||||
|
||||
// update total number of vertices
|
||||
|
||||
numberOfVertices += vertexCounter;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
BoxBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
BoxBufferGeometry.prototype.constructor = BoxBufferGeometry;
|
||||
|
||||
|
||||
export { BoxGeometry, BoxBufferGeometry };
|
121
lib/geometries/CircleGeometry.js
Normal file
@ -0,0 +1,121 @@
|
||||
/**
|
||||
* @author benaadams / https://twitter.com/ben_a_adams
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
* @author hughes
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { Vector2 } from '../math/Vector2.js';
|
||||
|
||||
// CircleGeometry
|
||||
|
||||
function CircleGeometry( radius, segments, thetaStart, thetaLength ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'CircleGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
segments: segments,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
CircleGeometry.prototype = Object.create( Geometry.prototype );
|
||||
CircleGeometry.prototype.constructor = CircleGeometry;
|
||||
|
||||
// CircleBufferGeometry
|
||||
|
||||
function CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'CircleBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
segments: segments,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
radius = radius || 1;
|
||||
segments = segments !== undefined ? Math.max( 3, segments ) : 8;
|
||||
|
||||
thetaStart = thetaStart !== undefined ? thetaStart : 0;
|
||||
thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
// helper variables
|
||||
|
||||
var i, s;
|
||||
var vertex = new Vector3();
|
||||
var uv = new Vector2();
|
||||
|
||||
// center point
|
||||
|
||||
vertices.push( 0, 0, 0 );
|
||||
normals.push( 0, 0, 1 );
|
||||
uvs.push( 0.5, 0.5 );
|
||||
|
||||
for ( s = 0, i = 3; s <= segments; s ++, i += 3 ) {
|
||||
|
||||
var segment = thetaStart + s / segments * thetaLength;
|
||||
|
||||
// vertex
|
||||
|
||||
vertex.x = radius * Math.cos( segment );
|
||||
vertex.y = radius * Math.sin( segment );
|
||||
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
// normal
|
||||
|
||||
normals.push( 0, 0, 1 );
|
||||
|
||||
// uvs
|
||||
|
||||
uv.x = ( vertices[ i ] / radius + 1 ) / 2;
|
||||
uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2;
|
||||
|
||||
uvs.push( uv.x, uv.y );
|
||||
|
||||
}
|
||||
|
||||
// indices
|
||||
|
||||
for ( i = 1; i <= segments; i ++ ) {
|
||||
|
||||
indices.push( i, i + 1, 0 );
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
}
|
||||
|
||||
CircleBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
CircleBufferGeometry.prototype.constructor = CircleBufferGeometry;
|
||||
|
||||
|
||||
export { CircleGeometry, CircleBufferGeometry };
|
55
lib/geometries/ConeGeometry.js
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @author abelnation / http://github.com/abelnation
|
||||
*/
|
||||
|
||||
import { CylinderGeometry } from './CylinderGeometry.js';
|
||||
import { CylinderBufferGeometry } from './CylinderGeometry.js';
|
||||
|
||||
// ConeGeometry
|
||||
|
||||
function ConeGeometry( radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
|
||||
|
||||
CylinderGeometry.call( this, 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength );
|
||||
|
||||
this.type = 'ConeGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
height: height,
|
||||
radialSegments: radialSegments,
|
||||
heightSegments: heightSegments,
|
||||
openEnded: openEnded,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
ConeGeometry.prototype = Object.create( CylinderGeometry.prototype );
|
||||
ConeGeometry.prototype.constructor = ConeGeometry;
|
||||
|
||||
// ConeBufferGeometry
|
||||
|
||||
function ConeBufferGeometry( radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
|
||||
|
||||
CylinderBufferGeometry.call( this, 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength );
|
||||
|
||||
this.type = 'ConeBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
height: height,
|
||||
radialSegments: radialSegments,
|
||||
heightSegments: heightSegments,
|
||||
openEnded: openEnded,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
ConeBufferGeometry.prototype = Object.create( CylinderBufferGeometry.prototype );
|
||||
ConeBufferGeometry.prototype.constructor = ConeBufferGeometry;
|
||||
|
||||
|
||||
export { ConeGeometry, ConeBufferGeometry };
|
316
lib/geometries/CylinderGeometry.js
Normal file
@ -0,0 +1,316 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { Vector2 } from '../math/Vector2.js';
|
||||
|
||||
// CylinderGeometry
|
||||
|
||||
function CylinderGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'CylinderGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radiusTop: radiusTop,
|
||||
radiusBottom: radiusBottom,
|
||||
height: height,
|
||||
radialSegments: radialSegments,
|
||||
heightSegments: heightSegments,
|
||||
openEnded: openEnded,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new CylinderBufferGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
CylinderGeometry.prototype = Object.create( Geometry.prototype );
|
||||
CylinderGeometry.prototype.constructor = CylinderGeometry;
|
||||
|
||||
// CylinderBufferGeometry
|
||||
|
||||
function CylinderBufferGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'CylinderBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radiusTop: radiusTop,
|
||||
radiusBottom: radiusBottom,
|
||||
height: height,
|
||||
radialSegments: radialSegments,
|
||||
heightSegments: heightSegments,
|
||||
openEnded: openEnded,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
var scope = this;
|
||||
|
||||
radiusTop = radiusTop !== undefined ? radiusTop : 1;
|
||||
radiusBottom = radiusBottom !== undefined ? radiusBottom : 1;
|
||||
height = height || 1;
|
||||
|
||||
radialSegments = Math.floor( radialSegments ) || 8;
|
||||
heightSegments = Math.floor( heightSegments ) || 1;
|
||||
|
||||
openEnded = openEnded !== undefined ? openEnded : false;
|
||||
thetaStart = thetaStart !== undefined ? thetaStart : 0.0;
|
||||
thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
// helper variables
|
||||
|
||||
var index = 0;
|
||||
var indexArray = [];
|
||||
var halfHeight = height / 2;
|
||||
var groupStart = 0;
|
||||
|
||||
// generate geometry
|
||||
|
||||
generateTorso();
|
||||
|
||||
if ( openEnded === false ) {
|
||||
|
||||
if ( radiusTop > 0 ) generateCap( true );
|
||||
if ( radiusBottom > 0 ) generateCap( false );
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
function generateTorso() {
|
||||
|
||||
var x, y;
|
||||
var normal = new Vector3();
|
||||
var vertex = new Vector3();
|
||||
|
||||
var groupCount = 0;
|
||||
|
||||
// this will be used to calculate the normal
|
||||
var slope = ( radiusBottom - radiusTop ) / height;
|
||||
|
||||
// generate vertices, normals and uvs
|
||||
|
||||
for ( y = 0; y <= heightSegments; y ++ ) {
|
||||
|
||||
var indexRow = [];
|
||||
|
||||
var v = y / heightSegments;
|
||||
|
||||
// calculate the radius of the current row
|
||||
|
||||
var radius = v * ( radiusBottom - radiusTop ) + radiusTop;
|
||||
|
||||
for ( x = 0; x <= radialSegments; x ++ ) {
|
||||
|
||||
var u = x / radialSegments;
|
||||
|
||||
var theta = u * thetaLength + thetaStart;
|
||||
|
||||
var sinTheta = Math.sin( theta );
|
||||
var cosTheta = Math.cos( theta );
|
||||
|
||||
// vertex
|
||||
|
||||
vertex.x = radius * sinTheta;
|
||||
vertex.y = - v * height + halfHeight;
|
||||
vertex.z = radius * cosTheta;
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
// normal
|
||||
|
||||
normal.set( sinTheta, slope, cosTheta ).normalize();
|
||||
normals.push( normal.x, normal.y, normal.z );
|
||||
|
||||
// uv
|
||||
|
||||
uvs.push( u, 1 - v );
|
||||
|
||||
// save index of vertex in respective row
|
||||
|
||||
indexRow.push( index ++ );
|
||||
|
||||
}
|
||||
|
||||
// now save vertices of the row in our index array
|
||||
|
||||
indexArray.push( indexRow );
|
||||
|
||||
}
|
||||
|
||||
// generate indices
|
||||
|
||||
for ( x = 0; x < radialSegments; x ++ ) {
|
||||
|
||||
for ( y = 0; y < heightSegments; y ++ ) {
|
||||
|
||||
// we use the index array to access the correct indices
|
||||
|
||||
var a = indexArray[ y ][ x ];
|
||||
var b = indexArray[ y + 1 ][ x ];
|
||||
var c = indexArray[ y + 1 ][ x + 1 ];
|
||||
var d = indexArray[ y ][ x + 1 ];
|
||||
|
||||
// faces
|
||||
|
||||
indices.push( a, b, d );
|
||||
indices.push( b, c, d );
|
||||
|
||||
// update group counter
|
||||
|
||||
groupCount += 6;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// add a group to the geometry. this will ensure multi material support
|
||||
|
||||
scope.addGroup( groupStart, groupCount, 0 );
|
||||
|
||||
// calculate new start value for groups
|
||||
|
||||
groupStart += groupCount;
|
||||
|
||||
}
|
||||
|
||||
function generateCap( top ) {
|
||||
|
||||
var x, centerIndexStart, centerIndexEnd;
|
||||
|
||||
var uv = new Vector2();
|
||||
var vertex = new Vector3();
|
||||
|
||||
var groupCount = 0;
|
||||
|
||||
var radius = ( top === true ) ? radiusTop : radiusBottom;
|
||||
var sign = ( top === true ) ? 1 : - 1;
|
||||
|
||||
// save the index of the first center vertex
|
||||
centerIndexStart = index;
|
||||
|
||||
// first we generate the center vertex data of the cap.
|
||||
// because the geometry needs one set of uvs per face,
|
||||
// we must generate a center vertex per face/segment
|
||||
|
||||
for ( x = 1; x <= radialSegments; x ++ ) {
|
||||
|
||||
// vertex
|
||||
|
||||
vertices.push( 0, halfHeight * sign, 0 );
|
||||
|
||||
// normal
|
||||
|
||||
normals.push( 0, sign, 0 );
|
||||
|
||||
// uv
|
||||
|
||||
uvs.push( 0.5, 0.5 );
|
||||
|
||||
// increase index
|
||||
|
||||
index ++;
|
||||
|
||||
}
|
||||
|
||||
// save the index of the last center vertex
|
||||
|
||||
centerIndexEnd = index;
|
||||
|
||||
// now we generate the surrounding vertices, normals and uvs
|
||||
|
||||
for ( x = 0; x <= radialSegments; x ++ ) {
|
||||
|
||||
var u = x / radialSegments;
|
||||
var theta = u * thetaLength + thetaStart;
|
||||
|
||||
var cosTheta = Math.cos( theta );
|
||||
var sinTheta = Math.sin( theta );
|
||||
|
||||
// vertex
|
||||
|
||||
vertex.x = radius * sinTheta;
|
||||
vertex.y = halfHeight * sign;
|
||||
vertex.z = radius * cosTheta;
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
// normal
|
||||
|
||||
normals.push( 0, sign, 0 );
|
||||
|
||||
// uv
|
||||
|
||||
uv.x = ( cosTheta * 0.5 ) + 0.5;
|
||||
uv.y = ( sinTheta * 0.5 * sign ) + 0.5;
|
||||
uvs.push( uv.x, uv.y );
|
||||
|
||||
// increase index
|
||||
|
||||
index ++;
|
||||
|
||||
}
|
||||
|
||||
// generate indices
|
||||
|
||||
for ( x = 0; x < radialSegments; x ++ ) {
|
||||
|
||||
var c = centerIndexStart + x;
|
||||
var i = centerIndexEnd + x;
|
||||
|
||||
if ( top === true ) {
|
||||
|
||||
// face top
|
||||
|
||||
indices.push( i, i + 1, c );
|
||||
|
||||
} else {
|
||||
|
||||
// face bottom
|
||||
|
||||
indices.push( i + 1, i, c );
|
||||
|
||||
}
|
||||
|
||||
groupCount += 3;
|
||||
|
||||
}
|
||||
|
||||
// add a group to the geometry. this will ensure multi material support
|
||||
|
||||
scope.addGroup( groupStart, groupCount, top === true ? 1 : 2 );
|
||||
|
||||
// calculate new start value for groups
|
||||
|
||||
groupStart += groupCount;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
CylinderBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
CylinderBufferGeometry.prototype.constructor = CylinderBufferGeometry;
|
||||
|
||||
|
||||
export { CylinderGeometry, CylinderBufferGeometry };
|
88
lib/geometries/DodecahedronGeometry.js
Normal file
@ -0,0 +1,88 @@
|
||||
/**
|
||||
* @author Abe Pazos / https://hamoid.com
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { PolyhedronBufferGeometry } from './PolyhedronGeometry.js';
|
||||
|
||||
// DodecahedronGeometry
|
||||
|
||||
function DodecahedronGeometry( radius, detail ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'DodecahedronGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new DodecahedronBufferGeometry( radius, detail ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
DodecahedronGeometry.prototype = Object.create( Geometry.prototype );
|
||||
DodecahedronGeometry.prototype.constructor = DodecahedronGeometry;
|
||||
|
||||
// DodecahedronBufferGeometry
|
||||
|
||||
function DodecahedronBufferGeometry( radius, detail ) {
|
||||
|
||||
var t = ( 1 + Math.sqrt( 5 ) ) / 2;
|
||||
var r = 1 / t;
|
||||
|
||||
var vertices = [
|
||||
|
||||
// (±1, ±1, ±1)
|
||||
- 1, - 1, - 1, - 1, - 1, 1,
|
||||
- 1, 1, - 1, - 1, 1, 1,
|
||||
1, - 1, - 1, 1, - 1, 1,
|
||||
1, 1, - 1, 1, 1, 1,
|
||||
|
||||
// (0, ±1/φ, ±φ)
|
||||
0, - r, - t, 0, - r, t,
|
||||
0, r, - t, 0, r, t,
|
||||
|
||||
// (±1/φ, ±φ, 0)
|
||||
- r, - t, 0, - r, t, 0,
|
||||
r, - t, 0, r, t, 0,
|
||||
|
||||
// (±φ, 0, ±1/φ)
|
||||
- t, 0, - r, t, 0, - r,
|
||||
- t, 0, r, t, 0, r
|
||||
];
|
||||
|
||||
var indices = [
|
||||
3, 11, 7, 3, 7, 15, 3, 15, 13,
|
||||
7, 19, 17, 7, 17, 6, 7, 6, 15,
|
||||
17, 4, 8, 17, 8, 10, 17, 10, 6,
|
||||
8, 0, 16, 8, 16, 2, 8, 2, 10,
|
||||
0, 12, 1, 0, 1, 18, 0, 18, 16,
|
||||
6, 10, 2, 6, 2, 13, 6, 13, 15,
|
||||
2, 16, 18, 2, 18, 3, 2, 3, 13,
|
||||
18, 1, 9, 18, 9, 11, 18, 11, 3,
|
||||
4, 14, 12, 4, 12, 0, 4, 0, 8,
|
||||
11, 9, 5, 11, 5, 19, 11, 19, 7,
|
||||
19, 5, 14, 19, 14, 4, 19, 4, 17,
|
||||
1, 12, 14, 1, 14, 5, 1, 5, 9
|
||||
];
|
||||
|
||||
PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
|
||||
|
||||
this.type = 'DodecahedronBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
DodecahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
|
||||
DodecahedronBufferGeometry.prototype.constructor = DodecahedronBufferGeometry;
|
||||
|
||||
|
||||
export { DodecahedronGeometry, DodecahedronBufferGeometry };
|
113
lib/geometries/EdgesGeometry.js
Normal file
@ -0,0 +1,113 @@
|
||||
/**
|
||||
* @author WestLangley / http://github.com/WestLangley
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { _Math } from '../math/Math.js';
|
||||
|
||||
function EdgesGeometry( geometry, thresholdAngle ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'EdgesGeometry';
|
||||
|
||||
this.parameters = {
|
||||
thresholdAngle: thresholdAngle
|
||||
};
|
||||
|
||||
thresholdAngle = ( thresholdAngle !== undefined ) ? thresholdAngle : 1;
|
||||
|
||||
// buffer
|
||||
|
||||
var vertices = [];
|
||||
|
||||
// helper variables
|
||||
|
||||
var thresholdDot = Math.cos( _Math.DEG2RAD * thresholdAngle );
|
||||
var edge = [ 0, 0 ], edges = {}, edge1, edge2;
|
||||
var key, keys = [ 'a', 'b', 'c' ];
|
||||
|
||||
// prepare source geometry
|
||||
|
||||
var geometry2;
|
||||
|
||||
if ( geometry.isBufferGeometry ) {
|
||||
|
||||
geometry2 = new Geometry();
|
||||
geometry2.fromBufferGeometry( geometry );
|
||||
|
||||
} else {
|
||||
|
||||
geometry2 = geometry.clone();
|
||||
|
||||
}
|
||||
|
||||
geometry2.mergeVertices();
|
||||
geometry2.computeFaceNormals();
|
||||
|
||||
var sourceVertices = geometry2.vertices;
|
||||
var faces = geometry2.faces;
|
||||
|
||||
// now create a data structure where each entry represents an edge with its adjoining faces
|
||||
|
||||
for ( var i = 0, l = faces.length; i < l; i ++ ) {
|
||||
|
||||
var face = faces[ i ];
|
||||
|
||||
for ( var j = 0; j < 3; j ++ ) {
|
||||
|
||||
edge1 = face[ keys[ j ] ];
|
||||
edge2 = face[ keys[ ( j + 1 ) % 3 ] ];
|
||||
edge[ 0 ] = Math.min( edge1, edge2 );
|
||||
edge[ 1 ] = Math.max( edge1, edge2 );
|
||||
|
||||
key = edge[ 0 ] + ',' + edge[ 1 ];
|
||||
|
||||
if ( edges[ key ] === undefined ) {
|
||||
|
||||
edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ], face1: i, face2: undefined };
|
||||
|
||||
} else {
|
||||
|
||||
edges[ key ].face2 = i;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// generate vertices
|
||||
|
||||
for ( key in edges ) {
|
||||
|
||||
var e = edges[ key ];
|
||||
|
||||
// an edge is only rendered if the angle (in degrees) between the face normals of the adjoining faces exceeds this value. default = 1 degree.
|
||||
|
||||
if ( e.face2 === undefined || faces[ e.face1 ].normal.dot( faces[ e.face2 ].normal ) <= thresholdDot ) {
|
||||
|
||||
var vertex = sourceVertices[ e.index1 ];
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
vertex = sourceVertices[ e.index2 ];
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
|
||||
}
|
||||
|
||||
EdgesGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
EdgesGeometry.prototype.constructor = EdgesGeometry;
|
||||
|
||||
|
||||
export { EdgesGeometry };
|
832
lib/geometries/ExtrudeGeometry.js
Normal file
@ -0,0 +1,832 @@
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
*
|
||||
* Creates extruded geometry from a path shape.
|
||||
*
|
||||
* parameters = {
|
||||
*
|
||||
* curveSegments: <int>, // number of points on the curves
|
||||
* steps: <int>, // number of points for z-side extrusions / used for subdividing segments of extrude spline too
|
||||
* depth: <float>, // Depth to extrude the shape
|
||||
*
|
||||
* bevelEnabled: <bool>, // turn on bevel
|
||||
* bevelThickness: <float>, // how deep into the original shape bevel goes
|
||||
* bevelSize: <float>, // how far from shape outline is bevel
|
||||
* bevelSegments: <int>, // number of bevel layers
|
||||
*
|
||||
* extrudePath: <THREE.Curve> // curve to extrude shape along
|
||||
*
|
||||
* UVGenerator: <Object> // object that provides UV generator functions
|
||||
*
|
||||
* }
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector2 } from '../math/Vector2.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { ShapeUtils } from '../extras/ShapeUtils.js';
|
||||
|
||||
// ExtrudeGeometry
|
||||
|
||||
function ExtrudeGeometry( shapes, options ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'ExtrudeGeometry';
|
||||
|
||||
this.parameters = {
|
||||
shapes: shapes,
|
||||
options: options
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new ExtrudeBufferGeometry( shapes, options ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
ExtrudeGeometry.prototype = Object.create( Geometry.prototype );
|
||||
ExtrudeGeometry.prototype.constructor = ExtrudeGeometry;
|
||||
|
||||
ExtrudeGeometry.prototype.toJSON = function () {
|
||||
|
||||
var data = Geometry.prototype.toJSON.call( this );
|
||||
|
||||
var shapes = this.parameters.shapes;
|
||||
var options = this.parameters.options;
|
||||
|
||||
return toJSON( shapes, options, data );
|
||||
|
||||
};
|
||||
|
||||
// ExtrudeBufferGeometry
|
||||
|
||||
function ExtrudeBufferGeometry( shapes, options ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'ExtrudeBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
shapes: shapes,
|
||||
options: options
|
||||
};
|
||||
|
||||
shapes = Array.isArray( shapes ) ? shapes : [ shapes ];
|
||||
|
||||
var scope = this;
|
||||
|
||||
var verticesArray = [];
|
||||
var uvArray = [];
|
||||
|
||||
for ( var i = 0, l = shapes.length; i < l; i ++ ) {
|
||||
|
||||
var shape = shapes[ i ];
|
||||
addShape( shape );
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( verticesArray, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvArray, 2 ) );
|
||||
|
||||
this.computeVertexNormals();
|
||||
|
||||
// functions
|
||||
|
||||
function addShape( shape ) {
|
||||
|
||||
var placeholder = [];
|
||||
|
||||
// options
|
||||
|
||||
var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
|
||||
var steps = options.steps !== undefined ? options.steps : 1;
|
||||
var depth = options.depth !== undefined ? options.depth : 100;
|
||||
|
||||
var bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true;
|
||||
var bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 6;
|
||||
var bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 2;
|
||||
var bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3;
|
||||
|
||||
var extrudePath = options.extrudePath;
|
||||
|
||||
var uvgen = options.UVGenerator !== undefined ? options.UVGenerator : WorldUVGenerator;
|
||||
|
||||
// deprecated options
|
||||
|
||||
if ( options.amount !== undefined ) {
|
||||
|
||||
console.warn( 'THREE.ExtrudeBufferGeometry: amount has been renamed to depth.' );
|
||||
depth = options.amount;
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
var extrudePts, extrudeByPath = false;
|
||||
var splineTube, binormal, normal, position2;
|
||||
|
||||
if ( extrudePath ) {
|
||||
|
||||
extrudePts = extrudePath.getSpacedPoints( steps );
|
||||
|
||||
extrudeByPath = true;
|
||||
bevelEnabled = false; // bevels not supported for path extrusion
|
||||
|
||||
// SETUP TNB variables
|
||||
|
||||
// TODO1 - have a .isClosed in spline?
|
||||
|
||||
splineTube = extrudePath.computeFrenetFrames( steps, false );
|
||||
|
||||
// console.log(splineTube, 'splineTube', splineTube.normals.length, 'steps', steps, 'extrudePts', extrudePts.length);
|
||||
|
||||
binormal = new Vector3();
|
||||
normal = new Vector3();
|
||||
position2 = new Vector3();
|
||||
|
||||
}
|
||||
|
||||
// Safeguards if bevels are not enabled
|
||||
|
||||
if ( ! bevelEnabled ) {
|
||||
|
||||
bevelSegments = 0;
|
||||
bevelThickness = 0;
|
||||
bevelSize = 0;
|
||||
|
||||
}
|
||||
|
||||
// Variables initialization
|
||||
|
||||
var ahole, h, hl; // looping of holes
|
||||
|
||||
var shapePoints = shape.extractPoints( curveSegments );
|
||||
|
||||
var vertices = shapePoints.shape;
|
||||
var holes = shapePoints.holes;
|
||||
|
||||
var reverse = ! ShapeUtils.isClockWise( vertices );
|
||||
|
||||
if ( reverse ) {
|
||||
|
||||
vertices = vertices.reverse();
|
||||
|
||||
// Maybe we should also check if holes are in the opposite direction, just to be safe ...
|
||||
|
||||
for ( h = 0, hl = holes.length; h < hl; h ++ ) {
|
||||
|
||||
ahole = holes[ h ];
|
||||
|
||||
if ( ShapeUtils.isClockWise( ahole ) ) {
|
||||
|
||||
holes[ h ] = ahole.reverse();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
var faces = ShapeUtils.triangulateShape( vertices, holes );
|
||||
|
||||
/* Vertices */
|
||||
|
||||
var contour = vertices; // vertices has all points but contour has only points of circumference
|
||||
|
||||
for ( h = 0, hl = holes.length; h < hl; h ++ ) {
|
||||
|
||||
ahole = holes[ h ];
|
||||
|
||||
vertices = vertices.concat( ahole );
|
||||
|
||||
}
|
||||
|
||||
|
||||
function scalePt2( pt, vec, size ) {
|
||||
|
||||
if ( ! vec ) console.error( "THREE.ExtrudeGeometry: vec does not exist" );
|
||||
|
||||
return vec.clone().multiplyScalar( size ).add( pt );
|
||||
|
||||
}
|
||||
|
||||
var b, bs, t, z,
|
||||
vert, vlen = vertices.length,
|
||||
face, flen = faces.length;
|
||||
|
||||
|
||||
// Find directions for point movement
|
||||
|
||||
|
||||
function getBevelVec( inPt, inPrev, inNext ) {
|
||||
|
||||
// computes for inPt the corresponding point inPt' on a new contour
|
||||
// shifted by 1 unit (length of normalized vector) to the left
|
||||
// if we walk along contour clockwise, this new contour is outside the old one
|
||||
//
|
||||
// inPt' is the intersection of the two lines parallel to the two
|
||||
// adjacent edges of inPt at a distance of 1 unit on the left side.
|
||||
|
||||
var v_trans_x, v_trans_y, shrink_by; // resulting translation vector for inPt
|
||||
|
||||
// good reading for geometry algorithms (here: line-line intersection)
|
||||
// http://geomalgorithms.com/a05-_intersect-1.html
|
||||
|
||||
var v_prev_x = inPt.x - inPrev.x,
|
||||
v_prev_y = inPt.y - inPrev.y;
|
||||
var v_next_x = inNext.x - inPt.x,
|
||||
v_next_y = inNext.y - inPt.y;
|
||||
|
||||
var v_prev_lensq = ( v_prev_x * v_prev_x + v_prev_y * v_prev_y );
|
||||
|
||||
// check for collinear edges
|
||||
var collinear0 = ( v_prev_x * v_next_y - v_prev_y * v_next_x );
|
||||
|
||||
if ( Math.abs( collinear0 ) > Number.EPSILON ) {
|
||||
|
||||
// not collinear
|
||||
|
||||
// length of vectors for normalizing
|
||||
|
||||
var v_prev_len = Math.sqrt( v_prev_lensq );
|
||||
var v_next_len = Math.sqrt( v_next_x * v_next_x + v_next_y * v_next_y );
|
||||
|
||||
// shift adjacent points by unit vectors to the left
|
||||
|
||||
var ptPrevShift_x = ( inPrev.x - v_prev_y / v_prev_len );
|
||||
var ptPrevShift_y = ( inPrev.y + v_prev_x / v_prev_len );
|
||||
|
||||
var ptNextShift_x = ( inNext.x - v_next_y / v_next_len );
|
||||
var ptNextShift_y = ( inNext.y + v_next_x / v_next_len );
|
||||
|
||||
// scaling factor for v_prev to intersection point
|
||||
|
||||
var sf = ( ( ptNextShift_x - ptPrevShift_x ) * v_next_y -
|
||||
( ptNextShift_y - ptPrevShift_y ) * v_next_x ) /
|
||||
( v_prev_x * v_next_y - v_prev_y * v_next_x );
|
||||
|
||||
// vector from inPt to intersection point
|
||||
|
||||
v_trans_x = ( ptPrevShift_x + v_prev_x * sf - inPt.x );
|
||||
v_trans_y = ( ptPrevShift_y + v_prev_y * sf - inPt.y );
|
||||
|
||||
// Don't normalize!, otherwise sharp corners become ugly
|
||||
// but prevent crazy spikes
|
||||
var v_trans_lensq = ( v_trans_x * v_trans_x + v_trans_y * v_trans_y );
|
||||
if ( v_trans_lensq <= 2 ) {
|
||||
|
||||
return new Vector2( v_trans_x, v_trans_y );
|
||||
|
||||
} else {
|
||||
|
||||
shrink_by = Math.sqrt( v_trans_lensq / 2 );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// handle special case of collinear edges
|
||||
|
||||
var direction_eq = false; // assumes: opposite
|
||||
if ( v_prev_x > Number.EPSILON ) {
|
||||
|
||||
if ( v_next_x > Number.EPSILON ) {
|
||||
|
||||
direction_eq = true;
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
if ( v_prev_x < - Number.EPSILON ) {
|
||||
|
||||
if ( v_next_x < - Number.EPSILON ) {
|
||||
|
||||
direction_eq = true;
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
if ( Math.sign( v_prev_y ) === Math.sign( v_next_y ) ) {
|
||||
|
||||
direction_eq = true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( direction_eq ) {
|
||||
|
||||
// console.log("Warning: lines are a straight sequence");
|
||||
v_trans_x = - v_prev_y;
|
||||
v_trans_y = v_prev_x;
|
||||
shrink_by = Math.sqrt( v_prev_lensq );
|
||||
|
||||
} else {
|
||||
|
||||
// console.log("Warning: lines are a straight spike");
|
||||
v_trans_x = v_prev_x;
|
||||
v_trans_y = v_prev_y;
|
||||
shrink_by = Math.sqrt( v_prev_lensq / 2 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return new Vector2( v_trans_x / shrink_by, v_trans_y / shrink_by );
|
||||
|
||||
}
|
||||
|
||||
|
||||
var contourMovements = [];
|
||||
|
||||
for ( var i = 0, il = contour.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
|
||||
|
||||
if ( j === il ) j = 0;
|
||||
if ( k === il ) k = 0;
|
||||
|
||||
// (j)---(i)---(k)
|
||||
// console.log('i,j,k', i, j , k)
|
||||
|
||||
contourMovements[ i ] = getBevelVec( contour[ i ], contour[ j ], contour[ k ] );
|
||||
|
||||
}
|
||||
|
||||
var holesMovements = [],
|
||||
oneHoleMovements, verticesMovements = contourMovements.concat();
|
||||
|
||||
for ( h = 0, hl = holes.length; h < hl; h ++ ) {
|
||||
|
||||
ahole = holes[ h ];
|
||||
|
||||
oneHoleMovements = [];
|
||||
|
||||
for ( i = 0, il = ahole.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
|
||||
|
||||
if ( j === il ) j = 0;
|
||||
if ( k === il ) k = 0;
|
||||
|
||||
// (j)---(i)---(k)
|
||||
oneHoleMovements[ i ] = getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] );
|
||||
|
||||
}
|
||||
|
||||
holesMovements.push( oneHoleMovements );
|
||||
verticesMovements = verticesMovements.concat( oneHoleMovements );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Loop bevelSegments, 1 for the front, 1 for the back
|
||||
|
||||
for ( b = 0; b < bevelSegments; b ++ ) {
|
||||
|
||||
//for ( b = bevelSegments; b > 0; b -- ) {
|
||||
|
||||
t = b / bevelSegments;
|
||||
z = bevelThickness * Math.cos( t * Math.PI / 2 );
|
||||
bs = bevelSize * Math.sin( t * Math.PI / 2 );
|
||||
|
||||
// contract shape
|
||||
|
||||
for ( i = 0, il = contour.length; i < il; i ++ ) {
|
||||
|
||||
vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
|
||||
|
||||
v( vert.x, vert.y, - z );
|
||||
|
||||
}
|
||||
|
||||
// expand holes
|
||||
|
||||
for ( h = 0, hl = holes.length; h < hl; h ++ ) {
|
||||
|
||||
ahole = holes[ h ];
|
||||
oneHoleMovements = holesMovements[ h ];
|
||||
|
||||
for ( i = 0, il = ahole.length; i < il; i ++ ) {
|
||||
|
||||
vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
|
||||
|
||||
v( vert.x, vert.y, - z );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bs = bevelSize;
|
||||
|
||||
// Back facing vertices
|
||||
|
||||
for ( i = 0; i < vlen; i ++ ) {
|
||||
|
||||
vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
|
||||
|
||||
if ( ! extrudeByPath ) {
|
||||
|
||||
v( vert.x, vert.y, 0 );
|
||||
|
||||
} else {
|
||||
|
||||
// v( vert.x, vert.y + extrudePts[ 0 ].y, extrudePts[ 0 ].x );
|
||||
|
||||
normal.copy( splineTube.normals[ 0 ] ).multiplyScalar( vert.x );
|
||||
binormal.copy( splineTube.binormals[ 0 ] ).multiplyScalar( vert.y );
|
||||
|
||||
position2.copy( extrudePts[ 0 ] ).add( normal ).add( binormal );
|
||||
|
||||
v( position2.x, position2.y, position2.z );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Add stepped vertices...
|
||||
// Including front facing vertices
|
||||
|
||||
var s;
|
||||
|
||||
for ( s = 1; s <= steps; s ++ ) {
|
||||
|
||||
for ( i = 0; i < vlen; i ++ ) {
|
||||
|
||||
vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
|
||||
|
||||
if ( ! extrudeByPath ) {
|
||||
|
||||
v( vert.x, vert.y, depth / steps * s );
|
||||
|
||||
} else {
|
||||
|
||||
// v( vert.x, vert.y + extrudePts[ s - 1 ].y, extrudePts[ s - 1 ].x );
|
||||
|
||||
normal.copy( splineTube.normals[ s ] ).multiplyScalar( vert.x );
|
||||
binormal.copy( splineTube.binormals[ s ] ).multiplyScalar( vert.y );
|
||||
|
||||
position2.copy( extrudePts[ s ] ).add( normal ).add( binormal );
|
||||
|
||||
v( position2.x, position2.y, position2.z );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Add bevel segments planes
|
||||
|
||||
//for ( b = 1; b <= bevelSegments; b ++ ) {
|
||||
for ( b = bevelSegments - 1; b >= 0; b -- ) {
|
||||
|
||||
t = b / bevelSegments;
|
||||
z = bevelThickness * Math.cos( t * Math.PI / 2 );
|
||||
bs = bevelSize * Math.sin( t * Math.PI / 2 );
|
||||
|
||||
// contract shape
|
||||
|
||||
for ( i = 0, il = contour.length; i < il; i ++ ) {
|
||||
|
||||
vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
|
||||
v( vert.x, vert.y, depth + z );
|
||||
|
||||
}
|
||||
|
||||
// expand holes
|
||||
|
||||
for ( h = 0, hl = holes.length; h < hl; h ++ ) {
|
||||
|
||||
ahole = holes[ h ];
|
||||
oneHoleMovements = holesMovements[ h ];
|
||||
|
||||
for ( i = 0, il = ahole.length; i < il; i ++ ) {
|
||||
|
||||
vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
|
||||
|
||||
if ( ! extrudeByPath ) {
|
||||
|
||||
v( vert.x, vert.y, depth + z );
|
||||
|
||||
} else {
|
||||
|
||||
v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Faces */
|
||||
|
||||
// Top and bottom faces
|
||||
|
||||
buildLidFaces();
|
||||
|
||||
// Sides faces
|
||||
|
||||
buildSideFaces();
|
||||
|
||||
|
||||
///// Internal functions
|
||||
|
||||
function buildLidFaces() {
|
||||
|
||||
var start = verticesArray.length / 3;
|
||||
|
||||
if ( bevelEnabled ) {
|
||||
|
||||
var layer = 0; // steps + 1
|
||||
var offset = vlen * layer;
|
||||
|
||||
// Bottom faces
|
||||
|
||||
for ( i = 0; i < flen; i ++ ) {
|
||||
|
||||
face = faces[ i ];
|
||||
f3( face[ 2 ] + offset, face[ 1 ] + offset, face[ 0 ] + offset );
|
||||
|
||||
}
|
||||
|
||||
layer = steps + bevelSegments * 2;
|
||||
offset = vlen * layer;
|
||||
|
||||
// Top faces
|
||||
|
||||
for ( i = 0; i < flen; i ++ ) {
|
||||
|
||||
face = faces[ i ];
|
||||
f3( face[ 0 ] + offset, face[ 1 ] + offset, face[ 2 ] + offset );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// Bottom faces
|
||||
|
||||
for ( i = 0; i < flen; i ++ ) {
|
||||
|
||||
face = faces[ i ];
|
||||
f3( face[ 2 ], face[ 1 ], face[ 0 ] );
|
||||
|
||||
}
|
||||
|
||||
// Top faces
|
||||
|
||||
for ( i = 0; i < flen; i ++ ) {
|
||||
|
||||
face = faces[ i ];
|
||||
f3( face[ 0 ] + vlen * steps, face[ 1 ] + vlen * steps, face[ 2 ] + vlen * steps );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
scope.addGroup( start, verticesArray.length / 3 - start, 0 );
|
||||
|
||||
}
|
||||
|
||||
// Create faces for the z-sides of the shape
|
||||
|
||||
function buildSideFaces() {
|
||||
|
||||
var start = verticesArray.length / 3;
|
||||
var layeroffset = 0;
|
||||
sidewalls( contour, layeroffset );
|
||||
layeroffset += contour.length;
|
||||
|
||||
for ( h = 0, hl = holes.length; h < hl; h ++ ) {
|
||||
|
||||
ahole = holes[ h ];
|
||||
sidewalls( ahole, layeroffset );
|
||||
|
||||
//, true
|
||||
layeroffset += ahole.length;
|
||||
|
||||
}
|
||||
|
||||
|
||||
scope.addGroup( start, verticesArray.length / 3 - start, 1 );
|
||||
|
||||
|
||||
}
|
||||
|
||||
function sidewalls( contour, layeroffset ) {
|
||||
|
||||
var j, k;
|
||||
i = contour.length;
|
||||
|
||||
while ( -- i >= 0 ) {
|
||||
|
||||
j = i;
|
||||
k = i - 1;
|
||||
if ( k < 0 ) k = contour.length - 1;
|
||||
|
||||
//console.log('b', i,j, i-1, k,vertices.length);
|
||||
|
||||
var s = 0,
|
||||
sl = steps + bevelSegments * 2;
|
||||
|
||||
for ( s = 0; s < sl; s ++ ) {
|
||||
|
||||
var slen1 = vlen * s;
|
||||
var slen2 = vlen * ( s + 1 );
|
||||
|
||||
var a = layeroffset + j + slen1,
|
||||
b = layeroffset + k + slen1,
|
||||
c = layeroffset + k + slen2,
|
||||
d = layeroffset + j + slen2;
|
||||
|
||||
f4( a, b, c, d );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function v( x, y, z ) {
|
||||
|
||||
placeholder.push( x );
|
||||
placeholder.push( y );
|
||||
placeholder.push( z );
|
||||
|
||||
}
|
||||
|
||||
|
||||
function f3( a, b, c ) {
|
||||
|
||||
addVertex( a );
|
||||
addVertex( b );
|
||||
addVertex( c );
|
||||
|
||||
var nextIndex = verticesArray.length / 3;
|
||||
var uvs = uvgen.generateTopUV( scope, verticesArray, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
|
||||
|
||||
addUV( uvs[ 0 ] );
|
||||
addUV( uvs[ 1 ] );
|
||||
addUV( uvs[ 2 ] );
|
||||
|
||||
}
|
||||
|
||||
function f4( a, b, c, d ) {
|
||||
|
||||
addVertex( a );
|
||||
addVertex( b );
|
||||
addVertex( d );
|
||||
|
||||
addVertex( b );
|
||||
addVertex( c );
|
||||
addVertex( d );
|
||||
|
||||
|
||||
var nextIndex = verticesArray.length / 3;
|
||||
var uvs = uvgen.generateSideWallUV( scope, verticesArray, nextIndex - 6, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
|
||||
|
||||
addUV( uvs[ 0 ] );
|
||||
addUV( uvs[ 1 ] );
|
||||
addUV( uvs[ 3 ] );
|
||||
|
||||
addUV( uvs[ 1 ] );
|
||||
addUV( uvs[ 2 ] );
|
||||
addUV( uvs[ 3 ] );
|
||||
|
||||
}
|
||||
|
||||
function addVertex( index ) {
|
||||
|
||||
verticesArray.push( placeholder[ index * 3 + 0 ] );
|
||||
verticesArray.push( placeholder[ index * 3 + 1 ] );
|
||||
verticesArray.push( placeholder[ index * 3 + 2 ] );
|
||||
|
||||
}
|
||||
|
||||
|
||||
function addUV( vector2 ) {
|
||||
|
||||
uvArray.push( vector2.x );
|
||||
uvArray.push( vector2.y );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ExtrudeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
ExtrudeBufferGeometry.prototype.constructor = ExtrudeBufferGeometry;
|
||||
|
||||
ExtrudeBufferGeometry.prototype.toJSON = function () {
|
||||
|
||||
var data = BufferGeometry.prototype.toJSON.call( this );
|
||||
|
||||
var shapes = this.parameters.shapes;
|
||||
var options = this.parameters.options;
|
||||
|
||||
return toJSON( shapes, options, data );
|
||||
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
var WorldUVGenerator = {
|
||||
|
||||
generateTopUV: function ( geometry, vertices, indexA, indexB, indexC ) {
|
||||
|
||||
var a_x = vertices[ indexA * 3 ];
|
||||
var a_y = vertices[ indexA * 3 + 1 ];
|
||||
var b_x = vertices[ indexB * 3 ];
|
||||
var b_y = vertices[ indexB * 3 + 1 ];
|
||||
var c_x = vertices[ indexC * 3 ];
|
||||
var c_y = vertices[ indexC * 3 + 1 ];
|
||||
|
||||
return [
|
||||
new Vector2( a_x, a_y ),
|
||||
new Vector2( b_x, b_y ),
|
||||
new Vector2( c_x, c_y )
|
||||
];
|
||||
|
||||
},
|
||||
|
||||
generateSideWallUV: function ( geometry, vertices, indexA, indexB, indexC, indexD ) {
|
||||
|
||||
var a_x = vertices[ indexA * 3 ];
|
||||
var a_y = vertices[ indexA * 3 + 1 ];
|
||||
var a_z = vertices[ indexA * 3 + 2 ];
|
||||
var b_x = vertices[ indexB * 3 ];
|
||||
var b_y = vertices[ indexB * 3 + 1 ];
|
||||
var b_z = vertices[ indexB * 3 + 2 ];
|
||||
var c_x = vertices[ indexC * 3 ];
|
||||
var c_y = vertices[ indexC * 3 + 1 ];
|
||||
var c_z = vertices[ indexC * 3 + 2 ];
|
||||
var d_x = vertices[ indexD * 3 ];
|
||||
var d_y = vertices[ indexD * 3 + 1 ];
|
||||
var d_z = vertices[ indexD * 3 + 2 ];
|
||||
|
||||
if ( Math.abs( a_y - b_y ) < 0.01 ) {
|
||||
|
||||
return [
|
||||
new Vector2( a_x, 1 - a_z ),
|
||||
new Vector2( b_x, 1 - b_z ),
|
||||
new Vector2( c_x, 1 - c_z ),
|
||||
new Vector2( d_x, 1 - d_z )
|
||||
];
|
||||
|
||||
} else {
|
||||
|
||||
return [
|
||||
new Vector2( a_y, 1 - a_z ),
|
||||
new Vector2( b_y, 1 - b_z ),
|
||||
new Vector2( c_y, 1 - c_z ),
|
||||
new Vector2( d_y, 1 - d_z )
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
function toJSON( shapes, options, data ) {
|
||||
|
||||
//
|
||||
|
||||
data.shapes = [];
|
||||
|
||||
if ( Array.isArray( shapes ) ) {
|
||||
|
||||
for ( var i = 0, l = shapes.length; i < l; i ++ ) {
|
||||
|
||||
var shape = shapes[ i ];
|
||||
|
||||
data.shapes.push( shape.uuid );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
data.shapes.push( shapes.uuid );
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
if ( options.extrudePath !== undefined ) data.options.extrudePath = options.extrudePath.toJSON();
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
export { ExtrudeGeometry, ExtrudeBufferGeometry };
|
22
lib/geometries/Geometries.js
Normal file
@ -0,0 +1,22 @@
|
||||
export { WireframeGeometry } from './WireframeGeometry.js';
|
||||
export { ParametricGeometry, ParametricBufferGeometry } from './ParametricGeometry.js';
|
||||
export { TetrahedronGeometry, TetrahedronBufferGeometry } from './TetrahedronGeometry.js';
|
||||
export { OctahedronGeometry, OctahedronBufferGeometry } from './OctahedronGeometry.js';
|
||||
export { IcosahedronGeometry, IcosahedronBufferGeometry } from './IcosahedronGeometry.js';
|
||||
export { DodecahedronGeometry, DodecahedronBufferGeometry } from './DodecahedronGeometry.js';
|
||||
export { PolyhedronGeometry, PolyhedronBufferGeometry } from './PolyhedronGeometry.js';
|
||||
export { TubeGeometry, TubeBufferGeometry } from './TubeGeometry.js';
|
||||
export { TorusKnotGeometry, TorusKnotBufferGeometry } from './TorusKnotGeometry.js';
|
||||
export { TorusGeometry, TorusBufferGeometry } from './TorusGeometry.js';
|
||||
export { TextGeometry, TextBufferGeometry } from './TextGeometry.js';
|
||||
export { SphereGeometry, SphereBufferGeometry } from './SphereGeometry.js';
|
||||
export { RingGeometry, RingBufferGeometry } from './RingGeometry.js';
|
||||
export { PlaneGeometry, PlaneBufferGeometry } from './PlaneGeometry.js';
|
||||
export { LatheGeometry, LatheBufferGeometry } from './LatheGeometry.js';
|
||||
export { ShapeGeometry, ShapeBufferGeometry } from './ShapeGeometry.js';
|
||||
export { ExtrudeGeometry, ExtrudeBufferGeometry } from './ExtrudeGeometry.js';
|
||||
export { EdgesGeometry } from './EdgesGeometry.js';
|
||||
export { ConeGeometry, ConeBufferGeometry } from './ConeGeometry.js';
|
||||
export { CylinderGeometry, CylinderBufferGeometry } from './CylinderGeometry.js';
|
||||
export { CircleGeometry, CircleBufferGeometry } from './CircleGeometry.js';
|
||||
export { BoxGeometry, BoxBufferGeometry } from './BoxGeometry.js';
|
64
lib/geometries/IcosahedronGeometry.js
Normal file
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* @author timothypratley / https://github.com/timothypratley
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { PolyhedronBufferGeometry } from './PolyhedronGeometry.js';
|
||||
|
||||
// IcosahedronGeometry
|
||||
|
||||
function IcosahedronGeometry( radius, detail ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'IcosahedronGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new IcosahedronBufferGeometry( radius, detail ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
IcosahedronGeometry.prototype = Object.create( Geometry.prototype );
|
||||
IcosahedronGeometry.prototype.constructor = IcosahedronGeometry;
|
||||
|
||||
// IcosahedronBufferGeometry
|
||||
|
||||
function IcosahedronBufferGeometry( radius, detail ) {
|
||||
|
||||
var t = ( 1 + Math.sqrt( 5 ) ) / 2;
|
||||
|
||||
var vertices = [
|
||||
- 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t, 0,
|
||||
0, - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t,
|
||||
t, 0, - 1, t, 0, 1, - t, 0, - 1, - t, 0, 1
|
||||
];
|
||||
|
||||
var indices = [
|
||||
0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
|
||||
1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
|
||||
3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
|
||||
4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1
|
||||
];
|
||||
|
||||
PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
|
||||
|
||||
this.type = 'IcosahedronBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
IcosahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
|
||||
IcosahedronBufferGeometry.prototype.constructor = IcosahedronBufferGeometry;
|
||||
|
||||
|
||||
export { IcosahedronGeometry, IcosahedronBufferGeometry };
|
186
lib/geometries/LatheGeometry.js
Normal file
@ -0,0 +1,186 @@
|
||||
/**
|
||||
* @author zz85 / https://github.com/zz85
|
||||
* @author bhouston / http://clara.io
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { Vector2 } from '../math/Vector2.js';
|
||||
import { _Math } from '../math/Math.js';
|
||||
|
||||
// LatheGeometry
|
||||
|
||||
function LatheGeometry( points, segments, phiStart, phiLength ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'LatheGeometry';
|
||||
|
||||
this.parameters = {
|
||||
points: points,
|
||||
segments: segments,
|
||||
phiStart: phiStart,
|
||||
phiLength: phiLength
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new LatheBufferGeometry( points, segments, phiStart, phiLength ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
LatheGeometry.prototype = Object.create( Geometry.prototype );
|
||||
LatheGeometry.prototype.constructor = LatheGeometry;
|
||||
|
||||
// LatheBufferGeometry
|
||||
|
||||
function LatheBufferGeometry( points, segments, phiStart, phiLength ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'LatheBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
points: points,
|
||||
segments: segments,
|
||||
phiStart: phiStart,
|
||||
phiLength: phiLength
|
||||
};
|
||||
|
||||
segments = Math.floor( segments ) || 12;
|
||||
phiStart = phiStart || 0;
|
||||
phiLength = phiLength || Math.PI * 2;
|
||||
|
||||
// clamp phiLength so it's in range of [ 0, 2PI ]
|
||||
|
||||
phiLength = _Math.clamp( phiLength, 0, Math.PI * 2 );
|
||||
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var uvs = [];
|
||||
|
||||
// helper variables
|
||||
|
||||
var base;
|
||||
var inverseSegments = 1.0 / segments;
|
||||
var vertex = new Vector3();
|
||||
var uv = new Vector2();
|
||||
var i, j;
|
||||
|
||||
// generate vertices and uvs
|
||||
|
||||
for ( i = 0; i <= segments; i ++ ) {
|
||||
|
||||
var phi = phiStart + i * inverseSegments * phiLength;
|
||||
|
||||
var sin = Math.sin( phi );
|
||||
var cos = Math.cos( phi );
|
||||
|
||||
for ( j = 0; j <= ( points.length - 1 ); j ++ ) {
|
||||
|
||||
// vertex
|
||||
|
||||
vertex.x = points[ j ].x * sin;
|
||||
vertex.y = points[ j ].y;
|
||||
vertex.z = points[ j ].x * cos;
|
||||
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
// uv
|
||||
|
||||
uv.x = i / segments;
|
||||
uv.y = j / ( points.length - 1 );
|
||||
|
||||
uvs.push( uv.x, uv.y );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// indices
|
||||
|
||||
for ( i = 0; i < segments; i ++ ) {
|
||||
|
||||
for ( j = 0; j < ( points.length - 1 ); j ++ ) {
|
||||
|
||||
base = j + i * points.length;
|
||||
|
||||
var a = base;
|
||||
var b = base + points.length;
|
||||
var c = base + points.length + 1;
|
||||
var d = base + 1;
|
||||
|
||||
// faces
|
||||
|
||||
indices.push( a, b, d );
|
||||
indices.push( b, c, d );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
// generate normals
|
||||
|
||||
this.computeVertexNormals();
|
||||
|
||||
// if the geometry is closed, we need to average the normals along the seam.
|
||||
// because the corresponding vertices are identical (but still have different UVs).
|
||||
|
||||
if ( phiLength === Math.PI * 2 ) {
|
||||
|
||||
var normals = this.attributes.normal.array;
|
||||
var n1 = new Vector3();
|
||||
var n2 = new Vector3();
|
||||
var n = new Vector3();
|
||||
|
||||
// this is the buffer offset for the last line of vertices
|
||||
|
||||
base = segments * points.length * 3;
|
||||
|
||||
for ( i = 0, j = 0; i < points.length; i ++, j += 3 ) {
|
||||
|
||||
// select the normal of the vertex in the first line
|
||||
|
||||
n1.x = normals[ j + 0 ];
|
||||
n1.y = normals[ j + 1 ];
|
||||
n1.z = normals[ j + 2 ];
|
||||
|
||||
// select the normal of the vertex in the last line
|
||||
|
||||
n2.x = normals[ base + j + 0 ];
|
||||
n2.y = normals[ base + j + 1 ];
|
||||
n2.z = normals[ base + j + 2 ];
|
||||
|
||||
// average normals
|
||||
|
||||
n.addVectors( n1, n2 ).normalize();
|
||||
|
||||
// assign the new values to both normals
|
||||
|
||||
normals[ j + 0 ] = normals[ base + j + 0 ] = n.x;
|
||||
normals[ j + 1 ] = normals[ base + j + 1 ] = n.y;
|
||||
normals[ j + 2 ] = normals[ base + j + 2 ] = n.z;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
LatheBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
LatheBufferGeometry.prototype.constructor = LatheBufferGeometry;
|
||||
|
||||
|
||||
export { LatheGeometry, LatheBufferGeometry };
|
60
lib/geometries/OctahedronGeometry.js
Normal file
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* @author timothypratley / https://github.com/timothypratley
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { PolyhedronBufferGeometry } from './PolyhedronGeometry.js';
|
||||
|
||||
// OctahedronGeometry
|
||||
|
||||
function OctahedronGeometry( radius, detail ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'OctahedronGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new OctahedronBufferGeometry( radius, detail ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
OctahedronGeometry.prototype = Object.create( Geometry.prototype );
|
||||
OctahedronGeometry.prototype.constructor = OctahedronGeometry;
|
||||
|
||||
// OctahedronBufferGeometry
|
||||
|
||||
function OctahedronBufferGeometry( radius, detail ) {
|
||||
|
||||
var vertices = [
|
||||
1, 0, 0, - 1, 0, 0, 0, 1, 0,
|
||||
0, - 1, 0, 0, 0, 1, 0, 0, - 1
|
||||
];
|
||||
|
||||
var indices = [
|
||||
0, 2, 4, 0, 4, 3, 0, 3, 5,
|
||||
0, 5, 2, 1, 2, 5, 1, 5, 3,
|
||||
1, 3, 4, 1, 4, 2
|
||||
];
|
||||
|
||||
PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
|
||||
|
||||
this.type = 'OctahedronBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
OctahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
|
||||
OctahedronBufferGeometry.prototype.constructor = OctahedronBufferGeometry;
|
||||
|
||||
|
||||
export { OctahedronGeometry, OctahedronBufferGeometry };
|
163
lib/geometries/ParametricGeometry.js
Normal file
@ -0,0 +1,163 @@
|
||||
/**
|
||||
* @author zz85 / https://github.com/zz85
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*
|
||||
* Parametric Surfaces Geometry
|
||||
* based on the brilliant article by @prideout http://prideout.net/blog/?p=44
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
|
||||
// ParametricGeometry
|
||||
|
||||
function ParametricGeometry( func, slices, stacks ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'ParametricGeometry';
|
||||
|
||||
this.parameters = {
|
||||
func: func,
|
||||
slices: slices,
|
||||
stacks: stacks
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new ParametricBufferGeometry( func, slices, stacks ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
ParametricGeometry.prototype = Object.create( Geometry.prototype );
|
||||
ParametricGeometry.prototype.constructor = ParametricGeometry;
|
||||
|
||||
// ParametricBufferGeometry
|
||||
|
||||
function ParametricBufferGeometry( func, slices, stacks ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'ParametricBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
func: func,
|
||||
slices: slices,
|
||||
stacks: stacks
|
||||
};
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
var EPS = 0.00001;
|
||||
|
||||
var normal = new Vector3();
|
||||
|
||||
var p0 = new Vector3(), p1 = new Vector3();
|
||||
var pu = new Vector3(), pv = new Vector3();
|
||||
|
||||
var i, j;
|
||||
|
||||
if ( func.length < 3 ) {
|
||||
|
||||
console.error( 'THREE.ParametricGeometry: Function must now modify a Vector3 as third parameter.' );
|
||||
|
||||
}
|
||||
|
||||
// generate vertices, normals and uvs
|
||||
|
||||
var sliceCount = slices + 1;
|
||||
|
||||
for ( i = 0; i <= stacks; i ++ ) {
|
||||
|
||||
var v = i / stacks;
|
||||
|
||||
for ( j = 0; j <= slices; j ++ ) {
|
||||
|
||||
var u = j / slices;
|
||||
|
||||
// vertex
|
||||
|
||||
func( u, v, p0 );
|
||||
vertices.push( p0.x, p0.y, p0.z );
|
||||
|
||||
// normal
|
||||
|
||||
// approximate tangent vectors via finite differences
|
||||
|
||||
if ( u - EPS >= 0 ) {
|
||||
|
||||
func( u - EPS, v, p1 );
|
||||
pu.subVectors( p0, p1 );
|
||||
|
||||
} else {
|
||||
|
||||
func( u + EPS, v, p1 );
|
||||
pu.subVectors( p1, p0 );
|
||||
|
||||
}
|
||||
|
||||
if ( v - EPS >= 0 ) {
|
||||
|
||||
func( u, v - EPS, p1 );
|
||||
pv.subVectors( p0, p1 );
|
||||
|
||||
} else {
|
||||
|
||||
func( u, v + EPS, p1 );
|
||||
pv.subVectors( p1, p0 );
|
||||
|
||||
}
|
||||
|
||||
// cross product of tangent vectors returns surface normal
|
||||
|
||||
normal.crossVectors( pu, pv ).normalize();
|
||||
normals.push( normal.x, normal.y, normal.z );
|
||||
|
||||
// uv
|
||||
|
||||
uvs.push( u, v );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// generate indices
|
||||
|
||||
for ( i = 0; i < stacks; i ++ ) {
|
||||
|
||||
for ( j = 0; j < slices; j ++ ) {
|
||||
|
||||
var a = i * sliceCount + j;
|
||||
var b = i * sliceCount + j + 1;
|
||||
var c = ( i + 1 ) * sliceCount + j + 1;
|
||||
var d = ( i + 1 ) * sliceCount + j;
|
||||
|
||||
// faces one and two
|
||||
|
||||
indices.push( a, b, d );
|
||||
indices.push( b, c, d );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
}
|
||||
|
||||
ParametricBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
ParametricBufferGeometry.prototype.constructor = ParametricBufferGeometry;
|
||||
|
||||
|
||||
export { ParametricGeometry, ParametricBufferGeometry };
|
126
lib/geometries/PlaneGeometry.js
Normal file
@ -0,0 +1,126 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
|
||||
// PlaneGeometry
|
||||
|
||||
function PlaneGeometry( width, height, widthSegments, heightSegments ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'PlaneGeometry';
|
||||
|
||||
this.parameters = {
|
||||
width: width,
|
||||
height: height,
|
||||
widthSegments: widthSegments,
|
||||
heightSegments: heightSegments
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
PlaneGeometry.prototype = Object.create( Geometry.prototype );
|
||||
PlaneGeometry.prototype.constructor = PlaneGeometry;
|
||||
|
||||
// PlaneBufferGeometry
|
||||
|
||||
function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'PlaneBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
width: width,
|
||||
height: height,
|
||||
widthSegments: widthSegments,
|
||||
heightSegments: heightSegments
|
||||
};
|
||||
|
||||
width = width || 1;
|
||||
height = height || 1;
|
||||
|
||||
var width_half = width / 2;
|
||||
var height_half = height / 2;
|
||||
|
||||
var gridX = Math.floor( widthSegments ) || 1;
|
||||
var gridY = Math.floor( heightSegments ) || 1;
|
||||
|
||||
var gridX1 = gridX + 1;
|
||||
var gridY1 = gridY + 1;
|
||||
|
||||
var segment_width = width / gridX;
|
||||
var segment_height = height / gridY;
|
||||
|
||||
var ix, iy;
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
// generate vertices, normals and uvs
|
||||
|
||||
for ( iy = 0; iy < gridY1; iy ++ ) {
|
||||
|
||||
var y = iy * segment_height - height_half;
|
||||
|
||||
for ( ix = 0; ix < gridX1; ix ++ ) {
|
||||
|
||||
var x = ix * segment_width - width_half;
|
||||
|
||||
vertices.push( x, - y, 0 );
|
||||
|
||||
normals.push( 0, 0, 1 );
|
||||
|
||||
uvs.push( ix / gridX );
|
||||
uvs.push( 1 - ( iy / gridY ) );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// indices
|
||||
|
||||
for ( iy = 0; iy < gridY; iy ++ ) {
|
||||
|
||||
for ( ix = 0; ix < gridX; ix ++ ) {
|
||||
|
||||
var a = ix + gridX1 * iy;
|
||||
var b = ix + gridX1 * ( iy + 1 );
|
||||
var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
|
||||
var d = ( ix + 1 ) + gridX1 * iy;
|
||||
|
||||
// faces
|
||||
|
||||
indices.push( a, b, d );
|
||||
indices.push( b, c, d );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
}
|
||||
|
||||
PlaneBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
PlaneBufferGeometry.prototype.constructor = PlaneBufferGeometry;
|
||||
|
||||
|
||||
export { PlaneGeometry, PlaneBufferGeometry };
|
341
lib/geometries/PolyhedronGeometry.js
Normal file
@ -0,0 +1,341 @@
|
||||
/**
|
||||
* @author clockworkgeek / https://github.com/clockworkgeek
|
||||
* @author timothypratley / https://github.com/timothypratley
|
||||
* @author WestLangley / http://github.com/WestLangley
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
import { Vector2 } from '../math/Vector2.js';
|
||||
|
||||
// PolyhedronGeometry
|
||||
|
||||
function PolyhedronGeometry( vertices, indices, radius, detail ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'PolyhedronGeometry';
|
||||
|
||||
this.parameters = {
|
||||
vertices: vertices,
|
||||
indices: indices,
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new PolyhedronBufferGeometry( vertices, indices, radius, detail ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
PolyhedronGeometry.prototype = Object.create( Geometry.prototype );
|
||||
PolyhedronGeometry.prototype.constructor = PolyhedronGeometry;
|
||||
|
||||
// PolyhedronBufferGeometry
|
||||
|
||||
function PolyhedronBufferGeometry( vertices, indices, radius, detail ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'PolyhedronBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
vertices: vertices,
|
||||
indices: indices,
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
radius = radius || 1;
|
||||
detail = detail || 0;
|
||||
|
||||
// default buffer data
|
||||
|
||||
var vertexBuffer = [];
|
||||
var uvBuffer = [];
|
||||
|
||||
// the subdivision creates the vertex buffer data
|
||||
|
||||
subdivide( detail );
|
||||
|
||||
// all vertices should lie on a conceptual sphere with a given radius
|
||||
|
||||
appplyRadius( radius );
|
||||
|
||||
// finally, create the uv data
|
||||
|
||||
generateUVs();
|
||||
|
||||
// build non-indexed geometry
|
||||
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertexBuffer, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( vertexBuffer.slice(), 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvBuffer, 2 ) );
|
||||
|
||||
if ( detail === 0 ) {
|
||||
|
||||
this.computeVertexNormals(); // flat normals
|
||||
|
||||
} else {
|
||||
|
||||
this.normalizeNormals(); // smooth normals
|
||||
|
||||
}
|
||||
|
||||
// helper functions
|
||||
|
||||
function subdivide( detail ) {
|
||||
|
||||
var a = new Vector3();
|
||||
var b = new Vector3();
|
||||
var c = new Vector3();
|
||||
|
||||
// iterate over all faces and apply a subdivison with the given detail value
|
||||
|
||||
for ( var i = 0; i < indices.length; i += 3 ) {
|
||||
|
||||
// get the vertices of the face
|
||||
|
||||
getVertexByIndex( indices[ i + 0 ], a );
|
||||
getVertexByIndex( indices[ i + 1 ], b );
|
||||
getVertexByIndex( indices[ i + 2 ], c );
|
||||
|
||||
// perform subdivision
|
||||
|
||||
subdivideFace( a, b, c, detail );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function subdivideFace( a, b, c, detail ) {
|
||||
|
||||
var cols = Math.pow( 2, detail );
|
||||
|
||||
// we use this multidimensional array as a data structure for creating the subdivision
|
||||
|
||||
var v = [];
|
||||
|
||||
var i, j;
|
||||
|
||||
// construct all of the vertices for this subdivision
|
||||
|
||||
for ( i = 0; i <= cols; i ++ ) {
|
||||
|
||||
v[ i ] = [];
|
||||
|
||||
var aj = a.clone().lerp( c, i / cols );
|
||||
var bj = b.clone().lerp( c, i / cols );
|
||||
|
||||
var rows = cols - i;
|
||||
|
||||
for ( j = 0; j <= rows; j ++ ) {
|
||||
|
||||
if ( j === 0 && i === cols ) {
|
||||
|
||||
v[ i ][ j ] = aj;
|
||||
|
||||
} else {
|
||||
|
||||
v[ i ][ j ] = aj.clone().lerp( bj, j / rows );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// construct all of the faces
|
||||
|
||||
for ( i = 0; i < cols; i ++ ) {
|
||||
|
||||
for ( j = 0; j < 2 * ( cols - i ) - 1; j ++ ) {
|
||||
|
||||
var k = Math.floor( j / 2 );
|
||||
|
||||
if ( j % 2 === 0 ) {
|
||||
|
||||
pushVertex( v[ i ][ k + 1 ] );
|
||||
pushVertex( v[ i + 1 ][ k ] );
|
||||
pushVertex( v[ i ][ k ] );
|
||||
|
||||
} else {
|
||||
|
||||
pushVertex( v[ i ][ k + 1 ] );
|
||||
pushVertex( v[ i + 1 ][ k + 1 ] );
|
||||
pushVertex( v[ i + 1 ][ k ] );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function appplyRadius( radius ) {
|
||||
|
||||
var vertex = new Vector3();
|
||||
|
||||
// iterate over the entire buffer and apply the radius to each vertex
|
||||
|
||||
for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
|
||||
|
||||
vertex.x = vertexBuffer[ i + 0 ];
|
||||
vertex.y = vertexBuffer[ i + 1 ];
|
||||
vertex.z = vertexBuffer[ i + 2 ];
|
||||
|
||||
vertex.normalize().multiplyScalar( radius );
|
||||
|
||||
vertexBuffer[ i + 0 ] = vertex.x;
|
||||
vertexBuffer[ i + 1 ] = vertex.y;
|
||||
vertexBuffer[ i + 2 ] = vertex.z;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function generateUVs() {
|
||||
|
||||
var vertex = new Vector3();
|
||||
|
||||
for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
|
||||
|
||||
vertex.x = vertexBuffer[ i + 0 ];
|
||||
vertex.y = vertexBuffer[ i + 1 ];
|
||||
vertex.z = vertexBuffer[ i + 2 ];
|
||||
|
||||
var u = azimuth( vertex ) / 2 / Math.PI + 0.5;
|
||||
var v = inclination( vertex ) / Math.PI + 0.5;
|
||||
uvBuffer.push( u, 1 - v );
|
||||
|
||||
}
|
||||
|
||||
correctUVs();
|
||||
|
||||
correctSeam();
|
||||
|
||||
}
|
||||
|
||||
function correctSeam() {
|
||||
|
||||
// handle case when face straddles the seam, see #3269
|
||||
|
||||
for ( var i = 0; i < uvBuffer.length; i += 6 ) {
|
||||
|
||||
// uv data of a single face
|
||||
|
||||
var x0 = uvBuffer[ i + 0 ];
|
||||
var x1 = uvBuffer[ i + 2 ];
|
||||
var x2 = uvBuffer[ i + 4 ];
|
||||
|
||||
var max = Math.max( x0, x1, x2 );
|
||||
var min = Math.min( x0, x1, x2 );
|
||||
|
||||
// 0.9 is somewhat arbitrary
|
||||
|
||||
if ( max > 0.9 && min < 0.1 ) {
|
||||
|
||||
if ( x0 < 0.2 ) uvBuffer[ i + 0 ] += 1;
|
||||
if ( x1 < 0.2 ) uvBuffer[ i + 2 ] += 1;
|
||||
if ( x2 < 0.2 ) uvBuffer[ i + 4 ] += 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function pushVertex( vertex ) {
|
||||
|
||||
vertexBuffer.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
}
|
||||
|
||||
function getVertexByIndex( index, vertex ) {
|
||||
|
||||
var stride = index * 3;
|
||||
|
||||
vertex.x = vertices[ stride + 0 ];
|
||||
vertex.y = vertices[ stride + 1 ];
|
||||
vertex.z = vertices[ stride + 2 ];
|
||||
|
||||
}
|
||||
|
||||
function correctUVs() {
|
||||
|
||||
var a = new Vector3();
|
||||
var b = new Vector3();
|
||||
var c = new Vector3();
|
||||
|
||||
var centroid = new Vector3();
|
||||
|
||||
var uvA = new Vector2();
|
||||
var uvB = new Vector2();
|
||||
var uvC = new Vector2();
|
||||
|
||||
for ( var i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) {
|
||||
|
||||
a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] );
|
||||
b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] );
|
||||
c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] );
|
||||
|
||||
uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] );
|
||||
uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] );
|
||||
uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] );
|
||||
|
||||
centroid.copy( a ).add( b ).add( c ).divideScalar( 3 );
|
||||
|
||||
var azi = azimuth( centroid );
|
||||
|
||||
correctUV( uvA, j + 0, a, azi );
|
||||
correctUV( uvB, j + 2, b, azi );
|
||||
correctUV( uvC, j + 4, c, azi );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function correctUV( uv, stride, vector, azimuth ) {
|
||||
|
||||
if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) {
|
||||
|
||||
uvBuffer[ stride ] = uv.x - 1;
|
||||
|
||||
}
|
||||
|
||||
if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) {
|
||||
|
||||
uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Angle around the Y axis, counter-clockwise when looking from above.
|
||||
|
||||
function azimuth( vector ) {
|
||||
|
||||
return Math.atan2( vector.z, - vector.x );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Angle above the XZ plane.
|
||||
|
||||
function inclination( vector ) {
|
||||
|
||||
return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
PolyhedronBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
PolyhedronBufferGeometry.prototype.constructor = PolyhedronBufferGeometry;
|
||||
|
||||
|
||||
export { PolyhedronGeometry, PolyhedronBufferGeometry };
|
152
lib/geometries/RingGeometry.js
Normal file
@ -0,0 +1,152 @@
|
||||
/**
|
||||
* @author Kaleb Murphy
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector2 } from '../math/Vector2.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
|
||||
// RingGeometry
|
||||
|
||||
function RingGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'RingGeometry';
|
||||
|
||||
this.parameters = {
|
||||
innerRadius: innerRadius,
|
||||
outerRadius: outerRadius,
|
||||
thetaSegments: thetaSegments,
|
||||
phiSegments: phiSegments,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
RingGeometry.prototype = Object.create( Geometry.prototype );
|
||||
RingGeometry.prototype.constructor = RingGeometry;
|
||||
|
||||
// RingBufferGeometry
|
||||
|
||||
function RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'RingBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
innerRadius: innerRadius,
|
||||
outerRadius: outerRadius,
|
||||
thetaSegments: thetaSegments,
|
||||
phiSegments: phiSegments,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
innerRadius = innerRadius || 0.5;
|
||||
outerRadius = outerRadius || 1;
|
||||
|
||||
thetaStart = thetaStart !== undefined ? thetaStart : 0;
|
||||
thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
|
||||
|
||||
thetaSegments = thetaSegments !== undefined ? Math.max( 3, thetaSegments ) : 8;
|
||||
phiSegments = phiSegments !== undefined ? Math.max( 1, phiSegments ) : 1;
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
// some helper variables
|
||||
|
||||
var segment;
|
||||
var radius = innerRadius;
|
||||
var radiusStep = ( ( outerRadius - innerRadius ) / phiSegments );
|
||||
var vertex = new Vector3();
|
||||
var uv = new Vector2();
|
||||
var j, i;
|
||||
|
||||
// generate vertices, normals and uvs
|
||||
|
||||
for ( j = 0; j <= phiSegments; j ++ ) {
|
||||
|
||||
for ( i = 0; i <= thetaSegments; i ++ ) {
|
||||
|
||||
// values are generate from the inside of the ring to the outside
|
||||
|
||||
segment = thetaStart + i / thetaSegments * thetaLength;
|
||||
|
||||
// vertex
|
||||
|
||||
vertex.x = radius * Math.cos( segment );
|
||||
vertex.y = radius * Math.sin( segment );
|
||||
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
// normal
|
||||
|
||||
normals.push( 0, 0, 1 );
|
||||
|
||||
// uv
|
||||
|
||||
uv.x = ( vertex.x / outerRadius + 1 ) / 2;
|
||||
uv.y = ( vertex.y / outerRadius + 1 ) / 2;
|
||||
|
||||
uvs.push( uv.x, uv.y );
|
||||
|
||||
}
|
||||
|
||||
// increase the radius for next row of vertices
|
||||
|
||||
radius += radiusStep;
|
||||
|
||||
}
|
||||
|
||||
// indices
|
||||
|
||||
for ( j = 0; j < phiSegments; j ++ ) {
|
||||
|
||||
var thetaSegmentLevel = j * ( thetaSegments + 1 );
|
||||
|
||||
for ( i = 0; i < thetaSegments; i ++ ) {
|
||||
|
||||
segment = i + thetaSegmentLevel;
|
||||
|
||||
var a = segment;
|
||||
var b = segment + thetaSegments + 1;
|
||||
var c = segment + thetaSegments + 2;
|
||||
var d = segment + 1;
|
||||
|
||||
// faces
|
||||
|
||||
indices.push( a, b, d );
|
||||
indices.push( b, c, d );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
}
|
||||
|
||||
RingBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
RingBufferGeometry.prototype.constructor = RingBufferGeometry;
|
||||
|
||||
|
||||
export { RingGeometry, RingBufferGeometry };
|
222
lib/geometries/ShapeGeometry.js
Normal file
@ -0,0 +1,222 @@
|
||||
/**
|
||||
* @author jonobr1 / http://jonobr1.com
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { ShapeUtils } from '../extras/ShapeUtils.js';
|
||||
|
||||
// ShapeGeometry
|
||||
|
||||
function ShapeGeometry( shapes, curveSegments ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'ShapeGeometry';
|
||||
|
||||
if ( typeof curveSegments === 'object' ) {
|
||||
|
||||
console.warn( 'THREE.ShapeGeometry: Options parameter has been removed.' );
|
||||
|
||||
curveSegments = curveSegments.curveSegments;
|
||||
|
||||
}
|
||||
|
||||
this.parameters = {
|
||||
shapes: shapes,
|
||||
curveSegments: curveSegments
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new ShapeBufferGeometry( shapes, curveSegments ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
ShapeGeometry.prototype = Object.create( Geometry.prototype );
|
||||
ShapeGeometry.prototype.constructor = ShapeGeometry;
|
||||
|
||||
ShapeGeometry.prototype.toJSON = function () {
|
||||
|
||||
var data = Geometry.prototype.toJSON.call( this );
|
||||
|
||||
var shapes = this.parameters.shapes;
|
||||
|
||||
return toJSON( shapes, data );
|
||||
|
||||
};
|
||||
|
||||
// ShapeBufferGeometry
|
||||
|
||||
function ShapeBufferGeometry( shapes, curveSegments ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'ShapeBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
shapes: shapes,
|
||||
curveSegments: curveSegments
|
||||
};
|
||||
|
||||
curveSegments = curveSegments || 12;
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
// helper variables
|
||||
|
||||
var groupStart = 0;
|
||||
var groupCount = 0;
|
||||
|
||||
// allow single and array values for "shapes" parameter
|
||||
|
||||
if ( Array.isArray( shapes ) === false ) {
|
||||
|
||||
addShape( shapes );
|
||||
|
||||
} else {
|
||||
|
||||
for ( var i = 0; i < shapes.length; i ++ ) {
|
||||
|
||||
addShape( shapes[ i ] );
|
||||
|
||||
this.addGroup( groupStart, groupCount, i ); // enables MultiMaterial support
|
||||
|
||||
groupStart += groupCount;
|
||||
groupCount = 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
|
||||
// helper functions
|
||||
|
||||
function addShape( shape ) {
|
||||
|
||||
var i, l, shapeHole;
|
||||
|
||||
var indexOffset = vertices.length / 3;
|
||||
var points = shape.extractPoints( curveSegments );
|
||||
|
||||
var shapeVertices = points.shape;
|
||||
var shapeHoles = points.holes;
|
||||
|
||||
// check direction of vertices
|
||||
|
||||
if ( ShapeUtils.isClockWise( shapeVertices ) === false ) {
|
||||
|
||||
shapeVertices = shapeVertices.reverse();
|
||||
|
||||
// also check if holes are in the opposite direction
|
||||
|
||||
for ( i = 0, l = shapeHoles.length; i < l; i ++ ) {
|
||||
|
||||
shapeHole = shapeHoles[ i ];
|
||||
|
||||
if ( ShapeUtils.isClockWise( shapeHole ) === true ) {
|
||||
|
||||
shapeHoles[ i ] = shapeHole.reverse();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var faces = ShapeUtils.triangulateShape( shapeVertices, shapeHoles );
|
||||
|
||||
// join vertices of inner and outer paths to a single array
|
||||
|
||||
for ( i = 0, l = shapeHoles.length; i < l; i ++ ) {
|
||||
|
||||
shapeHole = shapeHoles[ i ];
|
||||
shapeVertices = shapeVertices.concat( shapeHole );
|
||||
|
||||
}
|
||||
|
||||
// vertices, normals, uvs
|
||||
|
||||
for ( i = 0, l = shapeVertices.length; i < l; i ++ ) {
|
||||
|
||||
var vertex = shapeVertices[ i ];
|
||||
|
||||
vertices.push( vertex.x, vertex.y, 0 );
|
||||
normals.push( 0, 0, 1 );
|
||||
uvs.push( vertex.x, vertex.y ); // world uvs
|
||||
|
||||
}
|
||||
|
||||
// incides
|
||||
|
||||
for ( i = 0, l = faces.length; i < l; i ++ ) {
|
||||
|
||||
var face = faces[ i ];
|
||||
|
||||
var a = face[ 0 ] + indexOffset;
|
||||
var b = face[ 1 ] + indexOffset;
|
||||
var c = face[ 2 ] + indexOffset;
|
||||
|
||||
indices.push( a, b, c );
|
||||
groupCount += 3;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ShapeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
ShapeBufferGeometry.prototype.constructor = ShapeBufferGeometry;
|
||||
|
||||
ShapeBufferGeometry.prototype.toJSON = function () {
|
||||
|
||||
var data = BufferGeometry.prototype.toJSON.call( this );
|
||||
|
||||
var shapes = this.parameters.shapes;
|
||||
|
||||
return toJSON( shapes, data );
|
||||
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
function toJSON( shapes, data ) {
|
||||
|
||||
data.shapes = [];
|
||||
|
||||
if ( Array.isArray( shapes ) ) {
|
||||
|
||||
for ( var i = 0, l = shapes.length; i < l; i ++ ) {
|
||||
|
||||
var shape = shapes[ i ];
|
||||
|
||||
data.shapes.push( shape.uuid );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
data.shapes.push( shapes.uuid );
|
||||
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
export { ShapeGeometry, ShapeBufferGeometry };
|
152
lib/geometries/SphereGeometry.js
Normal file
@ -0,0 +1,152 @@
|
||||
/**
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author benaadams / https://twitter.com/ben_a_adams
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
|
||||
// SphereGeometry
|
||||
|
||||
function SphereGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'SphereGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
widthSegments: widthSegments,
|
||||
heightSegments: heightSegments,
|
||||
phiStart: phiStart,
|
||||
phiLength: phiLength,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
SphereGeometry.prototype = Object.create( Geometry.prototype );
|
||||
SphereGeometry.prototype.constructor = SphereGeometry;
|
||||
|
||||
// SphereBufferGeometry
|
||||
|
||||
function SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'SphereBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
widthSegments: widthSegments,
|
||||
heightSegments: heightSegments,
|
||||
phiStart: phiStart,
|
||||
phiLength: phiLength,
|
||||
thetaStart: thetaStart,
|
||||
thetaLength: thetaLength
|
||||
};
|
||||
|
||||
radius = radius || 1;
|
||||
|
||||
widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
|
||||
heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
|
||||
|
||||
phiStart = phiStart !== undefined ? phiStart : 0;
|
||||
phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;
|
||||
|
||||
thetaStart = thetaStart !== undefined ? thetaStart : 0;
|
||||
thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
|
||||
|
||||
var thetaEnd = thetaStart + thetaLength;
|
||||
|
||||
var ix, iy;
|
||||
|
||||
var index = 0;
|
||||
var grid = [];
|
||||
|
||||
var vertex = new Vector3();
|
||||
var normal = new Vector3();
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
// generate vertices, normals and uvs
|
||||
|
||||
for ( iy = 0; iy <= heightSegments; iy ++ ) {
|
||||
|
||||
var verticesRow = [];
|
||||
|
||||
var v = iy / heightSegments;
|
||||
|
||||
for ( ix = 0; ix <= widthSegments; ix ++ ) {
|
||||
|
||||
var u = ix / widthSegments;
|
||||
|
||||
// vertex
|
||||
|
||||
vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
|
||||
vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
|
||||
vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
|
||||
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
// normal
|
||||
|
||||
normal.set( vertex.x, vertex.y, vertex.z ).normalize();
|
||||
normals.push( normal.x, normal.y, normal.z );
|
||||
|
||||
// uv
|
||||
|
||||
uvs.push( u, 1 - v );
|
||||
|
||||
verticesRow.push( index ++ );
|
||||
|
||||
}
|
||||
|
||||
grid.push( verticesRow );
|
||||
|
||||
}
|
||||
|
||||
// indices
|
||||
|
||||
for ( iy = 0; iy < heightSegments; iy ++ ) {
|
||||
|
||||
for ( ix = 0; ix < widthSegments; ix ++ ) {
|
||||
|
||||
var a = grid[ iy ][ ix + 1 ];
|
||||
var b = grid[ iy ][ ix ];
|
||||
var c = grid[ iy + 1 ][ ix ];
|
||||
var d = grid[ iy + 1 ][ ix + 1 ];
|
||||
|
||||
if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d );
|
||||
if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
}
|
||||
|
||||
SphereBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
SphereBufferGeometry.prototype.constructor = SphereBufferGeometry;
|
||||
|
||||
|
||||
export { SphereGeometry, SphereBufferGeometry };
|
57
lib/geometries/TetrahedronGeometry.js
Normal file
@ -0,0 +1,57 @@
|
||||
/**
|
||||
* @author timothypratley / https://github.com/timothypratley
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { PolyhedronBufferGeometry } from './PolyhedronGeometry.js';
|
||||
|
||||
// TetrahedronGeometry
|
||||
|
||||
function TetrahedronGeometry( radius, detail ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'TetrahedronGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new TetrahedronBufferGeometry( radius, detail ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
TetrahedronGeometry.prototype = Object.create( Geometry.prototype );
|
||||
TetrahedronGeometry.prototype.constructor = TetrahedronGeometry;
|
||||
|
||||
// TetrahedronBufferGeometry
|
||||
|
||||
function TetrahedronBufferGeometry( radius, detail ) {
|
||||
|
||||
var vertices = [
|
||||
1, 1, 1, - 1, - 1, 1, - 1, 1, - 1, 1, - 1, - 1
|
||||
];
|
||||
|
||||
var indices = [
|
||||
2, 1, 0, 0, 3, 2, 1, 3, 0, 2, 3, 1
|
||||
];
|
||||
|
||||
PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
|
||||
|
||||
this.type = 'TetrahedronBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
detail: detail
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
TetrahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
|
||||
TetrahedronBufferGeometry.prototype.constructor = TetrahedronBufferGeometry;
|
||||
|
||||
|
||||
export { TetrahedronGeometry, TetrahedronBufferGeometry };
|
81
lib/geometries/TextGeometry.js
Normal file
@ -0,0 +1,81 @@
|
||||
/**
|
||||
* @author zz85 / http://www.lab4games.net/zz85/blog
|
||||
* @author alteredq / http://alteredqualia.com/
|
||||
*
|
||||
* Text = 3D Text
|
||||
*
|
||||
* parameters = {
|
||||
* font: <THREE.Font>, // font
|
||||
*
|
||||
* size: <float>, // size of the text
|
||||
* height: <float>, // thickness to extrude text
|
||||
* curveSegments: <int>, // number of points on the curves
|
||||
*
|
||||
* bevelEnabled: <bool>, // turn on bevel
|
||||
* bevelThickness: <float>, // how deep into text bevel goes
|
||||
* bevelSize: <float> // how far from text outline is bevel
|
||||
* }
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { ExtrudeBufferGeometry } from './ExtrudeGeometry.js';
|
||||
|
||||
// TextGeometry
|
||||
|
||||
function TextGeometry( text, parameters ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'TextGeometry';
|
||||
|
||||
this.parameters = {
|
||||
text: text,
|
||||
parameters: parameters
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new TextBufferGeometry( text, parameters ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
TextGeometry.prototype = Object.create( Geometry.prototype );
|
||||
TextGeometry.prototype.constructor = TextGeometry;
|
||||
|
||||
// TextBufferGeometry
|
||||
|
||||
function TextBufferGeometry( text, parameters ) {
|
||||
|
||||
parameters = parameters || {};
|
||||
|
||||
var font = parameters.font;
|
||||
|
||||
if ( ! ( font && font.isFont ) ) {
|
||||
|
||||
console.error( 'THREE.TextGeometry: font parameter is not an instance of THREE.Font.' );
|
||||
return new Geometry();
|
||||
|
||||
}
|
||||
|
||||
var shapes = font.generateShapes( text, parameters.size );
|
||||
|
||||
// translate parameters to ExtrudeGeometry API
|
||||
|
||||
parameters.depth = parameters.height !== undefined ? parameters.height : 50;
|
||||
|
||||
// defaults
|
||||
|
||||
if ( parameters.bevelThickness === undefined ) parameters.bevelThickness = 10;
|
||||
if ( parameters.bevelSize === undefined ) parameters.bevelSize = 8;
|
||||
if ( parameters.bevelEnabled === undefined ) parameters.bevelEnabled = false;
|
||||
|
||||
ExtrudeBufferGeometry.call( this, shapes, parameters );
|
||||
|
||||
this.type = 'TextBufferGeometry';
|
||||
|
||||
}
|
||||
|
||||
TextBufferGeometry.prototype = Object.create( ExtrudeBufferGeometry.prototype );
|
||||
TextBufferGeometry.prototype.constructor = TextBufferGeometry;
|
||||
|
||||
|
||||
export { TextGeometry, TextBufferGeometry };
|
142
lib/geometries/TorusGeometry.js
Normal file
@ -0,0 +1,142 @@
|
||||
/**
|
||||
* @author oosmoxiecode
|
||||
* @author mrdoob / http://mrdoob.com/
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
|
||||
// TorusGeometry
|
||||
|
||||
function TorusGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'TorusGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
tube: tube,
|
||||
radialSegments: radialSegments,
|
||||
tubularSegments: tubularSegments,
|
||||
arc: arc
|
||||
};
|
||||
|
||||
this.fromBufferGeometry( new TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
TorusGeometry.prototype = Object.create( Geometry.prototype );
|
||||
TorusGeometry.prototype.constructor = TorusGeometry;
|
||||
|
||||
// TorusBufferGeometry
|
||||
|
||||
function TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'TorusBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
tube: tube,
|
||||
radialSegments: radialSegments,
|
||||
tubularSegments: tubularSegments,
|
||||
arc: arc
|
||||
};
|
||||
|
||||
radius = radius || 1;
|
||||
tube = tube || 0.4;
|
||||
radialSegments = Math.floor( radialSegments ) || 8;
|
||||
tubularSegments = Math.floor( tubularSegments ) || 6;
|
||||
arc = arc || Math.PI * 2;
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
// helper variables
|
||||
|
||||
var center = new Vector3();
|
||||
var vertex = new Vector3();
|
||||
var normal = new Vector3();
|
||||
|
||||
var j, i;
|
||||
|
||||
// generate vertices, normals and uvs
|
||||
|
||||
for ( j = 0; j <= radialSegments; j ++ ) {
|
||||
|
||||
for ( i = 0; i <= tubularSegments; i ++ ) {
|
||||
|
||||
var u = i / tubularSegments * arc;
|
||||
var v = j / radialSegments * Math.PI * 2;
|
||||
|
||||
// vertex
|
||||
|
||||
vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
|
||||
vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
|
||||
vertex.z = tube * Math.sin( v );
|
||||
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
// normal
|
||||
|
||||
center.x = radius * Math.cos( u );
|
||||
center.y = radius * Math.sin( u );
|
||||
normal.subVectors( vertex, center ).normalize();
|
||||
|
||||
normals.push( normal.x, normal.y, normal.z );
|
||||
|
||||
// uv
|
||||
|
||||
uvs.push( i / tubularSegments );
|
||||
uvs.push( j / radialSegments );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// generate indices
|
||||
|
||||
for ( j = 1; j <= radialSegments; j ++ ) {
|
||||
|
||||
for ( i = 1; i <= tubularSegments; i ++ ) {
|
||||
|
||||
// indices
|
||||
|
||||
var a = ( tubularSegments + 1 ) * j + i - 1;
|
||||
var b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
|
||||
var c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
|
||||
var d = ( tubularSegments + 1 ) * j + i;
|
||||
|
||||
// faces
|
||||
|
||||
indices.push( a, b, d );
|
||||
indices.push( b, c, d );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
}
|
||||
|
||||
TorusBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
TorusBufferGeometry.prototype.constructor = TorusBufferGeometry;
|
||||
|
||||
|
||||
export { TorusGeometry, TorusBufferGeometry };
|
194
lib/geometries/TorusKnotGeometry.js
Normal file
@ -0,0 +1,194 @@
|
||||
/**
|
||||
* @author oosmoxiecode
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*
|
||||
* based on http://www.blackpawn.com/texts/pqtorus/
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
|
||||
// TorusKnotGeometry
|
||||
|
||||
function TorusKnotGeometry( radius, tube, tubularSegments, radialSegments, p, q, heightScale ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'TorusKnotGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
tube: tube,
|
||||
tubularSegments: tubularSegments,
|
||||
radialSegments: radialSegments,
|
||||
p: p,
|
||||
q: q
|
||||
};
|
||||
|
||||
if ( heightScale !== undefined ) console.warn( 'THREE.TorusKnotGeometry: heightScale has been deprecated. Use .scale( x, y, z ) instead.' );
|
||||
|
||||
this.fromBufferGeometry( new TorusKnotBufferGeometry( radius, tube, tubularSegments, radialSegments, p, q ) );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
TorusKnotGeometry.prototype = Object.create( Geometry.prototype );
|
||||
TorusKnotGeometry.prototype.constructor = TorusKnotGeometry;
|
||||
|
||||
// TorusKnotBufferGeometry
|
||||
|
||||
function TorusKnotBufferGeometry( radius, tube, tubularSegments, radialSegments, p, q ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'TorusKnotBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
radius: radius,
|
||||
tube: tube,
|
||||
tubularSegments: tubularSegments,
|
||||
radialSegments: radialSegments,
|
||||
p: p,
|
||||
q: q
|
||||
};
|
||||
|
||||
radius = radius || 1;
|
||||
tube = tube || 0.4;
|
||||
tubularSegments = Math.floor( tubularSegments ) || 64;
|
||||
radialSegments = Math.floor( radialSegments ) || 8;
|
||||
p = p || 2;
|
||||
q = q || 3;
|
||||
|
||||
// buffers
|
||||
|
||||
var indices = [];
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
// helper variables
|
||||
|
||||
var i, j;
|
||||
|
||||
var vertex = new Vector3();
|
||||
var normal = new Vector3();
|
||||
|
||||
var P1 = new Vector3();
|
||||
var P2 = new Vector3();
|
||||
|
||||
var B = new Vector3();
|
||||
var T = new Vector3();
|
||||
var N = new Vector3();
|
||||
|
||||
// generate vertices, normals and uvs
|
||||
|
||||
for ( i = 0; i <= tubularSegments; ++ i ) {
|
||||
|
||||
// the radian "u" is used to calculate the position on the torus curve of the current tubular segement
|
||||
|
||||
var u = i / tubularSegments * p * Math.PI * 2;
|
||||
|
||||
// now we calculate two points. P1 is our current position on the curve, P2 is a little farther ahead.
|
||||
// these points are used to create a special "coordinate space", which is necessary to calculate the correct vertex positions
|
||||
|
||||
calculatePositionOnCurve( u, p, q, radius, P1 );
|
||||
calculatePositionOnCurve( u + 0.01, p, q, radius, P2 );
|
||||
|
||||
// calculate orthonormal basis
|
||||
|
||||
T.subVectors( P2, P1 );
|
||||
N.addVectors( P2, P1 );
|
||||
B.crossVectors( T, N );
|
||||
N.crossVectors( B, T );
|
||||
|
||||
// normalize B, N. T can be ignored, we don't use it
|
||||
|
||||
B.normalize();
|
||||
N.normalize();
|
||||
|
||||
for ( j = 0; j <= radialSegments; ++ j ) {
|
||||
|
||||
// now calculate the vertices. they are nothing more than an extrusion of the torus curve.
|
||||
// because we extrude a shape in the xy-plane, there is no need to calculate a z-value.
|
||||
|
||||
var v = j / radialSegments * Math.PI * 2;
|
||||
var cx = - tube * Math.cos( v );
|
||||
var cy = tube * Math.sin( v );
|
||||
|
||||
// now calculate the final vertex position.
|
||||
// first we orient the extrusion with our basis vectos, then we add it to the current position on the curve
|
||||
|
||||
vertex.x = P1.x + ( cx * N.x + cy * B.x );
|
||||
vertex.y = P1.y + ( cx * N.y + cy * B.y );
|
||||
vertex.z = P1.z + ( cx * N.z + cy * B.z );
|
||||
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
// normal (P1 is always the center/origin of the extrusion, thus we can use it to calculate the normal)
|
||||
|
||||
normal.subVectors( vertex, P1 ).normalize();
|
||||
|
||||
normals.push( normal.x, normal.y, normal.z );
|
||||
|
||||
// uv
|
||||
|
||||
uvs.push( i / tubularSegments );
|
||||
uvs.push( j / radialSegments );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// generate indices
|
||||
|
||||
for ( j = 1; j <= tubularSegments; j ++ ) {
|
||||
|
||||
for ( i = 1; i <= radialSegments; i ++ ) {
|
||||
|
||||
// indices
|
||||
|
||||
var a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
|
||||
var b = ( radialSegments + 1 ) * j + ( i - 1 );
|
||||
var c = ( radialSegments + 1 ) * j + i;
|
||||
var d = ( radialSegments + 1 ) * ( j - 1 ) + i;
|
||||
|
||||
// faces
|
||||
|
||||
indices.push( a, b, d );
|
||||
indices.push( b, c, d );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
// this function calculates the current position on the torus curve
|
||||
|
||||
function calculatePositionOnCurve( u, p, q, radius, position ) {
|
||||
|
||||
var cu = Math.cos( u );
|
||||
var su = Math.sin( u );
|
||||
var quOverP = q / p * u;
|
||||
var cs = Math.cos( quOverP );
|
||||
|
||||
position.x = radius * ( 2 + cs ) * 0.5 * cu;
|
||||
position.y = radius * ( 2 + cs ) * su * 0.5;
|
||||
position.z = radius * Math.sin( quOverP ) * 0.5;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TorusKnotBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
TorusKnotBufferGeometry.prototype.constructor = TorusKnotBufferGeometry;
|
||||
|
||||
|
||||
export { TorusKnotGeometry, TorusKnotBufferGeometry };
|
223
lib/geometries/TubeGeometry.js
Normal file
@ -0,0 +1,223 @@
|
||||
/**
|
||||
* @author oosmoxiecode / https://github.com/oosmoxiecode
|
||||
* @author WestLangley / https://github.com/WestLangley
|
||||
* @author zz85 / https://github.com/zz85
|
||||
* @author miningold / https://github.com/miningold
|
||||
* @author jonobr1 / https://github.com/jonobr1
|
||||
* @author Mugen87 / https://github.com/Mugen87
|
||||
*
|
||||
*/
|
||||
|
||||
import { Geometry } from '../core/Geometry.js';
|
||||
import { BufferGeometry } from '../core/BufferGeometry.js';
|
||||
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
||||
import { Vector2 } from '../math/Vector2.js';
|
||||
import { Vector3 } from '../math/Vector3.js';
|
||||
|
||||
// TubeGeometry
|
||||
|
||||
function TubeGeometry( path, tubularSegments, radius, radialSegments, closed, taper ) {
|
||||
|
||||
Geometry.call( this );
|
||||
|
||||
this.type = 'TubeGeometry';
|
||||
|
||||
this.parameters = {
|
||||
path: path,
|
||||
tubularSegments: tubularSegments,
|
||||
radius: radius,
|
||||
radialSegments: radialSegments,
|
||||
closed: closed
|
||||
};
|
||||
|
||||
if ( taper !== undefined ) console.warn( 'THREE.TubeGeometry: taper has been removed.' );
|
||||
|
||||
var bufferGeometry = new TubeBufferGeometry( path, tubularSegments, radius, radialSegments, closed );
|
||||
|
||||
// expose internals
|
||||
|
||||
this.tangents = bufferGeometry.tangents;
|
||||
this.normals = bufferGeometry.normals;
|
||||
this.binormals = bufferGeometry.binormals;
|
||||
|
||||
// create geometry
|
||||
|
||||
this.fromBufferGeometry( bufferGeometry );
|
||||
this.mergeVertices();
|
||||
|
||||
}
|
||||
|
||||
TubeGeometry.prototype = Object.create( Geometry.prototype );
|
||||
TubeGeometry.prototype.constructor = TubeGeometry;
|
||||
|
||||
// TubeBufferGeometry
|
||||
|
||||
function TubeBufferGeometry( path, tubularSegments, radius, radialSegments, closed ) {
|
||||
|
||||
BufferGeometry.call( this );
|
||||
|
||||
this.type = 'TubeBufferGeometry';
|
||||
|
||||
this.parameters = {
|
||||
path: path,
|
||||
tubularSegments: tubularSegments,
|
||||
radius: radius,
|
||||
radialSegments: radialSegments,
|
||||
closed: closed
|
||||
};
|
||||
|
||||
tubularSegments = tubularSegments || 64;
|
||||
radius = radius || 1;
|
||||
radialSegments = radialSegments || 8;
|
||||
closed = closed || false;
|
||||
|
||||
var frames = path.computeFrenetFrames( tubularSegments, closed );
|
||||
|
||||
// expose internals
|
||||
|
||||
this.tangents = frames.tangents;
|
||||
this.normals = frames.normals;
|
||||
this.binormals = frames.binormals;
|
||||
|
||||
// helper variables
|
||||
|
||||
var vertex = new Vector3();
|
||||
var normal = new Vector3();
|
||||
var uv = new Vector2();
|
||||
var P = new Vector3();
|
||||
|
||||
var i, j;
|
||||
|
||||
// buffer
|
||||
|
||||
var vertices = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
var indices = [];
|
||||
|
||||
// create buffer data
|
||||
|
||||
generateBufferData();
|
||||
|
||||
// build geometry
|
||||
|
||||
this.setIndex( indices );
|
||||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
||||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
||||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
||||
|
||||
// functions
|
||||
|
||||
function generateBufferData() {
|
||||
|
||||
for ( i = 0; i < tubularSegments; i ++ ) {
|
||||
|
||||
generateSegment( i );
|
||||
|
||||
}
|
||||
|
||||
// if the geometry is not closed, generate the last row of vertices and normals
|
||||
// at the regular position on the given path
|
||||
//
|
||||
// if the geometry is closed, duplicate the first row of vertices and normals (uvs will differ)
|
||||
|
||||
generateSegment( ( closed === false ) ? tubularSegments : 0 );
|
||||
|
||||
// uvs are generated in a separate function.
|
||||
// this makes it easy compute correct values for closed geometries
|
||||
|
||||
generateUVs();
|
||||
|
||||
// finally create faces
|
||||
|
||||
generateIndices();
|
||||
|
||||
}
|
||||
|
||||
function generateSegment( i ) {
|
||||
|
||||
// we use getPointAt to sample evenly distributed points from the given path
|
||||
|
||||
P = path.getPointAt( i / tubularSegments, P );
|
||||
|
||||
// retrieve corresponding normal and binormal
|
||||
|
||||
var N = frames.normals[ i ];
|
||||
var B = frames.binormals[ i ];
|
||||
|
||||
// generate normals and vertices for the current segment
|
||||
|
||||
for ( j = 0; j <= radialSegments; j ++ ) {
|
||||
|
||||
var v = j / radialSegments * Math.PI * 2;
|
||||
|
||||
var sin = Math.sin( v );
|
||||
var cos = - Math.cos( v );
|
||||
|
||||
// normal
|
||||
|
||||
normal.x = ( cos * N.x + sin * B.x );
|
||||
normal.y = ( cos * N.y + sin * B.y );
|
||||
normal.z = ( cos * N.z + sin * B.z );
|
||||
normal.normalize();
|
||||
|
||||
normals.push( normal.x, normal.y, normal.z );
|
||||
|
||||
// vertex
|
||||
|
||||
vertex.x = P.x + radius * normal.x;
|
||||
vertex.y = P.y + radius * normal.y;
|
||||
vertex.z = P.z + radius * normal.z;
|
||||
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function generateIndices() {
|
||||
|
||||
for ( j = 1; j <= tubularSegments; j ++ ) {
|
||||
|
||||
for ( i = 1; i <= radialSegments; i ++ ) {
|
||||
|
||||
var a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
|
||||
var b = ( radialSegments + 1 ) * j + ( i - 1 );
|
||||
var c = ( radialSegments + 1 ) * j + i;
|
||||
var d = ( radialSegments + 1 ) * ( j - 1 ) + i;
|
||||
|
||||
// faces
|
||||
|
||||
indices.push( a, b, d );
|
||||
indices.push( b, c, d );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function generateUVs() {
|
||||
|
||||
for ( i = 0; i <= tubularSegments; i ++ ) {
|
||||
|
||||
for ( j = 0; j <= radialSegments; j ++ ) {
|
||||
|
||||
uv.x = i / tubularSegments;
|
||||
uv.y = j / radialSegments;
|
||||
|
||||
uvs.push( uv.x, uv.y );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TubeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
||||
TubeBufferGeometry.prototype.constructor = TubeBufferGeometry;
|
||||
|
||||
|
||||
export { TubeGeometry, TubeBufferGeometry };
|