mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Merge branch '3d' of https://github.com/jerrywwl/engine into 3d
This commit is contained in:
@@ -10,5 +10,6 @@ node_modules
|
||||
/package
|
||||
/tools/jsdoc_toolkit/jsdoc_toolkit-2.4.0
|
||||
/.project
|
||||
/dist
|
||||
npm-debug.log
|
||||
DebugInfos.json
|
||||
|
||||
+13
-13
@@ -23,17 +23,17 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import './animation-clip';
|
||||
import './audio-clip';
|
||||
import './dom-audio-clip';
|
||||
import './bitmap-font';
|
||||
import './material';
|
||||
import './mesh';
|
||||
import './opentype-font';
|
||||
import './skeleton';
|
||||
import './sprite';
|
||||
import './web-audio-clip';
|
||||
import './wx-game-audio-clip';
|
||||
export { default as AnimationClip } from "./animation-clip";
|
||||
export { AudioClip } from "./audio-clip";
|
||||
export { DOMAudioClip } from "./dom-audio-clip";
|
||||
export { BitmapFont } from "./bitmap-font";
|
||||
export { EffectAsset } from "./effect-asset";
|
||||
export { Material } from "./material";
|
||||
export { default as Mesh } from "./mesh";
|
||||
export { default as OpentypeFont } from "./opentype-font";
|
||||
export { default as Skeleton } from "./skeleton";
|
||||
export { default as Sprite } from "./sprite";
|
||||
export { default as WebAudioClip } from "./web-audio-clip";
|
||||
export { default as WxGameAudioClip } from "./wx-game-audio-clip";
|
||||
|
||||
import { PhysicsMaterial } from './physics/material';
|
||||
cc.PhysicsMaterial = PhysicsMaterial;
|
||||
export { PhysicsMaterial } from './physics/material';
|
||||
|
||||
@@ -52,3 +52,4 @@ export class PhysicsMaterial extends Asset {
|
||||
return this._cannonMaterial;
|
||||
}
|
||||
}
|
||||
cc.PhysicsMaterial = PhysicsMaterial;
|
||||
|
||||
@@ -2,7 +2,7 @@ import { vec3, color3, mat4 } from '../vmath';
|
||||
import { LinkedArray } from '../memop';
|
||||
import { Node } from '../scene-graph';
|
||||
import gfx from '../gfx';
|
||||
import renderer from '../renderer';
|
||||
import { renderer } from '../renderer';
|
||||
import { sphere, wireframe } from '../primitives';
|
||||
|
||||
let _right = vec3.create(1, 0, 0);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import renderer from '../../renderer';
|
||||
import { renderer } from '../../renderer';
|
||||
import OrbitCamera from './orbit-camera';
|
||||
import DrawMng from './draw-mng';
|
||||
import { createGrid } from './utils';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import renderer from '../renderer';
|
||||
import { renderer } from '../renderer';
|
||||
import { vec3, color3 } from '../vmath';
|
||||
import { Node } from '../scene-graph';
|
||||
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
// @ts-check
|
||||
import renderer from '../../renderer/index';
|
||||
import RenderSystemActor from './renderSystemActor';
|
||||
import { renderer } from '../../renderer/index';
|
||||
import { toRadian } from '../../core/vmath';
|
||||
import { ccclass, menu, property, executeInEditMode } from "../../core/data/class-decorator";
|
||||
import { Color, Enum, Rect } from '../../core/value-types';
|
||||
|
||||
@@ -32,9 +32,22 @@ import SkinningModelComponent from './skinning-model-component';
|
||||
import SkyboxComponent from './skybox-component';
|
||||
import { BoxColliderComponent, SphereColliderComponent } from './physics/collider-component';
|
||||
import { RigidBodyComponent } from './physics/rigid-body-component';
|
||||
import './physics/index';
|
||||
export * from './physics';
|
||||
import ParticleSystemComponent from './particle/particle-system-component';
|
||||
|
||||
export {
|
||||
CameraComponent,
|
||||
AnimationComponent,
|
||||
LightComponent,
|
||||
ModelComponent,
|
||||
SkinningModelComponent,
|
||||
SkyboxComponent,
|
||||
BoxColliderComponent,
|
||||
SphereColliderComponent,
|
||||
RigidBodyComponent,
|
||||
ParticleSystemComponent,
|
||||
};
|
||||
|
||||
cc.CameraComponent = CameraComponent;
|
||||
cc.AnimationComponent = AnimationComponent;
|
||||
cc.LightComponent = LightComponent;
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
// @ts-check
|
||||
import renderer from '../../renderer/index';
|
||||
import RenderSystemActor from './renderSystemActor';
|
||||
import { renderer } from '../../renderer/index';
|
||||
import { Color, Enum } from '../../core/value-types';
|
||||
import { toRadian } from '../../core/vmath';
|
||||
import { ccclass, menu, property, executeInEditMode } from "../../core/data/class-decorator";
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
import { color4, pseudoRandom } from '../../../../core/vmath';
|
||||
import { CCClass } from '../../../../core/data';
|
||||
import GradientRange from './gradient-range';
|
||||
import { property, ccclass } from '../../../../core/data/class-decorator';
|
||||
|
||||
const COLOR_OVERTIME_RAND_OFFSET = 91041;
|
||||
|
||||
@ccclass('cc.ColorOvertimeModule')
|
||||
export default class ColorOvertimeModule {
|
||||
|
||||
@property
|
||||
enable = false;
|
||||
|
||||
color = null;
|
||||
@property({
|
||||
type: GradientRange
|
||||
})
|
||||
color = new GradientRange();
|
||||
|
||||
animate(particle) {
|
||||
if (this.enable) {
|
||||
@@ -16,7 +23,7 @@ export default class ColorOvertimeModule {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.ColorOvertimeModule', ColorOvertimeModule, {
|
||||
enable: false,
|
||||
color: null
|
||||
});
|
||||
// CCClass.fastDefine('cc.ColorOvertimeModule', ColorOvertimeModule, {
|
||||
// enable: false,
|
||||
// color: null
|
||||
// });
|
||||
|
||||
@@ -2,6 +2,7 @@ import { lerp } from '../../../../core/vmath';
|
||||
import { Enum } from '../../../../core/value-types';
|
||||
import { AnimationCurve } from '../../../geom-utils';
|
||||
import { CCClass } from '../../../../core/data';
|
||||
import { property, ccclass } from '../../../../core/data/class-decorator';
|
||||
|
||||
const Mode = Enum({
|
||||
Constant: 0,
|
||||
@@ -10,22 +11,37 @@ const Mode = Enum({
|
||||
TwoConstants: 3
|
||||
});
|
||||
|
||||
@ccclass('cc.CurveRange')
|
||||
export default class CurveRange {
|
||||
|
||||
@property
|
||||
mode = Mode.Constant;
|
||||
|
||||
curve = null;
|
||||
@property({
|
||||
type: AnimationCurve
|
||||
})
|
||||
curve = new AnimationCurve();
|
||||
|
||||
curveMin = null;
|
||||
@property({
|
||||
type: AnimationCurve
|
||||
})
|
||||
curveMin = new AnimationCurve();
|
||||
|
||||
curveMax = null;
|
||||
@property({
|
||||
type: AnimationCurve
|
||||
})
|
||||
curveMax = new AnimationCurve();
|
||||
|
||||
@property
|
||||
constant = 0;
|
||||
|
||||
@property
|
||||
constantMin = 0;
|
||||
|
||||
@property
|
||||
constantMax = 0;
|
||||
|
||||
@property
|
||||
multiplier = 1;
|
||||
|
||||
constructor() {
|
||||
@@ -46,13 +62,13 @@ export default class CurveRange {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.CurveRange', CurveRange, {
|
||||
mode: Mode.Constant,
|
||||
constant: 0,
|
||||
constantMin: 0,
|
||||
constantMax: 0,
|
||||
curve: new AnimationCurve(),
|
||||
curveMin: new AnimationCurve(),
|
||||
curveMax: new AnimationCurve(),
|
||||
multiplier: 1
|
||||
});
|
||||
// CCClass.fastDefine('cc.CurveRange', CurveRange, {
|
||||
// mode: Mode.Constant,
|
||||
// constant: 0,
|
||||
// constantMin: 0,
|
||||
// constantMax: 0,
|
||||
// curve: new AnimationCurve(),
|
||||
// curveMin: new AnimationCurve(),
|
||||
// curveMax: new AnimationCurve(),
|
||||
// multiplier: 1
|
||||
// });
|
||||
|
||||
@@ -2,19 +2,32 @@ import { vec3, quat, pseudoRandom } from '../../../../core/vmath';
|
||||
import { calculateTransform, Space } from '../particle-general-function';
|
||||
import CurveRange from './curve-range';
|
||||
import { CCClass } from '../../../../core/data';
|
||||
import { property, ccclass } from '../../../../core/data/class-decorator';
|
||||
|
||||
const FORCE_OVERTIME_RAND_OFFSET = 212165;
|
||||
|
||||
@ccclass('cc.ForceOvertimeModule')
|
||||
export default class ForceOvertimeModule {
|
||||
|
||||
@property
|
||||
enable = false;
|
||||
|
||||
x = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
x = new CurveRange();
|
||||
|
||||
y = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
y = new CurveRange();
|
||||
|
||||
z = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
z = new CurveRange();
|
||||
|
||||
@property
|
||||
space = Space.Local;
|
||||
|
||||
// TODO:currently not supported
|
||||
@@ -38,11 +51,11 @@ export default class ForceOvertimeModule {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.ForceOvertimeModule',ForceOvertimeModule,{
|
||||
enable : false,
|
||||
x : new CurveRange(),
|
||||
y : new CurveRange(),
|
||||
z : new CurveRange(),
|
||||
space : Space.Local,
|
||||
randomized : false
|
||||
});
|
||||
// CCClass.fastDefine('cc.ForceOvertimeModule',ForceOvertimeModule,{
|
||||
// enable : false,
|
||||
// x : new CurveRange(),
|
||||
// y : new CurveRange(),
|
||||
// z : new CurveRange(),
|
||||
// space : Space.Local,
|
||||
// randomized : false
|
||||
// });
|
||||
|
||||
@@ -2,6 +2,7 @@ import { color4 } from '../../../../core/vmath';
|
||||
import { Enum } from '../../../../core/value-types';
|
||||
import Gradient from './gradient';
|
||||
import { CCClass } from '../../../../core/data';
|
||||
import { property, ccclass } from '../../../../core/data/class-decorator';
|
||||
|
||||
const GRADIENT_MODE_FIX = 0;
|
||||
const GRADIENT_MODE_BLEND = 1;
|
||||
@@ -20,21 +21,35 @@ const Mode = Enum({
|
||||
RandomColor: 4
|
||||
});
|
||||
|
||||
@ccclass('cc.GradientRange')
|
||||
export default class GradientRange {
|
||||
|
||||
@property
|
||||
mode = Mode.Color;
|
||||
|
||||
@property
|
||||
color = cc.Color.WHITE;
|
||||
|
||||
@property
|
||||
colorMin = cc.Color.WHITE;
|
||||
|
||||
@property
|
||||
colorMax = cc.Color.WHITE;
|
||||
|
||||
@property({
|
||||
type: Gradient
|
||||
})
|
||||
gradient = new Gradient();
|
||||
|
||||
gradientMin = null;
|
||||
@property({
|
||||
type: Gradient
|
||||
})
|
||||
gradientMin = new Gradient();
|
||||
|
||||
gradientMax = null;
|
||||
@property({
|
||||
type: Gradient
|
||||
})
|
||||
gradientMax = new Gradient();
|
||||
|
||||
evaluate(time, rndRatio) {
|
||||
switch (this.mode) {
|
||||
@@ -56,15 +71,15 @@ export default class GradientRange {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.GradientRange', GradientRange, {
|
||||
mode: Mode.Color,
|
||||
color: cc.Color.WHITE,
|
||||
colorMin: cc.Color.WHITE,
|
||||
colorMax: cc.Color.WHITE,
|
||||
gradient: new Gradient(),
|
||||
gradientMin: null,
|
||||
gradientMax: null
|
||||
});
|
||||
// CCClass.fastDefine('cc.GradientRange', GradientRange, {
|
||||
// mode: Mode.Color,
|
||||
// color: cc.Color.WHITE,
|
||||
// colorMin: cc.Color.WHITE,
|
||||
// colorMax: cc.Color.WHITE,
|
||||
// gradient: new Gradient(),
|
||||
// gradientMin: null,
|
||||
// gradientMax: null
|
||||
// });
|
||||
|
||||
export class GradientUniform {
|
||||
constructor(gr) {
|
||||
|
||||
@@ -1,46 +1,56 @@
|
||||
import { repeat } from '../../../../core/vmath';
|
||||
import { Enum, ValueType } from '../../../../core/value-types';
|
||||
import { CCClass } from '../../../../core/data';
|
||||
import { property, ccclass } from '../../../../core/data/class-decorator';
|
||||
|
||||
const Mode = Enum({
|
||||
Blend: 0,
|
||||
Fixed: 1
|
||||
});
|
||||
|
||||
@ccclass('cc.ColorKey')
|
||||
export class ColorKey {
|
||||
|
||||
@property
|
||||
color = cc.Color.WHITE;
|
||||
|
||||
@property
|
||||
time = 0;
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.ColorKey', ColorKey, {
|
||||
color: cc.Color.WHITE,
|
||||
time: 0
|
||||
});
|
||||
// CCClass.fastDefine('cc.ColorKey', ColorKey, {
|
||||
// color: cc.Color.WHITE,
|
||||
// time: 0
|
||||
// });
|
||||
|
||||
@ccclass('cc.AlphaKey')
|
||||
export class AlphaKey {
|
||||
|
||||
@property
|
||||
alpha = 1;
|
||||
|
||||
@property
|
||||
time = 0;
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.AlphaKey', AlphaKey, {
|
||||
alpha: 1,
|
||||
time: 0
|
||||
});
|
||||
// CCClass.fastDefine('cc.AlphaKey', AlphaKey, {
|
||||
// alpha: 1,
|
||||
// time: 0
|
||||
// });
|
||||
|
||||
export default class Gradient extends ValueType {
|
||||
@ccclass('cc.Gradient')
|
||||
export default class Gradient {
|
||||
|
||||
@property
|
||||
colorKeys = [];
|
||||
|
||||
@property
|
||||
alphaKeys = []
|
||||
|
||||
@property
|
||||
mode = Mode.Blend;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._color = cc.Color.WHITE;
|
||||
}
|
||||
|
||||
@@ -125,8 +135,8 @@ export default class Gradient extends ValueType {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.Gradient', Gradient, {
|
||||
mode: Mode.Blend,
|
||||
colorKeys: [],
|
||||
alphaKeys: []
|
||||
});
|
||||
// CCClass.fastDefine('cc.Gradient', Gradient, {
|
||||
// mode: Mode.Blend,
|
||||
// colorKeys: [],
|
||||
// alphaKeys: []
|
||||
// });
|
||||
|
||||
@@ -1,25 +1,43 @@
|
||||
import { vec3, lerp, pseudoRandom } from '../../../../core/vmath';
|
||||
import { CCClass } from '../../../../core/data';
|
||||
import { Space } from '../particle-general-function';
|
||||
import { property, ccclass } from '../../../../core/data/class-decorator';
|
||||
import CurveRange from './curve-range';
|
||||
|
||||
const LIMIT_VELOCITY_RAND_OFFSET = 23541;
|
||||
|
||||
@ccclass('cc.LimitVelocityOvertimeModule')
|
||||
export default class LimitVelocityOvertimeModule {
|
||||
|
||||
enable = false;
|
||||
|
||||
limitX = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
limitX = new CurveRange();
|
||||
|
||||
limitY = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
limitY = new CurveRange();
|
||||
|
||||
limitZ = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
limitZ = new CurveRange();
|
||||
|
||||
limit = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
limit = new CurveRange();
|
||||
|
||||
dampen = null;
|
||||
@property
|
||||
dampen = 0;
|
||||
|
||||
@property
|
||||
separateAxes = false;
|
||||
|
||||
@property
|
||||
space = Space.Local;
|
||||
|
||||
// TODO:functions related to drag are temporarily not supported
|
||||
@@ -59,17 +77,17 @@ function dampenBeyondLimit(vel, limit, dampen) {
|
||||
return abs * sgn;
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.LimitVelocityOvertimeModule',LimitVelocityOvertimeModule,{
|
||||
enable : false,
|
||||
limitX : null,
|
||||
limitY : null,
|
||||
limitZ : null,
|
||||
limit : null,
|
||||
dampen : null,
|
||||
separateAxes : false,
|
||||
space : Space.Local,
|
||||
// TODO:functions related to drag are temporarily not supported
|
||||
drag : null,
|
||||
multiplyDragByParticleSize : false,
|
||||
multiplyDragByParticleVelocity : false
|
||||
});
|
||||
// CCClass.fastDefine('cc.LimitVelocityOvertimeModule', LimitVelocityOvertimeModule, {
|
||||
// enable: false,
|
||||
// limitX: null,
|
||||
// limitY: null,
|
||||
// limitZ: null,
|
||||
// limit: null,
|
||||
// dampen: null,
|
||||
// separateAxes: false,
|
||||
// space: Space.Local,
|
||||
// // TODO:functions related to drag are temporarily not supported
|
||||
// drag: null,
|
||||
// multiplyDragByParticleSize: false,
|
||||
// multiplyDragByParticleVelocity: false
|
||||
// });
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
import { pseudoRandom } from "../../../../core/vmath";
|
||||
import { CCClass } from "../../../../core/data";
|
||||
import CurveRange from "./curve-range";
|
||||
import { property, ccclass } from "../../../../core/data/class-decorator";
|
||||
|
||||
const ROTATION_OVERTIME_RAND_OFFSET = 125292;
|
||||
|
||||
@ccclass('cc.RotationOvertimeModule')
|
||||
export default class RotationOvertimeModule {
|
||||
|
||||
@property
|
||||
enable = false;
|
||||
|
||||
@property
|
||||
_separateAxes = false;
|
||||
|
||||
get separateAxes() {
|
||||
@@ -23,11 +27,20 @@ export default class RotationOvertimeModule {
|
||||
}
|
||||
}
|
||||
|
||||
x = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
x = new CurveRange();
|
||||
|
||||
y = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
y = new CurveRange();
|
||||
|
||||
z = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
z = new CurveRange();
|
||||
|
||||
constructor() {
|
||||
|
||||
@@ -44,10 +57,10 @@ export default class RotationOvertimeModule {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.RotationOvertimeModule',RotationOvertimeModule,{
|
||||
enable : false,
|
||||
_separateAxes : false,
|
||||
x : new CurveRange(),
|
||||
y : new CurveRange(),
|
||||
z : new CurveRange()
|
||||
});
|
||||
// CCClass.fastDefine('cc.RotationOvertimeModule', RotationOvertimeModule, {
|
||||
// enable: false,
|
||||
// _separateAxes: false,
|
||||
// x: new CurveRange(),
|
||||
// y: new CurveRange(),
|
||||
// z: new CurveRange()
|
||||
// });
|
||||
|
||||
@@ -1,22 +1,38 @@
|
||||
import { vec3, pseudoRandom } from '../../../../core/vmath';
|
||||
import CurveRange from './curve-range';
|
||||
import { CCClass } from '../../../../core/data';
|
||||
import { property, ccclass } from '../../../../core/data/class-decorator';
|
||||
|
||||
const SIZE_OVERTIME_RAND_OFFSET = 39825;
|
||||
|
||||
@ccclass('cc.SizeOvertimeModule')
|
||||
export default class SizeOvertimeModule {
|
||||
|
||||
@property
|
||||
enable = false;
|
||||
|
||||
@property
|
||||
separateAxes = false;
|
||||
|
||||
size = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
size = new CurveRange();
|
||||
|
||||
x = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
x = new CurveRange();
|
||||
|
||||
y = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
y = new CurveRange();
|
||||
|
||||
z = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
z = new CurveRange();
|
||||
|
||||
animate(particle) {
|
||||
if (!this.separateAxes) {
|
||||
@@ -25,11 +41,11 @@ export default class SizeOvertimeModule {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.SizeOvertimeModule', SizeOvertimeModule, {
|
||||
enable: false,
|
||||
separateAxes: false,
|
||||
size: new CurveRange(),
|
||||
x: new CurveRange(),
|
||||
y: new CurveRange(),
|
||||
z: new CurveRange()
|
||||
});
|
||||
// CCClass.fastDefine('cc.SizeOvertimeModule', SizeOvertimeModule, {
|
||||
// enable: false,
|
||||
// separateAxes: false,
|
||||
// size: new CurveRange(),
|
||||
// x: new CurveRange(),
|
||||
// y: new CurveRange(),
|
||||
// z: new CurveRange()
|
||||
// });
|
||||
|
||||
@@ -2,6 +2,7 @@ import { repeat, lerp, pseudoRandom } from "../../../../core/vmath";
|
||||
import { Enum } from "../../../../core/value-types";
|
||||
import CurveRange from "./curve-range";
|
||||
import { CCClass } from "../../../../core/data";
|
||||
import { property, ccclass } from "../../../../core/data/class-decorator";
|
||||
|
||||
const TEXTURE_ANIMATION_RAND_OFFSET = 90794;
|
||||
|
||||
@@ -15,8 +16,10 @@ const Animation = Enum({
|
||||
SingleRow: 1
|
||||
});
|
||||
|
||||
@ccclass('cc.TextureAnimationModule')
|
||||
export default class TextureAnimationModule {
|
||||
|
||||
@property
|
||||
_enable = false;
|
||||
|
||||
get enable() {
|
||||
@@ -28,6 +31,7 @@ export default class TextureAnimationModule {
|
||||
this.ps.renderer._updateMaterialParams();
|
||||
}
|
||||
|
||||
@property
|
||||
_mode = Mode.Grid
|
||||
|
||||
get mode() {
|
||||
@@ -41,18 +45,29 @@ export default class TextureAnimationModule {
|
||||
}
|
||||
}
|
||||
|
||||
@property
|
||||
numTilesX = 0;
|
||||
|
||||
@property
|
||||
numTilesY = 0;
|
||||
|
||||
@property
|
||||
animation = Animation.WholeSheet;
|
||||
|
||||
frameOverTime = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
frameOverTime = new CurveRange();
|
||||
|
||||
startFrame = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
startFrame = new CurveRange();
|
||||
|
||||
@property
|
||||
cycleCount = 0;
|
||||
|
||||
@property
|
||||
_flipU = 0;
|
||||
|
||||
get flipU() {
|
||||
@@ -63,6 +78,7 @@ export default class TextureAnimationModule {
|
||||
console.error("particle texture animation's flipU is not supported!");
|
||||
}
|
||||
|
||||
@property
|
||||
_flipV = 0;
|
||||
|
||||
get flipV() {
|
||||
@@ -73,6 +89,7 @@ export default class TextureAnimationModule {
|
||||
console.error("particle texture animation's flipV is not supported!");
|
||||
}
|
||||
|
||||
@property
|
||||
_uvChannelMask = -1
|
||||
|
||||
get uvChannelMask() {
|
||||
@@ -83,8 +100,10 @@ export default class TextureAnimationModule {
|
||||
console.error("particle texture animation's uvChannelMask is not supported!");
|
||||
}
|
||||
|
||||
@property
|
||||
randomRow = false;
|
||||
|
||||
@property
|
||||
rowIndex = 0;
|
||||
|
||||
onInit(ps) {
|
||||
@@ -115,18 +134,18 @@ export default class TextureAnimationModule {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.TextureAnimationModule', TextureAnimationModule, {
|
||||
_enable: false,
|
||||
_mode: Mode.Grid,
|
||||
numTilesX: 0,
|
||||
numTilesY: 0,
|
||||
animation: Animation.WholeSheet,
|
||||
frameOverTime: new CurveRange(),
|
||||
startFrame: new CurveRange(),
|
||||
cycleCount: 0,
|
||||
_flipU: 0,
|
||||
_flipV: 0,
|
||||
_uvChannelMask: -1,
|
||||
randomRow: false,
|
||||
rowIndex: 0
|
||||
});
|
||||
// CCClass.fastDefine('cc.TextureAnimationModule', TextureAnimationModule, {
|
||||
// _enable: false,
|
||||
// _mode: Mode.Grid,
|
||||
// numTilesX: 0,
|
||||
// numTilesY: 0,
|
||||
// animation: Animation.WholeSheet,
|
||||
// frameOverTime: new CurveRange(),
|
||||
// startFrame: new CurveRange(),
|
||||
// cycleCount: 0,
|
||||
// _flipU: 0,
|
||||
// _flipV: 0,
|
||||
// _uvChannelMask: -1,
|
||||
// randomRow: false,
|
||||
// rowIndex: 0
|
||||
// });
|
||||
|
||||
@@ -2,21 +2,37 @@ import { vec3, quat, pseudoRandom } from '../../../../core/vmath';
|
||||
import { calculateTransform, Space } from '../particle-general-function';
|
||||
import CurveRange from './curve-range';
|
||||
import { CCClass } from '../../../../core/data';
|
||||
import { property, ccclass } from '../../../../core/data/class-decorator';
|
||||
|
||||
const VELOCITY_OVERTIME_RAND_OFFSET = 197866;
|
||||
|
||||
@ccclass('cc.VelocityOvertimeModule')
|
||||
export default class VelocityOvertimeModule {
|
||||
|
||||
@property
|
||||
enable = false;
|
||||
|
||||
x = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
x = new CurveRange();
|
||||
|
||||
y = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
y = new CurveRange();
|
||||
|
||||
z = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
z = new CurveRange();
|
||||
|
||||
speedModifier = null;
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
speedModifier = new CurveRange();
|
||||
|
||||
@property
|
||||
space = Space.Local;
|
||||
|
||||
constructor() {
|
||||
@@ -40,11 +56,11 @@ export default class VelocityOvertimeModule {
|
||||
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.VelocityOvertimeModule', VelocityOvertimeModule, {
|
||||
enable: false,
|
||||
x: new CurveRange(),
|
||||
y: new CurveRange(),
|
||||
z: new CurveRange(),
|
||||
speedModifier: new CurveRange(),
|
||||
space: Space.Local
|
||||
});
|
||||
// CCClass.fastDefine('cc.VelocityOvertimeModule', VelocityOvertimeModule, {
|
||||
// enable: false,
|
||||
// x: new CurveRange(),
|
||||
// y: new CurveRange(),
|
||||
// z: new CurveRange(),
|
||||
// speedModifier: new CurveRange(),
|
||||
// space: Space.Local
|
||||
// });
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { repeat } from "../../../core/vmath";
|
||||
import CurveRange from "./animator/curve-range";
|
||||
import { CCClass } from "../../../core/data";
|
||||
import { property, ccclass } from "../../../core/data/class-decorator";
|
||||
|
||||
|
||||
@ccclass('cc.Burst')
|
||||
export default class Burst {
|
||||
|
||||
@property
|
||||
_time = 0;
|
||||
|
||||
get time() {
|
||||
@@ -16,10 +18,13 @@ export default class Burst {
|
||||
this._curTime = val;
|
||||
}
|
||||
|
||||
@property
|
||||
minCount = 30;
|
||||
|
||||
@property
|
||||
maxCount = 30;
|
||||
|
||||
@property
|
||||
_repeatCount = 1;
|
||||
|
||||
get repeatCount() {
|
||||
@@ -31,8 +36,12 @@ export default class Burst {
|
||||
this._remainingCount = val;
|
||||
}
|
||||
|
||||
@property
|
||||
repeatInterval = 1;
|
||||
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
count = null;
|
||||
|
||||
constructor() {
|
||||
@@ -58,11 +67,11 @@ export default class Burst {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.Burst', Burst, {
|
||||
_time: 0,
|
||||
minCount: 30,
|
||||
maxCount: 30,
|
||||
_repeatCount: 1,
|
||||
repeatInterval: 1,
|
||||
count: new CurveRange
|
||||
});
|
||||
// CCClass.fastDefine('cc.Burst', Burst, {
|
||||
// _time: 0,
|
||||
// minCount: 30,
|
||||
// maxCount: 30,
|
||||
// _repeatCount: 1,
|
||||
// repeatInterval: 1,
|
||||
// count: new CurveRange
|
||||
// });
|
||||
|
||||
@@ -3,6 +3,7 @@ import { vec3, vec2, random, randomRange, mat4, quat, clamp, repeat, pingPong }
|
||||
import { Enum, Vec3 } from '../../../../core/value-types';
|
||||
import CurveRange from '../animator/curve-range';
|
||||
import { CCClass } from '../../../../core/data';
|
||||
import { property, ccclass } from '../../../../core/data/class-decorator';
|
||||
|
||||
let _intermediVec = vec3.create(0, 0, 0);
|
||||
let _intermediArr = new Array();
|
||||
@@ -29,14 +30,19 @@ const ArcMode = Enum({
|
||||
PingPong: 2
|
||||
});
|
||||
|
||||
@ccclass('cc.ShapeModule')
|
||||
export default class ShapeModule {
|
||||
|
||||
@property
|
||||
enable = false;
|
||||
|
||||
@property
|
||||
shapeType = ShapeType.Box;
|
||||
|
||||
@property
|
||||
emitFrom = EmitLocation.Base;
|
||||
|
||||
@property
|
||||
_position = new Vec3(0, 0, 0);
|
||||
|
||||
get position() {
|
||||
@@ -47,7 +53,7 @@ export default class ShapeModule {
|
||||
this.constructMat();
|
||||
}
|
||||
|
||||
|
||||
@property
|
||||
_rotation = new Vec3(0, 0, 0);
|
||||
|
||||
get rotation() {
|
||||
@@ -58,7 +64,7 @@ export default class ShapeModule {
|
||||
this.constructMat();
|
||||
}
|
||||
|
||||
|
||||
@property
|
||||
_scale = new Vec3(0, 0, 0);
|
||||
|
||||
get scale() {
|
||||
@@ -69,30 +75,45 @@ export default class ShapeModule {
|
||||
this.constructMat();
|
||||
}
|
||||
|
||||
@property
|
||||
alignToDirection = false;
|
||||
|
||||
@property
|
||||
randomDirectionAmount = 0;
|
||||
|
||||
@property
|
||||
sphericalDirectionAmount = 0;
|
||||
|
||||
@property
|
||||
randomPositionAmount = 0;
|
||||
|
||||
@property
|
||||
radius = 0;
|
||||
|
||||
@property
|
||||
radiusThickness = 1;
|
||||
|
||||
@property
|
||||
arc = 0;
|
||||
|
||||
@property
|
||||
arcMode = ArcMode.Random;
|
||||
|
||||
@property
|
||||
arcSpread = 0;
|
||||
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
arcSpeed = new CurveRange();
|
||||
|
||||
@property
|
||||
angle = 0;
|
||||
|
||||
@property
|
||||
length = 0;
|
||||
|
||||
@property
|
||||
boxThickness = new Vec3(0, 0, 0);
|
||||
|
||||
constructor() {
|
||||
@@ -284,24 +305,24 @@ function applyBoxThickness(pos, thickness) {
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.ShapeModule', ShapeModule, {
|
||||
enable: false,
|
||||
shapeType: ShapeType.Box,
|
||||
emitFrom: EmitLocation.Base,
|
||||
_position: new Vec3(0, 0, 0),
|
||||
_rotation: new Vec3(0, 0, 0),
|
||||
_scale: new Vec3(0, 0, 0),
|
||||
alignToDirection: false,
|
||||
randomDirectionAmount: 0,
|
||||
sphericalDirectionAmount: 0,
|
||||
randomPositionAmount: 0,
|
||||
radius: 0,
|
||||
radiusThickness: 1,
|
||||
arc: 0,
|
||||
arcMode: ArcMode.Random,
|
||||
arcSpread: 0,
|
||||
arcSpeed: null,
|
||||
angle: 0,
|
||||
length: 0,
|
||||
boxThickness: new Vec3(0, 0, 0)
|
||||
});
|
||||
// CCClass.fastDefine('cc.ShapeModule', ShapeModule, {
|
||||
// enable: false,
|
||||
// shapeType: ShapeType.Box,
|
||||
// emitFrom: EmitLocation.Base,
|
||||
// _position: new Vec3(0, 0, 0),
|
||||
// _rotation: new Vec3(0, 0, 0),
|
||||
// _scale: new Vec3(0, 0, 0),
|
||||
// alignToDirection: false,
|
||||
// randomDirectionAmount: 0,
|
||||
// sphericalDirectionAmount: 0,
|
||||
// randomPositionAmount: 0,
|
||||
// radius: 0,
|
||||
// radiusThickness: 1,
|
||||
// arc: 0,
|
||||
// arcMode: ArcMode.Random,
|
||||
// arcSpread: 0,
|
||||
// arcSpeed: null,
|
||||
// angle: 0,
|
||||
// length: 0,
|
||||
// boxThickness: new Vec3(0, 0, 0)
|
||||
// });
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
import { Component } from '../../../components/component';
|
||||
import { vec3, vec2, mat4, quat, randomRangeInt, pseudoRandom } from '../../../core/vmath';
|
||||
import registry from '../../misc/registry';
|
||||
import Gradient, { ColorKey, AlphaKey } from './animator/gradient';
|
||||
import CurveRange from './animator/curve-range';
|
||||
import GradientRange from './animator/gradient-range';
|
||||
import ParticleSystemRenderer from './renderer/particle-system-renderer';
|
||||
@@ -18,29 +16,12 @@ import ShapeModule from './emitter/shape-module';
|
||||
import Burst from './burst';
|
||||
import { particleEmitZAxis, Space } from './particle-general-function';
|
||||
import { INT_MAX } from '../../../core/vmath/bits';
|
||||
import ParticleSystemGpuRenderer from './renderer/particle-system-gpu-renderer';
|
||||
import { ccclass, executionOrder, executeInEditMode, property, requireComponent } from '../../../core/data/class-decorator';
|
||||
|
||||
registry.registerClass('Burst', Burst);
|
||||
registry.registerClass('ColorKey', ColorKey);
|
||||
registry.registerClass('AlphaKey', AlphaKey);
|
||||
registry.registerClass('Gradient', Gradient);
|
||||
registry.registerClass('GradientRange', GradientRange);
|
||||
registry.registerClass('CurveRange', CurveRange);
|
||||
registry.registerClass('SizeOvertimeModule', SizeOvertimeModule);
|
||||
registry.registerClass('ColorOverLifetimeModule', ColorOverLifetimeModule);
|
||||
registry.registerClass('ParticleSystemRenderer', ParticleSystemRenderer);
|
||||
registry.registerClass('ParticleSystemGpuRenderer', ParticleSystemGpuRenderer);
|
||||
registry.registerClass('VelocityOvertimeModule', VelocityOvertimeModule);
|
||||
registry.registerClass('ForceOvertimeModule', ForceOvertimeModule);
|
||||
registry.registerClass('LimitVelocityOvertimeModule', LimitVelocityOvertimeModule);
|
||||
registry.registerClass('RotationOvertimeModule', RotationOvertimeModule);
|
||||
registry.registerClass('TextureAnimationModule', TextureAnimationModule);
|
||||
registry.registerClass('ShapeModule', ShapeModule);
|
||||
import { ccclass, executionOrder, executeInEditMode, property, requireComponent, menu } from '../../../core/data/class-decorator';
|
||||
|
||||
let _world_mat = mat4.create();
|
||||
|
||||
@ccclass('cc.ParticleSystemComponent')
|
||||
@menu('Components/ParticleSystemComponent')
|
||||
@requireComponent(ParticleSystemRenderer)
|
||||
@executionOrder(99)
|
||||
@executeInEditMode
|
||||
@@ -49,33 +30,35 @@ export default class ParticleSystemComponent extends Component {
|
||||
@property
|
||||
capacity = 2000;
|
||||
|
||||
@property
|
||||
startColor = null;
|
||||
@property({
|
||||
type:GradientRange
|
||||
})
|
||||
startColor = new GradientRange();
|
||||
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
startSize = null;
|
||||
startSize = new CurveRange();
|
||||
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
startSpeed = null;
|
||||
startSpeed = new CurveRange();
|
||||
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
startRotation = null;
|
||||
startRotation = new CurveRange();
|
||||
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
startDelay = null;
|
||||
startDelay = new CurveRange();
|
||||
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
startLifetime = null;
|
||||
startLifetime = new CurveRange();
|
||||
|
||||
@property
|
||||
duration = 5.0;
|
||||
@@ -126,35 +109,35 @@ export default class ParticleSystemComponent extends Component {
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
gravityModifier = null;
|
||||
gravityModifier = new CurveRange();
|
||||
|
||||
// emission module
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
rateOverTime = null;
|
||||
rateOverTime = new CurveRange();
|
||||
|
||||
@property({
|
||||
type: CurveRange
|
||||
})
|
||||
rateOverDistance = null;
|
||||
rateOverDistance = new CurveRange();
|
||||
|
||||
@property({
|
||||
type: [Burst]
|
||||
})
|
||||
bursts = null;
|
||||
bursts = new Array();
|
||||
|
||||
// color over lifetime module
|
||||
@property({
|
||||
type: ColorOverLifetimeModule
|
||||
})
|
||||
colorOverLifetimeModule = null;
|
||||
colorOverLifetimeModule = new ColorOverLifetimeModule();
|
||||
|
||||
// shpae module
|
||||
@property({
|
||||
type: ShapeModule
|
||||
})
|
||||
shapeModule = null;
|
||||
shapeModule = new ShapeModule();
|
||||
|
||||
// particle system renderer
|
||||
@property({
|
||||
@@ -166,32 +149,32 @@ export default class ParticleSystemComponent extends Component {
|
||||
@property({
|
||||
type: SizeOvertimeModule
|
||||
})
|
||||
sizeOvertimeModule = null;
|
||||
sizeOvertimeModule = new SizeOvertimeModule();
|
||||
|
||||
@property({
|
||||
type: VelocityOvertimeModule
|
||||
})
|
||||
velocityOvertimeModule = null;
|
||||
velocityOvertimeModule = new VelocityOvertimeModule();
|
||||
|
||||
@property({
|
||||
type: ForceOvertimeModule
|
||||
})
|
||||
forceOvertimeModule = null;
|
||||
forceOvertimeModule = new ForceOvertimeModule();
|
||||
|
||||
@property({
|
||||
type: LimitVelocityOvertimeModule
|
||||
})
|
||||
limitVelocityOvertimeModule = null;
|
||||
limitVelocityOvertimeModule = new LimitVelocityOvertimeModule();
|
||||
|
||||
@property({
|
||||
type: RotationOvertimeModule
|
||||
})
|
||||
rotationOvertimeModule = null;
|
||||
rotationOvertimeModule = new RotationOvertimeModule();
|
||||
|
||||
@property({
|
||||
type: TextureAnimationModule
|
||||
})
|
||||
textureAnimationModule = null;
|
||||
textureAnimationModule = new TextureAnimationModule();
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
@@ -216,6 +199,7 @@ export default class ParticleSystemComponent extends Component {
|
||||
|
||||
onLoad() {
|
||||
// HACK, TODO
|
||||
this.renderer = this.getComponent(ParticleSystemRenderer);
|
||||
this.renderer.onInit();
|
||||
this.shapeModule.onInit(this);
|
||||
this.textureAnimationModule.onInit(this);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import renderer from '../../../../renderer';
|
||||
import { renderer } from '../../../../renderer';
|
||||
import { vec3, vec4, vec2, mat4 } from '../../../../core/vmath';
|
||||
import gfx from '../../../../renderer/gfx';
|
||||
import { Material } from '../../../assets/material';
|
||||
@@ -109,10 +109,12 @@ export default class ParticleSystemRenderer extends RenderableComponent {
|
||||
}
|
||||
this._updateMaterialParams();
|
||||
this._updateModel();
|
||||
this.onEnable();
|
||||
}
|
||||
|
||||
onEnable() {
|
||||
this._scene.addModel(this._model);
|
||||
if (this._model != null)
|
||||
this._scene.addModel(this._model);
|
||||
}
|
||||
|
||||
onDisable() {
|
||||
|
||||
@@ -52,6 +52,7 @@ export class PhysicsBody {
|
||||
|
||||
public bind(node: Node) {
|
||||
this._node = node;
|
||||
this.pullTransform();
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
@@ -59,6 +60,8 @@ export class PhysicsBody {
|
||||
}
|
||||
|
||||
public _onAdded() {
|
||||
this.pullTransform();
|
||||
|
||||
this._onWorldBeforeStepListener = this._onWorldBeforeStep.bind(this);
|
||||
this._cannonBody.world.addEventListener('beforeStep', this._onWorldBeforeStepListener);
|
||||
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { PhysicsBoxShape, PhysicsSphereShape, PhysicsBody } from './body';
|
||||
import { ContactMaterial } from './contact-material';
|
||||
import './physics-system';
|
||||
import './world';
|
||||
export {
|
||||
PhysicsBoxShape,
|
||||
PhysicsSphereShape,
|
||||
PhysicsBody,
|
||||
ContactMaterial,
|
||||
};
|
||||
export * from './physics-system';
|
||||
export * from './world';
|
||||
|
||||
// @ts-ignore
|
||||
cc.PhysicsBody = PhysicsBody;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import CANNON from 'cannon';
|
||||
import { Vec3 } from '../../../core/value-types';
|
||||
import { PhysicsMaterial } from '../../assets/physics/material';
|
||||
import { DataFlow, PhysicsBody, PhysicsShape } from './body';
|
||||
import { ContactMaterial } from './contact-material';
|
||||
import { getWrap, setWrap, toCannonOptions } from './util';
|
||||
import { Vec3 } from '../../../core/value-types';
|
||||
|
||||
export interface RaycastOptions {
|
||||
collisionFilterMask?: number;
|
||||
@@ -46,8 +46,10 @@ export class PhysicsWorld {
|
||||
|
||||
constructor() {
|
||||
this._defaultMaterial = new PhysicsMaterial();
|
||||
// @ts-ignore
|
||||
this._defaultContactMaterial = new ContactMaterial(this._defaultMaterial, { friction: 0.3, restitution: 0.0 });
|
||||
const mtl = new PhysicsMaterial();
|
||||
mtl.friction = 0.3;
|
||||
mtl.restitution = 0;
|
||||
this._defaultContactMaterial = new ContactMaterial(this._defaultMaterial, mtl);
|
||||
|
||||
this._cannonWorld = new CANNON.World();
|
||||
setWrap<PhysicsWorld>(this._cannonWorld, this);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import render from '../../renderer';
|
||||
import { renderer } from '../../renderer';
|
||||
export default class RenderSystem {
|
||||
constructor () {
|
||||
this._scene = new render.Scene();
|
||||
this._scene = new renderer.Scene();
|
||||
}
|
||||
|
||||
init () {
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
// @ts-check
|
||||
import { mat4 } from '../../core/vmath';
|
||||
import ModelComponent from './model-component';
|
||||
import renderer from '../../renderer/index';
|
||||
import { renderer } from '../../renderer/index';
|
||||
import { ccclass, property, executionOrder, menu,executeInEditMode } from '../../core/data/class-decorator';
|
||||
import utils from '../misc/utils';
|
||||
import Skeleton from '../assets/skeleton';
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
// @ts-check
|
||||
import renderer from '../../renderer/index';
|
||||
import { renderer } from '../../renderer/index';
|
||||
import { box } from '../primitive/index';
|
||||
import { Material } from '../assets/material';
|
||||
import { ccclass, property, menu, executeInEditMode } from '../../core/data/class-decorator';
|
||||
|
||||
+10
-5
@@ -23,15 +23,20 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import './assets';
|
||||
import './builtin';
|
||||
// import './debugger';
|
||||
import './framework';
|
||||
import './memop';
|
||||
export * from './assets';
|
||||
export * from './builtin';
|
||||
export * from './framework';
|
||||
export * from './memop';
|
||||
import utils from './misc/utils';
|
||||
import * as primitives from './primitive';
|
||||
import * as geometry from './geom-utils';
|
||||
|
||||
export {
|
||||
utils,
|
||||
primitives,
|
||||
geometry,
|
||||
};
|
||||
|
||||
cc.geometry = geometry;
|
||||
cc.primitives = primitives;
|
||||
cc.utils = utils;
|
||||
@@ -1,4 +1,4 @@
|
||||
import renderer from '../../renderer';
|
||||
import { renderer } from '../../renderer';
|
||||
import find from '../../scene-graph/find';
|
||||
import Mesh from '../assets/mesh';
|
||||
import gfx from '../../renderer/gfx/index';
|
||||
|
||||
+2
-1
@@ -29,7 +29,8 @@ import inputManager from './platform/event-manager/CCInputManager';
|
||||
import debug from './platform/CCDebug';
|
||||
import { addon } from './utils/js';
|
||||
|
||||
import renderer from '../renderer';
|
||||
import { renderer } from '../renderer';
|
||||
import gfx from '../renderer/gfx';
|
||||
import builtinResMgr from '../3d/builtin/init';
|
||||
import { ProgramLib } from '../renderer/core/program-lib';
|
||||
|
||||
|
||||
+11
-6
@@ -28,11 +28,16 @@ import * as vmath from './vmath';
|
||||
|
||||
cc.vmath = vmath;
|
||||
|
||||
import './utils';
|
||||
import './data';
|
||||
import './event';
|
||||
import './platform';
|
||||
import './value-types';
|
||||
export * from './utils';
|
||||
export * from './data';
|
||||
export * from './event';
|
||||
export * from './platform';
|
||||
export * from './value-types';
|
||||
// Cause error:
|
||||
// export * from './game';
|
||||
import './game';
|
||||
import './scheduler';
|
||||
export * from './scheduler';
|
||||
// Cause error:
|
||||
// export * from './director';
|
||||
import './director';
|
||||
export { vmath };
|
||||
|
||||
@@ -24,9 +24,14 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import './js';
|
||||
import './path';
|
||||
import './prefab-helper';
|
||||
import * as js from './js';
|
||||
import * as path from './path';
|
||||
|
||||
export const js = cc.js;
|
||||
export const path = cc.path;
|
||||
export * from './prefab-helper';
|
||||
export {
|
||||
js,
|
||||
path,
|
||||
};
|
||||
|
||||
// export const js = cc.js;
|
||||
// export const path = cc.path;
|
||||
@@ -1,307 +0,0 @@
|
||||
/**
|
||||
* Bit twiddling hacks for JavaScript.
|
||||
*
|
||||
* Author: Mikola Lysenko
|
||||
*
|
||||
* Ported from Stanford bit twiddling hack library:
|
||||
* http://graphics.stanford.edu/~seander/bithacks.html
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
// Number of bits in an integer
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export const INT_BITS = 32;
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export const INT_MAX = 0x7fffffff;
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export const INT_MIN = -1<<(INT_BITS-1);
|
||||
|
||||
/**
|
||||
* Returns -1, 0, +1 depending on sign of x
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function sign(v) {
|
||||
return (v > 0) - (v < 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes absolute value of integer
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function abs(v) {
|
||||
let mask = v >> (INT_BITS-1);
|
||||
return (v ^ mask) - mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes minimum of integers x and y
|
||||
*
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @returns {number}
|
||||
*/
|
||||
export function min(x, y) {
|
||||
return y ^ ((x ^ y) & -(x < y));
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes maximum of integers x and y
|
||||
*
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @returns {number}
|
||||
*/
|
||||
export function max(x, y) {
|
||||
return x ^ ((x ^ y) & -(x < y));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a number is a power of two
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isPow2(v) {
|
||||
return !(v & (v-1)) && (!!v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes log base 2 of v
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function log2(v) {
|
||||
let r, shift;
|
||||
r = (v > 0xFFFF) << 4; v >>>= r;
|
||||
shift = (v > 0xFF ) << 3; v >>>= shift; r |= shift;
|
||||
shift = (v > 0xF ) << 2; v >>>= shift; r |= shift;
|
||||
shift = (v > 0x3 ) << 1; v >>>= shift; r |= shift;
|
||||
return r | (v >> 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes log base 10 of v
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function log10(v) {
|
||||
return (v >= 1000000000) ? 9 : (v >= 100000000) ? 8 : (v >= 10000000) ? 7 :
|
||||
(v >= 1000000) ? 6 : (v >= 100000) ? 5 : (v >= 10000) ? 4 :
|
||||
(v >= 1000) ? 3 : (v >= 100) ? 2 : (v >= 10) ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts number of bits
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function popCount(v) {
|
||||
v = v - ((v >>> 1) & 0x55555555);
|
||||
v = (v & 0x33333333) + ((v >>> 2) & 0x33333333);
|
||||
return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts number of trailing zeros
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function countTrailingZeros(v) {
|
||||
let c = 32;
|
||||
v &= -v;
|
||||
if (v) c--;
|
||||
if (v & 0x0000FFFF) c -= 16;
|
||||
if (v & 0x00FF00FF) c -= 8;
|
||||
if (v & 0x0F0F0F0F) c -= 4;
|
||||
if (v & 0x33333333) c -= 2;
|
||||
if (v & 0x55555555) c -= 1;
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds to next power of 2
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function nextPow2(v) {
|
||||
v += v === 0;
|
||||
--v;
|
||||
v |= v >>> 1;
|
||||
v |= v >>> 2;
|
||||
v |= v >>> 4;
|
||||
v |= v >>> 8;
|
||||
v |= v >>> 16;
|
||||
return v + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds down to previous power of 2
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function prevPow2(v) {
|
||||
v |= v >>> 1;
|
||||
v |= v >>> 2;
|
||||
v |= v >>> 4;
|
||||
v |= v >>> 8;
|
||||
v |= v >>> 16;
|
||||
return v - (v>>>1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes parity of word
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function parity(v) {
|
||||
v ^= v >>> 16;
|
||||
v ^= v >>> 8;
|
||||
v ^= v >>> 4;
|
||||
v &= 0xf;
|
||||
return (0x6996 >>> v) & 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
const REVERSE_TABLE = new Array(256);
|
||||
|
||||
(function(tab) {
|
||||
for(let i=0; i<256; ++i) {
|
||||
let v = i, r = i, s = 7;
|
||||
for (v >>>= 1; v; v >>>= 1) {
|
||||
r <<= 1;
|
||||
r |= v & 1;
|
||||
--s;
|
||||
}
|
||||
tab[i] = (r << s) & 0xff;
|
||||
}
|
||||
})(REVERSE_TABLE);
|
||||
|
||||
/**
|
||||
* Reverse bits in a 32 bit word
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function reverse(v) {
|
||||
return (REVERSE_TABLE[v & 0xff] << 24) |
|
||||
(REVERSE_TABLE[(v >>> 8) & 0xff] << 16) |
|
||||
(REVERSE_TABLE[(v >>> 16) & 0xff] << 8) |
|
||||
REVERSE_TABLE[(v >>> 24) & 0xff];
|
||||
}
|
||||
|
||||
/**
|
||||
* Interleave bits of 2 coordinates with 16 bits. Useful for fast quadtree codes
|
||||
*
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @returns {number}
|
||||
*/
|
||||
export function interleave2(x, y) {
|
||||
x &= 0xFFFF;
|
||||
x = (x | (x << 8)) & 0x00FF00FF;
|
||||
x = (x | (x << 4)) & 0x0F0F0F0F;
|
||||
x = (x | (x << 2)) & 0x33333333;
|
||||
x = (x | (x << 1)) & 0x55555555;
|
||||
|
||||
y &= 0xFFFF;
|
||||
y = (y | (y << 8)) & 0x00FF00FF;
|
||||
y = (y | (y << 4)) & 0x0F0F0F0F;
|
||||
y = (y | (y << 2)) & 0x33333333;
|
||||
y = (y | (y << 1)) & 0x55555555;
|
||||
|
||||
return x | (y << 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the nth interleaved component
|
||||
*
|
||||
* @param {number} v
|
||||
* @param {number} n
|
||||
* @returns {number}
|
||||
*/
|
||||
export function deinterleave2(v, n) {
|
||||
v = (v >>> n) & 0x55555555;
|
||||
v = (v | (v >>> 1)) & 0x33333333;
|
||||
v = (v | (v >>> 2)) & 0x0F0F0F0F;
|
||||
v = (v | (v >>> 4)) & 0x00FF00FF;
|
||||
v = (v | (v >>> 16)) & 0x000FFFF;
|
||||
return (v << 16) >> 16;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interleave bits of 3 coordinates, each with 10 bits. Useful for fast octree codes
|
||||
*
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @param {number} z
|
||||
* @returns {number}
|
||||
*/
|
||||
export function interleave3(x, y, z) {
|
||||
x &= 0x3FF;
|
||||
x = (x | (x<<16)) & 4278190335;
|
||||
x = (x | (x<<8)) & 251719695;
|
||||
x = (x | (x<<4)) & 3272356035;
|
||||
x = (x | (x<<2)) & 1227133513;
|
||||
|
||||
y &= 0x3FF;
|
||||
y = (y | (y<<16)) & 4278190335;
|
||||
y = (y | (y<<8)) & 251719695;
|
||||
y = (y | (y<<4)) & 3272356035;
|
||||
y = (y | (y<<2)) & 1227133513;
|
||||
x |= (y << 1);
|
||||
|
||||
z &= 0x3FF;
|
||||
z = (z | (z<<16)) & 4278190335;
|
||||
z = (z | (z<<8)) & 251719695;
|
||||
z = (z | (z<<4)) & 3272356035;
|
||||
z = (z | (z<<2)) & 1227133513;
|
||||
|
||||
return x | (z << 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts nth interleaved component of a 3-tuple
|
||||
*
|
||||
* @param {number} v
|
||||
* @param {number} n
|
||||
* @returns {number}
|
||||
*/
|
||||
export function deinterleave3(v, n) {
|
||||
v = (v >>> n) & 1227133513;
|
||||
v = (v | (v>>>2)) & 3272356035;
|
||||
v = (v | (v>>>4)) & 251719695;
|
||||
v = (v | (v>>>8)) & 4278190335;
|
||||
v = (v | (v>>>16)) & 0x3FF;
|
||||
return (v<<22)>>22;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes next combination in colexicographic order (this is mistakenly called nextPermutation on the bit twiddling hacks page)
|
||||
*
|
||||
* @param {number} v
|
||||
* @returns {number}
|
||||
*/
|
||||
export function nextCombination(v) {
|
||||
let t = v | (v - 1);
|
||||
return (t + 1) | (((~t & -~t) - 1) >>> (countTrailingZeros(v) + 1));
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
/**
|
||||
* Bit twiddling hacks for JavaScript.
|
||||
*
|
||||
* Author: Mikola Lysenko
|
||||
*
|
||||
* Ported from Stanford bit twiddling hack library:
|
||||
* http://graphics.stanford.edu/~seander/bithacks.html
|
||||
*/
|
||||
|
||||
/* tslint:disable:no-bitwise */
|
||||
|
||||
// Number of bits in an integer
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export const INT_BITS = 32;
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export const INT_MAX = 0x7fffffff;
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export const INT_MIN = -1 << (INT_BITS - 1);
|
||||
|
||||
/**
|
||||
* Returns -1, 0, +1 depending on sign of x.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function sign (v: number) {
|
||||
return ((v > 0) as unknown as number) - ((v < 0) as unknown as number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes absolute value of integer.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function abs (v: number) {
|
||||
const mask = v >> (INT_BITS - 1);
|
||||
return (v ^ mask) - mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes minimum of integers x and y.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
export function min (x: number, y: number) {
|
||||
return y ^ ((x ^ y) & -(x < y));
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes maximum of integers x and y.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
export function max (x: number, y: number) {
|
||||
return x ^ ((x ^ y) & -(x < y));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a number is a power of two.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function isPow2 (v: number) {
|
||||
return !(v & (v - 1)) && (!!v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes log base 2 of v.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function log2 (v: number) {
|
||||
let r: number;
|
||||
let shift: number;
|
||||
r = ((v > 0xFFFF) as unknown as number) << 4; v >>>= r;
|
||||
shift = ((v > 0xFF) as unknown as number) << 3; v >>>= shift; r |= shift;
|
||||
shift = ((v > 0xF) as unknown as number) << 2; v >>>= shift; r |= shift;
|
||||
shift = ((v > 0x3) as unknown as number) << 1; v >>>= shift; r |= shift;
|
||||
return r | (v >> 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes log base 10 of v.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function log10 (v: number) {
|
||||
return (v >= 1000000000) ? 9 : (v >= 100000000) ? 8 : (v >= 10000000) ? 7 :
|
||||
(v >= 1000000) ? 6 : (v >= 100000) ? 5 : (v >= 10000) ? 4 :
|
||||
(v >= 1000) ? 3 : (v >= 100) ? 2 : (v >= 10) ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts number of bits.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function popCount (v: number) {
|
||||
v = v - ((v >>> 1) & 0x55555555);
|
||||
v = (v & 0x33333333) + ((v >>> 2) & 0x33333333);
|
||||
return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts number of trailing zeros.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function countTrailingZeros (v: number) {
|
||||
let c = 32;
|
||||
v &= -v;
|
||||
if (v) { c--; }
|
||||
if (v & 0x0000FFFF) { c -= 16; }
|
||||
if (v & 0x00FF00FF) { c -= 8; }
|
||||
if (v & 0x0F0F0F0F) { c -= 4; }
|
||||
if (v & 0x33333333) { c -= 2; }
|
||||
if (v & 0x55555555) { c -= 1; }
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds to next power of 2.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function nextPow2 (v: number) {
|
||||
v += ((v === 0) as unknown as number);
|
||||
--v;
|
||||
v |= v >>> 1;
|
||||
v |= v >>> 2;
|
||||
v |= v >>> 4;
|
||||
v |= v >>> 8;
|
||||
v |= v >>> 16;
|
||||
return v + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds down to previous power of 2.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function prevPow2 (v: number) {
|
||||
v |= v >>> 1;
|
||||
v |= v >>> 2;
|
||||
v |= v >>> 4;
|
||||
v |= v >>> 8;
|
||||
v |= v >>> 16;
|
||||
return v - (v >>> 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes parity of word.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function parity (v: number) {
|
||||
v ^= v >>> 16;
|
||||
v ^= v >>> 8;
|
||||
v ^= v >>> 4;
|
||||
v &= 0xf;
|
||||
return (0x6996 >>> v) & 1;
|
||||
}
|
||||
|
||||
const REVERSE_TABLE: number[] = new Array(256);
|
||||
|
||||
((tab: number[]) => {
|
||||
for (let i = 0; i < 256; ++i) {
|
||||
let v = i;
|
||||
let r = i;
|
||||
let s = 7;
|
||||
for (v >>>= 1; v; v >>>= 1) {
|
||||
r <<= 1;
|
||||
r |= v & 1;
|
||||
--s;
|
||||
}
|
||||
tab[i] = (r << s) & 0xff;
|
||||
}
|
||||
})(REVERSE_TABLE);
|
||||
|
||||
/**
|
||||
* Reverse bits in a 32 bit word.
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function reverse (v: number) {
|
||||
return (REVERSE_TABLE[v & 0xff] << 24) |
|
||||
(REVERSE_TABLE[(v >>> 8) & 0xff] << 16) |
|
||||
(REVERSE_TABLE[(v >>> 16) & 0xff] << 8) |
|
||||
REVERSE_TABLE[(v >>> 24) & 0xff];
|
||||
}
|
||||
|
||||
/**
|
||||
* Interleave bits of 2 coordinates with 16 bits. Useful for fast quadtree codes.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
export function interleave2 (x: number, y: number) {
|
||||
x &= 0xFFFF;
|
||||
x = (x | (x << 8)) & 0x00FF00FF;
|
||||
x = (x | (x << 4)) & 0x0F0F0F0F;
|
||||
x = (x | (x << 2)) & 0x33333333;
|
||||
x = (x | (x << 1)) & 0x55555555;
|
||||
|
||||
y &= 0xFFFF;
|
||||
y = (y | (y << 8)) & 0x00FF00FF;
|
||||
y = (y | (y << 4)) & 0x0F0F0F0F;
|
||||
y = (y | (y << 2)) & 0x33333333;
|
||||
y = (y | (y << 1)) & 0x55555555;
|
||||
|
||||
return x | (y << 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the nth interleaved component.
|
||||
*
|
||||
* @param v
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
export function deinterleave2 (v: number, n: number) {
|
||||
v = (v >>> n) & 0x55555555;
|
||||
v = (v | (v >>> 1)) & 0x33333333;
|
||||
v = (v | (v >>> 2)) & 0x0F0F0F0F;
|
||||
v = (v | (v >>> 4)) & 0x00FF00FF;
|
||||
v = (v | (v >>> 16)) & 0x000FFFF;
|
||||
return (v << 16) >> 16;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interleave bits of 3 coordinates, each with 10 bits. Useful for fast octree codes.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return
|
||||
*/
|
||||
export function interleave3 (x: number, y: number, z: number) {
|
||||
x &= 0x3FF;
|
||||
x = (x | (x << 16)) & 4278190335;
|
||||
x = (x | (x << 8)) & 251719695;
|
||||
x = (x | (x << 4)) & 3272356035;
|
||||
x = (x | (x << 2)) & 1227133513;
|
||||
|
||||
y &= 0x3FF;
|
||||
y = (y | (y << 16)) & 4278190335;
|
||||
y = (y | (y << 8)) & 251719695;
|
||||
y = (y | (y << 4)) & 3272356035;
|
||||
y = (y | (y << 2)) & 1227133513;
|
||||
x |= (y << 1);
|
||||
|
||||
z &= 0x3FF;
|
||||
z = (z | (z << 16)) & 4278190335;
|
||||
z = (z | (z << 8)) & 251719695;
|
||||
z = (z | (z << 4)) & 3272356035;
|
||||
z = (z | (z << 2)) & 1227133513;
|
||||
|
||||
return x | (z << 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts nth interleaved component of a 3-tuple.
|
||||
*
|
||||
* @param v
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
export function deinterleave3 (v: number, n: number) {
|
||||
v = (v >>> n) & 1227133513;
|
||||
v = (v | (v >>> 2)) & 3272356035;
|
||||
v = (v | (v >>> 4)) & 251719695;
|
||||
v = (v | (v >>> 8)) & 4278190335;
|
||||
v = (v | (v >>> 16)) & 0x3FF;
|
||||
return (v << 22) >> 22;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes next combination in colexicographic order (this is
|
||||
* mistakenly called nextPermutation on the bit twiddling hacks page).
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
export function nextCombination (v: number) {
|
||||
const t = v | (v - 1);
|
||||
return (t + 1) | (((~t & -~t) - 1) >>> (countTrailingZeros(v) + 1));
|
||||
}
|
||||
@@ -1,283 +0,0 @@
|
||||
import { EPSILON } from './utils';
|
||||
|
||||
/**
|
||||
* Represents a color with red(r), green(g), blue(b) component of that color.
|
||||
*/
|
||||
class color3 {
|
||||
/**
|
||||
* Creates a color, with components specified separately.
|
||||
*
|
||||
* @param {number} r - Value assigned to r component.
|
||||
* @param {number} g - Value assigned to g component.
|
||||
* @param {number} b - Value assigned to b component.
|
||||
*/
|
||||
constructor(r = 1, g = 1, b = 1) {
|
||||
/**
|
||||
* The r component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.r = r;
|
||||
|
||||
/**
|
||||
* The g component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.g = g;
|
||||
|
||||
/**
|
||||
* The b component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a color, with components specified separately, or a black color if not specified
|
||||
*
|
||||
* @param {number} r - Value assigned to r component.
|
||||
* @param {number} g - Value assigned to g component.
|
||||
* @param {number} b - Value assigned to b component.
|
||||
* @return {color3} The newly created color.
|
||||
*/
|
||||
static create(r = 1, g = 1, b = 1) {
|
||||
return new color3(r, g, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a color.
|
||||
*
|
||||
* @param {color3} a - Color to clone.
|
||||
* @returns {color3} The newly created color.
|
||||
*/
|
||||
static clone(a) {
|
||||
return new color3(a.r, a.g, a.b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a color into another.
|
||||
*
|
||||
* @param {color3} out - The color to modified.
|
||||
* @param {color3} a - The specified color.
|
||||
* @returns {color3} out.
|
||||
*/
|
||||
static copy(out, a) {
|
||||
out.r = a.r;
|
||||
out.g = a.g;
|
||||
out.b = a.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the components of a color to the given values.
|
||||
*
|
||||
* @param {color3} out - The color to modified.
|
||||
* @param {number} r - Value assigned to r component.
|
||||
* @param {number} g - Value assigned to g component.
|
||||
* @param {number} b - Value assigned to b component.
|
||||
* @returns {color3} out.
|
||||
*/
|
||||
static set(out, r, g, b) {
|
||||
out.r = r;
|
||||
out.g = g;
|
||||
out.b = b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the hexadecimal formal color into rgb formal.
|
||||
*
|
||||
* @param {color3} out - Color to store result.
|
||||
* @param {Number} hex - The color's hexadecimal formal.
|
||||
* @returns {color3} out.
|
||||
* @function
|
||||
*/
|
||||
static fromHex(out, hex) {
|
||||
let r = ((hex >> 16)) / 255.0;
|
||||
let g = ((hex >> 8) & 0xff) / 255.0;
|
||||
let b = ((hex) & 0xff) / 255.0;
|
||||
|
||||
out.r = r;
|
||||
out.g = g;
|
||||
out.b = b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add components of two colors, respectively.
|
||||
*
|
||||
* @param {color3} out - Color to store result.
|
||||
* @param {color3} a - The first operand.
|
||||
* @param {color3} b - The second operand.
|
||||
* @returns {color3} out.
|
||||
*/
|
||||
static add(out, a, b) {
|
||||
out.r = a.r + b.r;
|
||||
out.g = a.g + b.g;
|
||||
out.b = a.b + b.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract components of color b from components of color a, respectively.
|
||||
*
|
||||
* @param {color3} out - Color to store result.
|
||||
* @param {color3} a - The a.
|
||||
* @param {color3} b - The b.
|
||||
* @returns {color3} out.
|
||||
*/
|
||||
static subtract(out, a, b) {
|
||||
out.r = a.r - b.r;
|
||||
out.g = a.g - b.g;
|
||||
out.b = a.b - b.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color3.subtract}.
|
||||
*/
|
||||
static sub(out, a, b) {
|
||||
return color3.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply components of two colors, respectively.
|
||||
*
|
||||
* @param {color3} out - Color to store result.
|
||||
* @param {color3} a - The first operand.
|
||||
* @param {color3} b - The second operand.
|
||||
* @returns {color3} out.
|
||||
*/
|
||||
static multiply(out, a, b) {
|
||||
out.r = a.r * b.r;
|
||||
out.g = a.g * b.g;
|
||||
out.b = a.b * b.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color3.multiply}.
|
||||
*/
|
||||
static mul(out, a, b) {
|
||||
return color3.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide components of color a by components of color b, respectively.
|
||||
*
|
||||
* @param {color3} out - Color to store result.
|
||||
* @param {color3} a - The first operand.
|
||||
* @param {color3} b - The second operand.
|
||||
* @returns {color3} out.
|
||||
*/
|
||||
static divide(out, a, b) {
|
||||
out.r = a.r / b.r;
|
||||
out.g = a.g / b.g;
|
||||
out.b = a.b / b.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color3.divide}.
|
||||
*/
|
||||
static div(out, a, b) {
|
||||
return color3.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a color by a number.
|
||||
*
|
||||
* @param {color3} out - Color to store result.
|
||||
* @param {color3} a - Color to scale.
|
||||
* @param {number} b - The scale number.
|
||||
* @returns {color3} out.
|
||||
*/
|
||||
static scale(out, a, b) {
|
||||
out.r = a.r * b;
|
||||
out.g = a.g * b;
|
||||
out.b = a.b * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two colors.
|
||||
*
|
||||
* @param {color3} out - Color to store result.
|
||||
* @param {color3} a - The first operand.
|
||||
* @param {color3} b - The second operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {color3} out.
|
||||
*/
|
||||
static lerp(out, a, b, t) {
|
||||
let ar = a.r,
|
||||
ag = a.g,
|
||||
ab = a.b;
|
||||
out.r = ar + t * (b.r - ar);
|
||||
out.g = ag + t * (b.g - ag);
|
||||
out.b = ab + t * (b.b - ab);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a color.
|
||||
*
|
||||
* @param {color3} a - The color.
|
||||
* @returns {String} - String representation of this color.
|
||||
*/
|
||||
static str(a) {
|
||||
return `color3(${a.r}, ${a.g}, ${a.b})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a color into array.
|
||||
*
|
||||
* @param {Array} out - Array to store result.
|
||||
* @param {color3} a - The color.
|
||||
* @returns {Array} out.
|
||||
*/
|
||||
static array(out, a) {
|
||||
let scale = a instanceof cc.Color ? 1 / 255 : 1;
|
||||
out[0] = a.r * scale;
|
||||
out[1] = a.g * scale;
|
||||
out[2] = a.b * scale;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified colors are equal. (Compared using ===)
|
||||
*
|
||||
* @param {color3} a - The first color.
|
||||
* @param {color3} b - The second color.
|
||||
* @returns {Boolean} True if the colors are equal, false otherwise.
|
||||
*/
|
||||
static exactEquals(a, b) {
|
||||
return a.r === b.r && a.g === b.g && a.b === b.b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified colors are approximately equal.
|
||||
*
|
||||
* @param {color3} a - The first color.
|
||||
* @param {color3} b - The second color.
|
||||
* @returns {Boolean} True if the colors are approximately equal, false otherwise.
|
||||
*/
|
||||
static equals(a, b) {
|
||||
let a0 = a.r, a1 = a.g, a2 = a.b;
|
||||
let b0 = b.r, b1 = b.g, b2 = b.b;
|
||||
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a color's rgb formal into the hexadecimal one.
|
||||
*
|
||||
* @param {color3} a - The color.
|
||||
* @returns {Number} - The color's hexadecimal formal.
|
||||
*/
|
||||
static hex(a) {
|
||||
return (a.r * 255) << 16 | (a.g * 255) << 8 | (a.b * 255);
|
||||
}
|
||||
}
|
||||
|
||||
export default color3;
|
||||
@@ -0,0 +1,283 @@
|
||||
import { EPSILON } from './utils';
|
||||
|
||||
/**
|
||||
* Represents a color with red(r), green(g), blue(b) component of that color.
|
||||
*/
|
||||
class color3 {
|
||||
|
||||
/**
|
||||
* Creates a color, with components specified separately, or a black color if not specified
|
||||
*
|
||||
* @param r - Value assigned to r component.
|
||||
* @param g - Value assigned to g component.
|
||||
* @param b - Value assigned to b component.
|
||||
* @return The newly created color.
|
||||
*/
|
||||
public static create (r = 1, g = 1, b = 1) {
|
||||
return new color3(r, g, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a color.
|
||||
*
|
||||
* @param a - Color to clone.
|
||||
* @return The newly created color.
|
||||
*/
|
||||
public static clone (a) {
|
||||
return new color3(a.r, a.g, a.b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a color into another.
|
||||
*
|
||||
* @param out - The color to modified.
|
||||
* @param a - The specified color.
|
||||
* @return out.
|
||||
*/
|
||||
public static copy (out, a) {
|
||||
out.r = a.r;
|
||||
out.g = a.g;
|
||||
out.b = a.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the components of a color to the given values.
|
||||
*
|
||||
* @param out - The color to modified.
|
||||
* @param r - Value assigned to r component.
|
||||
* @param g - Value assigned to g component.
|
||||
* @param b - Value assigned to b component.
|
||||
* @return out.
|
||||
*/
|
||||
public static set (out, r, g, b) {
|
||||
out.r = r;
|
||||
out.g = g;
|
||||
out.b = b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the hexadecimal formal color into rgb formal.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param hex - The color's hexadecimal formal.
|
||||
* @return out.
|
||||
* @function
|
||||
*/
|
||||
public static fromHex (out, hex) {
|
||||
const r = ((hex >> 16)) / 255.0;
|
||||
const g = ((hex >> 8) & 0xff) / 255.0;
|
||||
const b = ((hex) & 0xff) / 255.0;
|
||||
|
||||
out.r = r;
|
||||
out.g = g;
|
||||
out.b = b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add components of two colors, respectively.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static add (out, a, b) {
|
||||
out.r = a.r + b.r;
|
||||
out.g = a.g + b.g;
|
||||
out.b = a.b + b.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract components of color b from components of color a, respectively.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The a.
|
||||
* @param b - The b.
|
||||
* @return out.
|
||||
*/
|
||||
public static subtract (out, a, b) {
|
||||
out.r = a.r - b.r;
|
||||
out.g = a.g - b.g;
|
||||
out.b = a.b - b.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color3.subtract}.
|
||||
*/
|
||||
public static sub (out, a, b) {
|
||||
return color3.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply components of two colors, respectively.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiply (out, a, b) {
|
||||
out.r = a.r * b.r;
|
||||
out.g = a.g * b.g;
|
||||
out.b = a.b * b.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color3.multiply}.
|
||||
*/
|
||||
public static mul (out, a, b) {
|
||||
return color3.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide components of color a by components of color b, respectively.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static divide (out, a, b) {
|
||||
out.r = a.r / b.r;
|
||||
out.g = a.g / b.g;
|
||||
out.b = a.b / b.b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color3.divide}.
|
||||
*/
|
||||
public static div (out, a, b) {
|
||||
return color3.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a color by a number.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - Color to scale.
|
||||
* @param b - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static scale (out, a, b) {
|
||||
out.r = a.r * b;
|
||||
out.g = a.g * b;
|
||||
out.b = a.b * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two colors.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out.
|
||||
*/
|
||||
public static lerp (out, a, b, t) {
|
||||
const ar = a.r,
|
||||
ag = a.g,
|
||||
ab = a.b;
|
||||
out.r = ar + t * (b.r - ar);
|
||||
out.g = ag + t * (b.g - ag);
|
||||
out.b = ab + t * (b.b - ab);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a color.
|
||||
*
|
||||
* @param a - The color.
|
||||
* @return - String representation of this color.
|
||||
*/
|
||||
public static str (a) {
|
||||
return `color3(${a.r}, ${a.g}, ${a.b})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a color into array.
|
||||
*
|
||||
* @param out - Array to store result.
|
||||
* @param a - The color.
|
||||
* @return out.
|
||||
*/
|
||||
public static array (out, a) {
|
||||
const scale = a instanceof cc.Color ? 1 / 255 : 1;
|
||||
out[0] = a.r * scale;
|
||||
out[1] = a.g * scale;
|
||||
out[2] = a.b * scale;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified colors are equal. (Compared using ===)
|
||||
*
|
||||
* @param a - The first color.
|
||||
* @param b - The second color.
|
||||
* @return True if the colors are equal, false otherwise.
|
||||
*/
|
||||
public static exactEquals (a, b) {
|
||||
return a.r === b.r && a.g === b.g && a.b === b.b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified colors are approximately equal.
|
||||
*
|
||||
* @param a - The first color.
|
||||
* @param b - The second color.
|
||||
* @return True if the colors are approximately equal, false otherwise.
|
||||
*/
|
||||
public static equals (a, b) {
|
||||
const a0 = a.r, a1 = a.g, a2 = a.b;
|
||||
const b0 = b.r, b1 = b.g, b2 = b.b;
|
||||
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a color's rgb formal into the hexadecimal one.
|
||||
*
|
||||
* @param a - The color.
|
||||
* @return - The color's hexadecimal formal.
|
||||
*/
|
||||
public static hex (a) {
|
||||
return (a.r * 255) << 16 | (a.g * 255) << 8 | (a.b * 255);
|
||||
}
|
||||
/**
|
||||
* Creates a color, with components specified separately.
|
||||
*
|
||||
* @param r - Value assigned to r component.
|
||||
* @param g - Value assigned to g component.
|
||||
* @param b - Value assigned to b component.
|
||||
*/
|
||||
constructor (r = 1, g = 1, b = 1) {
|
||||
/**
|
||||
* The r component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.r = r;
|
||||
|
||||
/**
|
||||
* The g component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.g = g;
|
||||
|
||||
/**
|
||||
* The b component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
|
||||
export default color3;
|
||||
@@ -1,306 +0,0 @@
|
||||
import { EPSILON } from './utils';
|
||||
|
||||
/**
|
||||
* Represents a color with red(r), green(g), blue(b) component of that color and
|
||||
* and an extra alpha(a) component indicating how opaque this color is.
|
||||
*/
|
||||
class color4 {
|
||||
/**
|
||||
* Creates a color, with components specified separately.
|
||||
*
|
||||
* @param {number} r - Value assigned to r component.
|
||||
* @param {number} g - Value assigned to g component.
|
||||
* @param {number} b - Value assigned to b component.
|
||||
* @param {number} a - Value assigned to a component.
|
||||
*/
|
||||
constructor(r = 1, g = 1, b = 1, a = 1) {
|
||||
/**
|
||||
* The r component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.r = r;
|
||||
|
||||
/**
|
||||
* The g component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.g = g;
|
||||
|
||||
/**
|
||||
* The b component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.b = b;
|
||||
|
||||
/**
|
||||
* The a component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a white color, or components specified separately.
|
||||
*
|
||||
* @param {number} r - Value assigned to r component.
|
||||
* @param {number} g - Value assigned to g component.
|
||||
* @param {number} b - Value assigned to b component.
|
||||
* @param {number} a - Value assigned to a component.
|
||||
* @return {color4} The newly created color.
|
||||
*/
|
||||
static create(r = 1, g = 1, b = 1, a = 1) {
|
||||
return new color4(r, g, b, a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a color.
|
||||
*
|
||||
* @param {color4} a - Color to clone.
|
||||
* @returns {color4} The newly created color.
|
||||
*/
|
||||
static clone(a) {
|
||||
return new color4(a.r, a.g, a.b, a.a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a color into another.
|
||||
*
|
||||
* @param {color4} out - The color to modified.
|
||||
* @param {color4} a - The specified color.
|
||||
* @returns {color4} out.
|
||||
*/
|
||||
static copy(out, a) {
|
||||
out.r = a.r;
|
||||
out.g = a.g;
|
||||
out.b = a.b;
|
||||
out.a = a.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the components of a color to the given values.
|
||||
*
|
||||
* @param {color4} out - The color to modified.
|
||||
* @param {number} r - Value assigned to r component.
|
||||
* @param {number} g - Value assigned to g component.
|
||||
* @param {number} b - Value assigned to b component.
|
||||
* @param {number} a - Value assigned to a component.
|
||||
* @returns {color4} out.
|
||||
*/
|
||||
static set(out, r, g, b, a) {
|
||||
out.r = r;
|
||||
out.g = g;
|
||||
out.b = b;
|
||||
out.a = a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the hexadecimal formal color into rgb formal.
|
||||
*
|
||||
* @param {color4} out - Color to store result.
|
||||
* @param {Number} hex - The color's hexadecimal formal.
|
||||
* @returns {color4} out.
|
||||
* @function
|
||||
*/
|
||||
static fromHex(out, hex) {
|
||||
let r = ((hex >> 24)) / 255.0;
|
||||
let g = ((hex >> 16) & 0xff) / 255.0;
|
||||
let b = ((hex >> 8) & 0xff) / 255.0;
|
||||
let a = ((hex) & 0xff) / 255.0;
|
||||
|
||||
out.r = r;
|
||||
out.g = g;
|
||||
out.b = b;
|
||||
out.a = a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add components of two colors, respectively.
|
||||
*
|
||||
* @param {color4} out - Color to store result.
|
||||
* @param {color4} a - The first operand.
|
||||
* @param {color4} b - The second operand.
|
||||
* @returns {color4} out.
|
||||
*/
|
||||
static add(out, a, b) {
|
||||
out.r = a.r + b.r;
|
||||
out.g = a.g + b.g;
|
||||
out.b = a.b + b.b;
|
||||
out.a = a.a + b.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract components of color b from components of color a, respectively.
|
||||
*
|
||||
* @param {color4} out - Color to store result.
|
||||
* @param {color4} a - The a.
|
||||
* @param {color4} b - The b.
|
||||
* @returns {color4} out.
|
||||
*/
|
||||
static subtract(out, a, b) {
|
||||
out.r = a.r - b.r;
|
||||
out.g = a.g - b.g;
|
||||
out.b = a.b - b.b;
|
||||
out.a = a.a - b.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color4.subtract}.
|
||||
*/
|
||||
static sub(out, a, b) {
|
||||
return color4.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply components of two colors, respectively.
|
||||
*
|
||||
* @param {color4} out - Color to store result.
|
||||
* @param {color4} a - The first operand.
|
||||
* @param {color4} b - The second operand.
|
||||
* @returns {color4} out.
|
||||
*/
|
||||
static multiply(out, a, b) {
|
||||
out.r = a.r * b.r;
|
||||
out.g = a.g * b.g;
|
||||
out.b = a.b * b.b;
|
||||
out.a = a.a * b.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color4.multiply}.
|
||||
*/
|
||||
static mul(out, a, b) {
|
||||
return color4.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide components of color a by components of color b, respectively.
|
||||
*
|
||||
* @param {color4} out - Color to store result.
|
||||
* @param {color4} a - The first operand.
|
||||
* @param {color4} b - The second operand.
|
||||
* @returns {color4} out.
|
||||
*/
|
||||
static divide(out, a, b) {
|
||||
out.r = a.r / b.r;
|
||||
out.g = a.g / b.g;
|
||||
out.b = a.b / b.b;
|
||||
out.a = a.a / b.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color4.divide}.
|
||||
*/
|
||||
static div(out, a, b) {
|
||||
return color4.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a color by a number.
|
||||
*
|
||||
* @param {color4} out - Color to store result.
|
||||
* @param {color4} a - Color to scale.
|
||||
* @param {number} b - The scale number.
|
||||
* @returns {color4} out.
|
||||
*/
|
||||
static scale(out, a, b) {
|
||||
out.r = a.r * b;
|
||||
out.g = a.g * b;
|
||||
out.b = a.b * b;
|
||||
out.a = a.a * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two colors.
|
||||
*
|
||||
* @param {color4} out - Color to store result.
|
||||
* @param {color4} a - The first operand.
|
||||
* @param {color4} b - The second operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {color4} out.
|
||||
*/
|
||||
static lerp(out, a, b, t) {
|
||||
let ar = a.r,
|
||||
ag = a.g,
|
||||
ab = a.b,
|
||||
aa = a.a;
|
||||
out.r = ar + t * (b.r - ar);
|
||||
out.g = ag + t * (b.g - ag);
|
||||
out.b = ab + t * (b.b - ab);
|
||||
out.a = aa + t * (b.a - aa);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a color.
|
||||
*
|
||||
* @param {color4} a - The color.
|
||||
* @returns {String} - String representation of this color.
|
||||
*/
|
||||
static str(a) {
|
||||
return `color4(${a.r}, ${a.g}, ${a.b}, ${a.a})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a color into array.
|
||||
*
|
||||
* @param {Array} out - Array to store result.
|
||||
* @param {color4} a - The color.
|
||||
* @returns {Array} out.
|
||||
*/
|
||||
static array(out, a) {
|
||||
let scale = (a instanceof cc.Color || a.a > 1) ? 1 / 255 : 1;
|
||||
out[0] = a.r * scale;
|
||||
out[1] = a.g * scale;
|
||||
out[2] = a.b * scale;
|
||||
out[3] = a.a * scale;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified colors are equal. (Compared using ===)
|
||||
*
|
||||
* @param {color4} a - The first color.
|
||||
* @param {color4} b - The second color.
|
||||
* @returns {Boolean} True if the colors are equal, false otherwise.
|
||||
*/
|
||||
static exactEquals(a, b) {
|
||||
return a.r === b.r && a.g === b.g && a.b === b.b && a.a === b.a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified colors are approximately equal.
|
||||
*
|
||||
* @param {color4} a - The first color.
|
||||
* @param {color4} b - The second color.
|
||||
* @returns {Boolean} True if the colors are approximately equal, false otherwise.
|
||||
*/
|
||||
static equals(a, b) {
|
||||
let a0 = a.r, a1 = a.g, a2 = a.b, a3 = a.a;
|
||||
let b0 = b.r, b1 = b.g, b2 = b.b, b3 = b.a;
|
||||
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a color's rgb formal into the hexadecimal one.
|
||||
*
|
||||
* @param {color4} a - The color.
|
||||
* @returns {Number} - The color's hexadecimal formal.
|
||||
*/
|
||||
static hex(a) {
|
||||
return ((a.r * 255) << 24 | (a.g * 255) << 16 | (a.b * 255) << 8 | a.a * 255) >>> 0;
|
||||
}
|
||||
}
|
||||
|
||||
export default color4;
|
||||
@@ -0,0 +1,306 @@
|
||||
import { EPSILON } from './utils';
|
||||
|
||||
/**
|
||||
* Represents a color with red(r), green(g), blue(b) component of that color and
|
||||
* and an extra alpha(a) component indicating how opaque this color is.
|
||||
*/
|
||||
class color4 {
|
||||
|
||||
/**
|
||||
* Creates a white color, or components specified separately.
|
||||
*
|
||||
* @param r - Value assigned to r component.
|
||||
* @param g - Value assigned to g component.
|
||||
* @param b - Value assigned to b component.
|
||||
* @param a - Value assigned to a component.
|
||||
* @return The newly created color.
|
||||
*/
|
||||
public static create (r = 1, g = 1, b = 1, a = 1) {
|
||||
return new color4(r, g, b, a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a color.
|
||||
*
|
||||
* @param a - Color to clone.
|
||||
* @return The newly created color.
|
||||
*/
|
||||
public static clone (a) {
|
||||
return new color4(a.r, a.g, a.b, a.a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a color into another.
|
||||
*
|
||||
* @param out - The color to modified.
|
||||
* @param a - The specified color.
|
||||
* @return out.
|
||||
*/
|
||||
public static copy (out, a) {
|
||||
out.r = a.r;
|
||||
out.g = a.g;
|
||||
out.b = a.b;
|
||||
out.a = a.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the components of a color to the given values.
|
||||
*
|
||||
* @param out - The color to modified.
|
||||
* @param r - Value assigned to r component.
|
||||
* @param g - Value assigned to g component.
|
||||
* @param b - Value assigned to b component.
|
||||
* @param a - Value assigned to a component.
|
||||
* @return out.
|
||||
*/
|
||||
public static set (out, r, g, b, a) {
|
||||
out.r = r;
|
||||
out.g = g;
|
||||
out.b = b;
|
||||
out.a = a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the hexadecimal formal color into rgb formal.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param hex - The color's hexadecimal formal.
|
||||
* @return out.
|
||||
* @function
|
||||
*/
|
||||
public static fromHex (out, hex) {
|
||||
const r = ((hex >> 24)) / 255.0;
|
||||
const g = ((hex >> 16) & 0xff) / 255.0;
|
||||
const b = ((hex >> 8) & 0xff) / 255.0;
|
||||
const a = ((hex) & 0xff) / 255.0;
|
||||
|
||||
out.r = r;
|
||||
out.g = g;
|
||||
out.b = b;
|
||||
out.a = a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add components of two colors, respectively.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static add (out, a, b) {
|
||||
out.r = a.r + b.r;
|
||||
out.g = a.g + b.g;
|
||||
out.b = a.b + b.b;
|
||||
out.a = a.a + b.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract components of color b from components of color a, respectively.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The a.
|
||||
* @param b - The b.
|
||||
* @return out.
|
||||
*/
|
||||
public static subtract (out, a, b) {
|
||||
out.r = a.r - b.r;
|
||||
out.g = a.g - b.g;
|
||||
out.b = a.b - b.b;
|
||||
out.a = a.a - b.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color4.subtract}.
|
||||
*/
|
||||
public static sub (out, a, b) {
|
||||
return color4.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply components of two colors, respectively.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiply (out, a, b) {
|
||||
out.r = a.r * b.r;
|
||||
out.g = a.g * b.g;
|
||||
out.b = a.b * b.b;
|
||||
out.a = a.a * b.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color4.multiply}.
|
||||
*/
|
||||
public static mul (out, a, b) {
|
||||
return color4.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide components of color a by components of color b, respectively.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static divide (out, a, b) {
|
||||
out.r = a.r / b.r;
|
||||
out.g = a.g / b.g;
|
||||
out.b = a.b / b.b;
|
||||
out.a = a.a / b.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link color4.divide}.
|
||||
*/
|
||||
public static div (out, a, b) {
|
||||
return color4.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a color by a number.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - Color to scale.
|
||||
* @param b - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static scale (out, a, b) {
|
||||
out.r = a.r * b;
|
||||
out.g = a.g * b;
|
||||
out.b = a.b * b;
|
||||
out.a = a.a * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two colors.
|
||||
*
|
||||
* @param out - Color to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out.
|
||||
*/
|
||||
public static lerp (out, a, b, t) {
|
||||
const ar = a.r,
|
||||
ag = a.g,
|
||||
ab = a.b,
|
||||
aa = a.a;
|
||||
out.r = ar + t * (b.r - ar);
|
||||
out.g = ag + t * (b.g - ag);
|
||||
out.b = ab + t * (b.b - ab);
|
||||
out.a = aa + t * (b.a - aa);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a color.
|
||||
*
|
||||
* @param a - The color.
|
||||
* @return - String representation of this color.
|
||||
*/
|
||||
public static str (a) {
|
||||
return `color4(${a.r}, ${a.g}, ${a.b}, ${a.a})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a color into array.
|
||||
*
|
||||
* @param out - Array to store result.
|
||||
* @param a - The color.
|
||||
* @return out.
|
||||
*/
|
||||
public static array (out, a) {
|
||||
const scale = (a instanceof cc.Color || a.a > 1) ? 1 / 255 : 1;
|
||||
out[0] = a.r * scale;
|
||||
out[1] = a.g * scale;
|
||||
out[2] = a.b * scale;
|
||||
out[3] = a.a * scale;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified colors are equal. (Compared using ===)
|
||||
*
|
||||
* @param a - The first color.
|
||||
* @param b - The second color.
|
||||
* @return True if the colors are equal, false otherwise.
|
||||
*/
|
||||
public static exactEquals (a, b) {
|
||||
return a.r === b.r && a.g === b.g && a.b === b.b && a.a === b.a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified colors are approximately equal.
|
||||
*
|
||||
* @param a - The first color.
|
||||
* @param b - The second color.
|
||||
* @return True if the colors are approximately equal, false otherwise.
|
||||
*/
|
||||
public static equals (a, b) {
|
||||
const a0 = a.r, a1 = a.g, a2 = a.b, a3 = a.a;
|
||||
const b0 = b.r, b1 = b.g, b2 = b.b, b3 = b.a;
|
||||
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a color's rgb formal into the hexadecimal one.
|
||||
*
|
||||
* @param a - The color.
|
||||
* @return - The color's hexadecimal formal.
|
||||
*/
|
||||
public static hex (a) {
|
||||
return ((a.r * 255) << 24 | (a.g * 255) << 16 | (a.b * 255) << 8 | a.a * 255) >>> 0;
|
||||
}
|
||||
/**
|
||||
* Creates a color, with components specified separately.
|
||||
*
|
||||
* @param r - Value assigned to r component.
|
||||
* @param g - Value assigned to g component.
|
||||
* @param b - Value assigned to b component.
|
||||
* @param a - Value assigned to a component.
|
||||
*/
|
||||
constructor (r = 1, g = 1, b = 1, a = 1) {
|
||||
/**
|
||||
* The r component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.r = r;
|
||||
|
||||
/**
|
||||
* The g component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.g = g;
|
||||
|
||||
/**
|
||||
* The b component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.b = b;
|
||||
|
||||
/**
|
||||
* The a component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.a = a;
|
||||
}
|
||||
}
|
||||
|
||||
export default color4;
|
||||
Vendored
+77
@@ -0,0 +1,77 @@
|
||||
|
||||
declare interface IVec2Like {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
declare interface IVec3Like {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
}
|
||||
|
||||
declare interface IVec4Like {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
w: number;
|
||||
}
|
||||
|
||||
declare interface IQuatLike {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
w: number;
|
||||
}
|
||||
|
||||
declare interface IMat2Like {
|
||||
m00: number;
|
||||
m01: number;
|
||||
m02: number;
|
||||
m03: number;
|
||||
}
|
||||
|
||||
declare interface IMat23Like {
|
||||
m00: number;
|
||||
m01: number;
|
||||
m02: number;
|
||||
m03: number;
|
||||
m04: number;
|
||||
m05: number;
|
||||
}
|
||||
|
||||
declare interface IMat3Like {
|
||||
m00: number;
|
||||
m01: number;
|
||||
m02: number;
|
||||
m03: number;
|
||||
m04: number;
|
||||
m05: number;
|
||||
m06: number;
|
||||
m07: number;
|
||||
m08: number;
|
||||
}
|
||||
|
||||
declare interface IMat4Like {
|
||||
m00: number;
|
||||
m01: number;
|
||||
m02: number;
|
||||
m03: number;
|
||||
m04: number;
|
||||
m05: number;
|
||||
m06: number;
|
||||
m07: number;
|
||||
m08: number;
|
||||
m09: number;
|
||||
m10: number;
|
||||
m11: number;
|
||||
m12: number;
|
||||
m13: number;
|
||||
m14: number;
|
||||
m15: number;
|
||||
}
|
||||
|
||||
declare interface IWritableArrayLike<T> {
|
||||
readonly length: number;
|
||||
[index: number]: T;
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
export * from './utils';
|
||||
|
||||
// NOTE: there is no syntax for: export {* as bits} from './lib/bits';
|
||||
import * as bits_ from './bits';
|
||||
import * as bits from './bits';
|
||||
/**
|
||||
* Export module bits.
|
||||
*/
|
||||
export let bits = bits_;
|
||||
export { bits };
|
||||
|
||||
export { default as vec2 } from './vec2';
|
||||
export { default as vec3 } from './vec3';
|
||||
@@ -16,4 +16,4 @@ export { default as mat23 } from './mat23';
|
||||
export { default as mat3 } from './mat3';
|
||||
export { default as mat4 } from './mat4';
|
||||
export { default as color3 } from './color3';
|
||||
export { default as color4 } from './color4';
|
||||
export { default as color4 } from './color4';
|
||||
@@ -1,443 +0,0 @@
|
||||
import { EPSILON } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 2x2 matrix.
|
||||
*/
|
||||
class mat2 {
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param {Number} m00 - Value assigned to element at column 0 row 0.
|
||||
* @param {Number} m01 - Value assigned to element at column 0 row 1.
|
||||
* @param {Number} m02 - Value assigned to element at column 1 row 0.
|
||||
* @param {Number} m03 - Value assigned to element at column 1 row 1.
|
||||
*/
|
||||
constructor(m00 = 1, m01 = 0, m02 = 0, m03 = 1) {
|
||||
/**
|
||||
* The element at column 0 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m00 = m00;
|
||||
|
||||
/**
|
||||
* The element at column 0 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m01 = m01;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m02 = m02;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m03 = m03;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param {Number} m00 - Value assigned to element at column 0 row 0.
|
||||
* @param {Number} m01 - Value assigned to element at column 0 row 1.
|
||||
* @param {Number} m02 - Value assigned to element at column 1 row 0.
|
||||
* @param {Number} m03 - Value assigned to element at column 1 row 1.
|
||||
* @returns {mat2} The newly created matrix.
|
||||
*/
|
||||
static create(m00 = 1, m01 = 0, m02 = 0, m03 = 1) {
|
||||
return new mat2(m00, m01, m02, m03);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a matrix.
|
||||
*
|
||||
* @param {mat2} a - Matrix to clone.
|
||||
* @returns {mat2} The newly created matrix.
|
||||
*/
|
||||
static clone(a) {
|
||||
return new mat2(a.m00, a.m01, a.m02, a.m03);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a matrix into another.
|
||||
*
|
||||
* @param {mat2} out - Matrix to modified.
|
||||
* @param {mat2} a - The specified matrix.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static copy(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = a.m02;
|
||||
out.m03 = a.m03;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a matrix as identity matrix.
|
||||
*
|
||||
* @param {mat2} out - Matrix to modified.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static identity(out) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the elements of a matrix to the given values.
|
||||
*
|
||||
* @param {mat2} out - The matrix to modified.
|
||||
* @param {Number} m00 - Value assigned to element at column 0 row 0.
|
||||
* @param {Number} m01 - Value assigned to element at column 0 row 1.
|
||||
* @param {Number} m10 - Value assigned to element at column 1 row 0.
|
||||
* @param {Number} m11 - Value assigned to element at column 1 row 1.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static set(out, m00, m01, m10, m11) {
|
||||
out.m00 = m00;
|
||||
out.m01 = m01;
|
||||
out.m02 = m10;
|
||||
out.m03 = m11;
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Transposes a matrix.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - Matrix to transpose.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static transpose(out, a) {
|
||||
// If we are transposing ourselves we can skip a few steps but have to cache some values
|
||||
if (out === a) {
|
||||
let a1 = a.m01;
|
||||
out.m01 = a.m02;
|
||||
out.m02 = a1;
|
||||
} else {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m02;
|
||||
out.m02 = a.m01;
|
||||
out.m03 = a.m03;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts a matrix.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - Matrix to invert.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static invert(out, a) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03;
|
||||
|
||||
// Calculate the determinant
|
||||
let det = a0 * a3 - a2 * a1;
|
||||
|
||||
if (!det) {
|
||||
return null;
|
||||
}
|
||||
det = 1.0 / det;
|
||||
|
||||
out.m00 = a3 * det;
|
||||
out.m01 = -a1 * det;
|
||||
out.m02 = -a2 * det;
|
||||
out.m03 = a0 * det;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the adjugate of a matrix.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - Matrix to calculate.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static adjoint(out, a) {
|
||||
// Caching this value is nessecary if out == a
|
||||
let a0 = a.m00;
|
||||
out.m00 = a.m03;
|
||||
out.m01 = -a.m01;
|
||||
out.m02 = -a.m02;
|
||||
out.m03 = a0;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the determinant of a matrix.
|
||||
*
|
||||
* @param {mat2} a - Matrix to calculate.
|
||||
* @returns {Number} Determinant of a.
|
||||
*/
|
||||
static determinant(a) {
|
||||
return a.m00 * a.m03 - a.m02 * a.m01;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two matrices explicitly.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - The first operand.
|
||||
* @param {mat2} b - The second operand.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static multiply(out, a, b) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03;
|
||||
let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03;
|
||||
out.m00 = a0 * b0 + a2 * b1;
|
||||
out.m01 = a1 * b0 + a3 * b1;
|
||||
out.m02 = a0 * b2 + a2 * b3;
|
||||
out.m03 = a1 * b2 + a3 * b3;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat2.multiply}.
|
||||
*/
|
||||
static mul(out, a, b) {
|
||||
return mat2.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a matrix by the given angle.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - Matrix to rotate.
|
||||
* @param {Number} rad - The rotation angle.
|
||||
* @returns {mat2} out
|
||||
*/
|
||||
static rotate(out, a, rad) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03,
|
||||
s = Math.sin(rad),
|
||||
c = Math.cos(rad);
|
||||
out.m00 = a0 * c + a2 * s;
|
||||
out.m01 = a1 * c + a3 * s;
|
||||
out.m02 = a0 * -s + a2 * c;
|
||||
out.m03 = a1 * -s + a3 * c;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales the matrix given by a scale vector.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - Matrix to scale.
|
||||
* @param {vec2} v - The scale vector.
|
||||
* @returns {mat2} out
|
||||
**/
|
||||
static scale(out, a, v) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03,
|
||||
v0 = v.x, v1 = v.y;
|
||||
out.m00 = a0 * v0;
|
||||
out.m01 = a1 * v0;
|
||||
out.m02 = a2 * v1;
|
||||
out.m03 = a3 * v1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a given angle.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat2.set(dest, 1, 0, 0, 1);
|
||||
* mat2.rotate(dest, dest, rad);
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {Number} rad - The rotation angle.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static fromRotation(out, rad) {
|
||||
let s = Math.sin(rad),
|
||||
c = Math.cos(rad);
|
||||
out.m00 = c;
|
||||
out.m01 = s;
|
||||
out.m02 = -s;
|
||||
out.m03 = c;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a scale vector.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat2.set(dest, 1, 0, 0, 1);
|
||||
* mat2.scale(dest, dest, vec);
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {vec2} v - Scale vector.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static fromScaling(out, v) {
|
||||
out.m00 = v.x;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = v.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of a matrix.
|
||||
*
|
||||
* @param {mat2} a - The matrix.
|
||||
* @returns {String} String representation of this matrix.
|
||||
*/
|
||||
static str(a) {
|
||||
return `mat2(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store elements of a matrix into array.
|
||||
*
|
||||
* @param {array} out - Array to store result.
|
||||
* @param {mat2} m - The matrix.
|
||||
* @returns {Array} out.
|
||||
*/
|
||||
static array(out, m) {
|
||||
out[0] = m.m00;
|
||||
out[1] = m.m01;
|
||||
out[2] = m.m02;
|
||||
out[3] = m.m03;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Frobenius norm of a matrix.
|
||||
*
|
||||
* @param {mat2} a - Matrix to calculate Frobenius norm of.
|
||||
* @returns {Number} - The frobenius norm.
|
||||
*/
|
||||
static frob(a) {
|
||||
return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix.
|
||||
* @param {mat2} L - The lower triangular matrix.
|
||||
* @param {mat2} D - The diagonal matrix.
|
||||
* @param {mat2} U - The upper triangular matrix.
|
||||
* @param {mat2} a - The input matrix to factorize.
|
||||
*/
|
||||
static LDU(L, D, U, a) {
|
||||
L.m02 = a.m02 / a.m00;
|
||||
U.m00 = a.m00;
|
||||
U.m01 = a.m01;
|
||||
U.m03 = a.m03 - L.m02 * U.m01;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - The first operand.
|
||||
* @param {mat2} b - The second operand.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static add(out, a, b) {
|
||||
out.m00 = a.m00 + b.m00;
|
||||
out.m01 = a.m01 + b.m01;
|
||||
out.m02 = a.m02 + b.m02;
|
||||
out.m03 = a.m03 + b.m03;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts matrix b from matrix a.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - The first operand.
|
||||
* @param {mat2} b - The second operand.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static subtract(out, a, b) {
|
||||
out.m00 = a.m00 - b.m00;
|
||||
out.m01 = a.m01 - b.m01;
|
||||
out.m02 = a.m02 - b.m02;
|
||||
out.m03 = a.m03 - b.m03;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat2.subtract}.
|
||||
*/
|
||||
static sub(out, a, b) {
|
||||
return mat2.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are equal. (Compared using ===)
|
||||
*
|
||||
* @param {mat2} a - The first matrix.
|
||||
* @param {mat2} b - The second matrix.
|
||||
* @returns {Boolean} True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
static exactEquals(a, b) {
|
||||
return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are approximately equal.
|
||||
*
|
||||
* @param {mat2} a - The first matrix.
|
||||
* @param {mat2} b - The second matrix.
|
||||
* @returns {Boolean} True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
static equals(a, b) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03;
|
||||
let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03;
|
||||
return (
|
||||
Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply each element of a matrix by a scalar number.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - Matrix to scale
|
||||
* @param {Number} b - The scale number.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static multiplyScalar(out, a, b) {
|
||||
out.m00 = a.m00 * b;
|
||||
out.m01 = a.m01 * b;
|
||||
out.m02 = a.m02 * b;
|
||||
out.m03 = a.m03 * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices after multiplying each element of the second operand by a scalar number.
|
||||
*
|
||||
* @param {mat2} out - Matrix to store result.
|
||||
* @param {mat2} a - The first operand.
|
||||
* @param {mat2} b - The second operand.
|
||||
* @param {Number} scale - The scale number.
|
||||
* @returns {mat2} out.
|
||||
*/
|
||||
static multiplyScalarAndAdd(out, a, b, scale) {
|
||||
out.m00 = a.m00 + (b.m00 * scale);
|
||||
out.m01 = a.m01 + (b.m01 * scale);
|
||||
out.m02 = a.m02 + (b.m02 * scale);
|
||||
out.m03 = a.m03 + (b.m03 * scale);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
export default mat2;
|
||||
@@ -0,0 +1,442 @@
|
||||
import { EPSILON } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 2x2 matrix.
|
||||
*/
|
||||
class mat2 {
|
||||
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param m00 - Value assigned to element at column 0 row 0.
|
||||
* @param m01 - Value assigned to element at column 0 row 1.
|
||||
* @param m02 - Value assigned to element at column 1 row 0.
|
||||
* @param m03 - Value assigned to element at column 1 row 1.
|
||||
* @return The newly created matrix.
|
||||
*/
|
||||
public static create(m00 = 1, m01 = 0, m02 = 0, m03 = 1) {
|
||||
return new mat2(m00, m01, m02, m03);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a matrix.
|
||||
*
|
||||
* @param a - Matrix to clone.
|
||||
* @return The newly created matrix.
|
||||
*/
|
||||
public static clone(a) {
|
||||
return new mat2(a.m00, a.m01, a.m02, a.m03);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a matrix into another.
|
||||
*
|
||||
* @param out - Matrix to modified.
|
||||
* @param a - The specified matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static copy(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = a.m02;
|
||||
out.m03 = a.m03;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a matrix as identity matrix.
|
||||
*
|
||||
* @param out - Matrix to modified.
|
||||
* @return out.
|
||||
*/
|
||||
public static identity(out) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the elements of a matrix to the given values.
|
||||
*
|
||||
* @param out - The matrix to modified.
|
||||
* @param m00 - Value assigned to element at column 0 row 0.
|
||||
* @param m01 - Value assigned to element at column 0 row 1.
|
||||
* @param m10 - Value assigned to element at column 1 row 0.
|
||||
* @param m11 - Value assigned to element at column 1 row 1.
|
||||
* @return out.
|
||||
*/
|
||||
public static set(out, m00, m01, m10, m11) {
|
||||
out.m00 = m00;
|
||||
out.m01 = m01;
|
||||
out.m02 = m10;
|
||||
out.m03 = m11;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transposes a matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to transpose.
|
||||
* @return out.
|
||||
*/
|
||||
public static transpose(out, a) {
|
||||
// If we are transposing ourselves we can skip a few steps but have to cache some values
|
||||
if (out === a) {
|
||||
const a1 = a.m01;
|
||||
out.m01 = a.m02;
|
||||
out.m02 = a1;
|
||||
} else {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m02;
|
||||
out.m02 = a.m01;
|
||||
out.m03 = a.m03;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts a matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to invert.
|
||||
* @return out.
|
||||
*/
|
||||
public static invert(out, a) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03;
|
||||
|
||||
// Calculate the determinant
|
||||
let det = a0 * a3 - a2 * a1;
|
||||
|
||||
if (!det) {
|
||||
return null;
|
||||
}
|
||||
det = 1.0 / det;
|
||||
|
||||
out.m00 = a3 * det;
|
||||
out.m01 = -a1 * det;
|
||||
out.m02 = -a2 * det;
|
||||
out.m03 = a0 * det;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the adjugate of a matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to calculate.
|
||||
* @return out.
|
||||
*/
|
||||
public static adjoint(out, a) {
|
||||
// Caching this value is nessecary if out == a
|
||||
const a0 = a.m00;
|
||||
out.m00 = a.m03;
|
||||
out.m01 = -a.m01;
|
||||
out.m02 = -a.m02;
|
||||
out.m03 = a0;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the determinant of a matrix.
|
||||
*
|
||||
* @param a - Matrix to calculate.
|
||||
* @return Determinant of a.
|
||||
*/
|
||||
public static determinant(a) {
|
||||
return a.m00 * a.m03 - a.m02 * a.m01;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two matrices explicitly.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiply(out, a, b) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03;
|
||||
const b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03;
|
||||
out.m00 = a0 * b0 + a2 * b1;
|
||||
out.m01 = a1 * b0 + a3 * b1;
|
||||
out.m02 = a0 * b2 + a2 * b3;
|
||||
out.m03 = a1 * b2 + a3 * b3;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat2.multiply}.
|
||||
*/
|
||||
public static mul(out, a, b) {
|
||||
return mat2.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a matrix by the given angle.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to rotate.
|
||||
* @param rad - The rotation angle.
|
||||
* @return out
|
||||
*/
|
||||
public static rotate(out, a, rad) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03,
|
||||
s = Math.sin(rad),
|
||||
c = Math.cos(rad);
|
||||
out.m00 = a0 * c + a2 * s;
|
||||
out.m01 = a1 * c + a3 * s;
|
||||
out.m02 = a0 * -s + a2 * c;
|
||||
out.m03 = a1 * -s + a3 * c;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales the matrix given by a scale vector.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to scale.
|
||||
* @param v - The scale vector.
|
||||
* @return out
|
||||
**/
|
||||
public static scale(out, a, v) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03,
|
||||
v0 = v.x, v1 = v.y;
|
||||
out.m00 = a0 * v0;
|
||||
out.m01 = a1 * v0;
|
||||
out.m02 = a2 * v1;
|
||||
out.m03 = a3 * v1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a given angle.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat2.set(dest, 1, 0, 0, 1);
|
||||
* mat2.rotate(dest, dest, rad);
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param rad - The rotation angle.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromRotation(out, rad) {
|
||||
const s = Math.sin(rad),
|
||||
c = Math.cos(rad);
|
||||
out.m00 = c;
|
||||
out.m01 = s;
|
||||
out.m02 = -s;
|
||||
out.m03 = c;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a scale vector.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat2.set(dest, 1, 0, 0, 1);
|
||||
* mat2.scale(dest, dest, vec);
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param v - Scale vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromScaling(out, v) {
|
||||
out.m00 = v.x;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = v.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of a matrix.
|
||||
*
|
||||
* @param a - The matrix.
|
||||
* @return String representation of this matrix.
|
||||
*/
|
||||
public static str(a) {
|
||||
return `mat2(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store elements of a matrix into array.
|
||||
*
|
||||
* @param out - Array to store result.
|
||||
* @param m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static array(out, m) {
|
||||
out[0] = m.m00;
|
||||
out[1] = m.m01;
|
||||
out[2] = m.m02;
|
||||
out[3] = m.m03;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Frobenius norm of a matrix.
|
||||
*
|
||||
* @param a - Matrix to calculate Frobenius norm of.
|
||||
* @return - The frobenius norm.
|
||||
*/
|
||||
public static frob(a) {
|
||||
return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix.
|
||||
* @param L - The lower triangular matrix.
|
||||
* @param D - The diagonal matrix.
|
||||
* @param U - The upper triangular matrix.
|
||||
* @param a - The input matrix to factorize.
|
||||
*/
|
||||
public static LDU(L, D, U, a) {
|
||||
L.m02 = a.m02 / a.m00;
|
||||
U.m00 = a.m00;
|
||||
U.m01 = a.m01;
|
||||
U.m03 = a.m03 - L.m02 * U.m01;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static add(out, a, b) {
|
||||
out.m00 = a.m00 + b.m00;
|
||||
out.m01 = a.m01 + b.m01;
|
||||
out.m02 = a.m02 + b.m02;
|
||||
out.m03 = a.m03 + b.m03;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts matrix b from matrix a.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static subtract(out, a, b) {
|
||||
out.m00 = a.m00 - b.m00;
|
||||
out.m01 = a.m01 - b.m01;
|
||||
out.m02 = a.m02 - b.m02;
|
||||
out.m03 = a.m03 - b.m03;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat2.subtract}.
|
||||
*/
|
||||
public static sub(out, a, b) {
|
||||
return mat2.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are equal. (Compared using ===)
|
||||
*
|
||||
* @param a - The first matrix.
|
||||
* @param b - The second matrix.
|
||||
* @return True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
public static exactEquals(a, b) {
|
||||
return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are approximately equal.
|
||||
*
|
||||
* @param a - The first matrix.
|
||||
* @param b - The second matrix.
|
||||
* @return True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
public static equals(a, b) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03;
|
||||
const b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03;
|
||||
return (
|
||||
Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply each element of a matrix by a scalar number.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to scale
|
||||
* @param b - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiplyScalar(out, a, b) {
|
||||
out.m00 = a.m00 * b;
|
||||
out.m01 = a.m01 * b;
|
||||
out.m02 = a.m02 * b;
|
||||
out.m03 = a.m03 * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices after multiplying each element of the second operand by a scalar number.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param scale - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiplyScalarAndAdd(out, a, b, scale) {
|
||||
out.m00 = a.m00 + (b.m00 * scale);
|
||||
out.m01 = a.m01 + (b.m01 * scale);
|
||||
out.m02 = a.m02 + (b.m02 * scale);
|
||||
out.m03 = a.m03 + (b.m03 * scale);
|
||||
return out;
|
||||
}
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param m00 - Value assigned to element at column 0 row 0.
|
||||
* @param m01 - Value assigned to element at column 0 row 1.
|
||||
* @param m02 - Value assigned to element at column 1 row 0.
|
||||
* @param m03 - Value assigned to element at column 1 row 1.
|
||||
*/
|
||||
constructor(m00 = 1, m01 = 0, m02 = 0, m03 = 1) {
|
||||
/**
|
||||
* The element at column 0 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m00 = m00;
|
||||
|
||||
/**
|
||||
* The element at column 0 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m01 = m01;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m02 = m02;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m03 = m03;
|
||||
}
|
||||
}
|
||||
|
||||
export default mat2;
|
||||
@@ -1,547 +0,0 @@
|
||||
import { EPSILON } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 2x3 matrix.
|
||||
*
|
||||
* A mat23 contains six elements defined as:
|
||||
* <pre>
|
||||
* [a, c, tx,
|
||||
* b, d, ty]
|
||||
* </pre>
|
||||
* This is a short form for the 3x3 matrix:
|
||||
* <pre>
|
||||
* [a, c, tx,
|
||||
* b, d, ty,
|
||||
* 0, 0, 1]
|
||||
* </pre>
|
||||
* The last row is ignored so the array is shorter and operations are faster.
|
||||
*/
|
||||
class mat23 {
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param {Number} m00 - Value assigned to element a.
|
||||
* @param {Number} m01 - Value assigned to element b.
|
||||
* @param {Number} m02 - Value assigned to element c.
|
||||
* @param {Number} m03 - Value assigned to element d.
|
||||
* @param {Number} m04 - Value assigned to element tx.
|
||||
* @param {Number} m05 - Value assigned to element ty.
|
||||
*/
|
||||
constructor(m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) {
|
||||
/**
|
||||
* The element a.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m00 = m00;
|
||||
|
||||
/**
|
||||
* The element b.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m01 = m01;
|
||||
|
||||
/**
|
||||
* The element c.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m02 = m02;
|
||||
|
||||
/**
|
||||
* The element d.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m03 = m03;
|
||||
|
||||
/**
|
||||
* The element tx.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m04 = m04;
|
||||
|
||||
/**
|
||||
* The element ty.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m05 = m05;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param {Number} m00 - Value assigned to element a.
|
||||
* @param {Number} m01 - Value assigned to element b.
|
||||
* @param {Number} m02 - Value assigned to element c.
|
||||
* @param {Number} m03 - Value assigned to element d.
|
||||
* @param {Number} m04 - Value assigned to element tx.
|
||||
* @param {Number} m05 - Value assigned to element ty.
|
||||
* @returns {mat23} The newly created matrix.
|
||||
*/
|
||||
static create(m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) {
|
||||
return new mat23(m00, m01, m02, m03, m04, m05);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a matrix.
|
||||
*
|
||||
* @param {mat23} a - Matrix to clone.
|
||||
* @returns {mat23} The newly created matrix.
|
||||
*/
|
||||
static clone(a) {
|
||||
return new mat23(
|
||||
a.m00, a.m01,
|
||||
a.m02, a.m03,
|
||||
a.m04, a.m05
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a matrix into another.
|
||||
*
|
||||
* @param {mat23} out - Matrix to modified.
|
||||
* @param {mat23} a - The specified matrix.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static copy(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = a.m02;
|
||||
out.m03 = a.m03;
|
||||
out.m04 = a.m04;
|
||||
out.m05 = a.m05;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a matrix as identity matrix.
|
||||
*
|
||||
* @param {mat23} out - Matrix to modified.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static identity(out) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 1;
|
||||
out.m04 = 0;
|
||||
out.m05 = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the elements of a matrix to the given values.
|
||||
*
|
||||
* @param {mat23} out - The matrix to modified.
|
||||
* @param {Number} a - Value assigned to element a.
|
||||
* @param {Number} b - Value assigned to element b.
|
||||
* @param {Number} c - Value assigned to element c.
|
||||
* @param {Number} d - Value assigned to element d.
|
||||
* @param {Number} tx - Value assigned to element tx.
|
||||
* @param {Number} ty - Value assigned to element ty.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static set(out, a, b, c, d, tx, ty) {
|
||||
out.m00 = a;
|
||||
out.m01 = b;
|
||||
out.m02 = c;
|
||||
out.m03 = d;
|
||||
out.m04 = tx;
|
||||
out.m05 = ty;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts a matrix.
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {mat23} a - Matrix to invert.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static invert(out, a) {
|
||||
let aa = a.m00, ab = a.m01, ac = a.m02, ad = a.m03,
|
||||
atx = a.m04, aty = a.m05;
|
||||
|
||||
let det = aa * ad - ab * ac;
|
||||
if (!det) {
|
||||
return null;
|
||||
}
|
||||
det = 1.0 / det;
|
||||
|
||||
out.m00 = ad * det;
|
||||
out.m01 = -ab * det;
|
||||
out.m02 = -ac * det;
|
||||
out.m03 = aa * det;
|
||||
out.m04 = (ac * aty - ad * atx) * det;
|
||||
out.m05 = (ab * atx - aa * aty) * det;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the determinant of a matrix.
|
||||
*
|
||||
* @param {mat23} a - Matrix to calculate.
|
||||
* @returns {Number} Determinant of a.
|
||||
*/
|
||||
static determinant(a) {
|
||||
return a.m00 * a.m03 - a.m01 * a.m02;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two matrices explicitly.
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {mat23} a - The first operand.
|
||||
* @param {mat23} b - The second operand.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static multiply(out, a, b) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
|
||||
b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05;
|
||||
out.m00 = a0 * b0 + a2 * b1;
|
||||
out.m01 = a1 * b0 + a3 * b1;
|
||||
out.m02 = a0 * b2 + a2 * b3;
|
||||
out.m03 = a1 * b2 + a3 * b3;
|
||||
out.m04 = a0 * b4 + a2 * b5 + a4;
|
||||
out.m05 = a1 * b4 + a3 * b5 + a5;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat23.multiply}.
|
||||
*/
|
||||
static mul(out, a, b) {
|
||||
return mat23.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a matrix by the given angle.
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {mat23} a - Matrix to rotate.
|
||||
* @param {Number} rad - The rotation angle.
|
||||
* @returns {mat23} out
|
||||
*/
|
||||
static rotate(out, a, rad) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
|
||||
s = Math.sin(rad),
|
||||
c = Math.cos(rad);
|
||||
out.m00 = a0 * c + a2 * s;
|
||||
out.m01 = a1 * c + a3 * s;
|
||||
out.m02 = a0 * -s + a2 * c;
|
||||
out.m03 = a1 * -s + a3 * c;
|
||||
out.m04 = a4;
|
||||
out.m05 = a5;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a matrix with a scale matrix given by a scale vector.
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {mat23} a - Matrix to multiply.
|
||||
* @param {vec2} v - The scale vector.
|
||||
* @returns {mat23} out
|
||||
**/
|
||||
static scale(out, a, v) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
|
||||
v0 = v.x, v1 = v.y;
|
||||
out.m00 = a0 * v0;
|
||||
out.m01 = a1 * v0;
|
||||
out.m02 = a2 * v1;
|
||||
out.m03 = a3 * v1;
|
||||
out.m04 = a4;
|
||||
out.m05 = a5;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a matrix with a translation matrix given by a translation offset.
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {mat23} a - Matrix to multiply.
|
||||
* @param {vec2} v - The translation offset.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static translate(out, a, v) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
|
||||
v0 = v.x, v1 = v.y;
|
||||
out.m00 = a0;
|
||||
out.m01 = a1;
|
||||
out.m02 = a2;
|
||||
out.m03 = a3;
|
||||
out.m04 = a0 * v0 + a2 * v1 + a4;
|
||||
out.m05 = a1 * v0 + a3 * v1 + a5;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a given angle.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat23.identity(dest);
|
||||
* mat23.rotate(dest, dest, rad);
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {Number} rad - The rotation angle.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static fromRotation(out, rad) {
|
||||
let s = Math.sin(rad), c = Math.cos(rad);
|
||||
out.m00 = c;
|
||||
out.m01 = s;
|
||||
out.m02 = -s;
|
||||
out.m03 = c;
|
||||
out.m04 = 0;
|
||||
out.m05 = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a scale vector.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat23.identity(dest);
|
||||
* mat23.scale(dest, dest, vec);
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {vec2} v - Scale vector.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static fromScaling(out, v) {
|
||||
out.m00 = v.m00;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = v.m01;
|
||||
out.m04 = 0;
|
||||
out.m05 = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a translation offset.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat23.identity(dest);
|
||||
* mat23.translate(dest, dest, vec);
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {vec2} v - The translation offset.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static fromTranslation(out, v) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 1;
|
||||
out.m04 = v.x;
|
||||
out.m05 = v.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a rotation, translation offset and scale vector.
|
||||
* This is equivalent to (but faster than):
|
||||
*
|
||||
* mat23.identity(dest);
|
||||
* mat23.translate(dest, vec);
|
||||
* let tmp = mat23.create();
|
||||
* mat23.fromRotation(tmp, rot);
|
||||
* mat23.multiply(dest, dest, tmp);
|
||||
* mat23.fromScaling(tmp, scale);
|
||||
* mat23.multiply(dest, dest, tmp);
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {number} r - Rotation radian.
|
||||
* @param {vec2} t - Translation offset.
|
||||
* @param {vec2} s - Scale vector.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static fromRTS(out, r, t, s) {
|
||||
let sr = Math.sin(r), cr = Math.cos(r);
|
||||
out.m00 = cr * s.x;
|
||||
out.m01 = sr * s.x;
|
||||
out.m02 = -sr * s.y;
|
||||
out.m03 = cr * s.y;
|
||||
out.m04 = t.x;
|
||||
out.m05 = t.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of a matrix.
|
||||
*
|
||||
* @param {mat23} a - The matrix.
|
||||
* @returns {String} String representation of this matrix.
|
||||
*/
|
||||
static str(a) {
|
||||
return `mat23(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store elements of a matrix into array.
|
||||
*
|
||||
* @param {array} out - Array to store result.
|
||||
* @param {mat23} m - The matrix.
|
||||
* @returns {Array} out.
|
||||
*/
|
||||
static array(out, m) {
|
||||
out[0] = m.m00;
|
||||
out[1] = m.m01;
|
||||
out[2] = m.m02;
|
||||
out[3] = m.m03;
|
||||
out[4] = m.m04;
|
||||
out[5] = m.m05;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store elements of a matrix into 16 floats array.
|
||||
*
|
||||
* @param {array} out
|
||||
* @param {mat23} m
|
||||
* @returns {array}
|
||||
*/
|
||||
static array4x4(out, m) {
|
||||
out[0] = m.m00;
|
||||
out[1] = m.m01;
|
||||
out[2] = 0;
|
||||
out[3] = 0;
|
||||
out[4] = m.m02;
|
||||
out[5] = m.m03;
|
||||
out[6] = 0;
|
||||
out[7] = 0;
|
||||
out[8] = 0;
|
||||
out[9] = 0;
|
||||
out[10] = 1;
|
||||
out[11] = 0;
|
||||
out[12] = m.m04;
|
||||
out[13] = m.m05;
|
||||
out[14] = 0;
|
||||
out[15] = 1;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Frobenius norm of a matrix.
|
||||
*
|
||||
* @param {mat23} a - Matrix to calculate Frobenius norm of.
|
||||
* @returns {Number} - The frobenius norm.
|
||||
*/
|
||||
static frob(a) {
|
||||
return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices.
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {mat23} a - The first operand.
|
||||
* @param {mat23} b - The second operand.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static add(out, a, b) {
|
||||
out.m00 = a.m00 + b.m00;
|
||||
out.m01 = a.m01 + b.m01;
|
||||
out.m02 = a.m02 + b.m02;
|
||||
out.m03 = a.m03 + b.m03;
|
||||
out.m04 = a.m04 + b.m04;
|
||||
out.m05 = a.m05 + b.m05;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts matrix b from matrix a.
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {mat23} a - The first operand.
|
||||
* @param {mat23} b - The second operand.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static subtract(out, a, b) {
|
||||
out.m00 = a.m00 - b.m00;
|
||||
out.m01 = a.m01 - b.m01;
|
||||
out.m02 = a.m02 - b.m02;
|
||||
out.m03 = a.m03 - b.m03;
|
||||
out.m04 = a.m04 - b.m04;
|
||||
out.m05 = a.m05 - b.m05;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat23.subtract}.
|
||||
*/
|
||||
static sub(out, a, b) {
|
||||
return mat23.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply each element of a matrix by a scalar number.
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {mat23} a - Matrix to scale
|
||||
* @param {Number} b - The scale number.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static multiplyScalar(out, a, b) {
|
||||
out.m00 = a.m00 * b;
|
||||
out.m01 = a.m01 * b;
|
||||
out.m02 = a.m02 * b;
|
||||
out.m03 = a.m03 * b;
|
||||
out.m04 = a.m04 * b;
|
||||
out.m05 = a.m05 * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices after multiplying each element of the second operand by a scalar number.
|
||||
*
|
||||
* @param {mat23} out - Matrix to store result.
|
||||
* @param {mat23} a - The first operand.
|
||||
* @param {mat23} b - The second operand.
|
||||
* @param {Number} scale - The scale number.
|
||||
* @returns {mat23} out.
|
||||
*/
|
||||
static multiplyScalarAndAdd(out, a, b, scale) {
|
||||
out.m00 = a.m00 + (b.m00 * scale);
|
||||
out.m01 = a.m01 + (b.m01 * scale);
|
||||
out.m02 = a.m02 + (b.m02 * scale);
|
||||
out.m03 = a.m03 + (b.m03 * scale);
|
||||
out.m04 = a.m04 + (b.m04 * scale);
|
||||
out.m05 = a.m05 + (b.m05 * scale);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are equal. (Compared using ===)
|
||||
*
|
||||
* @param {mat23} a - The first matrix.
|
||||
* @param {mat23} b - The second matrix.
|
||||
* @returns {Boolean} True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
static exactEquals(a, b) {
|
||||
return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03 && a.m04 === b.m04 && a.m05 === b.m05;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are approximately equal.
|
||||
*
|
||||
* @param {mat23} a - The first matrix.
|
||||
* @param {mat23} b - The second matrix.
|
||||
* @returns {Boolean} True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
static equals(a, b) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05;
|
||||
let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05;
|
||||
return (
|
||||
Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
|
||||
Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
|
||||
Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default mat23;
|
||||
@@ -0,0 +1,547 @@
|
||||
import { EPSILON } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 2x3 matrix.
|
||||
*
|
||||
* A mat23 contains six elements defined as:
|
||||
* <pre>
|
||||
* [a, c, tx,
|
||||
* b, d, ty]
|
||||
* </pre>
|
||||
* This is a short form for the 3x3 matrix:
|
||||
* <pre>
|
||||
* [a, c, tx,
|
||||
* b, d, ty,
|
||||
* 0, 0, 1]
|
||||
* </pre>
|
||||
* The last row is ignored so the array is shorter and operations are faster.
|
||||
*/
|
||||
class mat23 {
|
||||
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param m00 - Value assigned to element a.
|
||||
* @param m01 - Value assigned to element b.
|
||||
* @param m02 - Value assigned to element c.
|
||||
* @param m03 - Value assigned to element d.
|
||||
* @param m04 - Value assigned to element tx.
|
||||
* @param m05 - Value assigned to element ty.
|
||||
* @return The newly created matrix.
|
||||
*/
|
||||
public static create(m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) {
|
||||
return new mat23(m00, m01, m02, m03, m04, m05);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a matrix.
|
||||
*
|
||||
* @param a - Matrix to clone.
|
||||
* @return The newly created matrix.
|
||||
*/
|
||||
public static clone(a) {
|
||||
return new mat23(
|
||||
a.m00, a.m01,
|
||||
a.m02, a.m03,
|
||||
a.m04, a.m05,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a matrix into another.
|
||||
*
|
||||
* @param out - Matrix to modified.
|
||||
* @param a - The specified matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static copy(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = a.m02;
|
||||
out.m03 = a.m03;
|
||||
out.m04 = a.m04;
|
||||
out.m05 = a.m05;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a matrix as identity matrix.
|
||||
*
|
||||
* @param out - Matrix to modified.
|
||||
* @return out.
|
||||
*/
|
||||
public static identity(out) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 1;
|
||||
out.m04 = 0;
|
||||
out.m05 = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the elements of a matrix to the given values.
|
||||
*
|
||||
* @param out - The matrix to modified.
|
||||
* @param a - Value assigned to element a.
|
||||
* @param b - Value assigned to element b.
|
||||
* @param c - Value assigned to element c.
|
||||
* @param d - Value assigned to element d.
|
||||
* @param tx - Value assigned to element tx.
|
||||
* @param ty - Value assigned to element ty.
|
||||
* @return out.
|
||||
*/
|
||||
public static set(out, a, b, c, d, tx, ty) {
|
||||
out.m00 = a;
|
||||
out.m01 = b;
|
||||
out.m02 = c;
|
||||
out.m03 = d;
|
||||
out.m04 = tx;
|
||||
out.m05 = ty;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts a matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to invert.
|
||||
* @return out.
|
||||
*/
|
||||
public static invert(out, a) {
|
||||
const aa = a.m00, ab = a.m01, ac = a.m02, ad = a.m03,
|
||||
atx = a.m04, aty = a.m05;
|
||||
|
||||
let det = aa * ad - ab * ac;
|
||||
if (!det) {
|
||||
return null;
|
||||
}
|
||||
det = 1.0 / det;
|
||||
|
||||
out.m00 = ad * det;
|
||||
out.m01 = -ab * det;
|
||||
out.m02 = -ac * det;
|
||||
out.m03 = aa * det;
|
||||
out.m04 = (ac * aty - ad * atx) * det;
|
||||
out.m05 = (ab * atx - aa * aty) * det;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the determinant of a matrix.
|
||||
*
|
||||
* @param a - Matrix to calculate.
|
||||
* @return Determinant of a.
|
||||
*/
|
||||
public static determinant(a) {
|
||||
return a.m00 * a.m03 - a.m01 * a.m02;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two matrices explicitly.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiply(out, a, b) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
|
||||
b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05;
|
||||
out.m00 = a0 * b0 + a2 * b1;
|
||||
out.m01 = a1 * b0 + a3 * b1;
|
||||
out.m02 = a0 * b2 + a2 * b3;
|
||||
out.m03 = a1 * b2 + a3 * b3;
|
||||
out.m04 = a0 * b4 + a2 * b5 + a4;
|
||||
out.m05 = a1 * b4 + a3 * b5 + a5;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat23.multiply}.
|
||||
*/
|
||||
public static mul(out, a, b) {
|
||||
return mat23.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a matrix by the given angle.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to rotate.
|
||||
* @param rad - The rotation angle.
|
||||
* @return out
|
||||
*/
|
||||
public static rotate(out, a, rad) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
|
||||
s = Math.sin(rad),
|
||||
c = Math.cos(rad);
|
||||
out.m00 = a0 * c + a2 * s;
|
||||
out.m01 = a1 * c + a3 * s;
|
||||
out.m02 = a0 * -s + a2 * c;
|
||||
out.m03 = a1 * -s + a3 * c;
|
||||
out.m04 = a4;
|
||||
out.m05 = a5;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a matrix with a scale matrix given by a scale vector.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to multiply.
|
||||
* @param v - The scale vector.
|
||||
* @return out
|
||||
**/
|
||||
public static scale(out, a, v) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
|
||||
v0 = v.x, v1 = v.y;
|
||||
out.m00 = a0 * v0;
|
||||
out.m01 = a1 * v0;
|
||||
out.m02 = a2 * v1;
|
||||
out.m03 = a3 * v1;
|
||||
out.m04 = a4;
|
||||
out.m05 = a5;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a matrix with a translation matrix given by a translation offset.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to multiply.
|
||||
* @param v - The translation offset.
|
||||
* @return out.
|
||||
*/
|
||||
public static translate(out, a, v) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
|
||||
v0 = v.x, v1 = v.y;
|
||||
out.m00 = a0;
|
||||
out.m01 = a1;
|
||||
out.m02 = a2;
|
||||
out.m03 = a3;
|
||||
out.m04 = a0 * v0 + a2 * v1 + a4;
|
||||
out.m05 = a1 * v0 + a3 * v1 + a5;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a given angle.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat23.identity(dest);
|
||||
* mat23.rotate(dest, dest, rad);
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param rad - The rotation angle.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromRotation(out, rad) {
|
||||
const s = Math.sin(rad), c = Math.cos(rad);
|
||||
out.m00 = c;
|
||||
out.m01 = s;
|
||||
out.m02 = -s;
|
||||
out.m03 = c;
|
||||
out.m04 = 0;
|
||||
out.m05 = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a scale vector.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat23.identity(dest);
|
||||
* mat23.scale(dest, dest, vec);
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param v - Scale vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromScaling(out, v) {
|
||||
out.m00 = v.m00;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = v.m01;
|
||||
out.m04 = 0;
|
||||
out.m05 = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a translation offset.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat23.identity(dest);
|
||||
* mat23.translate(dest, dest, vec);
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param v - The translation offset.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromTranslation(out, v) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 1;
|
||||
out.m04 = v.x;
|
||||
out.m05 = v.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a rotation, translation offset and scale vector.
|
||||
* This is equivalent to (but faster than):
|
||||
*
|
||||
* mat23.identity(dest);
|
||||
* mat23.translate(dest, vec);
|
||||
* let tmp = mat23.create();
|
||||
* mat23.fromRotation(tmp, rot);
|
||||
* mat23.multiply(dest, dest, tmp);
|
||||
* mat23.fromScaling(tmp, scale);
|
||||
* mat23.multiply(dest, dest, tmp);
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param r - Rotation radian.
|
||||
* @param t - Translation offset.
|
||||
* @param s - Scale vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromRTS(out, r, t, s) {
|
||||
const sr = Math.sin(r), cr = Math.cos(r);
|
||||
out.m00 = cr * s.x;
|
||||
out.m01 = sr * s.x;
|
||||
out.m02 = -sr * s.y;
|
||||
out.m03 = cr * s.y;
|
||||
out.m04 = t.x;
|
||||
out.m05 = t.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of a matrix.
|
||||
*
|
||||
* @param a - The matrix.
|
||||
* @return String representation of this matrix.
|
||||
*/
|
||||
public static str(a) {
|
||||
return `mat23(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store elements of a matrix into array.
|
||||
*
|
||||
* @param out - Array to store result.
|
||||
* @param m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static array(out, m) {
|
||||
out[0] = m.m00;
|
||||
out[1] = m.m01;
|
||||
out[2] = m.m02;
|
||||
out[3] = m.m03;
|
||||
out[4] = m.m04;
|
||||
out[5] = m.m05;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store elements of a matrix into 16 floats array.
|
||||
*
|
||||
* @param out
|
||||
* @param m
|
||||
* @return
|
||||
*/
|
||||
public static array4x4(out, m) {
|
||||
out[0] = m.m00;
|
||||
out[1] = m.m01;
|
||||
out[2] = 0;
|
||||
out[3] = 0;
|
||||
out[4] = m.m02;
|
||||
out[5] = m.m03;
|
||||
out[6] = 0;
|
||||
out[7] = 0;
|
||||
out[8] = 0;
|
||||
out[9] = 0;
|
||||
out[10] = 1;
|
||||
out[11] = 0;
|
||||
out[12] = m.m04;
|
||||
out[13] = m.m05;
|
||||
out[14] = 0;
|
||||
out[15] = 1;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Frobenius norm of a matrix.
|
||||
*
|
||||
* @param a - Matrix to calculate Frobenius norm of.
|
||||
* @return - The frobenius norm.
|
||||
*/
|
||||
public static frob(a) {
|
||||
return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static add(out, a, b) {
|
||||
out.m00 = a.m00 + b.m00;
|
||||
out.m01 = a.m01 + b.m01;
|
||||
out.m02 = a.m02 + b.m02;
|
||||
out.m03 = a.m03 + b.m03;
|
||||
out.m04 = a.m04 + b.m04;
|
||||
out.m05 = a.m05 + b.m05;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts matrix b from matrix a.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static subtract(out, a, b) {
|
||||
out.m00 = a.m00 - b.m00;
|
||||
out.m01 = a.m01 - b.m01;
|
||||
out.m02 = a.m02 - b.m02;
|
||||
out.m03 = a.m03 - b.m03;
|
||||
out.m04 = a.m04 - b.m04;
|
||||
out.m05 = a.m05 - b.m05;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat23.subtract}.
|
||||
*/
|
||||
public static sub(out, a, b) {
|
||||
return mat23.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply each element of a matrix by a scalar number.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to scale
|
||||
* @param b - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiplyScalar(out, a, b) {
|
||||
out.m00 = a.m00 * b;
|
||||
out.m01 = a.m01 * b;
|
||||
out.m02 = a.m02 * b;
|
||||
out.m03 = a.m03 * b;
|
||||
out.m04 = a.m04 * b;
|
||||
out.m05 = a.m05 * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices after multiplying each element of the second operand by a scalar number.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param scale - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiplyScalarAndAdd(out, a, b, scale) {
|
||||
out.m00 = a.m00 + (b.m00 * scale);
|
||||
out.m01 = a.m01 + (b.m01 * scale);
|
||||
out.m02 = a.m02 + (b.m02 * scale);
|
||||
out.m03 = a.m03 + (b.m03 * scale);
|
||||
out.m04 = a.m04 + (b.m04 * scale);
|
||||
out.m05 = a.m05 + (b.m05 * scale);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are equal. (Compared using ===)
|
||||
*
|
||||
* @param a - The first matrix.
|
||||
* @param b - The second matrix.
|
||||
* @return True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
public static exactEquals(a, b) {
|
||||
return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03 && a.m04 === b.m04 && a.m05 === b.m05;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are approximately equal.
|
||||
*
|
||||
* @param a - The first matrix.
|
||||
* @param b - The second matrix.
|
||||
* @return True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
public static equals(a, b) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05;
|
||||
const b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05;
|
||||
return (
|
||||
Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
|
||||
Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
|
||||
Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5))
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param m00 - Value assigned to element a.
|
||||
* @param m01 - Value assigned to element b.
|
||||
* @param m02 - Value assigned to element c.
|
||||
* @param m03 - Value assigned to element d.
|
||||
* @param m04 - Value assigned to element tx.
|
||||
* @param m05 - Value assigned to element ty.
|
||||
*/
|
||||
constructor(m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) {
|
||||
/**
|
||||
* The element a.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m00 = m00;
|
||||
|
||||
/**
|
||||
* The element b.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m01 = m01;
|
||||
|
||||
/**
|
||||
* The element c.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m02 = m02;
|
||||
|
||||
/**
|
||||
* The element d.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m03 = m03;
|
||||
|
||||
/**
|
||||
* The element tx.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m04 = m04;
|
||||
|
||||
/**
|
||||
* The element ty.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m05 = m05;
|
||||
}
|
||||
}
|
||||
|
||||
export default mat23;
|
||||
@@ -1,851 +0,0 @@
|
||||
import { EPSILON } from './utils';
|
||||
import vec3 from './vec3';
|
||||
|
||||
/**
|
||||
* Mathematical 3x3 matrix.
|
||||
*
|
||||
* NOTE: we use column-major matrix for all matrix calculation.
|
||||
*
|
||||
* This may lead to some confusion when referencing OpenGL documentation,
|
||||
* however, which represents out all matricies in column-major format.
|
||||
* This means that while in code a matrix may be typed out as:
|
||||
*
|
||||
* [1, 0, 0, 0,
|
||||
* 0, 1, 0, 0,
|
||||
* 0, 0, 1, 0,
|
||||
* x, y, z, 0]
|
||||
*
|
||||
* The same matrix in the [OpenGL documentation](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml)
|
||||
* is written as:
|
||||
*
|
||||
* 1 0 0 x
|
||||
* 0 1 0 y
|
||||
* 0 0 1 z
|
||||
* 0 0 0 0
|
||||
*
|
||||
* Please rest assured, however, that they are the same thing!
|
||||
* This is not unique to glMatrix, either, as OpenGL developers have long been confused by the
|
||||
* apparent lack of consistency between the memory layout and the documentation.
|
||||
*/
|
||||
class mat3 {
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param {Number} m00 - Value assigned to element at column 0 row 0.
|
||||
* @param {Number} m01 - Value assigned to element at column 0 row 1.
|
||||
* @param {Number} m02 - Value assigned to element at column 0 row 2.
|
||||
* @param {Number} m03 - Value assigned to element at column 1 row 0.
|
||||
* @param {Number} m04 - Value assigned to element at column 1 row 1.
|
||||
* @param {Number} m05 - Value assigned to element at column 1 row 2.
|
||||
* @param {Number} m06 - Value assigned to element at column 2 row 0.
|
||||
* @param {Number} m07 - Value assigned to element at column 2 row 1.
|
||||
* @param {Number} m08 - Value assigned to element at column 2 row 2.
|
||||
*/
|
||||
constructor(
|
||||
m00 = 1, m01 = 0, m02 = 0,
|
||||
m03 = 0, m04 = 1, m05 = 0,
|
||||
m06 = 0, m07 = 0, m08 = 1
|
||||
) {
|
||||
/**
|
||||
* The element at column 0 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m00 = m00;
|
||||
|
||||
/**
|
||||
* The element at column 0 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m01 = m01;
|
||||
|
||||
/**
|
||||
* The element at column 0 row 2.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m02 = m02;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m03 = m03;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m04 = m04;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 2.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m05 = m05;
|
||||
|
||||
/**
|
||||
* The element at column 2 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m06 = m06;
|
||||
|
||||
/**
|
||||
* The element at column 2 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m07 = m07;
|
||||
|
||||
/**
|
||||
* The element at column 2 row 2.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m08 = m08;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param {Number} m00 - Value assigned to element at column 0 row 0.
|
||||
* @param {Number} m01 - Value assigned to element at column 0 row 1.
|
||||
* @param {Number} m02 - Value assigned to element at column 0 row 2.
|
||||
* @param {Number} m03 - Value assigned to element at column 1 row 0.
|
||||
* @param {Number} m04 - Value assigned to element at column 1 row 1.
|
||||
* @param {Number} m05 - Value assigned to element at column 1 row 2.
|
||||
* @param {Number} m06 - Value assigned to element at column 2 row 0.
|
||||
* @param {Number} m07 - Value assigned to element at column 2 row 1.
|
||||
* @param {Number} m08 - Value assigned to element at column 2 row 2.
|
||||
* @returns {mat3} The newly created matrix.
|
||||
*/
|
||||
static create(m00 = 1, m01 = 0, m02 = 0, m03 = 0, m04 = 1, m05 = 0, m06 = 0, m07 = 0, m08 = 1) {
|
||||
return new mat3(m00, m01, m02, m03, m04, m05, m06, m07, m08);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a matrix.
|
||||
*
|
||||
* @param {mat3} a - Matrix to clone.
|
||||
* @returns {mat3} The newly created matrix.
|
||||
*/
|
||||
static clone(a) {
|
||||
return new mat3(
|
||||
a.m00, a.m01, a.m02,
|
||||
a.m03, a.m04, a.m05,
|
||||
a.m06, a.m07, a.m08
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a matrix into another.
|
||||
*
|
||||
* @param {mat3} out - Matrix to modified.
|
||||
* @param {mat3} a - The specified matrix.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static copy(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = a.m02;
|
||||
out.m03 = a.m03;
|
||||
out.m04 = a.m04;
|
||||
out.m05 = a.m05;
|
||||
out.m06 = a.m06;
|
||||
out.m07 = a.m07;
|
||||
out.m08 = a.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the elements of a matrix to the given values.
|
||||
*
|
||||
* @param {mat3} out - The matrix to modified.
|
||||
* @param {Number} m00 - Value assigned to element at column 0 row 0.
|
||||
* @param {Number} m01 - Value assigned to element at column 0 row 1.
|
||||
* @param {Number} m02 - Value assigned to element at column 0 row 2.
|
||||
* @param {Number} m10 - Value assigned to element at column 1 row 0.
|
||||
* @param {Number} m11 - Value assigned to element at column 1 row 1.
|
||||
* @param {Number} m12 - Value assigned to element at column 1 row 2.
|
||||
* @param {Number} m20 - Value assigned to element at column 2 row 0.
|
||||
* @param {Number} m21 - Value assigned to element at column 2 row 1.
|
||||
* @param {Number} m22 - Value assigned to element at column 2 row 2.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static set(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) {
|
||||
out.m00 = m00;
|
||||
out.m01 = m01;
|
||||
out.m02 = m02;
|
||||
out.m03 = m10;
|
||||
out.m04 = m11;
|
||||
out.m05 = m12;
|
||||
out.m06 = m20;
|
||||
out.m07 = m21;
|
||||
out.m08 = m22;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* return an identity matrix.
|
||||
*
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static identity(out) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 0;
|
||||
out.m04 = 1;
|
||||
out.m05 = 0;
|
||||
out.m06 = 0;
|
||||
out.m07 = 0;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transposes a matrix.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - Matrix to transpose.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static transpose(out, a) {
|
||||
// If we are transposing ourselves we can skip a few steps but have to cache some values
|
||||
if (out === a) {
|
||||
let a01 = a.m01, a02 = a.m02, a12 = a.m05;
|
||||
out.m01 = a.m03;
|
||||
out.m02 = a.m06;
|
||||
out.m03 = a01;
|
||||
out.m05 = a.m07;
|
||||
out.m06 = a02;
|
||||
out.m07 = a12;
|
||||
} else {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m03;
|
||||
out.m02 = a.m06;
|
||||
out.m03 = a.m01;
|
||||
out.m04 = a.m04;
|
||||
out.m05 = a.m07;
|
||||
out.m06 = a.m02;
|
||||
out.m07 = a.m05;
|
||||
out.m08 = a.m08;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts a matrix.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - Matrix to invert.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static invert(out, a) {
|
||||
let a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
let b01 = a22 * a11 - a12 * a21;
|
||||
let b11 = -a22 * a10 + a12 * a20;
|
||||
let b21 = a21 * a10 - a11 * a20;
|
||||
|
||||
// Calculate the determinant
|
||||
let det = a00 * b01 + a01 * b11 + a02 * b21;
|
||||
|
||||
if (!det) {
|
||||
return null;
|
||||
}
|
||||
det = 1.0 / det;
|
||||
|
||||
out.m00 = b01 * det;
|
||||
out.m01 = (-a22 * a01 + a02 * a21) * det;
|
||||
out.m02 = (a12 * a01 - a02 * a11) * det;
|
||||
out.m03 = b11 * det;
|
||||
out.m04 = (a22 * a00 - a02 * a20) * det;
|
||||
out.m05 = (-a12 * a00 + a02 * a10) * det;
|
||||
out.m06 = b21 * det;
|
||||
out.m07 = (-a21 * a00 + a01 * a20) * det;
|
||||
out.m08 = (a11 * a00 - a01 * a10) * det;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the adjugate of a matrix.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - Matrix to calculate.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static adjoint(out, a) {
|
||||
let a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
out.m00 = (a11 * a22 - a12 * a21);
|
||||
out.m01 = (a02 * a21 - a01 * a22);
|
||||
out.m02 = (a01 * a12 - a02 * a11);
|
||||
out.m03 = (a12 * a20 - a10 * a22);
|
||||
out.m04 = (a00 * a22 - a02 * a20);
|
||||
out.m05 = (a02 * a10 - a00 * a12);
|
||||
out.m06 = (a10 * a21 - a11 * a20);
|
||||
out.m07 = (a01 * a20 - a00 * a21);
|
||||
out.m08 = (a00 * a11 - a01 * a10);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the determinant of a matrix.
|
||||
*
|
||||
* @param {mat3} a - Matrix to calculate.
|
||||
* @returns {Number} Determinant of a.
|
||||
*/
|
||||
static determinant(a) {
|
||||
let a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two matrices explicitly.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - The first operand.
|
||||
* @param {mat3} b - The second operand.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static multiply(out, a, b) {
|
||||
let a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
let b00 = b.m00, b01 = b.m01, b02 = b.m02;
|
||||
let b10 = b.m03, b11 = b.m04, b12 = b.m05;
|
||||
let b20 = b.m06, b21 = b.m07, b22 = b.m08;
|
||||
|
||||
out.m00 = b00 * a00 + b01 * a10 + b02 * a20;
|
||||
out.m01 = b00 * a01 + b01 * a11 + b02 * a21;
|
||||
out.m02 = b00 * a02 + b01 * a12 + b02 * a22;
|
||||
|
||||
out.m03 = b10 * a00 + b11 * a10 + b12 * a20;
|
||||
out.m04 = b10 * a01 + b11 * a11 + b12 * a21;
|
||||
out.m05 = b10 * a02 + b11 * a12 + b12 * a22;
|
||||
|
||||
out.m06 = b20 * a00 + b21 * a10 + b22 * a20;
|
||||
out.m07 = b20 * a01 + b21 * a11 + b22 * a21;
|
||||
out.m08 = b20 * a02 + b21 * a12 + b22 * a22;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat3.multiply}.
|
||||
*/
|
||||
static mul(out, a, b) {
|
||||
return mat3.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a matrix with a translation matrix given by a translation offset.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - Matrix to multiply.
|
||||
* @param {vec2} v - The translation offset.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static translate(out, a, v) {
|
||||
let a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
let x = v.x, y = v.y;
|
||||
|
||||
out.m00 = a00;
|
||||
out.m01 = a01;
|
||||
out.m02 = a02;
|
||||
|
||||
out.m03 = a10;
|
||||
out.m04 = a11;
|
||||
out.m05 = a12;
|
||||
|
||||
out.m06 = x * a00 + y * a10 + a20;
|
||||
out.m07 = x * a01 + y * a11 + a21;
|
||||
out.m08 = x * a02 + y * a12 + a22;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a matrix by the given angle.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - Matrix to rotate.
|
||||
* @param {Number} rad - The rotation angle.
|
||||
* @returns {mat3} out
|
||||
*/
|
||||
static rotate(out, a, rad) {
|
||||
let a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
let s = Math.sin(rad);
|
||||
let c = Math.cos(rad);
|
||||
|
||||
out.m00 = c * a00 + s * a10;
|
||||
out.m01 = c * a01 + s * a11;
|
||||
out.m02 = c * a02 + s * a12;
|
||||
|
||||
out.m03 = c * a10 - s * a00;
|
||||
out.m04 = c * a11 - s * a01;
|
||||
out.m05 = c * a12 - s * a02;
|
||||
|
||||
out.m06 = a20;
|
||||
out.m07 = a21;
|
||||
out.m08 = a22;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a matrix with a scale matrix given by a scale vector.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - Matrix to multiply.
|
||||
* @param {vec2} v - The scale vector.
|
||||
* @returns {mat3} out
|
||||
**/
|
||||
static scale(out, a, v) {
|
||||
let x = v.x, y = v.y;
|
||||
|
||||
out.m00 = x * a.m00;
|
||||
out.m01 = x * a.m01;
|
||||
out.m02 = x * a.m02;
|
||||
|
||||
out.m03 = y * a.m03;
|
||||
out.m04 = y * a.m04;
|
||||
out.m05 = y * a.m05;
|
||||
|
||||
out.m06 = a.m06;
|
||||
out.m07 = a.m07;
|
||||
out.m08 = a.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the upper-left 3x3 values of a 4x4 matrix into a 3x3 matrix.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat4} a - The 4x4 matrix.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static fromMat4(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = a.m02;
|
||||
out.m03 = a.m04;
|
||||
out.m04 = a.m05;
|
||||
out.m05 = a.m06;
|
||||
out.m06 = a.m08;
|
||||
out.m07 = a.m09;
|
||||
out.m08 = a.m10;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a translation offset.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat3.identity(dest);
|
||||
* mat3.translate(dest, dest, vec);
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {vec2} v - The translation offset.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static fromTranslation(out, v) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 0;
|
||||
out.m04 = 1;
|
||||
out.m05 = 0;
|
||||
out.m06 = v.x;
|
||||
out.m07 = v.y;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a given angle.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat3.identity(dest);
|
||||
* mat3.rotate(dest, dest, rad);
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {Number} rad - The rotation angle.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static fromRotation(out, rad) {
|
||||
let s = Math.sin(rad), c = Math.cos(rad);
|
||||
|
||||
out.m00 = c;
|
||||
out.m01 = s;
|
||||
out.m02 = 0;
|
||||
|
||||
out.m03 = -s;
|
||||
out.m04 = c;
|
||||
out.m05 = 0;
|
||||
|
||||
out.m06 = 0;
|
||||
out.m07 = 0;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a scale vector.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat3.identity(dest);
|
||||
* mat3.scale(dest, dest, vec);
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {vec2} v - Scale vector.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static fromScaling(out, v) {
|
||||
out.m00 = v.x;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
|
||||
out.m03 = 0;
|
||||
out.m04 = v.y;
|
||||
out.m05 = 0;
|
||||
|
||||
out.m06 = 0;
|
||||
out.m07 = 0;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the values from a 2x3 matrix into a 3x3 matrix.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat23} a - The 2x3 matrix.
|
||||
* @returns {mat3} out.
|
||||
**/
|
||||
static fromMat2d(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = 0;
|
||||
|
||||
out.m03 = a.m02;
|
||||
out.m04 = a.m03;
|
||||
out.m05 = 0;
|
||||
|
||||
out.m06 = a.m04;
|
||||
out.m07 = a.m05;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a 3x3 matrix from the given quaternion.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {quat} q - The quaternion.
|
||||
*
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static fromQuat(out, q) {
|
||||
let x = q.x, y = q.y, z = q.z, w = q.w;
|
||||
let x2 = x + x;
|
||||
let y2 = y + y;
|
||||
let z2 = z + z;
|
||||
|
||||
let xx = x * x2;
|
||||
let yx = y * x2;
|
||||
let yy = y * y2;
|
||||
let zx = z * x2;
|
||||
let zy = z * y2;
|
||||
let zz = z * z2;
|
||||
let wx = w * x2;
|
||||
let wy = w * y2;
|
||||
let wz = w * z2;
|
||||
|
||||
out.m00 = 1 - yy - zz;
|
||||
out.m03 = yx - wz;
|
||||
out.m06 = zx + wy;
|
||||
|
||||
out.m01 = yx + wz;
|
||||
out.m04 = 1 - xx - zz;
|
||||
out.m07 = zy - wx;
|
||||
|
||||
out.m02 = zx - wy;
|
||||
out.m05 = zy + wx;
|
||||
out.m08 = 1 - xx - yy;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a 3x3 matrix from view direction and up direction.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {vec3} view - View direction (must be normalized).
|
||||
* @param {vec3} [up] - Up direction, default is (0,1,0) (must be normalized).
|
||||
*
|
||||
* @returns {mat3} out
|
||||
*/
|
||||
static fromViewUp(out, view, up) {
|
||||
let _fromViewUpIIFE = (function () {
|
||||
let default_up = vec3.create(0, 1, 0);
|
||||
let x = vec3.create(0, 0, 0);
|
||||
let y = vec3.create(0, 0, 0);
|
||||
|
||||
return function (out, view, up) {
|
||||
if (vec3.sqrMag(view) < EPSILON * EPSILON) {
|
||||
mat3.identity(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
up = up || default_up;
|
||||
vec3.normalize(x, vec3.cross(x, up, view));
|
||||
|
||||
if (vec3.sqrMag(x) < EPSILON * EPSILON) {
|
||||
mat3.identity(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
vec3.cross(y, view, x);
|
||||
mat3.set(
|
||||
out,
|
||||
x.x, x.y, x.z,
|
||||
y.x, y.y, y.z,
|
||||
view.x, view.y, view.z
|
||||
);
|
||||
|
||||
return out;
|
||||
};
|
||||
})();
|
||||
return _fromViewUpIIFE(out, view, up);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat4} a - A 4x4 matrix to derive the normal matrix from.
|
||||
*
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static normalFromMat4(out, a) {
|
||||
let a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03,
|
||||
a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07,
|
||||
a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11,
|
||||
a30 = a.m12, a31 = a.m13, a32 = a.m14, a33 = a.m15;
|
||||
|
||||
let b00 = a00 * a11 - a01 * a10;
|
||||
let b01 = a00 * a12 - a02 * a10;
|
||||
let b02 = a00 * a13 - a03 * a10;
|
||||
let b03 = a01 * a12 - a02 * a11;
|
||||
let b04 = a01 * a13 - a03 * a11;
|
||||
let b05 = a02 * a13 - a03 * a12;
|
||||
let b06 = a20 * a31 - a21 * a30;
|
||||
let b07 = a20 * a32 - a22 * a30;
|
||||
let b08 = a20 * a33 - a23 * a30;
|
||||
let b09 = a21 * a32 - a22 * a31;
|
||||
let b10 = a21 * a33 - a23 * a31;
|
||||
let b11 = a22 * a33 - a23 * a32;
|
||||
|
||||
// Calculate the determinant
|
||||
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
|
||||
|
||||
if (!det) {
|
||||
return null;
|
||||
}
|
||||
det = 1.0 / det;
|
||||
|
||||
out.m00 = (a11 * b11 - a12 * b10 + a13 * b09) * det;
|
||||
out.m01 = (a12 * b08 - a10 * b11 - a13 * b07) * det;
|
||||
out.m02 = (a10 * b10 - a11 * b08 + a13 * b06) * det;
|
||||
|
||||
out.m03 = (a02 * b10 - a01 * b11 - a03 * b09) * det;
|
||||
out.m04 = (a00 * b11 - a02 * b08 + a03 * b07) * det;
|
||||
out.m05 = (a01 * b08 - a00 * b10 - a03 * b06) * det;
|
||||
|
||||
out.m06 = (a31 * b05 - a32 * b04 + a33 * b03) * det;
|
||||
out.m07 = (a32 * b02 - a30 * b05 - a33 * b01) * det;
|
||||
out.m08 = (a30 * b04 - a31 * b02 + a33 * b00) * det;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of a matrix.
|
||||
*
|
||||
* @param {mat3} a - The matrix.
|
||||
* @returns {String} String representation of this matrix.
|
||||
*/
|
||||
static str(a) {
|
||||
return `mat3(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05}, ${a.m06}, ${a.m07}, ${a.m08})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store elements of a matrix into array.
|
||||
*
|
||||
* @param {array} out - Array to store result.
|
||||
* @param {mat3} m - The matrix.
|
||||
* @returns {Array} out.
|
||||
*/
|
||||
static array(out, m) {
|
||||
out[0] = m.m00;
|
||||
out[1] = m.m01;
|
||||
out[2] = m.m02;
|
||||
out[3] = m.m03;
|
||||
out[4] = m.m04;
|
||||
out[5] = m.m05;
|
||||
out[6] = m.m06;
|
||||
out[7] = m.m07;
|
||||
out[8] = m.m08;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Frobenius norm of a matrix.
|
||||
*
|
||||
* @param {mat3} a - Matrix to calculate Frobenius norm of.
|
||||
* @returns {Number} - The frobenius norm.
|
||||
*/
|
||||
static frob(a) {
|
||||
return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + Math.pow(a.m06, 2) + Math.pow(a.m07, 2) + Math.pow(a.m08, 2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - The first operand.
|
||||
* @param {mat3} b - The second operand.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static add(out, a, b) {
|
||||
out.m00 = a.m00 + b.m00;
|
||||
out.m01 = a.m01 + b.m01;
|
||||
out.m02 = a.m02 + b.m02;
|
||||
out.m03 = a.m03 + b.m03;
|
||||
out.m04 = a.m04 + b.m04;
|
||||
out.m05 = a.m05 + b.m05;
|
||||
out.m06 = a.m06 + b.m06;
|
||||
out.m07 = a.m07 + b.m07;
|
||||
out.m08 = a.m08 + b.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts matrix b from matrix a.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - The first operand.
|
||||
* @param {mat3} b - The second operand.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static subtract(out, a, b) {
|
||||
out.m00 = a.m00 - b.m00;
|
||||
out.m01 = a.m01 - b.m01;
|
||||
out.m02 = a.m02 - b.m02;
|
||||
out.m03 = a.m03 - b.m03;
|
||||
out.m04 = a.m04 - b.m04;
|
||||
out.m05 = a.m05 - b.m05;
|
||||
out.m06 = a.m06 - b.m06;
|
||||
out.m07 = a.m07 - b.m07;
|
||||
out.m08 = a.m08 - b.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat3.subtract}.
|
||||
*/
|
||||
static sub(out, a, b) {
|
||||
return mat3.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply each element of a matrix by a scalar number.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - Matrix to scale
|
||||
* @param {Number} b - The scale number.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static multiplyScalar(out, a, b) {
|
||||
out.m00 = a.m00 * b;
|
||||
out.m01 = a.m01 * b;
|
||||
out.m02 = a.m02 * b;
|
||||
out.m03 = a.m03 * b;
|
||||
out.m04 = a.m04 * b;
|
||||
out.m05 = a.m05 * b;
|
||||
out.m06 = a.m06 * b;
|
||||
out.m07 = a.m07 * b;
|
||||
out.m08 = a.m08 * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices after multiplying each element of the second operand by a scalar number.
|
||||
*
|
||||
* @param {mat3} out - Matrix to store result.
|
||||
* @param {mat3} a - The first operand.
|
||||
* @param {mat3} b - The second operand.
|
||||
* @param {Number} scale - The scale number.
|
||||
* @returns {mat3} out.
|
||||
*/
|
||||
static multiplyScalarAndAdd(out, a, b, scale) {
|
||||
out.m00 = a.m00 + (b.m00 * scale);
|
||||
out.m01 = a.m01 + (b.m01 * scale);
|
||||
out.m02 = a.m02 + (b.m02 * scale);
|
||||
out.m03 = a.m03 + (b.m03 * scale);
|
||||
out.m04 = a.m04 + (b.m04 * scale);
|
||||
out.m05 = a.m05 + (b.m05 * scale);
|
||||
out.m06 = a.m06 + (b.m06 * scale);
|
||||
out.m07 = a.m07 + (b.m07 * scale);
|
||||
out.m08 = a.m08 + (b.m08 * scale);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are equal. (Compared using ===)
|
||||
*
|
||||
* @param {mat3} a - The first matrix.
|
||||
* @param {mat3} b - The second matrix.
|
||||
* @returns {Boolean} True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
static exactEquals(a, b) {
|
||||
return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 &&
|
||||
a.m03 === b.m03 && a.m04 === b.m04 && a.m05 === b.m05 &&
|
||||
a.m06 === b.m06 && a.m07 === b.m07 && a.m08 === b.m08;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are approximately equal.
|
||||
*
|
||||
* @param {mat3} a - The first matrix.
|
||||
* @param {mat3} b - The second matrix.
|
||||
* @returns {Boolean} True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
static equals(a, b) {
|
||||
let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05, a6 = a.m06, a7 = a.m07, a8 = a.m08;
|
||||
let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05, b6 = b.m06, b7 = b.m07, b8 = b.m08;
|
||||
return (
|
||||
Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
|
||||
Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
|
||||
Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) &&
|
||||
Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) &&
|
||||
Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) &&
|
||||
Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default mat3;
|
||||
@@ -0,0 +1,851 @@
|
||||
import { EPSILON } from './utils';
|
||||
import vec3 from './vec3';
|
||||
|
||||
/**
|
||||
* Mathematical 3x3 matrix.
|
||||
*
|
||||
* NOTE: we use column-major matrix for all matrix calculation.
|
||||
*
|
||||
* This may lead to some confusion when referencing OpenGL documentation,
|
||||
* however, which represents out all matricies in column-major format.
|
||||
* This means that while in code a matrix may be typed out as:
|
||||
*
|
||||
* [1, 0, 0, 0,
|
||||
* 0, 1, 0, 0,
|
||||
* 0, 0, 1, 0,
|
||||
* x, y, z, 0]
|
||||
*
|
||||
* The same matrix in the [OpenGL documentation](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml)
|
||||
* is written as:
|
||||
*
|
||||
* 1 0 0 x
|
||||
* 0 1 0 y
|
||||
* 0 0 1 z
|
||||
* 0 0 0 0
|
||||
*
|
||||
* Please rest assured, however, that they are the same thing!
|
||||
* This is not unique to glMatrix, either, as OpenGL developers have long been confused by the
|
||||
* apparent lack of consistency between the memory layout and the documentation.
|
||||
*/
|
||||
class mat3 {
|
||||
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param m00 - Value assigned to element at column 0 row 0.
|
||||
* @param m01 - Value assigned to element at column 0 row 1.
|
||||
* @param m02 - Value assigned to element at column 0 row 2.
|
||||
* @param m03 - Value assigned to element at column 1 row 0.
|
||||
* @param m04 - Value assigned to element at column 1 row 1.
|
||||
* @param m05 - Value assigned to element at column 1 row 2.
|
||||
* @param m06 - Value assigned to element at column 2 row 0.
|
||||
* @param m07 - Value assigned to element at column 2 row 1.
|
||||
* @param m08 - Value assigned to element at column 2 row 2.
|
||||
* @return The newly created matrix.
|
||||
*/
|
||||
public static create(m00 = 1, m01 = 0, m02 = 0, m03 = 0, m04 = 1, m05 = 0, m06 = 0, m07 = 0, m08 = 1) {
|
||||
return new mat3(m00, m01, m02, m03, m04, m05, m06, m07, m08);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a matrix.
|
||||
*
|
||||
* @param a - Matrix to clone.
|
||||
* @return The newly created matrix.
|
||||
*/
|
||||
public static clone(a) {
|
||||
return new mat3(
|
||||
a.m00, a.m01, a.m02,
|
||||
a.m03, a.m04, a.m05,
|
||||
a.m06, a.m07, a.m08,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a matrix into another.
|
||||
*
|
||||
* @param out - Matrix to modified.
|
||||
* @param a - The specified matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static copy(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = a.m02;
|
||||
out.m03 = a.m03;
|
||||
out.m04 = a.m04;
|
||||
out.m05 = a.m05;
|
||||
out.m06 = a.m06;
|
||||
out.m07 = a.m07;
|
||||
out.m08 = a.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the elements of a matrix to the given values.
|
||||
*
|
||||
* @param out - The matrix to modified.
|
||||
* @param m00 - Value assigned to element at column 0 row 0.
|
||||
* @param m01 - Value assigned to element at column 0 row 1.
|
||||
* @param m02 - Value assigned to element at column 0 row 2.
|
||||
* @param m10 - Value assigned to element at column 1 row 0.
|
||||
* @param m11 - Value assigned to element at column 1 row 1.
|
||||
* @param m12 - Value assigned to element at column 1 row 2.
|
||||
* @param m20 - Value assigned to element at column 2 row 0.
|
||||
* @param m21 - Value assigned to element at column 2 row 1.
|
||||
* @param m22 - Value assigned to element at column 2 row 2.
|
||||
* @return out.
|
||||
*/
|
||||
public static set(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) {
|
||||
out.m00 = m00;
|
||||
out.m01 = m01;
|
||||
out.m02 = m02;
|
||||
out.m03 = m10;
|
||||
out.m04 = m11;
|
||||
out.m05 = m12;
|
||||
out.m06 = m20;
|
||||
out.m07 = m21;
|
||||
out.m08 = m22;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* return an identity matrix.
|
||||
*
|
||||
* @return out.
|
||||
*/
|
||||
public static identity(out) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 0;
|
||||
out.m04 = 1;
|
||||
out.m05 = 0;
|
||||
out.m06 = 0;
|
||||
out.m07 = 0;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transposes a matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to transpose.
|
||||
* @return out.
|
||||
*/
|
||||
public static transpose(out, a) {
|
||||
// If we are transposing ourselves we can skip a few steps but have to cache some values
|
||||
if (out === a) {
|
||||
const a01 = a.m01, a02 = a.m02, a12 = a.m05;
|
||||
out.m01 = a.m03;
|
||||
out.m02 = a.m06;
|
||||
out.m03 = a01;
|
||||
out.m05 = a.m07;
|
||||
out.m06 = a02;
|
||||
out.m07 = a12;
|
||||
} else {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m03;
|
||||
out.m02 = a.m06;
|
||||
out.m03 = a.m01;
|
||||
out.m04 = a.m04;
|
||||
out.m05 = a.m07;
|
||||
out.m06 = a.m02;
|
||||
out.m07 = a.m05;
|
||||
out.m08 = a.m08;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts a matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to invert.
|
||||
* @return out.
|
||||
*/
|
||||
public static invert(out, a) {
|
||||
const a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
const b01 = a22 * a11 - a12 * a21;
|
||||
const b11 = -a22 * a10 + a12 * a20;
|
||||
const b21 = a21 * a10 - a11 * a20;
|
||||
|
||||
// Calculate the determinant
|
||||
let det = a00 * b01 + a01 * b11 + a02 * b21;
|
||||
|
||||
if (!det) {
|
||||
return null;
|
||||
}
|
||||
det = 1.0 / det;
|
||||
|
||||
out.m00 = b01 * det;
|
||||
out.m01 = (-a22 * a01 + a02 * a21) * det;
|
||||
out.m02 = (a12 * a01 - a02 * a11) * det;
|
||||
out.m03 = b11 * det;
|
||||
out.m04 = (a22 * a00 - a02 * a20) * det;
|
||||
out.m05 = (-a12 * a00 + a02 * a10) * det;
|
||||
out.m06 = b21 * det;
|
||||
out.m07 = (-a21 * a00 + a01 * a20) * det;
|
||||
out.m08 = (a11 * a00 - a01 * a10) * det;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the adjugate of a matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to calculate.
|
||||
* @return out.
|
||||
*/
|
||||
public static adjoint(out, a) {
|
||||
const a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
out.m00 = (a11 * a22 - a12 * a21);
|
||||
out.m01 = (a02 * a21 - a01 * a22);
|
||||
out.m02 = (a01 * a12 - a02 * a11);
|
||||
out.m03 = (a12 * a20 - a10 * a22);
|
||||
out.m04 = (a00 * a22 - a02 * a20);
|
||||
out.m05 = (a02 * a10 - a00 * a12);
|
||||
out.m06 = (a10 * a21 - a11 * a20);
|
||||
out.m07 = (a01 * a20 - a00 * a21);
|
||||
out.m08 = (a00 * a11 - a01 * a10);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the determinant of a matrix.
|
||||
*
|
||||
* @param a - Matrix to calculate.
|
||||
* @return Determinant of a.
|
||||
*/
|
||||
public static determinant(a) {
|
||||
const a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two matrices explicitly.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiply(out, a, b) {
|
||||
const a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
const b00 = b.m00, b01 = b.m01, b02 = b.m02;
|
||||
const b10 = b.m03, b11 = b.m04, b12 = b.m05;
|
||||
const b20 = b.m06, b21 = b.m07, b22 = b.m08;
|
||||
|
||||
out.m00 = b00 * a00 + b01 * a10 + b02 * a20;
|
||||
out.m01 = b00 * a01 + b01 * a11 + b02 * a21;
|
||||
out.m02 = b00 * a02 + b01 * a12 + b02 * a22;
|
||||
|
||||
out.m03 = b10 * a00 + b11 * a10 + b12 * a20;
|
||||
out.m04 = b10 * a01 + b11 * a11 + b12 * a21;
|
||||
out.m05 = b10 * a02 + b11 * a12 + b12 * a22;
|
||||
|
||||
out.m06 = b20 * a00 + b21 * a10 + b22 * a20;
|
||||
out.m07 = b20 * a01 + b21 * a11 + b22 * a21;
|
||||
out.m08 = b20 * a02 + b21 * a12 + b22 * a22;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat3.multiply}.
|
||||
*/
|
||||
public static mul(out, a, b) {
|
||||
return mat3.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a matrix with a translation matrix given by a translation offset.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to multiply.
|
||||
* @param v - The translation offset.
|
||||
* @return out.
|
||||
*/
|
||||
public static translate(out, a, v) {
|
||||
const a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
const x = v.x, y = v.y;
|
||||
|
||||
out.m00 = a00;
|
||||
out.m01 = a01;
|
||||
out.m02 = a02;
|
||||
|
||||
out.m03 = a10;
|
||||
out.m04 = a11;
|
||||
out.m05 = a12;
|
||||
|
||||
out.m06 = x * a00 + y * a10 + a20;
|
||||
out.m07 = x * a01 + y * a11 + a21;
|
||||
out.m08 = x * a02 + y * a12 + a22;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a matrix by the given angle.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to rotate.
|
||||
* @param rad - The rotation angle.
|
||||
* @return out
|
||||
*/
|
||||
public static rotate(out, a, rad) {
|
||||
const a00 = a.m00, a01 = a.m01, a02 = a.m02,
|
||||
a10 = a.m03, a11 = a.m04, a12 = a.m05,
|
||||
a20 = a.m06, a21 = a.m07, a22 = a.m08;
|
||||
|
||||
const s = Math.sin(rad);
|
||||
const c = Math.cos(rad);
|
||||
|
||||
out.m00 = c * a00 + s * a10;
|
||||
out.m01 = c * a01 + s * a11;
|
||||
out.m02 = c * a02 + s * a12;
|
||||
|
||||
out.m03 = c * a10 - s * a00;
|
||||
out.m04 = c * a11 - s * a01;
|
||||
out.m05 = c * a12 - s * a02;
|
||||
|
||||
out.m06 = a20;
|
||||
out.m07 = a21;
|
||||
out.m08 = a22;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a matrix with a scale matrix given by a scale vector.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to multiply.
|
||||
* @param v - The scale vector.
|
||||
* @return out
|
||||
**/
|
||||
public static scale(out, a, v) {
|
||||
const x = v.x, y = v.y;
|
||||
|
||||
out.m00 = x * a.m00;
|
||||
out.m01 = x * a.m01;
|
||||
out.m02 = x * a.m02;
|
||||
|
||||
out.m03 = y * a.m03;
|
||||
out.m04 = y * a.m04;
|
||||
out.m05 = y * a.m05;
|
||||
|
||||
out.m06 = a.m06;
|
||||
out.m07 = a.m07;
|
||||
out.m08 = a.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the upper-left 3x3 values of a 4x4 matrix into a 3x3 matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The 4x4 matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromMat4(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = a.m02;
|
||||
out.m03 = a.m04;
|
||||
out.m04 = a.m05;
|
||||
out.m05 = a.m06;
|
||||
out.m06 = a.m08;
|
||||
out.m07 = a.m09;
|
||||
out.m08 = a.m10;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a translation offset.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat3.identity(dest);
|
||||
* mat3.translate(dest, dest, vec);
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param v - The translation offset.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromTranslation(out, v) {
|
||||
out.m00 = 1;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
out.m03 = 0;
|
||||
out.m04 = 1;
|
||||
out.m05 = 0;
|
||||
out.m06 = v.x;
|
||||
out.m07 = v.y;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a given angle.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat3.identity(dest);
|
||||
* mat3.rotate(dest, dest, rad);
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param rad - The rotation angle.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromRotation(out, rad) {
|
||||
const s = Math.sin(rad), c = Math.cos(rad);
|
||||
|
||||
out.m00 = c;
|
||||
out.m01 = s;
|
||||
out.m02 = 0;
|
||||
|
||||
out.m03 = -s;
|
||||
out.m04 = c;
|
||||
out.m05 = 0;
|
||||
|
||||
out.m06 = 0;
|
||||
out.m07 = 0;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix from a scale vector.
|
||||
* This is equivalent to (but much faster than):
|
||||
*
|
||||
* mat3.identity(dest);
|
||||
* mat3.scale(dest, dest, vec);
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param v - Scale vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromScaling(out, v) {
|
||||
out.m00 = v.x;
|
||||
out.m01 = 0;
|
||||
out.m02 = 0;
|
||||
|
||||
out.m03 = 0;
|
||||
out.m04 = v.y;
|
||||
out.m05 = 0;
|
||||
|
||||
out.m06 = 0;
|
||||
out.m07 = 0;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the values from a 2x3 matrix into a 3x3 matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The 2x3 matrix.
|
||||
* @return out.
|
||||
**/
|
||||
public static fromMat2d(out, a) {
|
||||
out.m00 = a.m00;
|
||||
out.m01 = a.m01;
|
||||
out.m02 = 0;
|
||||
|
||||
out.m03 = a.m02;
|
||||
out.m04 = a.m03;
|
||||
out.m05 = 0;
|
||||
|
||||
out.m06 = a.m04;
|
||||
out.m07 = a.m05;
|
||||
out.m08 = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a 3x3 matrix from the given quaternion.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param q - The quaternion.
|
||||
*
|
||||
* @return out.
|
||||
*/
|
||||
public static fromQuat(out, q) {
|
||||
const x = q.x, y = q.y, z = q.z, w = q.w;
|
||||
const x2 = x + x;
|
||||
const y2 = y + y;
|
||||
const z2 = z + z;
|
||||
|
||||
const xx = x * x2;
|
||||
const yx = y * x2;
|
||||
const yy = y * y2;
|
||||
const zx = z * x2;
|
||||
const zy = z * y2;
|
||||
const zz = z * z2;
|
||||
const wx = w * x2;
|
||||
const wy = w * y2;
|
||||
const wz = w * z2;
|
||||
|
||||
out.m00 = 1 - yy - zz;
|
||||
out.m03 = yx - wz;
|
||||
out.m06 = zx + wy;
|
||||
|
||||
out.m01 = yx + wz;
|
||||
out.m04 = 1 - xx - zz;
|
||||
out.m07 = zy - wx;
|
||||
|
||||
out.m02 = zx - wy;
|
||||
out.m05 = zy + wx;
|
||||
out.m08 = 1 - xx - yy;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a 3x3 matrix from view direction and up direction.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param view - View direction (must be normalized).
|
||||
* @param [up] - Up direction, default is (0,1,0) (must be normalized).
|
||||
*
|
||||
* @return out
|
||||
*/
|
||||
public static fromViewUp(out, view, up) {
|
||||
const _fromViewUpIIFE = (function() {
|
||||
const default_up = vec3.create(0, 1, 0);
|
||||
const x = vec3.create(0, 0, 0);
|
||||
const y = vec3.create(0, 0, 0);
|
||||
|
||||
return function(out, view, up) {
|
||||
if (vec3.sqrMag(view) < EPSILON * EPSILON) {
|
||||
mat3.identity(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
up = up || default_up;
|
||||
vec3.normalize(x, vec3.cross(x, up, view));
|
||||
|
||||
if (vec3.sqrMag(x) < EPSILON * EPSILON) {
|
||||
mat3.identity(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
vec3.cross(y, view, x);
|
||||
mat3.set(
|
||||
out,
|
||||
x.x, x.y, x.z,
|
||||
y.x, y.y, y.z,
|
||||
view.x, view.y, view.z,
|
||||
);
|
||||
|
||||
return out;
|
||||
};
|
||||
})();
|
||||
return _fromViewUpIIFE(out, view, up);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - A 4x4 matrix to derive the normal matrix from.
|
||||
*
|
||||
* @return out.
|
||||
*/
|
||||
public static normalFromMat4(out, a) {
|
||||
const a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03,
|
||||
a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07,
|
||||
a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11,
|
||||
a30 = a.m12, a31 = a.m13, a32 = a.m14, a33 = a.m15;
|
||||
|
||||
const b00 = a00 * a11 - a01 * a10;
|
||||
const b01 = a00 * a12 - a02 * a10;
|
||||
const b02 = a00 * a13 - a03 * a10;
|
||||
const b03 = a01 * a12 - a02 * a11;
|
||||
const b04 = a01 * a13 - a03 * a11;
|
||||
const b05 = a02 * a13 - a03 * a12;
|
||||
const b06 = a20 * a31 - a21 * a30;
|
||||
const b07 = a20 * a32 - a22 * a30;
|
||||
const b08 = a20 * a33 - a23 * a30;
|
||||
const b09 = a21 * a32 - a22 * a31;
|
||||
const b10 = a21 * a33 - a23 * a31;
|
||||
const b11 = a22 * a33 - a23 * a32;
|
||||
|
||||
// Calculate the determinant
|
||||
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
|
||||
|
||||
if (!det) {
|
||||
return null;
|
||||
}
|
||||
det = 1.0 / det;
|
||||
|
||||
out.m00 = (a11 * b11 - a12 * b10 + a13 * b09) * det;
|
||||
out.m01 = (a12 * b08 - a10 * b11 - a13 * b07) * det;
|
||||
out.m02 = (a10 * b10 - a11 * b08 + a13 * b06) * det;
|
||||
|
||||
out.m03 = (a02 * b10 - a01 * b11 - a03 * b09) * det;
|
||||
out.m04 = (a00 * b11 - a02 * b08 + a03 * b07) * det;
|
||||
out.m05 = (a01 * b08 - a00 * b10 - a03 * b06) * det;
|
||||
|
||||
out.m06 = (a31 * b05 - a32 * b04 + a33 * b03) * det;
|
||||
out.m07 = (a32 * b02 - a30 * b05 - a33 * b01) * det;
|
||||
out.m08 = (a30 * b04 - a31 * b02 + a33 * b00) * det;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of a matrix.
|
||||
*
|
||||
* @param a - The matrix.
|
||||
* @return String representation of this matrix.
|
||||
*/
|
||||
public static str(a) {
|
||||
return `mat3(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05}, ${a.m06}, ${a.m07}, ${a.m08})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store elements of a matrix into array.
|
||||
*
|
||||
* @param out - Array to store result.
|
||||
* @param m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static array(out, m) {
|
||||
out[0] = m.m00;
|
||||
out[1] = m.m01;
|
||||
out[2] = m.m02;
|
||||
out[3] = m.m03;
|
||||
out[4] = m.m04;
|
||||
out[5] = m.m05;
|
||||
out[6] = m.m06;
|
||||
out[7] = m.m07;
|
||||
out[8] = m.m08;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Frobenius norm of a matrix.
|
||||
*
|
||||
* @param a - Matrix to calculate Frobenius norm of.
|
||||
* @return - The frobenius norm.
|
||||
*/
|
||||
public static frob(a) {
|
||||
return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + Math.pow(a.m06, 2) + Math.pow(a.m07, 2) + Math.pow(a.m08, 2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static add(out, a, b) {
|
||||
out.m00 = a.m00 + b.m00;
|
||||
out.m01 = a.m01 + b.m01;
|
||||
out.m02 = a.m02 + b.m02;
|
||||
out.m03 = a.m03 + b.m03;
|
||||
out.m04 = a.m04 + b.m04;
|
||||
out.m05 = a.m05 + b.m05;
|
||||
out.m06 = a.m06 + b.m06;
|
||||
out.m07 = a.m07 + b.m07;
|
||||
out.m08 = a.m08 + b.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts matrix b from matrix a.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static subtract(out, a, b) {
|
||||
out.m00 = a.m00 - b.m00;
|
||||
out.m01 = a.m01 - b.m01;
|
||||
out.m02 = a.m02 - b.m02;
|
||||
out.m03 = a.m03 - b.m03;
|
||||
out.m04 = a.m04 - b.m04;
|
||||
out.m05 = a.m05 - b.m05;
|
||||
out.m06 = a.m06 - b.m06;
|
||||
out.m07 = a.m07 - b.m07;
|
||||
out.m08 = a.m08 - b.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link mat3.subtract}.
|
||||
*/
|
||||
public static sub(out, a, b) {
|
||||
return mat3.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply each element of a matrix by a scalar number.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - Matrix to scale
|
||||
* @param b - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiplyScalar(out, a, b) {
|
||||
out.m00 = a.m00 * b;
|
||||
out.m01 = a.m01 * b;
|
||||
out.m02 = a.m02 * b;
|
||||
out.m03 = a.m03 * b;
|
||||
out.m04 = a.m04 * b;
|
||||
out.m05 = a.m05 * b;
|
||||
out.m06 = a.m06 * b;
|
||||
out.m07 = a.m07 * b;
|
||||
out.m08 = a.m08 * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two matrices after multiplying each element of the second operand by a scalar number.
|
||||
*
|
||||
* @param out - Matrix to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param scale - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiplyScalarAndAdd(out, a, b, scale) {
|
||||
out.m00 = a.m00 + (b.m00 * scale);
|
||||
out.m01 = a.m01 + (b.m01 * scale);
|
||||
out.m02 = a.m02 + (b.m02 * scale);
|
||||
out.m03 = a.m03 + (b.m03 * scale);
|
||||
out.m04 = a.m04 + (b.m04 * scale);
|
||||
out.m05 = a.m05 + (b.m05 * scale);
|
||||
out.m06 = a.m06 + (b.m06 * scale);
|
||||
out.m07 = a.m07 + (b.m07 * scale);
|
||||
out.m08 = a.m08 + (b.m08 * scale);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are equal. (Compared using ===)
|
||||
*
|
||||
* @param a - The first matrix.
|
||||
* @param b - The second matrix.
|
||||
* @return True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
public static exactEquals(a, b) {
|
||||
return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 &&
|
||||
a.m03 === b.m03 && a.m04 === b.m04 && a.m05 === b.m05 &&
|
||||
a.m06 === b.m06 && a.m07 === b.m07 && a.m08 === b.m08;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified matrices are approximately equal.
|
||||
*
|
||||
* @param a - The first matrix.
|
||||
* @param b - The second matrix.
|
||||
* @return True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
public static equals(a, b) {
|
||||
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05, a6 = a.m06, a7 = a.m07, a8 = a.m08;
|
||||
const b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05, b6 = b.m06, b7 = b.m07, b8 = b.m08;
|
||||
return (
|
||||
Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
|
||||
Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
|
||||
Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) &&
|
||||
Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) &&
|
||||
Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) &&
|
||||
Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8))
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Creates a matrix, with elements specified separately.
|
||||
*
|
||||
* @param m00 - Value assigned to element at column 0 row 0.
|
||||
* @param m01 - Value assigned to element at column 0 row 1.
|
||||
* @param m02 - Value assigned to element at column 0 row 2.
|
||||
* @param m03 - Value assigned to element at column 1 row 0.
|
||||
* @param m04 - Value assigned to element at column 1 row 1.
|
||||
* @param m05 - Value assigned to element at column 1 row 2.
|
||||
* @param m06 - Value assigned to element at column 2 row 0.
|
||||
* @param m07 - Value assigned to element at column 2 row 1.
|
||||
* @param m08 - Value assigned to element at column 2 row 2.
|
||||
*/
|
||||
constructor(
|
||||
m00 = 1, m01 = 0, m02 = 0,
|
||||
m03 = 0, m04 = 1, m05 = 0,
|
||||
m06 = 0, m07 = 0, m08 = 1,
|
||||
) {
|
||||
/**
|
||||
* The element at column 0 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m00 = m00;
|
||||
|
||||
/**
|
||||
* The element at column 0 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m01 = m01;
|
||||
|
||||
/**
|
||||
* The element at column 0 row 2.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m02 = m02;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m03 = m03;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m04 = m04;
|
||||
|
||||
/**
|
||||
* The element at column 1 row 2.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m05 = m05;
|
||||
|
||||
/**
|
||||
* The element at column 2 row 0.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m06 = m06;
|
||||
|
||||
/**
|
||||
* The element at column 2 row 1.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m07 = m07;
|
||||
|
||||
/**
|
||||
* The element at column 2 row 2.
|
||||
* @type {number}
|
||||
* */
|
||||
this.m08 = m08;
|
||||
}
|
||||
}
|
||||
|
||||
export default mat3;
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,834 +0,0 @@
|
||||
import vec3 from './vec3';
|
||||
import vec4 from './vec4';
|
||||
import mat3 from './mat3';
|
||||
import { toDegree } from './utils';
|
||||
|
||||
let halfToRad = 0.5 * Math.PI / 180.0;
|
||||
|
||||
/**
|
||||
* Mathematical quaternion.
|
||||
*
|
||||
* A quaternion is a hypercomplex number represented by w + xi + yj + zk, where
|
||||
* x, y, z and w are real numbers(called here its components), and i, j, and k are the fundamental quaternion units.
|
||||
*/
|
||||
class quat {
|
||||
/**
|
||||
* Creates a quaternion, with components specified separately.
|
||||
*
|
||||
* @param {number} x - Value assigned to x component.
|
||||
* @param {number} y - Value assigned to y component.
|
||||
* @param {number} z - Value assigned to z component.
|
||||
* @param {number} w - Value assigned to w component.
|
||||
*/
|
||||
constructor(x = 0, y = 0, z = 0, w = 1) {
|
||||
/**
|
||||
* The x component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.x = x;
|
||||
|
||||
/**
|
||||
* The y component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.y = y;
|
||||
|
||||
/**
|
||||
* The z component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.z = z;
|
||||
|
||||
/**
|
||||
* The w component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.w = w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a quaternion, with components specified separately.
|
||||
*
|
||||
* @param {number} x - Value assigned to x component.
|
||||
* @param {number} y - Value assigned to y component.
|
||||
* @param {number} z - Value assigned to z component.
|
||||
* @param {number} w - Value assigned to w component.
|
||||
* @return {quat} The newly created quaternion.
|
||||
*/
|
||||
static create(x = 0, y = 0, z = 0, w = 1) {
|
||||
return new quat(x, y, z, w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a quaternion.
|
||||
*
|
||||
* @param {quat} a - Quaternion to clone.
|
||||
* @returns {quat} The newly created quaternion.
|
||||
*/
|
||||
static clone(a) {
|
||||
return new quat(a.x, a.y, a.z, a.w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a quaternion into another.
|
||||
*
|
||||
* @param {quat} out - Quaternion to modified.
|
||||
* @param {quat} a - The specified quaternion.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static copy(out, a) {
|
||||
return vec4.copy(out, a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the components of a quaternion to the given values.
|
||||
*
|
||||
* @param {quat} out - The quaternion to modified.
|
||||
* @param {Number} x - Value set to x component.
|
||||
* @param {Number} y - Value set to y component.
|
||||
* @param {Number} z - Value set to z component.
|
||||
* @param {Number} w - Value set to w component.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static set(out, x, y, z, w) {
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
out.z = z;
|
||||
out.w = w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a quaternion as identity quaternion.
|
||||
*
|
||||
* @param {quat} out - Quaternion to set.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static identity(out) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
out.z = 0;
|
||||
out.w = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a quaternion to represent the shortest rotation from one
|
||||
* vector to another.
|
||||
*
|
||||
* Both vectors are assumed to be unit length.
|
||||
*
|
||||
* @param {quat} out - Quaternion to set.
|
||||
* @param {vec3} a - The initial vector.
|
||||
* @param {vec3} b - The destination vector.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static rotationTo(out, a, b) {
|
||||
let rotationToIIFE = (function () {
|
||||
let tmpvec3 = vec3.create(0, 0, 0);
|
||||
let xUnitVec3 = vec3.create(1, 0, 0);
|
||||
let yUnitVec3 = vec3.create(0, 1, 0);
|
||||
|
||||
return function (out, a, b) {
|
||||
let dot = vec3.dot(a, b);
|
||||
if (dot < -0.999999) {
|
||||
vec3.cross(tmpvec3, xUnitVec3, a);
|
||||
if (vec3.magnitude(tmpvec3) < 0.000001) {
|
||||
vec3.cross(tmpvec3, yUnitVec3, a);
|
||||
}
|
||||
vec3.normalize(tmpvec3, tmpvec3);
|
||||
quat.fromAxisAngle(out, tmpvec3, Math.PI);
|
||||
return out;
|
||||
} else if (dot > 0.999999) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
out.z = 0;
|
||||
out.w = 1;
|
||||
return out;
|
||||
} else {
|
||||
vec3.cross(tmpvec3, a, b);
|
||||
out.x = tmpvec3.x;
|
||||
out.y = tmpvec3.y;
|
||||
out.z = tmpvec3.z;
|
||||
out.w = 1 + dot;
|
||||
return quat.normalize(out, out);
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
return rotationToIIFE(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the rotation axis and angle for a given
|
||||
* quaternion. If a quaternion is created with
|
||||
* fromAxisAngle, this method will return the same
|
||||
* values as provided in the original parameter list
|
||||
* OR functionally equivalent values.
|
||||
* Example: The quaternion formed by axis [0, 0, 1] and
|
||||
* angle -90 is the same as the quaternion formed by
|
||||
* [0, 0, 1] and 270. This method favors the latter.
|
||||
* @param {vec3} out_axis - Vector to store the rotation axis.
|
||||
* @param {quat} q - Quaternion to be decomposed.
|
||||
* @return {Number} - Angle, in radians, of the rotation.
|
||||
*/
|
||||
static getAxisAngle(out_axis, q) {
|
||||
let rad = Math.acos(q.w) * 2.0;
|
||||
let s = Math.sin(rad / 2.0);
|
||||
if (s != 0.0) {
|
||||
out_axis.x = q.x / s;
|
||||
out_axis.y = q.y / s;
|
||||
out_axis.z = q.z / s;
|
||||
} else {
|
||||
// If s is zero, return any axis (no rotation - axis does not matter)
|
||||
out_axis.x = 1;
|
||||
out_axis.y = 0;
|
||||
out_axis.z = 0;
|
||||
}
|
||||
return rad;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two quaternions.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - The first operand.
|
||||
* @param {quat} b - The second operand.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static multiply(out, a, b) {
|
||||
let ax = a.x, ay = a.y, az = a.z, aw = a.w,
|
||||
bx = b.x, by = b.y, bz = b.z, bw = b.w;
|
||||
|
||||
out.x = ax * bw + aw * bx + ay * bz - az * by;
|
||||
out.y = ay * bw + aw * by + az * bx - ax * bz;
|
||||
out.z = az * bw + aw * bz + ax * by - ay * bx;
|
||||
out.w = aw * bw - ax * bx - ay * by - az * bz;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link quat.multiply}.
|
||||
*/
|
||||
static mul(out, a, b) {
|
||||
return quat.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a quaternion with a number.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - Quaternion to scale.
|
||||
* @param {quat} b - The scale number.
|
||||
* @returns {quat} out.
|
||||
* */
|
||||
static scale(out, a, b) {
|
||||
out.x = a.x * b;
|
||||
out.y = a.y * b;
|
||||
out.z = a.z * b;
|
||||
out.w = a.w * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about the X axis.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - Quaternion to rotate.
|
||||
* @param {number} rad - Angle (in radians) to rotate.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static rotateX(out, a, rad) {
|
||||
rad *= 0.5;
|
||||
|
||||
let ax = a.x, ay = a.y, az = a.z, aw = a.w,
|
||||
bx = Math.sin(rad), bw = Math.cos(rad);
|
||||
|
||||
out.x = ax * bw + aw * bx;
|
||||
out.y = ay * bw + az * bx;
|
||||
out.z = az * bw - ay * bx;
|
||||
out.w = aw * bw - ax * bx;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about the Y axis.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - Quaternion to rotate.
|
||||
* @param {number} rad - Angle (in radians) to rotate.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static rotateY(out, a, rad) {
|
||||
rad *= 0.5;
|
||||
|
||||
let ax = a.x, ay = a.y, az = a.z, aw = a.w,
|
||||
by = Math.sin(rad), bw = Math.cos(rad);
|
||||
|
||||
out.x = ax * bw - az * by;
|
||||
out.y = ay * bw + aw * by;
|
||||
out.z = az * bw + ax * by;
|
||||
out.w = aw * bw - ay * by;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about the Z axis.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - Quaternion to rotate.
|
||||
* @param {number} rad - Angle (in radians) to rotate.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static rotateZ(out, a, rad) {
|
||||
rad *= 0.5;
|
||||
|
||||
let ax = a.x, ay = a.y, az = a.z, aw = a.w,
|
||||
bz = Math.sin(rad), bw = Math.cos(rad);
|
||||
|
||||
out.x = ax * bw + ay * bz;
|
||||
out.y = ay * bw - ax * bz;
|
||||
out.z = az * bw + aw * bz;
|
||||
out.w = aw * bw - az * bz;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about a world space axis.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} rot - Quaternion to rotate.
|
||||
* @param {vec3} axis - The axis around which to rotate in world space.
|
||||
* @param {number} rad - Angle (in radians) to rotate.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static rotateAround(out, rot, axis, rad) {
|
||||
let rotateAroundIIFE = (function () {
|
||||
let v3_tmp = vec3.create(0, 0, 0);
|
||||
let q_tmp = quat.create();
|
||||
|
||||
return function (out, rot, axis, rad) {
|
||||
// get inv-axis (local to rot)
|
||||
quat.invert(q_tmp, rot);
|
||||
vec3.transformQuat(v3_tmp, axis, q_tmp);
|
||||
// rotate by inv-axis
|
||||
quat.fromAxisAngle(q_tmp, v3_tmp, rad);
|
||||
quat.mul(out, rot, q_tmp);
|
||||
|
||||
return out;
|
||||
};
|
||||
})();
|
||||
return rotateAroundIIFE(out, rot, axis, rad);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about a local space axis.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} rot - Quaternion to rotate.
|
||||
* @param {vec3} axis - The axis around which to rotate in local space.
|
||||
* @param {number} rad - Angle (in radians) to rotate.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static rotateAroundLocal(out, rot, axis, rad) {
|
||||
let rotateAroundLocalIIFE = (function () {
|
||||
let q_tmp = quat.create();
|
||||
|
||||
return function (out, rot, axis, rad) {
|
||||
quat.fromAxisAngle(q_tmp, axis, rad);
|
||||
quat.mul(out, rot, q_tmp);
|
||||
|
||||
return out;
|
||||
};
|
||||
})();
|
||||
|
||||
return rotateAroundLocalIIFE(out, rot, axis, rad);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the W component of a quaternion from the X, Y, and Z components.
|
||||
* Assumes that quaternion is 1 unit in length.
|
||||
* Any existing W component will be ignored.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - Quaternion to calculate W.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static calculateW(out, a) {
|
||||
let x = a.x, y = a.y, z = a.z;
|
||||
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
out.z = z;
|
||||
out.w = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of two quaternions.
|
||||
*
|
||||
* @param {quat} a - The first operand.
|
||||
* @param {quat} b - The second operand.
|
||||
* @returns {Number} - The dot product of a and b.
|
||||
*/
|
||||
static dot(a, b) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two quaternions.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - The first operand.
|
||||
* @param {quat} b - The second operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static lerp(out, a, b, t) {
|
||||
let ax = a.x,
|
||||
ay = a.y,
|
||||
az = a.z,
|
||||
aw = a.w;
|
||||
out.x = ax + t * (b.x - ax);
|
||||
out.y = ay + t * (b.y - ay);
|
||||
out.z = az + t * (b.z - az);
|
||||
out.w = aw + t * (b.w - aw);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a spherical linear interpolation between two quaternions.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - The first operand.
|
||||
* @param {quat} b - The second operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static slerp(out, a, b, t) {
|
||||
// benchmarks:
|
||||
// http://jsperf.com/quaternion-slerp-implementations
|
||||
|
||||
let ax = a.x, ay = a.y, az = a.z, aw = a.w,
|
||||
bx = b.x, by = b.y, bz = b.z, bw = b.w;
|
||||
|
||||
let omega, cosom, sinom, scale0, scale1;
|
||||
|
||||
// calc cosine
|
||||
cosom = ax * bx + ay * by + az * bz + aw * bw;
|
||||
// adjust signs (if necessary)
|
||||
if (cosom < 0.0) {
|
||||
cosom = -cosom;
|
||||
bx = -bx;
|
||||
by = -by;
|
||||
bz = -bz;
|
||||
bw = -bw;
|
||||
}
|
||||
// calculate coefficients
|
||||
if ((1.0 - cosom) > 0.000001) {
|
||||
// standard case (slerp)
|
||||
omega = Math.acos(cosom);
|
||||
sinom = Math.sin(omega);
|
||||
scale0 = Math.sin((1.0 - t) * omega) / sinom;
|
||||
scale1 = Math.sin(t * omega) / sinom;
|
||||
} else {
|
||||
// "from" and "to" quaternions are very close
|
||||
// ... so we can do a linear interpolation
|
||||
scale0 = 1.0 - t;
|
||||
scale1 = t;
|
||||
}
|
||||
// calculate final values
|
||||
out.x = scale0 * ax + scale1 * bx;
|
||||
out.y = scale0 * ay + scale1 * by;
|
||||
out.z = scale0 * az + scale1 * bz;
|
||||
out.w = scale0 * aw + scale1 * bw;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a spherical linear interpolation with two control points.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - The first operand.
|
||||
* @param {quat} b - The second operand.
|
||||
* @param {quat} c - The third operand.
|
||||
* @param {quat} d - The fourth operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {quat} out
|
||||
*/
|
||||
static sqlerp(out, a, b, c, d, t) {
|
||||
let sqlerpIIFE = (function () {
|
||||
let temp1 = quat.create();
|
||||
let temp2 = quat.create();
|
||||
|
||||
return function (out, a, b, c, d, t) {
|
||||
quat.slerp(temp1, a, d, t);
|
||||
quat.slerp(temp2, b, c, t);
|
||||
quat.slerp(out, temp1, temp2, 2 * t * (1 - t));
|
||||
|
||||
return out;
|
||||
};
|
||||
}());
|
||||
return sqlerpIIFE(out, a, b, c, d, t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the inverse of a quaternion.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - Quaternion to calculate inverse of.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static invert(out, a) {
|
||||
let a0 = a.x, a1 = a.y, a2 = a.z, a3 = a.w;
|
||||
let dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
|
||||
let invDot = dot ? 1.0 / dot : 0;
|
||||
|
||||
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
|
||||
|
||||
out.x = -a0 * invDot;
|
||||
out.y = -a1 * invDot;
|
||||
out.z = -a2 * invDot;
|
||||
out.w = a3 * invDot;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the conjugate of a quaternion.
|
||||
* If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - Quaternion to calculate conjugate of.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static conjugate(out, a) {
|
||||
out.x = -a.x;
|
||||
out.y = -a.y;
|
||||
out.z = -a.z;
|
||||
out.w = a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the length of a quaternion.
|
||||
*
|
||||
* @param {quat} a - The quaternion.
|
||||
* @returns {Number} Length of the quaternion.
|
||||
*/
|
||||
static magnitude(a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z,
|
||||
w = a.w;
|
||||
return Math.sqrt(x * x + y * y + z * z + w * w);
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link quat.magnitude}.
|
||||
*/
|
||||
static mag(a) {
|
||||
return quat.magnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared length of a quaternion.
|
||||
*
|
||||
* @param {quat} a - The quaternion.
|
||||
* @returns {Number} Squared length of the quaternion.
|
||||
*/
|
||||
static squaredMagnitude(a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z,
|
||||
w = a.w;
|
||||
return x * x + y * y + z * z + w * w;
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link quat.squaredMagnitude}
|
||||
*/
|
||||
static sqrMag(a) {
|
||||
return quat.squaredMagnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a quaternion.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {quat} a - Quaternion to normalize.
|
||||
* @returns {quat} out.
|
||||
* @function
|
||||
*/
|
||||
static normalize(out, a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z,
|
||||
w = a.w;
|
||||
let len = x * x + y * y + z * z + w * w;
|
||||
if (len > 0) {
|
||||
len = 1 / Math.sqrt(len);
|
||||
out.x = x * len;
|
||||
out.y = y * len;
|
||||
out.z = z * len;
|
||||
out.w = w * len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the specified quaternion with values corresponding to the given
|
||||
* axes. Each axis is a vec3 and is expected to be unit length and
|
||||
* perpendicular to all other specified axes.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {vec3} xAxis - Vector representing the local "right" direction.
|
||||
* @param {vec3} yAxis - Vector representing the local "up" direction.
|
||||
* @param {vec3} zAxis - Vector representing the viewing direction.
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static fromAxes(out, xAxis, yAxis, zAxis) {
|
||||
let fromAxesIIFE = (function () {
|
||||
let matr = mat3.create();
|
||||
|
||||
return function (out, xAxis, yAxis, zAxis) {
|
||||
mat3.set(matr,
|
||||
xAxis.x, xAxis.y, xAxis.z,
|
||||
yAxis.x, yAxis.y, yAxis.z,
|
||||
zAxis.x, zAxis.y, zAxis.z
|
||||
);
|
||||
return quat.normalize(out, quat.fromMat3(out, matr));
|
||||
};
|
||||
})();
|
||||
return fromAxesIIFE(out, xAxis, yAxis, zAxis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a quaternion from view direction and up direction
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {vec3} view - View direction (must be normalized).
|
||||
* @param {vec3} [up] - Up direction, default is (0,1,0) (must be normalized).
|
||||
*
|
||||
* @returns {quat} out.
|
||||
*/
|
||||
static fromViewUp(out, view, up) {
|
||||
let fromViewUpIIFE = (function () {
|
||||
let matr = mat3.create();
|
||||
|
||||
return function (out, view, up) {
|
||||
mat3.fromViewUp(matr, view, up);
|
||||
if (!matr) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return quat.normalize(out, quat.fromMat3(out, matr));
|
||||
};
|
||||
})();
|
||||
return fromViewUpIIFE(out, view, up);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a quaternion from the given angle and rotation axis,
|
||||
* then returns it.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {vec3} axis - The axis around which to rotate.
|
||||
* @param {Number} rad - The angle in radians.
|
||||
* @returns {quat} out.
|
||||
**/
|
||||
static fromAxisAngle(out, axis, rad) {
|
||||
rad = rad * 0.5;
|
||||
let s = Math.sin(rad);
|
||||
out.x = s * axis.x;
|
||||
out.y = s * axis.y;
|
||||
out.z = s * axis.z;
|
||||
out.w = Math.cos(rad);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a quaternion from the given 3x3 rotation matrix.
|
||||
*
|
||||
* NOTE: The resultant quaternion is not normalized, so you should be sure
|
||||
* to re-normalize the quaternion yourself where necessary.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {mat3} m - The rotation matrix.
|
||||
* @returns {quat} out.
|
||||
* @function
|
||||
*/
|
||||
static fromMat3(out, m) {
|
||||
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
|
||||
|
||||
let m00 = m.m00, m01 = m.m03, m02 = m.m06,
|
||||
m10 = m.m01, m11 = m.m04, m12 = m.m07,
|
||||
m20 = m.m02, m21 = m.m05, m22 = m.m08;
|
||||
|
||||
let trace = m00 + m11 + m22;
|
||||
|
||||
if (trace > 0) {
|
||||
let s = 0.5 / Math.sqrt(trace + 1.0);
|
||||
|
||||
out.w = 0.25 / s;
|
||||
out.x = (m21 - m12) * s;
|
||||
out.y = (m02 - m20) * s;
|
||||
out.z = (m10 - m01) * s;
|
||||
|
||||
} else if ((m00 > m11) && (m00 > m22)) {
|
||||
let s = 2.0 * Math.sqrt(1.0 + m00 - m11 - m22);
|
||||
|
||||
out.w = (m21 - m12) / s;
|
||||
out.x = 0.25 * s;
|
||||
out.y = (m01 + m10) / s;
|
||||
out.z = (m02 + m20) / s;
|
||||
|
||||
} else if (m11 > m22) {
|
||||
let s = 2.0 * Math.sqrt(1.0 + m11 - m00 - m22);
|
||||
|
||||
out.w = (m02 - m20) / s;
|
||||
out.x = (m01 + m10) / s;
|
||||
out.y = 0.25 * s;
|
||||
out.z = (m12 + m21) / s;
|
||||
|
||||
} else {
|
||||
let s = 2.0 * Math.sqrt(1.0 + m22 - m00 - m11);
|
||||
|
||||
out.w = (m10 - m01) / s;
|
||||
out.x = (m02 + m20) / s;
|
||||
out.y = (m12 + m21) / s;
|
||||
out.z = 0.25 * s;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a quaternion from the given euler angle x, y, z.
|
||||
*
|
||||
* @param {quat} out - Quaternion to store result.
|
||||
* @param {number} x - Angle to rotate around X axis in degrees.
|
||||
* @param {number} y - Angle to rotate around Y axis in degrees.
|
||||
* @param {number} z - Angle to rotate around Z axis in degrees.
|
||||
* @returns {quat} out.
|
||||
* @function
|
||||
*/
|
||||
static fromEuler(out, x, y, z) {
|
||||
x *= halfToRad;
|
||||
y *= halfToRad;
|
||||
z *= halfToRad;
|
||||
|
||||
let sx = Math.sin(x);
|
||||
let cx = Math.cos(x);
|
||||
let sy = Math.sin(y);
|
||||
let cy = Math.cos(y);
|
||||
let sz = Math.sin(z);
|
||||
let cz = Math.cos(z);
|
||||
|
||||
out.x = sx * cy * cz + cx * sy * sz;
|
||||
out.y = cx * sy * cz + sx * cy * sz;
|
||||
out.z = cx * cy * sz - sx * sy * cz;
|
||||
out.w = cx * cy * cz - sx * sy * sz;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a quaternion back to euler angle (in degrees).
|
||||
*
|
||||
* @param {vec3} out - Euler angle stored as a vec3
|
||||
* @param {number} q - the quaternion to be converted
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static toAxisX(out) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a quaternion back to euler angle (in degrees).
|
||||
*
|
||||
* @param {vec3} out - Euler angle stored as a vec3
|
||||
* @param {number} q - the quaternion to be converted
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static toEuler(out, q) {
|
||||
let x = q.x, y = q.y, z = q.z, w = q.w;
|
||||
let heading, attitude, bank;
|
||||
let test = x * y + z * w;
|
||||
if (test > 0.499999) { // singularity at north pole
|
||||
heading = 2 * Math.atan2(x,w);
|
||||
attitude = Math.PI/2;
|
||||
bank = 0;
|
||||
}
|
||||
if (test < -0.499999) { // singularity at south pole
|
||||
heading = -2 * Math.atan2(x,w);
|
||||
attitude = - Math.PI/2;
|
||||
bank = 0;
|
||||
}
|
||||
if(isNaN(heading)){
|
||||
let sqx = x*x;
|
||||
let sqy = y*y;
|
||||
let sqz = z*z;
|
||||
heading = Math.atan2(2*y*w - 2*x*z , 1 - 2*sqy - 2*sqz); // heading
|
||||
attitude = Math.asin(2*test); // attitude
|
||||
bank = Math.atan2(2*x*w - 2*y*z , 1 - 2*sqx - 2*sqz); // bank
|
||||
}
|
||||
|
||||
// Keep angles [0..360].
|
||||
out.y = toDegree(heading) + (heading < 0 ? 360 : 0);
|
||||
out.z = toDegree(attitude) + (attitude < 0 ? 360 : 0);
|
||||
out.x = toDegree(bank) + (bank < 0 ? 360 : 0);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a quaternion.
|
||||
*
|
||||
* @param {quat} a - The quaternion.
|
||||
* @returns {String} - String representation of this quaternion.
|
||||
*/
|
||||
static str(a) {
|
||||
return `quat(${a.x}, ${a.y}, ${a.z}, ${a.w})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a quaternion into array.
|
||||
*
|
||||
* @param {Array} out - Array to store result.
|
||||
* @param {quat} q - The quaternion.
|
||||
* @returns {Array} out.
|
||||
*/
|
||||
static array(out, q) {
|
||||
out[0] = q.x;
|
||||
out[1] = q.y;
|
||||
out[2] = q.z;
|
||||
out[3] = q.w;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified quaternions are equal. (Compared using ===)
|
||||
*
|
||||
* @param {quat} a - The first quaternion.
|
||||
* @param {quat} b - The second quaternion.
|
||||
* @returns {Boolean} True if the quaternions are equal, false otherwise.
|
||||
*/
|
||||
static exactEquals(a, b) {
|
||||
return vec4.exactEquals(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified quaternions are approximately equal.
|
||||
*
|
||||
* @param {quat} a The first quaternion.
|
||||
* @param {quat} b The second quaternion.
|
||||
* @returns {Boolean} True if the quaternions are approximately equal, false otherwise.
|
||||
*/
|
||||
static equals(a, b) {
|
||||
return vec4.equals(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
export default quat;
|
||||
@@ -0,0 +1,773 @@
|
||||
/// <reference path="./fwd-decls" />
|
||||
|
||||
import mat3 from './mat3';
|
||||
import { toDegree } from './utils';
|
||||
import vec3 from './vec3';
|
||||
import vec4 from './vec4';
|
||||
|
||||
const halfToRad = 0.5 * Math.PI / 180.0;
|
||||
|
||||
/**
|
||||
* Mathematical quaternion.
|
||||
*
|
||||
* A quaternion is a hypercomplex number represented by w + xi + yj + zk, where
|
||||
* x, y, z and w are real numbers(called here its components), and i, j, and k are the fundamental quaternion units.
|
||||
*/
|
||||
export default class quat {
|
||||
|
||||
/**
|
||||
* Creates a quaternion, with components specified separately.
|
||||
*
|
||||
* @param x - Value assigned to x component.
|
||||
* @param y - Value assigned to y component.
|
||||
* @param z - Value assigned to z component.
|
||||
* @param w - Value assigned to w component.
|
||||
* @return The newly created quaternion.
|
||||
*/
|
||||
public static create (x = 0, y = 0, z = 0, w = 1) {
|
||||
return new quat(x, y, z, w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a quaternion.
|
||||
*
|
||||
* @param a - Quaternion to clone.
|
||||
* @return The newly created quaternion.
|
||||
*/
|
||||
public static clone (a: quat) {
|
||||
return new quat(a.x, a.y, a.z, a.w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a quaternion into another.
|
||||
*
|
||||
* @param out - Quaternion to modified.
|
||||
* @param a - The specified quaternion.
|
||||
* @return out.
|
||||
*/
|
||||
public static copy<Out extends IQuatLike> (out: Out, a: quat) {
|
||||
return vec4.copy(out, a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the components of a quaternion to the given values.
|
||||
*
|
||||
* @param out - The quaternion to modified.
|
||||
* @param x - Value set to x component.
|
||||
* @param y - Value set to y component.
|
||||
* @param z - Value set to z component.
|
||||
* @param w - Value set to w component.
|
||||
* @return out.
|
||||
*/
|
||||
public static set<Out extends IQuatLike> (out: Out, x: number, y: number, z: number, w: number) {
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
out.z = z;
|
||||
out.w = w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a quaternion as identity quaternion.
|
||||
*
|
||||
* @param out - Quaternion to set.
|
||||
* @return out.
|
||||
*/
|
||||
public static identity<Out extends IQuatLike> (out: Out) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
out.z = 0;
|
||||
out.w = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a quaternion to represent the shortest rotation from one
|
||||
* vector to another.
|
||||
*
|
||||
* Both vectors are assumed to be unit length.
|
||||
*
|
||||
* @param out - Quaternion to set.
|
||||
* @param a - The initial vector.
|
||||
* @param b - The destination vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static rotationTo<Out extends IQuatLike> (out: Out, a: quat, b: quat) {
|
||||
const dot = vec3.dot(a, b);
|
||||
if (dot < -0.999999) {
|
||||
vec3.cross(tmpVec3, tmpXUnitVec3, a);
|
||||
if (vec3.magnitude(tmpVec3) < 0.000001) {
|
||||
vec3.cross(tmpVec3, tmpYUnitVec3, a);
|
||||
}
|
||||
vec3.normalize(tmpVec3, tmpVec3);
|
||||
quat.fromAxisAngle(out, tmpVec3, Math.PI);
|
||||
return out;
|
||||
} else if (dot > 0.999999) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
out.z = 0;
|
||||
out.w = 1;
|
||||
return out;
|
||||
} else {
|
||||
vec3.cross(tmpVec3, a, b);
|
||||
out.x = tmpVec3.x;
|
||||
out.y = tmpVec3.y;
|
||||
out.z = tmpVec3.z;
|
||||
out.w = 1 + dot;
|
||||
return quat.normalize(out, out);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the rotation axis and angle for a given
|
||||
* quaternion. If a quaternion is created with
|
||||
* fromAxisAngle, this method will return the same
|
||||
* values as provided in the original parameter list
|
||||
* OR functionally equivalent values.
|
||||
* Example: The quaternion formed by axis [0, 0, 1] and
|
||||
* angle -90 is the same as the quaternion formed by
|
||||
* [0, 0, 1] and 270. This method favors the latter.
|
||||
* @param {vec3} out_axis - Vector to store the rotation axis.
|
||||
* @param {quat} q - Quaternion to be decomposed.
|
||||
* @return - Angle, in radians, of the rotation.
|
||||
*/
|
||||
public static getAxisAngle<Out extends IVec3Like> (outAxis: Out, q: quat) {
|
||||
const rad = Math.acos(q.w) * 2.0;
|
||||
const s = Math.sin(rad / 2.0);
|
||||
if (s !== 0.0) {
|
||||
outAxis.x = q.x / s;
|
||||
outAxis.y = q.y / s;
|
||||
outAxis.z = q.z / s;
|
||||
} else {
|
||||
// If s is zero, return any axis (no rotation - axis does not matter)
|
||||
outAxis.x = 1;
|
||||
outAxis.y = 0;
|
||||
outAxis.z = 0;
|
||||
}
|
||||
return rad;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two quaternions.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiply<Out extends IQuatLike> (out: Out, a: quat, b: quat) {
|
||||
const { x: ax, y: ay, z: az, w: aw } = a;
|
||||
const { x: bx, y: by, z: bz, w: bw } = b;
|
||||
|
||||
out.x = ax * bw + aw * bx + ay * bz - az * by;
|
||||
out.y = ay * bw + aw * by + az * bx - ax * bz;
|
||||
out.z = az * bw + aw * bz + ax * by - ay * bx;
|
||||
out.w = aw * bw - ax * bx - ay * by - az * bz;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link quat.multiply}.
|
||||
*/
|
||||
public static mul<Out extends IQuatLike> (out: Out, a: quat, b: quat) {
|
||||
return quat.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a quaternion with a number.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - Quaternion to scale.
|
||||
* @param b - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static scale<Out extends IQuatLike> (out: Out, a: quat, b: number) {
|
||||
out.x = a.x * b;
|
||||
out.y = a.y * b;
|
||||
out.z = a.z * b;
|
||||
out.w = a.w * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about the X axis.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - Quaternion to rotate.
|
||||
* @param rad - Angle (in radians) to rotate.
|
||||
* @return out.
|
||||
*/
|
||||
public static rotateX<Out extends IQuatLike> (out: Out, a: quat, rad: number) {
|
||||
rad *= 0.5;
|
||||
|
||||
const { x: ax, y: ay, z: az, w: aw } = a;
|
||||
const bx = Math.sin(rad);
|
||||
const bw = Math.cos(rad);
|
||||
|
||||
out.x = ax * bw + aw * bx;
|
||||
out.y = ay * bw + az * bx;
|
||||
out.z = az * bw - ay * bx;
|
||||
out.w = aw * bw - ax * bx;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about the Y axis.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - Quaternion to rotate.
|
||||
* @param rad - Angle (in radians) to rotate.
|
||||
* @return out.
|
||||
*/
|
||||
public static rotateY<Out extends IQuatLike> (out: Out, a: quat, rad: number) {
|
||||
rad *= 0.5;
|
||||
|
||||
const { x: ax, y: ay, z: az, w: aw } = a;
|
||||
const by = Math.sin(rad);
|
||||
const bw = Math.cos(rad);
|
||||
|
||||
out.x = ax * bw - az * by;
|
||||
out.y = ay * bw + aw * by;
|
||||
out.z = az * bw + ax * by;
|
||||
out.w = aw * bw - ay * by;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about the Z axis.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - Quaternion to rotate.
|
||||
* @param rad - Angle (in radians) to rotate.
|
||||
* @return out.
|
||||
*/
|
||||
public static rotateZ<Out extends IQuatLike> (out: Out, a: quat, rad: number) {
|
||||
rad *= 0.5;
|
||||
|
||||
const { x: ax, y: ay, z: az, w: aw } = a;
|
||||
const bz = Math.sin(rad);
|
||||
const bw = Math.cos(rad);
|
||||
|
||||
out.x = ax * bw + ay * bz;
|
||||
out.y = ay * bw - ax * bz;
|
||||
out.z = az * bw + aw * bz;
|
||||
out.w = aw * bw - az * bz;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about a world space axis.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param rot - Quaternion to rotate.
|
||||
* @param axis - The axis around which to rotate in world space.
|
||||
* @param rad - Angle (in radians) to rotate.
|
||||
* @return out.
|
||||
*/
|
||||
public static rotateAround<Out extends IQuatLike> (out: Out, rot: quat, axis: IVec3Like, rad: number) {
|
||||
// get inv-axis (local to rot)
|
||||
quat.invert(tmpQuat1, rot);
|
||||
vec3.transformQuat(tmpVec3, axis, tmpQuat1);
|
||||
// rotate by inv-axis
|
||||
quat.fromAxisAngle(tmpQuat1, tmpVec3, rad);
|
||||
quat.mul(out, rot, tmpQuat1);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a quaternion by the given angle about a local space axis.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param rot - Quaternion to rotate.
|
||||
* @param axis - The axis around which to rotate in local space.
|
||||
* @param rad - Angle (in radians) to rotate.
|
||||
* @return out.
|
||||
*/
|
||||
public static rotateAroundLocal<Out extends IQuatLike> (out: Out, rot: quat, axis: IVec3Like, rad: number) {
|
||||
quat.fromAxisAngle(tmpQuat1, axis, rad);
|
||||
quat.mul(out, rot, tmpQuat1);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the W component of a quaternion from the X, Y, and Z components.
|
||||
* Assumes that quaternion is 1 unit in length.
|
||||
* Any existing W component will be ignored.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - Quaternion to calculate W.
|
||||
* @return out.
|
||||
*/
|
||||
public static calculateW<Out extends IQuatLike> (out: Out, a: quat) {
|
||||
const { x, y, z } = a;
|
||||
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
out.z = z;
|
||||
out.w = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of two quaternions.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return - The dot product of a and b.
|
||||
*/
|
||||
public static dot (a: quat, b: quat) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two quaternions.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out.
|
||||
*/
|
||||
public static lerp<Out extends IQuatLike> (out: Out, a: quat, b: quat, t: number) {
|
||||
const { x: ax, y: ay, z: az, w: aw } = a;
|
||||
out.x = ax + t * (b.x - ax);
|
||||
out.y = ay + t * (b.y - ay);
|
||||
out.z = az + t * (b.z - az);
|
||||
out.w = aw + t * (b.w - aw);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a spherical linear interpolation between two quaternions.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out.
|
||||
*/
|
||||
public static slerp<Out extends IQuatLike> (out: Out, a: quat, b: quat, t: number) {
|
||||
// benchmarks:
|
||||
// http://jsperf.com/quaternion-slerp-implementations
|
||||
|
||||
const { x: ax, y: ay, z: az, w: aw } = a;
|
||||
let { x: bx, y: by, z: bz, w: bw } = b;
|
||||
|
||||
let scale0 = 0;
|
||||
let scale1 = 0;
|
||||
|
||||
// calc cosine
|
||||
let cosom = ax * bx + ay * by + az * bz + aw * bw;
|
||||
// adjust signs (if necessary)
|
||||
if (cosom < 0.0) {
|
||||
cosom = -cosom;
|
||||
bx = -bx;
|
||||
by = -by;
|
||||
bz = -bz;
|
||||
bw = -bw;
|
||||
}
|
||||
// calculate coefficients
|
||||
if ((1.0 - cosom) > 0.000001) {
|
||||
// standard case (slerp)
|
||||
const omega = Math.acos(cosom);
|
||||
const sinom = Math.sin(omega);
|
||||
scale0 = Math.sin((1.0 - t) * omega) / sinom;
|
||||
scale1 = Math.sin(t * omega) / sinom;
|
||||
} else {
|
||||
// "from" and "to" quaternions are very close
|
||||
// ... so we can do a linear interpolation
|
||||
scale0 = 1.0 - t;
|
||||
scale1 = t;
|
||||
}
|
||||
// calculate final values
|
||||
out.x = scale0 * ax + scale1 * bx;
|
||||
out.y = scale0 * ay + scale1 * by;
|
||||
out.z = scale0 * az + scale1 * bz;
|
||||
out.w = scale0 * aw + scale1 * bw;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a spherical linear interpolation with two control points.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param c - The third operand.
|
||||
* @param d - The fourth operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out
|
||||
*/
|
||||
public static sqlerp<Out extends IQuatLike> (out: Out, a: quat, b: quat, c: quat, d: quat, t: number) {
|
||||
quat.slerp(tmpQuat1, a, d, t);
|
||||
quat.slerp(tmpQuat2, b, c, t);
|
||||
quat.slerp(out, tmpQuat1, tmpQuat2, 2 * t * (1 - t));
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the inverse of a quaternion.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - Quaternion to calculate inverse of.
|
||||
* @return out.
|
||||
*/
|
||||
public static invert<Out extends IQuatLike> (out: Out, a: quat) {
|
||||
const { x: a0, y: a1, z: a2, w: a3 } = a;
|
||||
const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
|
||||
const invDot = dot ? 1.0 / dot : 0;
|
||||
|
||||
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
|
||||
|
||||
out.x = -a0 * invDot;
|
||||
out.y = -a1 * invDot;
|
||||
out.z = -a2 * invDot;
|
||||
out.w = a3 * invDot;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the conjugate of a quaternion.
|
||||
* If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - Quaternion to calculate conjugate of.
|
||||
* @return out.
|
||||
*/
|
||||
public static conjugate<Out extends IQuatLike> (out: Out, a: quat) {
|
||||
out.x = -a.x;
|
||||
out.y = -a.y;
|
||||
out.z = -a.z;
|
||||
out.w = a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the length of a quaternion.
|
||||
*
|
||||
* @param a - The quaternion.
|
||||
* @return Length of the quaternion.
|
||||
*/
|
||||
public static magnitude (a: quat) {
|
||||
const { x, y, z, w } = a;
|
||||
return Math.sqrt(x * x + y * y + z * z + w * w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link quat.magnitude}.
|
||||
*/
|
||||
public static mag (a: quat) {
|
||||
return quat.magnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared length of a quaternion.
|
||||
*
|
||||
* @param a - The quaternion.
|
||||
* @return Squared length of the quaternion.
|
||||
*/
|
||||
public static squaredMagnitude (a: quat) {
|
||||
const { x, y, z, w } = a;
|
||||
return x * x + y * y + z * z + w * w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link quat.squaredMagnitude}
|
||||
*/
|
||||
public static sqrMag (a: quat) {
|
||||
return quat.squaredMagnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a quaternion.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param a - Quaternion to normalize.
|
||||
* @return out.
|
||||
* @function
|
||||
*/
|
||||
public static normalize<Out extends IQuatLike> (out: Out, a: quat) {
|
||||
const { x, y, z, w } = a;
|
||||
let len = x * x + y * y + z * z + w * w;
|
||||
if (len > 0) {
|
||||
len = 1 / Math.sqrt(len);
|
||||
out.x = x * len;
|
||||
out.y = y * len;
|
||||
out.z = z * len;
|
||||
out.w = w * len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the specified quaternion with values corresponding to the given
|
||||
* axes. Each axis is a vec3 and is expected to be unit length and
|
||||
* perpendicular to all other specified axes.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param xAxis - Vector representing the local "right" direction.
|
||||
* @param yAxis - Vector representing the local "up" direction.
|
||||
* @param zAxis - Vector representing the viewing direction.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromAxes<Out extends IQuatLike> (out: Out, xAxis: IVec3Like, yAxis: IVec3Like, zAxis: IVec3Like) {
|
||||
mat3.set(tmpMat3,
|
||||
xAxis.x, xAxis.y, xAxis.z,
|
||||
yAxis.x, yAxis.y, yAxis.z,
|
||||
zAxis.x, zAxis.y, zAxis.z,
|
||||
);
|
||||
return quat.normalize(out, quat.fromMat3(out, tmpMat3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a quaternion from view direction and up direction
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param view - View direction (must be normalized).
|
||||
* @param [up] - Up direction, default is (0,1,0) (must be normalized).
|
||||
*
|
||||
* @return out.
|
||||
*/
|
||||
public static fromViewUp<Out extends IQuatLike> (out: Out, view: IVec3Like, up: IVec3Like) {
|
||||
mat3.fromViewUp(tmpMat3, view, up);
|
||||
return quat.normalize(out, quat.fromMat3(out, tmpMat3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a quaternion from the given angle and rotation axis,
|
||||
* then returns it.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param axis - The axis around which to rotate.
|
||||
* @param rad - The angle in radians.
|
||||
* @return out.
|
||||
*/
|
||||
public static fromAxisAngle<Out extends IQuatLike> (out: Out, axis: IVec3Like, rad: number) {
|
||||
rad = rad * 0.5;
|
||||
const s = Math.sin(rad);
|
||||
out.x = s * axis.x;
|
||||
out.y = s * axis.y;
|
||||
out.z = s * axis.z;
|
||||
out.w = Math.cos(rad);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a quaternion from the given 3x3 rotation matrix.
|
||||
*
|
||||
* NOTE: The resultant quaternion is not normalized, so you should be sure
|
||||
* to re-normalize the quaternion yourself where necessary.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param m - The rotation matrix.
|
||||
* @return out.
|
||||
* @function
|
||||
*/
|
||||
public static fromMat3<Out extends IQuatLike> (out: Out, m: IMat3Like) {
|
||||
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
|
||||
|
||||
const {
|
||||
m00: m00, m03: m01, m06: m02,
|
||||
m01: m10, m04: m11, m07: m12,
|
||||
m02: m20, m05: m21, m08: m22,
|
||||
} = m;
|
||||
|
||||
const trace = m00 + m11 + m22;
|
||||
|
||||
if (trace > 0) {
|
||||
const s = 0.5 / Math.sqrt(trace + 1.0);
|
||||
|
||||
out.w = 0.25 / s;
|
||||
out.x = (m21 - m12) * s;
|
||||
out.y = (m02 - m20) * s;
|
||||
out.z = (m10 - m01) * s;
|
||||
|
||||
} else if ((m00 > m11) && (m00 > m22)) {
|
||||
const s = 2.0 * Math.sqrt(1.0 + m00 - m11 - m22);
|
||||
|
||||
out.w = (m21 - m12) / s;
|
||||
out.x = 0.25 * s;
|
||||
out.y = (m01 + m10) / s;
|
||||
out.z = (m02 + m20) / s;
|
||||
|
||||
} else if (m11 > m22) {
|
||||
const s = 2.0 * Math.sqrt(1.0 + m11 - m00 - m22);
|
||||
|
||||
out.w = (m02 - m20) / s;
|
||||
out.x = (m01 + m10) / s;
|
||||
out.y = 0.25 * s;
|
||||
out.z = (m12 + m21) / s;
|
||||
|
||||
} else {
|
||||
const s = 2.0 * Math.sqrt(1.0 + m22 - m00 - m11);
|
||||
|
||||
out.w = (m10 - m01) / s;
|
||||
out.x = (m02 + m20) / s;
|
||||
out.y = (m12 + m21) / s;
|
||||
out.z = 0.25 * s;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a quaternion from the given euler angle x, y, z.
|
||||
*
|
||||
* @param out - Quaternion to store result.
|
||||
* @param x - Angle to rotate around X axis in degrees.
|
||||
* @param y - Angle to rotate around Y axis in degrees.
|
||||
* @param z - Angle to rotate around Z axis in degrees.
|
||||
* @return out.
|
||||
* @function
|
||||
*/
|
||||
public static fromEuler<Out extends IQuatLike> (out: Out, x: number, y: number, z: number) {
|
||||
x *= halfToRad;
|
||||
y *= halfToRad;
|
||||
z *= halfToRad;
|
||||
|
||||
const sx = Math.sin(x);
|
||||
const cx = Math.cos(x);
|
||||
const sy = Math.sin(y);
|
||||
const cy = Math.cos(y);
|
||||
const sz = Math.sin(z);
|
||||
const cz = Math.cos(z);
|
||||
|
||||
out.x = sx * cy * cz + cx * sy * sz;
|
||||
out.y = cx * sy * cz + sx * cy * sz;
|
||||
out.z = cx * cy * sz - sx * sy * cz;
|
||||
out.w = cx * cy * cz - sx * sy * sz;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a quaternion back to euler angle (in degrees).
|
||||
*
|
||||
* @param out - Euler angle stored as a vec3
|
||||
* @param q - the quaternion to be converted
|
||||
* @return out.
|
||||
*/
|
||||
public static toEuler<Out extends IQuatLike> (out: Out, q: quat) {
|
||||
const { x, y, z, w } = q;
|
||||
let heading: number = 0;
|
||||
let attitude: number = 0;
|
||||
let bank: number = 0;
|
||||
const test = x * y + z * w;
|
||||
if (test > 0.499999) { // singularity at north pole
|
||||
heading = 2 * Math.atan2(x, w);
|
||||
attitude = Math.PI / 2;
|
||||
bank = 0;
|
||||
}
|
||||
if (test < -0.499999) { // singularity at south pole
|
||||
heading = -2 * Math.atan2(x, w);
|
||||
attitude = - Math.PI / 2;
|
||||
bank = 0;
|
||||
}
|
||||
if (isNaN(heading)) {
|
||||
const sqx = x * x;
|
||||
const sqy = y * y;
|
||||
const sqz = z * z;
|
||||
heading = Math.atan2(2 * y * w - 2 * x * z, 1 - 2 * sqy - 2 * sqz); // heading
|
||||
attitude = Math.asin(2 * test); // attitude
|
||||
bank = Math.atan2(2 * x * w - 2 * y * z, 1 - 2 * sqx - 2 * sqz); // bank
|
||||
}
|
||||
|
||||
// Keep angles [0..360].
|
||||
out.y = toDegree(heading) + (heading < 0 ? 360 : 0);
|
||||
out.z = toDegree(attitude) + (attitude < 0 ? 360 : 0);
|
||||
out.x = toDegree(bank) + (bank < 0 ? 360 : 0);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a quaternion.
|
||||
*
|
||||
* @param a - The quaternion.
|
||||
* @return - String representation of this quaternion.
|
||||
*/
|
||||
public static str (a: quat) {
|
||||
return `quat(${a.x}, ${a.y}, ${a.z}, ${a.w})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a quaternion into array.
|
||||
*
|
||||
* @param out - Array to store result.
|
||||
* @param q - The quaternion.
|
||||
* @return out.
|
||||
*/
|
||||
public static array<Out extends IWritableArrayLike<number>> (out: Out, q: quat) {
|
||||
out[0] = q.x;
|
||||
out[1] = q.y;
|
||||
out[2] = q.z;
|
||||
out[3] = q.w;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified quaternions are equal. (Compared using ===)
|
||||
*
|
||||
* @param a - The first quaternion.
|
||||
* @param b - The second quaternion.
|
||||
* @return True if the quaternions are equal, false otherwise.
|
||||
*/
|
||||
public static exactEquals (a: quat, b: quat) {
|
||||
return vec4.exactEquals(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified quaternions are approximately equal.
|
||||
*
|
||||
* @param a The first quaternion.
|
||||
* @param b The second quaternion.
|
||||
* @return True if the quaternions are approximately equal, false otherwise.
|
||||
*/
|
||||
public static equals (a: quat, b: quat) {
|
||||
return vec4.equals(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* The x component.
|
||||
*/
|
||||
public x: number;
|
||||
|
||||
/**
|
||||
* The y component.
|
||||
*/
|
||||
public y: number;
|
||||
|
||||
/**
|
||||
* The z component.
|
||||
*/
|
||||
public z: number;
|
||||
|
||||
/**
|
||||
* The w component.
|
||||
*/
|
||||
public w: number;
|
||||
|
||||
/**
|
||||
* Creates a quaternion, with components specified separately.
|
||||
*
|
||||
* @param x - Value assigned to x component.
|
||||
* @param y - Value assigned to y component.
|
||||
* @param z - Value assigned to z component.
|
||||
* @param w - Value assigned to w component.
|
||||
*/
|
||||
constructor (x = 0, y = 0, z = 0, w = 1) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
}
|
||||
|
||||
const tmpQuat1 = quat.create();
|
||||
const tmpQuat2 = quat.create();
|
||||
const tmpVec3 = vec3.create();
|
||||
const tmpMat3 = mat3.create();
|
||||
const tmpXUnitVec3 = vec3.create(1, 0, 0);
|
||||
const tmpYUnitVec3 = vec3.create(0, 1, 0);
|
||||
@@ -1,217 +0,0 @@
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
const _d2r = Math.PI / 180.0;
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
const _r2d = 180.0 / Math.PI;
|
||||
|
||||
/**
|
||||
* @property {number} EPSILON
|
||||
*/
|
||||
export const EPSILON = 0.000001;
|
||||
|
||||
/**
|
||||
* Tests whether or not the arguments have approximately the same value, within an absolute
|
||||
* or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less
|
||||
* than or equal to 1.0, and a relative tolerance is used for larger values)
|
||||
*
|
||||
* @param {Number} a The first number to test.
|
||||
* @param {Number} b The second number to test.
|
||||
* @returns {Boolean} True if the numbers are approximately equal, false otherwise.
|
||||
*/
|
||||
export function equals(a, b) {
|
||||
return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether or not the arguments have approximately the same value by given maxDiff
|
||||
*
|
||||
* @param {Number} a The first number to test.
|
||||
* @param {Number} b The second number to test.
|
||||
* @param {Number} maxDiff Maximum difference.
|
||||
* @returns {Boolean} True if the numbers are approximately equal, false otherwise.
|
||||
*/
|
||||
export function approx(a, b, maxDiff) {
|
||||
maxDiff = maxDiff || EPSILON;
|
||||
return Math.abs(a - b) <= maxDiff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamps a value between a minimum float and maximum float value.
|
||||
*
|
||||
* @method clamp
|
||||
* @param {number} val
|
||||
* @param {number} min
|
||||
* @param {number} max
|
||||
* @return {number}
|
||||
*/
|
||||
export function clamp(val, min, max) {
|
||||
return val < min ? min : val > max ? max : val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamps a value between 0 and 1.
|
||||
*
|
||||
* @method clamp01
|
||||
* @param {number} val
|
||||
* @return {number}
|
||||
*/
|
||||
export function clamp01(val) {
|
||||
return val < 0 ? 0 : val > 1 ? 1 : val;
|
||||
}
|
||||
|
||||
/**
|
||||
* @method lerp
|
||||
* @param {number} from
|
||||
* @param {number} to
|
||||
* @param {number} ratio - the interpolation coefficient
|
||||
* @return {number}
|
||||
*/
|
||||
export function lerp(from, to, ratio) {
|
||||
return from + (to - from) * ratio;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Degree To Radian
|
||||
*
|
||||
* @param {Number} a Angle in Degrees
|
||||
*/
|
||||
export function toRadian(a) {
|
||||
return a * _d2r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Radian To Degree
|
||||
*
|
||||
* @param {Number} a Angle in Radian
|
||||
*/
|
||||
export function toDegree(a) {
|
||||
return a * _r2d;
|
||||
}
|
||||
|
||||
/**
|
||||
* @method random
|
||||
*/
|
||||
export const random = Math.random;
|
||||
|
||||
/**
|
||||
* Returns a floating-point random number between min (inclusive) and max (exclusive).
|
||||
*
|
||||
* @method randomRange
|
||||
* @param {number} min
|
||||
* @param {number} max
|
||||
* @return {number} the random number
|
||||
*/
|
||||
export function randomRange(min, max) {
|
||||
return Math.random() * (max - min) + min;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random integer between min (inclusive) and max (exclusive).
|
||||
*
|
||||
* @method randomRangeInt
|
||||
* @param {number} min
|
||||
* @param {number} max
|
||||
* @return {number} the random integer
|
||||
*/
|
||||
export function randomRangeInt(min, max) {
|
||||
return Math.floor(randomRange(min, max));
|
||||
}
|
||||
|
||||
/**
|
||||
* Linear congruential generator using Hull-Dobell Theorem.
|
||||
*
|
||||
* @method pseudoRandom
|
||||
* @param {number} seed the random seed
|
||||
* @return {number} the pseudo random
|
||||
*/
|
||||
export function pseudoRandom(seed) {
|
||||
seed = (seed * 9301 + 49297) % 233280;
|
||||
return seed / 233280.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a floating-point pseudo-random number between min (inclusive) and max (exclusive).
|
||||
*
|
||||
* @method pseudoRandomRange
|
||||
* @param {number} seed
|
||||
* @param {number} min
|
||||
* @param {number} max
|
||||
* @return {number} the random number
|
||||
*/
|
||||
export function pseudoRandomRange(seed, min, max) {
|
||||
return pseudoRandom(seed) * (max - min) + min;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudo-random integer between min (inclusive) and max (exclusive).
|
||||
*
|
||||
* @method pseudoRandomRangeInt
|
||||
* @param {number} seed
|
||||
* @param {number} min
|
||||
* @param {number} max
|
||||
* @return {number} the random integer
|
||||
*/
|
||||
export function pseudoRandomRangeInt(seed, min, max) {
|
||||
return Math.floor(pseudoRandomRange(seed, min, max));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next power of two for the value
|
||||
*
|
||||
* @method nextPow2
|
||||
* @param {number} val
|
||||
* @return {number} the the next power of two
|
||||
*/
|
||||
export function nextPow2(val) {
|
||||
--val;
|
||||
val = (val >> 1) | val;
|
||||
val = (val >> 2) | val;
|
||||
val = (val >> 4) | val;
|
||||
val = (val >> 8) | val;
|
||||
val = (val >> 16) | val;
|
||||
++val;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns float remainder for t / length
|
||||
*
|
||||
* @method repeat
|
||||
* @param {number} t time start at 0
|
||||
* @param {number} length time of one cycle
|
||||
* @return {number} the time wrapped in the first cycle
|
||||
*/
|
||||
export function repeat(t, length) {
|
||||
return t - Math.floor(t / length) * length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns time wrapped in ping-pong mode
|
||||
*
|
||||
* @method repeat
|
||||
* @param {number} t time start at 0
|
||||
* @param {number} length time of one cycle
|
||||
* @return {number} the time wrapped in the first cycle
|
||||
*/
|
||||
export function pingPong(t, length) {
|
||||
t = repeat(t, length * 2);
|
||||
t = length - Math.abs(t - length);
|
||||
return t;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns ratio of a value within a given range
|
||||
*
|
||||
* @method repeat
|
||||
* @param {number} from start value
|
||||
* @param {number} to end value
|
||||
* @param {number} value given value
|
||||
* @return {number} the ratio between [from,to]
|
||||
*/
|
||||
export function inverseLerp(from, to, value) {
|
||||
return (value - from) / (to - from);
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
const _d2r = Math.PI / 180.0;
|
||||
|
||||
const _r2d = 180.0 / Math.PI;
|
||||
|
||||
export const EPSILON = 0.000001;
|
||||
|
||||
/**
|
||||
* Tests whether or not the arguments have approximately the same value, within an absolute
|
||||
* or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less
|
||||
* than or equal to 1.0, and a relative tolerance is used for larger values)
|
||||
*
|
||||
* @param a The first number to test.
|
||||
* @param b The second number to test.
|
||||
* @return True if the numbers are approximately equal, false otherwise.
|
||||
*/
|
||||
export function equals (a: number, b: number) {
|
||||
return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether or not the arguments have approximately the same value by given maxDiff
|
||||
*
|
||||
* @param a The first number to test.
|
||||
* @param b The second number to test.
|
||||
* @param maxDiff Maximum difference.
|
||||
* @return True if the numbers are approximately equal, false otherwise.
|
||||
*/
|
||||
export function approx (a: number, b: number, maxDiff: number) {
|
||||
maxDiff = maxDiff || EPSILON;
|
||||
return Math.abs(a - b) <= maxDiff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamps a value between a minimum float and maximum float value.
|
||||
*
|
||||
* @param val
|
||||
* @param min
|
||||
* @param max
|
||||
*/
|
||||
export function clamp (val: number, min: number, max: number) {
|
||||
return val < min ? min : val > max ? max : val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamps a value between 0 and 1.
|
||||
*
|
||||
* @param val
|
||||
*/
|
||||
export function clamp01 (val: number) {
|
||||
return val < 0 ? 0 : val > 1 ? 1 : val;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param from
|
||||
* @param to
|
||||
* @param ratio - The interpolation coefficient.
|
||||
*/
|
||||
export function lerp (from: number, to: number, ratio: number) {
|
||||
return from + (to - from) * ratio;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Degree To Radian
|
||||
*
|
||||
* @param {Number} a Angle in Degrees
|
||||
*/
|
||||
export function toRadian (a: number) {
|
||||
return a * _d2r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Radian To Degree
|
||||
*
|
||||
* @param {Number} a Angle in Radian
|
||||
*/
|
||||
export function toDegree (a: number) {
|
||||
return a * _r2d;
|
||||
}
|
||||
|
||||
/**
|
||||
* @method random
|
||||
*/
|
||||
export const random = Math.random;
|
||||
|
||||
/**
|
||||
* Returns a floating-point random number between min (inclusive) and max (exclusive).
|
||||
*
|
||||
* @method randomRange
|
||||
* @param min
|
||||
* @param max
|
||||
* @return The random number.
|
||||
*/
|
||||
export function randomRange (min: number, max: number) {
|
||||
return Math.random() * (max - min) + min;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random integer between min (inclusive) and max (exclusive).
|
||||
*
|
||||
* @param min
|
||||
* @param max
|
||||
* @return The random integer.
|
||||
*/
|
||||
export function randomRangeInt (min: number, max: number) {
|
||||
return Math.floor(randomRange(min, max));
|
||||
}
|
||||
|
||||
/**
|
||||
* Linear congruential generator using Hull-Dobell Theorem.
|
||||
*
|
||||
* @param seed The random seed.
|
||||
* @return The pseudo random.
|
||||
*/
|
||||
export function pseudoRandom (seed: number) {
|
||||
seed = (seed * 9301 + 49297) % 233280;
|
||||
return seed / 233280.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a floating-point pseudo-random number between min (inclusive) and max (exclusive).
|
||||
*
|
||||
* @param seed
|
||||
* @param min
|
||||
* @param max
|
||||
* @return The random number.
|
||||
*/
|
||||
export function pseudoRandomRange (seed: number, min: number, max: number) {
|
||||
return pseudoRandom(seed) * (max - min) + min;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudo-random integer between min (inclusive) and max (exclusive).
|
||||
*
|
||||
* @param seed
|
||||
* @param min
|
||||
* @param max
|
||||
* @return The random integer.
|
||||
*/
|
||||
export function pseudoRandomRangeInt (seed: number, min: number, max: number) {
|
||||
return Math.floor(pseudoRandomRange(seed, min, max));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next power of two for the value.
|
||||
*
|
||||
* @param val
|
||||
* @return The the next power of two.
|
||||
*/
|
||||
export function nextPow2 (val: number) {
|
||||
--val;
|
||||
val = (val >> 1) | val;
|
||||
val = (val >> 2) | val;
|
||||
val = (val >> 4) | val;
|
||||
val = (val >> 8) | val;
|
||||
val = (val >> 16) | val;
|
||||
++val;
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns float remainder for t / length.
|
||||
*
|
||||
* @param t Time start at 0.
|
||||
* @param length Time of one cycle.
|
||||
* @return The Time wrapped in the first cycle.
|
||||
*/
|
||||
export function repeat (t: number, length: number) {
|
||||
return t - Math.floor(t / length) * length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns time wrapped in ping-pong mode.
|
||||
*
|
||||
* @param t Time start at 0.
|
||||
* @param length Time of one cycle.
|
||||
* @return The time wrapped in the first cycle.
|
||||
*/
|
||||
export function pingPong (t: number, length: number) {
|
||||
t = repeat(t, length * 2);
|
||||
t = length - Math.abs(t - length);
|
||||
return t;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns ratio of a value within a given range.
|
||||
*
|
||||
* @param from Start value.
|
||||
* @param to End value.
|
||||
* @param value Given value.
|
||||
* @return The ratio between [from, to].
|
||||
*/
|
||||
export function inverseLerp (from: number, to: number, value: number) {
|
||||
return (value - from) / (to - from);
|
||||
}
|
||||
@@ -1,691 +0,0 @@
|
||||
import { EPSILON, random } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 2-dimensional vector.
|
||||
*
|
||||
* x, y is alias of the first, second component of vector, respectively.
|
||||
*/
|
||||
class vec2 {
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param {number} x - Value assigned to x component.
|
||||
* @param {number} y - Value assigned to y component.
|
||||
*/
|
||||
constructor(x = 0, y = 0) {
|
||||
/**
|
||||
* The x component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.x = x;
|
||||
|
||||
/**
|
||||
* The y component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param {number} x - Value assigned to x component.
|
||||
* @param {number} y - Value assigned to y component.
|
||||
* @return {vec2} The newly created vector.
|
||||
*/
|
||||
static create(x = 0, y = 0) {
|
||||
return new vec2(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a zero vector.
|
||||
*
|
||||
* @return {vec2} The newly created vector.
|
||||
*/
|
||||
static zero(out) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a vector.
|
||||
*
|
||||
* @param {vec2} a - Vector to clone.
|
||||
* @returns {vec2} The newly created vector.
|
||||
*/
|
||||
static clone(a) {
|
||||
return new vec2(a.x, a.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a vector into another.
|
||||
*
|
||||
* @param {vec2} out - The vector to modified.
|
||||
* @param {vec2} a - The specified vector.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static copy(out, a) {
|
||||
out.x = a.x;
|
||||
out.y = a.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the components of a vector to the given values.
|
||||
*
|
||||
* @param {vec2} out - The vector to modified.
|
||||
* @param {Number} x - Value set to x component.
|
||||
* @param {Number} y - Value set to y component.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static set(out, x, y) {
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static add(out, a, b) {
|
||||
out.x = a.x + b.x;
|
||||
out.y = a.y + b.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static subtract(out, a, b) {
|
||||
out.x = a.x - b.x;
|
||||
out.y = a.y - b.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec2.subtract}.
|
||||
*/
|
||||
static sub(out, a, b) {
|
||||
return vec2.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs multiply on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static multiply(out, a, b) {
|
||||
out.x = a.x * b.x;
|
||||
out.y = a.y * b.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec2.multiply}.
|
||||
*/
|
||||
static mul(out, a, b) {
|
||||
return vec2.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs division on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static divide(out, a, b) {
|
||||
out.x = a.x / b.x;
|
||||
out.y = a.y / b.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec2.divide}.
|
||||
*/
|
||||
static div(out, a, b) {
|
||||
return vec2.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.ceil on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to perform operation.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static ceil(out, a) {
|
||||
out.x = Math.ceil(a.x);
|
||||
out.y = Math.ceil(a.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.floor on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to perform operation.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static floor(out, a) {
|
||||
out.x = Math.floor(a.x);
|
||||
out.y = Math.floor(a.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static min(out, a, b) {
|
||||
out.x = Math.min(a.x, b.x);
|
||||
out.y = Math.min(a.y, b.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static max(out, a, b) {
|
||||
out.x = Math.max(a.x, b.x);
|
||||
out.y = Math.max(a.y, b.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.round on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to perform operation.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static round(out, a) {
|
||||
out.x = Math.round(a.x);
|
||||
out.y = Math.round(a.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a vector with a number.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to scale.
|
||||
* @param {number} b - The scale number.
|
||||
* @returns {vec2} out.
|
||||
* */
|
||||
static scale(out, a, b) {
|
||||
out.x = a.x * b;
|
||||
out.y = a.y * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors after scaling the second operand by a number.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @param {number} scale - The scale number before adding.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static scaleAndAdd(out, a, b, scale) {
|
||||
out.x = a.x + (b.x * scale);
|
||||
out.y = a.y + (b.y * scale);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the euclidian distance between two vectors.
|
||||
*
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {number} Distance between a and b.
|
||||
*/
|
||||
static distance(a, b) {
|
||||
let x = b.x - a.x,
|
||||
y = b.y - a.y;
|
||||
return Math.sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec2.distance}.
|
||||
*/
|
||||
static dist(a, b) {
|
||||
return vec2.distance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared euclidian distance between two vectors.
|
||||
*
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {number} Squared distance between a and b.
|
||||
*/
|
||||
static squaredDistance(a, b) {
|
||||
let x = b.x - a.x,
|
||||
y = b.y - a.y;
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec2.squaredDistance}.
|
||||
*/
|
||||
static sqrDist(a, b) {
|
||||
return vec2.squaredDistance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the length of a vector.
|
||||
*
|
||||
* @param {vec2} a - The vector.
|
||||
* @returns {Number} Length of the vector.
|
||||
*/
|
||||
static magnitude(a) {
|
||||
let x = a.x,
|
||||
y = a.y;
|
||||
return Math.sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec2.magnitude}.
|
||||
*/
|
||||
static mag(a) {
|
||||
return vec2.magnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared length of a vector.
|
||||
*
|
||||
* @param {vec2} a - The vector.
|
||||
* @returns {Number} Squared length of the vector.
|
||||
*/
|
||||
static squaredMagnitude(a) {
|
||||
let x = a.x,
|
||||
y = a.y;
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec2.squaredMagnitude}
|
||||
*/
|
||||
static sqrMag(a) {
|
||||
return vec2.squaredMagnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negates each component of a vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to negate.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static negate(out, a) {
|
||||
out.x = -a.x;
|
||||
out.y = -a.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invert the components of a vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to invert.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static inverse(out, a) {
|
||||
out.x = 1.0 / a.x;
|
||||
out.y = 1.0 / a.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely invert the components of a vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to invert.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static inverseSafe(out, a) {
|
||||
let x = a.x,
|
||||
y = a.y;
|
||||
|
||||
if (Math.abs(x) < EPSILON) {
|
||||
out.x = 0;
|
||||
} else {
|
||||
out.x = 1.0 / x;
|
||||
}
|
||||
|
||||
if (Math.abs(y) < EPSILON) {
|
||||
out.y = 0;
|
||||
} else {
|
||||
out.y = 1.0 / a.y;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to normalize.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static normalize(out, a) {
|
||||
let x = a.x,
|
||||
y = a.y;
|
||||
let len = x * x + y * y;
|
||||
if (len > 0) {
|
||||
//TODO: evaluate use of glm_invsqrt here?
|
||||
len = 1 / Math.sqrt(len);
|
||||
out.x = a.x * len;
|
||||
out.y = a.y * len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of two vectors.
|
||||
*
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {Number} Dot product of a and b.
|
||||
*/
|
||||
static dot(a, b) {
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the cross product of two vectors.
|
||||
* Note that the cross product must by definition produce a 3D vector.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static cross(out, a, b) {
|
||||
let z = a.x * b.y - a.y * b.x;
|
||||
out.x = out.y = 0;
|
||||
out.z = z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two vectors.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - The first operand.
|
||||
* @param {vec2} b - The second operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static lerp(out, a, b, t) {
|
||||
let ax = a.x,
|
||||
ay = a.y;
|
||||
out.x = ax + t * (b.x - ax);
|
||||
out.y = ay + t * (b.y - ay);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random vector uniformly distributed on a circle centered at the origin.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {Number} [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static random(out, scale) {
|
||||
scale = scale || 1.0;
|
||||
let r = random() * 2.0 * Math.PI;
|
||||
out.x = Math.cos(r) * scale;
|
||||
out.y = Math.sin(r) * scale;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 2x2 matrix.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to transform.
|
||||
* @param {mat2} m - The matrix.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static transformMat2(out, a, m) {
|
||||
let x = a.x,
|
||||
y = a.y;
|
||||
out.x = m.m00 * x + m.m02 * y;
|
||||
out.y = m.m01 * x + m.m03 * y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 2x3 matrix.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to transform.
|
||||
* @param {mat23} m - The matrix.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static transformMat23(out, a, m) {
|
||||
let x = a.x,
|
||||
y = a.y;
|
||||
out.x = m.m00 * x + m.m02 * y + m.m04;
|
||||
out.y = m.m01 * x + m.m03 * y + m.m05;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 3x3 matrix.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to transform.
|
||||
* @param {mat3} m - The matrix.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static transformMat3(out, a, m) {
|
||||
let x = a.x,
|
||||
y = a.y;
|
||||
out.x = m.m00 * x + m.m03 * y + m.m06;
|
||||
out.y = m.m01 * x + m.m04 * y + m.m07;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 4x4 matrix.
|
||||
* 3rd vector component is implicitly '0'.
|
||||
* 4th vector component is implicitly '1'.
|
||||
*
|
||||
* @param {vec2} out - Vector to store result.
|
||||
* @param {vec2} a - Vector to transform.
|
||||
* @param {mat4} m - The matrix.
|
||||
* @returns {vec2} out.
|
||||
*/
|
||||
static transformMat4(out, a, m) {
|
||||
let x = a.x,
|
||||
y = a.y;
|
||||
out.x = m.m00 * x + m.m04 * y + m.m12;
|
||||
out.y = m.m01 * x + m.m05 * y + m.m13;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform some operation over an array of vec2s.
|
||||
*
|
||||
* @param {Array} a the array of vectors to iterate over.
|
||||
* @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed.
|
||||
* @param {Number} offset Number of elements to skip at the beginning of the array.
|
||||
* @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array.
|
||||
* @param {Function} fn Function to call for each vector in the array.
|
||||
* @param {Object} [arg] additional argument to pass to fn.
|
||||
* @returns {Array} a.
|
||||
*/
|
||||
static forEach(a, stride, offset, count, fn, arg) {
|
||||
return vec2._forEach(a, stride, offset, count, fn, arg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a vector.
|
||||
*
|
||||
* @param {vec2} a - The vector.
|
||||
* @returns {String} - String representation of this vector.
|
||||
*/
|
||||
static str(a) {
|
||||
return `vec2(${a.x}, ${a.y})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a vector into array.
|
||||
*
|
||||
* @param {Array} out - Array to store result.
|
||||
* @param {vec2} v - The vector.
|
||||
* @returns {Array} out.
|
||||
*/
|
||||
static array(out, v) {
|
||||
out[0] = v.x;
|
||||
out[1] = v.y;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are equal. (Compared using ===)
|
||||
*
|
||||
* @param {vec2} a - The first vector.
|
||||
* @param {vec2} b - The second vector.
|
||||
* @returns {Boolean} True if the vectors are equal, false otherwise.
|
||||
*/
|
||||
static exactEquals(a, b) {
|
||||
return a.x === b.x && a.y === b.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are approximately equal.
|
||||
*
|
||||
* @param {vec2} a The first vector.
|
||||
* @param {vec2} b The second vector.
|
||||
* @returns {Boolean} True if the vectors are approximately equal, false otherwise.
|
||||
*/
|
||||
static equals(a, b) {
|
||||
let a0 = a.x, a1 = a.y;
|
||||
let b0 = b.x, b1 = b.y;
|
||||
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the angle between the two vectors.
|
||||
*
|
||||
* @param {vec2} a The first vector.
|
||||
* @param {vec2} b The second vector.
|
||||
* @returns {Number} The angle in radians.
|
||||
*/
|
||||
static angle(a, b) {
|
||||
return vec2._angle(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform some operation over an array of vec2s.
|
||||
*
|
||||
* @param {Array} a the array of vectors to iterate over.
|
||||
* @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed.
|
||||
* @param {Number} offset Number of elements to skip at the beginning of the array.
|
||||
* @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array.
|
||||
* @param {Function} fn Function to call for each vector in the array.
|
||||
* @param {Object} [arg] additional argument to pass to fn.
|
||||
* @returns {Array} a.
|
||||
* @ignore
|
||||
*/
|
||||
vec2._forEach = (function () {
|
||||
let vec = vec2.create(0, 0);
|
||||
|
||||
return function (a, stride, offset, count, fn, arg) {
|
||||
let i, l;
|
||||
if (!stride) {
|
||||
stride = 2;
|
||||
}
|
||||
|
||||
if (!offset) {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
if (count) {
|
||||
l = Math.min((count * stride) + offset, a.length);
|
||||
} else {
|
||||
l = a.length;
|
||||
}
|
||||
|
||||
for (i = offset; i < l; i += stride) {
|
||||
vec.x = a[i];
|
||||
vec.y = a[i + 1];
|
||||
fn(vec, vec, arg);
|
||||
a[i] = vec.x;
|
||||
a[i + 1] = vec.y;
|
||||
}
|
||||
|
||||
return a;
|
||||
};
|
||||
})();
|
||||
|
||||
vec2._angle = (function () {
|
||||
let tempA = vec2.create(0, 0);
|
||||
let tempB = vec2.create(0, 0);
|
||||
|
||||
return function (a, b) {
|
||||
vec2.normalize(tempA, a);
|
||||
vec2.normalize(tempB, b);
|
||||
let cosine = vec2.dot(tempA, tempB);
|
||||
if (cosine > 1.0) {
|
||||
return 0;
|
||||
}
|
||||
if (cosine < -1.0) {
|
||||
return Math.PI;
|
||||
}
|
||||
return Math.acos(cosine);
|
||||
};
|
||||
})();
|
||||
|
||||
export default vec2;
|
||||
@@ -0,0 +1,627 @@
|
||||
/// <reference path="./fwd-decls" />
|
||||
|
||||
import { EPSILON, random } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 2-dimensional vector.
|
||||
*
|
||||
* x, y is alias of the first, second component of vector, respectively.
|
||||
*/
|
||||
export default class vec2 {
|
||||
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param x - Value assigned to x component.
|
||||
* @param y - Value assigned to y component.
|
||||
* @return The newly created vector.
|
||||
*/
|
||||
public static create (x = 0, y = 0) {
|
||||
return new vec2(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a zero vector.
|
||||
*
|
||||
* @return The newly created vector.
|
||||
*/
|
||||
public static zero<Out extends IVec2Like> (out: Out) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a vector.
|
||||
*
|
||||
* @param a - Vector to clone.
|
||||
* @return The newly created vector.
|
||||
*/
|
||||
public static clone (a: vec2) {
|
||||
return new vec2(a.x, a.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a vector into another.
|
||||
*
|
||||
* @param out - The vector to modified.
|
||||
* @param a - The specified vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static copy<Out extends IVec2Like> (out: Out, a: vec2) {
|
||||
out.x = a.x;
|
||||
out.y = a.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the components of a vector to the given values.
|
||||
*
|
||||
* @param out - The vector to modified.
|
||||
* @param x - Value set to x component.
|
||||
* @param y - Value set to y component.
|
||||
* @return out.
|
||||
*/
|
||||
public static set<Out extends IVec2Like> (out: Out, x: number, y: number) {
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static add<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
|
||||
out.x = a.x + b.x;
|
||||
out.y = a.y + b.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static subtract<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
|
||||
out.x = a.x - b.x;
|
||||
out.y = a.y - b.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec2.subtract}.
|
||||
*/
|
||||
public static sub<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
|
||||
return vec2.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs multiply on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiply<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
|
||||
out.x = a.x * b.x;
|
||||
out.y = a.y * b.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec2.multiply}.
|
||||
*/
|
||||
public static mul<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
|
||||
return vec2.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs division on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static divide<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
|
||||
out.x = a.x / b.x;
|
||||
out.y = a.y / b.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec2.divide}.
|
||||
*/
|
||||
public static div<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
|
||||
return vec2.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.ceil on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to perform operation.
|
||||
* @return out.
|
||||
*/
|
||||
public static ceil<Out extends IVec2Like> (out: Out, a: vec2) {
|
||||
out.x = Math.ceil(a.x);
|
||||
out.y = Math.ceil(a.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.floor on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to perform operation.
|
||||
* @return out.
|
||||
*/
|
||||
public static floor<Out extends IVec2Like> (out: Out, a: vec2) {
|
||||
out.x = Math.floor(a.x);
|
||||
out.y = Math.floor(a.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static min<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
|
||||
out.x = Math.min(a.x, b.x);
|
||||
out.y = Math.min(a.y, b.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static max<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
|
||||
out.x = Math.max(a.x, b.x);
|
||||
out.y = Math.max(a.y, b.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.round on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to perform operation.
|
||||
* @return out.
|
||||
*/
|
||||
public static round<Out extends IVec2Like> (out: Out, a: vec2) {
|
||||
out.x = Math.round(a.x);
|
||||
out.y = Math.round(a.y);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a vector with a number.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to scale.
|
||||
* @param b - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static scale<Out extends IVec2Like> (out: Out, a: vec2, b: number) {
|
||||
out.x = a.x * b;
|
||||
out.y = a.y * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors after scaling the second operand by a number.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param scale - The scale number before adding.
|
||||
* @return out.
|
||||
*/
|
||||
public static scaleAndAdd<Out extends IVec2Like> (out: Out, a: vec2, b: vec2, scale: number) {
|
||||
out.x = a.x + (b.x * scale);
|
||||
out.y = a.y + (b.y * scale);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the euclidian distance between two vectors.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return Distance between a and b.
|
||||
*/
|
||||
public static distance (a: vec2, b: vec2) {
|
||||
const x = b.x - a.x;
|
||||
const y = b.y - a.y;
|
||||
return Math.sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec2.distance}.
|
||||
*/
|
||||
public static dist (a: vec2, b: vec2) {
|
||||
return vec2.distance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared euclidian distance between two vectors.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return Squared distance between a and b.
|
||||
*/
|
||||
public static squaredDistance (a: vec2, b: vec2) {
|
||||
const x = b.x - a.x;
|
||||
const y = b.y - a.y;
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec2.squaredDistance}.
|
||||
*/
|
||||
public static sqrDist (a: vec2, b: vec2) {
|
||||
return vec2.squaredDistance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the length of a vector.
|
||||
*
|
||||
* @param a - The vector.
|
||||
* @return Length of the vector.
|
||||
*/
|
||||
public static magnitude (a: vec2) {
|
||||
const { x, y } = a;
|
||||
return Math.sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec2.magnitude}.
|
||||
*/
|
||||
public static mag (a: vec2) {
|
||||
return vec2.magnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared length of a vector.
|
||||
*
|
||||
* @param a - The vector.
|
||||
* @return Squared length of the vector.
|
||||
*/
|
||||
public static squaredMagnitude (a: vec2) {
|
||||
const { x, y } = a;
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec2.squaredMagnitude}
|
||||
*/
|
||||
public static sqrMag (a: vec2) {
|
||||
return vec2.squaredMagnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negates each component of a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to negate.
|
||||
* @return out.
|
||||
*/
|
||||
public static negate<Out extends IVec2Like> (out: Out, a: vec2) {
|
||||
out.x = -a.x;
|
||||
out.y = -a.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invert the components of a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to invert.
|
||||
* @return out.
|
||||
*/
|
||||
public static inverse<Out extends IVec2Like> (out: Out, a: vec2) {
|
||||
out.x = 1.0 / a.x;
|
||||
out.y = 1.0 / a.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely invert the components of a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to invert.
|
||||
* @return out.
|
||||
*/
|
||||
public static inverseSafe<Out extends IVec2Like> (out: Out, a: vec2) {
|
||||
const { x, y } = a;
|
||||
|
||||
if (Math.abs(x) < EPSILON) {
|
||||
out.x = 0;
|
||||
} else {
|
||||
out.x = 1.0 / x;
|
||||
}
|
||||
|
||||
if (Math.abs(y) < EPSILON) {
|
||||
out.y = 0;
|
||||
} else {
|
||||
out.y = 1.0 / a.y;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to normalize.
|
||||
* @return out.
|
||||
*/
|
||||
public static normalize<Out extends IVec2Like> (out: Out, a: vec2) {
|
||||
const { x, y } = a;
|
||||
let len = x * x + y * y;
|
||||
if (len > 0) {
|
||||
// TODO: evaluate use of glm_invsqrt here?
|
||||
len = 1 / Math.sqrt(len);
|
||||
out.x = a.x * len;
|
||||
out.y = a.y * len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of two vectors.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return Dot product of a and b.
|
||||
*/
|
||||
public static dot (a: vec2, b: vec2) {
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the cross product of two vectors.
|
||||
* Note that the cross product must by definition produce a 3D vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static cross<Out extends IVec3Like> (out: Out, a: vec2, b: vec2) {
|
||||
const z = a.x * b.y - a.y * b.x;
|
||||
out.x = out.y = 0;
|
||||
out.z = z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two vectors.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out.
|
||||
*/
|
||||
public static lerp<Out extends IVec2Like> (out: Out, a: vec2, b: vec2, t: number) {
|
||||
const { x: ax, y: ay } = a;
|
||||
out.x = ax + t * (b.x - ax);
|
||||
out.y = ay + t * (b.y - ay);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random vector uniformly distributed on a circle centered at the origin.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned.
|
||||
* @return out.
|
||||
*/
|
||||
public static random<Out extends IVec2Like> (out: Out, scale: number) {
|
||||
scale = scale || 1.0;
|
||||
const r = random() * 2.0 * Math.PI;
|
||||
out.x = Math.cos(r) * scale;
|
||||
out.y = Math.sin(r) * scale;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 2x2 matrix.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformMat2<Out extends IVec2Like> (out: Out, a: vec2, m: IMat2Like) {
|
||||
const { x, y } = a;
|
||||
out.x = m.m00 * x + m.m02 * y;
|
||||
out.y = m.m01 * x + m.m03 * y;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 2x3 matrix.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformMat23<Out extends IVec2Like> (out: Out, a: vec2, m: IMat23Like) {
|
||||
const { x, y } = a;
|
||||
out.x = m.m00 * x + m.m02 * y + m.m04;
|
||||
out.y = m.m01 * x + m.m03 * y + m.m05;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 3x3 matrix.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformMat3<Out extends IVec2Like> (out: Out, a: vec2, m: IMat3Like) {
|
||||
const { x, y } = a;
|
||||
out.x = m.m00 * x + m.m03 * y + m.m06;
|
||||
out.y = m.m01 * x + m.m04 * y + m.m07;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 4x4 matrix.
|
||||
* 3rd vector component is implicitly '0'.
|
||||
* 4th vector component is implicitly '1'.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformMat4<Out extends IVec2Like> (out: Out, a: vec2, m: IMat4Like) {
|
||||
const { x, y } = a;
|
||||
out.x = m.m00 * x + m.m04 * y + m.m12;
|
||||
out.y = m.m01 * x + m.m05 * y + m.m13;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a vector.
|
||||
*
|
||||
* @param a - The vector.
|
||||
* @return - String representation of this vector.
|
||||
*/
|
||||
public static str (a: vec2) {
|
||||
return `vec2(${a.x}, ${a.y})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a vector into array.
|
||||
*
|
||||
* @param out - Array to store result.
|
||||
* @param v - The vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static array<Out extends IWritableArrayLike<number>> (out: Out, v: vec2) {
|
||||
out[0] = v.x;
|
||||
out[1] = v.y;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are equal. (Compared using ===)
|
||||
*
|
||||
* @param a - The first vector.
|
||||
* @param b - The second vector.
|
||||
* @return True if the vectors are equal, false otherwise.
|
||||
*/
|
||||
public static exactEquals (a: vec2, b: vec2) {
|
||||
return a.x === b.x && a.y === b.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are approximately equal.
|
||||
*
|
||||
* @param a The first vector.
|
||||
* @param b The second vector.
|
||||
* @return True if the vectors are approximately equal, false otherwise.
|
||||
*/
|
||||
public static equals (a: vec2, b: vec2) {
|
||||
const { x: a0, y: a1 } = a;
|
||||
const { x: b0, y: b1 } = b;
|
||||
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the angle between the two vectors.
|
||||
*
|
||||
* @param a The first vector.
|
||||
* @param b The second vector.
|
||||
* @return The angle in radians.
|
||||
*/
|
||||
public static angle (a: vec2, b: vec2) {
|
||||
vec2.normalize(tmpVec2A, a);
|
||||
vec2.normalize(tmpVec2B, b);
|
||||
const cosine = vec2.dot(tmpVec2A, tmpVec2B);
|
||||
if (cosine > 1.0) {
|
||||
return 0;
|
||||
}
|
||||
if (cosine < -1.0) {
|
||||
return Math.PI;
|
||||
}
|
||||
return Math.acos(cosine);
|
||||
}
|
||||
|
||||
/**
|
||||
* The x component.
|
||||
*/
|
||||
public x: number;
|
||||
|
||||
/**
|
||||
* The y component.
|
||||
*/
|
||||
public y: number;
|
||||
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param x - Value assigned to x component.
|
||||
* @param y - Value assigned to y component.
|
||||
*/
|
||||
constructor (x = 0, y = 0) {
|
||||
/**
|
||||
* The x component.
|
||||
*/
|
||||
this.x = x;
|
||||
|
||||
/**
|
||||
* The y component.
|
||||
*/
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
const tmpVec2A = vec2.create();
|
||||
const tmpVec2B = vec2.create();
|
||||
@@ -1,934 +0,0 @@
|
||||
import { EPSILON, random } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 3-dimensional vector.
|
||||
*
|
||||
* x, y, z is alias of the first, second, third component of vector, respectively.
|
||||
*/
|
||||
class vec3 {
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param {number} x - Value assigned to x component.
|
||||
* @param {number} y - Value assigned to y component.
|
||||
* @param {number} z - Value assigned to z component.
|
||||
*/
|
||||
constructor(x = 0, y = 0, z = 0) {
|
||||
/**
|
||||
* The x component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.x = x;
|
||||
|
||||
/**
|
||||
* The y component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.y = y;
|
||||
|
||||
/**
|
||||
* The z component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
static UNIT_X = { x: 1, y: 0, z: 0 };
|
||||
static UNIT_Y = { x: 0, y: 1, z: 0 };
|
||||
static UNIT_Z = { x: 0, y: 0, z: 1 };
|
||||
static ZERO = { x: 0, y: 0, z: 0 };
|
||||
static ONE = { x: 1, y: 1, z: 1 };
|
||||
static NEG_ONE = { x: -1, y: -1, z: -1 };
|
||||
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param {number} x - Value assigned to x component.
|
||||
* @param {number} y - Value assigned to y component.
|
||||
* @param {number} z - Value assigned to z component.
|
||||
* @return {vec3} The newly created vector.
|
||||
*/
|
||||
static create(x = 0, y = 0, z = 0) {
|
||||
return new vec3(x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a zero vector.
|
||||
*
|
||||
* @return {vec3} The newly created vector.
|
||||
*/
|
||||
static zero(out) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
out.z = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a vector.
|
||||
*
|
||||
* @param {vec3} a - Vector to clone.
|
||||
* @returns {vec3} The newly created vector.
|
||||
*/
|
||||
static clone(a) {
|
||||
return new vec3(a.x, a.y, a.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a vector into another.
|
||||
*
|
||||
* @param {vec3} out - The vector to modified.
|
||||
* @param {vec3} a - The specified vector.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static copy(out, a) {
|
||||
out.x = a.x;
|
||||
out.y = a.y;
|
||||
out.z = a.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the components of a vector to the given values.
|
||||
*
|
||||
* @param {vec3} out - The vector to modified.
|
||||
* @param {Number} x - Value set to x component.
|
||||
* @param {Number} y - Value set to y component.
|
||||
* @param {Number} z - Value set to z component.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static set(out, x, y, z) {
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
out.z = z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static add(out, a, b) {
|
||||
out.x = a.x + b.x;
|
||||
out.y = a.y + b.y;
|
||||
out.z = a.z + b.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static subtract(out, a, b) {
|
||||
out.x = a.x - b.x;
|
||||
out.y = a.y - b.y;
|
||||
out.z = a.z - b.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.subtract}.
|
||||
*/
|
||||
static sub(out, a, b) {
|
||||
return vec3.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs multiply on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static multiply(out, a, b) {
|
||||
out.x = a.x * b.x;
|
||||
out.y = a.y * b.y;
|
||||
out.z = a.z * b.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.multiply}.
|
||||
*/
|
||||
static mul(out, a, b) {
|
||||
return vec3.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs division on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static divide(out, a, b) {
|
||||
out.x = a.x / b.x;
|
||||
out.y = a.y / b.y;
|
||||
out.z = a.z / b.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.divide}.
|
||||
*/
|
||||
static div(out, a, b) {
|
||||
return vec3.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.ceil on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to perform operation.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static ceil(out, a) {
|
||||
out.x = Math.ceil(a.x);
|
||||
out.y = Math.ceil(a.y);
|
||||
out.z = Math.ceil(a.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.floor on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to perform operation.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static floor(out, a) {
|
||||
out.x = Math.floor(a.x);
|
||||
out.y = Math.floor(a.y);
|
||||
out.z = Math.floor(a.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static min(out, a, b) {
|
||||
out.x = Math.min(a.x, b.x);
|
||||
out.y = Math.min(a.y, b.y);
|
||||
out.z = Math.min(a.z, b.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static max(out, a, b) {
|
||||
out.x = Math.max(a.x, b.x);
|
||||
out.y = Math.max(a.y, b.y);
|
||||
out.z = Math.max(a.z, b.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.round on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to perform operation.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static round(out, a) {
|
||||
out.x = Math.round(a.x);
|
||||
out.y = Math.round(a.y);
|
||||
out.z = Math.round(a.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a vector with a number.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to scale.
|
||||
* @param {number} b - The scale number.
|
||||
* @returns {vec3} out.
|
||||
* */
|
||||
static scale(out, a, b) {
|
||||
out.x = a.x * b;
|
||||
out.y = a.y * b;
|
||||
out.z = a.z * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors after scaling the second operand by a number.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @param {number} scale - The scale number before adding.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static scaleAndAdd(out, a, b, scale) {
|
||||
out.x = a.x + (b.x * scale);
|
||||
out.y = a.y + (b.y * scale);
|
||||
out.z = a.z + (b.z * scale);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the euclidian distance between two vectors.
|
||||
*
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {number} Distance between a and b.
|
||||
*/
|
||||
static distance(a, b) {
|
||||
let x = b.x - a.x,
|
||||
y = b.y - a.y,
|
||||
z = b.z - a.z;
|
||||
return Math.sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.distance}.
|
||||
*/
|
||||
static dist(a, b) {
|
||||
return vec3.distance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared euclidian distance between two vectors.
|
||||
*
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {number} Squared distance between a and b.
|
||||
*/
|
||||
static squaredDistance(a, b) {
|
||||
let x = b.x - a.x,
|
||||
y = b.y - a.y,
|
||||
z = b.z - a.z;
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.squaredDistance}.
|
||||
*/
|
||||
static sqrDist(a, b) {
|
||||
return vec3.squaredDistance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the length of a vector.
|
||||
*
|
||||
* @param {vec3} a - The vector.
|
||||
* @returns {Number} Length of the vector.
|
||||
*/
|
||||
static magnitude(a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z;
|
||||
return Math.sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec3.magnitude}.
|
||||
*/
|
||||
static mag(a) {
|
||||
return vec3.magnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared length of a vector.
|
||||
*
|
||||
* @param {vec3} a - The vector.
|
||||
* @returns {Number} Squared length of the vector.
|
||||
*/
|
||||
static squaredMagnitude(a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z;
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec3.squaredMagnitude}
|
||||
*/
|
||||
static sqrMag(a) {
|
||||
return vec3.squaredMagnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negates each component of a vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to negate.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static negate(out, a) {
|
||||
out.x = -a.x;
|
||||
out.y = -a.y;
|
||||
out.z = -a.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts the components of a vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to invert.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static inverse(out, a) {
|
||||
out.x = 1.0 / a.x;
|
||||
out.y = 1.0 / a.y;
|
||||
out.z = 1.0 / a.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely inverts the components of a vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to invert.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static inverseSafe(out, a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z;
|
||||
|
||||
if (Math.abs(x) < EPSILON) {
|
||||
out.x = 0;
|
||||
} else {
|
||||
out.x = 1.0 / x;
|
||||
}
|
||||
|
||||
if (Math.abs(y) < EPSILON) {
|
||||
out.y = 0;
|
||||
} else {
|
||||
out.y = 1.0 / y;
|
||||
}
|
||||
|
||||
if (Math.abs(z) < EPSILON) {
|
||||
out.z = 0;
|
||||
} else {
|
||||
out.z = 1.0 / z;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a vector.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to normalize.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static normalize(out, a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z;
|
||||
|
||||
let len = x * x + y * y + z * z;
|
||||
if (len > 0) {
|
||||
//TODO: evaluate use of glm_invsqrt here?
|
||||
len = 1 / Math.sqrt(len);
|
||||
out.x = x * len;
|
||||
out.y = y * len;
|
||||
out.z = z * len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of two vectors.
|
||||
*
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {Number} Dot product of a and b.
|
||||
*/
|
||||
static dot(a, b) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the cross product of two vectors.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static cross(out, a, b) {
|
||||
let ax = a.x, ay = a.y, az = a.z,
|
||||
bx = b.x, by = b.y, bz = b.z;
|
||||
|
||||
out.x = ay * bz - az * by;
|
||||
out.y = az * bx - ax * bz;
|
||||
out.z = ax * by - ay * bx;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two vectors.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static lerp(out, a, b, t) {
|
||||
let ax = a.x,
|
||||
ay = a.y,
|
||||
az = a.z;
|
||||
out.x = ax + t * (b.x - ax);
|
||||
out.y = ay + t * (b.y - ay);
|
||||
out.z = az + t * (b.z - az);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a hermite interpolation with two control points.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @param {vec3} c - The third operand.
|
||||
* @param {vec3} d - The fourth operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static hermite(out, a, b, c, d, t) {
|
||||
let factorTimes2 = t * t,
|
||||
factor1 = factorTimes2 * (2 * t - 3) + 1,
|
||||
factor2 = factorTimes2 * (t - 2) + t,
|
||||
factor3 = factorTimes2 * (t - 1),
|
||||
factor4 = factorTimes2 * (3 - 2 * t);
|
||||
|
||||
out.x = a.x * factor1 + b.x * factor2 + c.x * factor3 + d.x * factor4;
|
||||
out.y = a.y * factor1 + b.y * factor2 + c.y * factor3 + d.y * factor4;
|
||||
out.z = a.z * factor1 + b.z * factor2 + c.z * factor3 + d.z * factor4;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a bezier interpolation with two control points.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @param {vec3} c - The third operand.
|
||||
* @param {vec3} d - The fourth operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static bezier(out, a, b, c, d, t) {
|
||||
let inverseFactor = 1 - t,
|
||||
inverseFactorTimesTwo = inverseFactor * inverseFactor,
|
||||
factorTimes2 = t * t,
|
||||
factor1 = inverseFactorTimesTwo * inverseFactor,
|
||||
factor2 = 3 * t * inverseFactorTimesTwo,
|
||||
factor3 = 3 * factorTimes2 * inverseFactor,
|
||||
factor4 = factorTimes2 * t;
|
||||
|
||||
out.x = a.x * factor1 + b.x * factor2 + c.x * factor3 + d.x * factor4;
|
||||
out.y = a.y * factor1 + b.y * factor2 + c.y * factor3 + d.y * factor4;
|
||||
out.z = a.z * factor1 + b.z * factor2 + c.z * factor3 + d.z * factor4;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random vector uniformly distributed on a sphere centered at the origin.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {Number} [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static random(out, scale) {
|
||||
scale = scale || 1.0;
|
||||
|
||||
let phi = random() * 2.0 * Math.PI;
|
||||
let theta = Math.acos(random() * 2 - 1);
|
||||
|
||||
out.x = Math.sin(theta) * Math.cos(phi) * scale;
|
||||
out.y = Math.sin(theta) * Math.sin(phi) * scale;
|
||||
out.z = Math.cos(theta) * scale;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a point vector with a 4x4 matrix,
|
||||
* i.e. 4th vector component is implicitly '1'.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to transform.
|
||||
* @param {mat4} m - The matrix.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static transformMat4(out, a, m) {
|
||||
let x = a.x, y = a.y, z = a.z,
|
||||
rhw = m.m03 * x + m.m07 * y + m.m11 * z + m.m15;
|
||||
rhw = rhw ? 1 / rhw : 1;
|
||||
out.x = (m.m00 * x + m.m04 * y + m.m08 * z + m.m12) * rhw;
|
||||
out.y = (m.m01 * x + m.m05 * y + m.m09 * z + m.m13) * rhw;
|
||||
out.z = (m.m02 * x + m.m06 * y + m.m10 * z + m.m14) * rhw;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a normal vector with a 4x4 matrix,
|
||||
* i.e. 4th vector component is implicitly '0'.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to transform.
|
||||
* @param {mat4} m - The matrix.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static transformMat4Normal(out, a, m) {
|
||||
let x = a.x, y = a.y, z = a.z,
|
||||
rhw = m.m03 * x + m.m07 * y + m.m11 * z;
|
||||
rhw = rhw ? 1 / rhw : 1;
|
||||
out.x = (m.m00 * x + m.m04 * y + m.m08 * z) * rhw;
|
||||
out.y = (m.m01 * x + m.m05 * y + m.m09 * z) * rhw;
|
||||
out.z = (m.m02 * x + m.m06 * y + m.m10 * z) * rhw;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 3x3 matrix.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to transform.
|
||||
* @param {mat3} m - The matrix.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static transformMat3(out, a, m) {
|
||||
let x = a.x, y = a.y, z = a.z;
|
||||
out.x = x * m.m00 + y * m.m03 + z * m.m06;
|
||||
out.y = x * m.m01 + y * m.m04 + z * m.m07;
|
||||
out.z = x * m.m02 + y * m.m05 + z * m.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a quaternion.
|
||||
*
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - Vector to transform.
|
||||
* @param {quat} q - The quaternion.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static transformQuat(out, a, q) {
|
||||
// benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
|
||||
|
||||
let x = a.x, y = a.y, z = a.z;
|
||||
let qx = q.x, qy = q.y, qz = q.z, qw = q.w;
|
||||
|
||||
// calculate quat * vec
|
||||
let ix = qw * x + qy * z - qz * y;
|
||||
let iy = qw * y + qz * x - qx * z;
|
||||
let iz = qw * z + qx * y - qy * x;
|
||||
let iw = -qx * x - qy * y - qz * z;
|
||||
|
||||
// calculate result * inverse quat
|
||||
out.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
|
||||
out.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
|
||||
out.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a 3D vector around the x-axis.
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The point to rotate.
|
||||
* @param {vec3} b - The origin of the rotation.
|
||||
* @param {Number} c - The angle of rotation.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static rotateX(out, a, b, c) {
|
||||
// Translate point to the origin
|
||||
let px = a.x - b.x;
|
||||
let py = a.y - b.y;
|
||||
let pz = a.z - b.z;
|
||||
|
||||
//perform rotation
|
||||
let rx = px;
|
||||
let ry = py * Math.cos(c) - pz * Math.sin(c);
|
||||
let rz = py * Math.sin(c) + pz * Math.cos(c);
|
||||
|
||||
//translate to correct position
|
||||
out.x = rx + b.x;
|
||||
out.y = ry + b.y;
|
||||
out.z = rz + b.z;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a 3D vector around the y-axis.
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The point to rotate.
|
||||
* @param {vec3} b - The origin of the rotation.
|
||||
* @param {Number} c - The angle of rotation.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static rotateY(out, a, b, c) {
|
||||
//Translate point to the origin
|
||||
let px = a.x - b.x;
|
||||
let py = a.y - b.y;
|
||||
let pz = a.z - b.z;
|
||||
|
||||
//perform rotation
|
||||
let rx = pz * Math.sin(c) + px * Math.cos(c);
|
||||
let ry = py;
|
||||
let rz = pz * Math.cos(c) - px * Math.sin(c);
|
||||
|
||||
//translate to correct position
|
||||
out.x = rx + b.x;
|
||||
out.y = ry + b.y;
|
||||
out.z = rz + b.z;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a 3D vector around the z-axis.
|
||||
* @param {vec3} out - Vector to store result.
|
||||
* @param {vec3} a - The point to rotate.
|
||||
* @param {vec3} b - The origin of the rotation.
|
||||
* @param {Number} c - The angle of rotation.
|
||||
* @returns {vec3} out.
|
||||
*/
|
||||
static rotateZ(out, a, b, c) {
|
||||
//Translate point to the origin
|
||||
let px = a.x - b.x;
|
||||
let py = a.y - b.y;
|
||||
let pz = a.z - b.z;
|
||||
|
||||
//perform rotation
|
||||
let rx = px * Math.cos(c) - py * Math.sin(c);
|
||||
let ry = px * Math.sin(c) + py * Math.cos(c);
|
||||
let rz = pz;
|
||||
|
||||
//translate to correct position
|
||||
out.x = rx + b.x;
|
||||
out.y = ry + b.y;
|
||||
out.z = rz + b.z;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a vector.
|
||||
*
|
||||
* @param {vec3} a - The vector.
|
||||
* @returns {String} - String representation of this vector.
|
||||
*/
|
||||
static str(a) {
|
||||
return `vec3(${a.x}, ${a.y}, ${a.z})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a vector into array.
|
||||
*
|
||||
* @param {Array} out - Array to store result.
|
||||
* @param {vec3} v - The vector.
|
||||
* @returns {Array} out.
|
||||
*/
|
||||
static array(out, v) {
|
||||
out[0] = v.x;
|
||||
out[1] = v.y;
|
||||
out[2] = v.z;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are equal. (Compared using ===)
|
||||
*
|
||||
* @param {vec3} a - The first vector.
|
||||
* @param {vec3} b - The second vector.
|
||||
* @returns {Boolean} True if the vectors are equal, false otherwise.
|
||||
*/
|
||||
static exactEquals(a, b) {
|
||||
return a.x === b.x && a.y === b.y && a.z === b.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are approximately equal.
|
||||
*
|
||||
* @param {vec3} a The first vector.
|
||||
* @param {vec3} b The second vector.
|
||||
* @returns {Boolean} True if the vectors are approximately equal, false otherwise.
|
||||
*/
|
||||
static equals(a, b) {
|
||||
let a0 = a.x, a1 = a.y, a2 = a.z;
|
||||
let b0 = b.x, b1 = b.y, b2 = b.z;
|
||||
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform some operation over an array of vec3s.
|
||||
*
|
||||
* @param {Array} a the array of vectors to iterate over.
|
||||
* @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed.
|
||||
* @param {Number} offset Number of elements to skip at the beginning of the array.
|
||||
* @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array.
|
||||
* @param {Function} fn Function to call for each vector in the array.
|
||||
* @param {Object} [arg] additional argument to pass to fn.
|
||||
* @returns {Array} a.
|
||||
*/
|
||||
static forEach(a, stride, offset, count, fn, arg) {
|
||||
return vec3._forEach(a, stride, offset, count, fn, arg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the angle between two vectors.
|
||||
* @param {vec3} a - The first operand.
|
||||
* @param {vec3} b - The second operand.
|
||||
* @returns {Number} - The angle in radians.
|
||||
*/
|
||||
static angle(a, b) {
|
||||
return vec3._angle(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects a vector onto a plane represented by its normal.
|
||||
* @param {vec3} out The result vector.
|
||||
* @param {vec3} a The vector.
|
||||
* @param {vec3} n The plane's normal.
|
||||
*/
|
||||
static projectOnPlane(out, a, n) {
|
||||
return vec3.sub(out, a, vec3.project(out, a, n));
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects a vector onto another vector.
|
||||
* @param {vec3} out The result vector.
|
||||
* @param {vec3} a The vector to project.
|
||||
* @param {vec3} b The vector onto which the projection performs.
|
||||
*/
|
||||
static project(out, a, b) {
|
||||
let sqrLen = vec3.squaredMagnitude(b);
|
||||
if (sqrLen < 0.000001)
|
||||
return vec3.set(out, 0, 0, 0);
|
||||
else
|
||||
return vec3.scale(out, b, vec3.dot(a, b) / sqrLen);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform some operation over an array of vec3s.
|
||||
*
|
||||
* @param {Array} a the array of vectors to iterate over.
|
||||
* @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed.
|
||||
* @param {Number} offset Number of elements to skip at the beginning of the array.
|
||||
* @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array.
|
||||
* @param {Function} fn Function to call for each vector in the array.
|
||||
* @param {Object} [arg] additional argument to pass to fn.
|
||||
* @returns {Array} a.
|
||||
* @ignore.
|
||||
*/
|
||||
vec3._forEach = (function () {
|
||||
let vec = vec3.create(0, 0, 0);
|
||||
|
||||
return function (a, stride, offset, count, fn, arg) {
|
||||
let i, l;
|
||||
if (!stride) {
|
||||
stride = 3;
|
||||
}
|
||||
|
||||
if (!offset) {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
if (count) {
|
||||
l = Math.min((count * stride) + offset, a.length);
|
||||
} else {
|
||||
l = a.length;
|
||||
}
|
||||
|
||||
for (i = offset; i < l; i += stride) {
|
||||
vec.x = a[i];
|
||||
vec.y = a[i + 1];
|
||||
vec.z = a[i + 2];
|
||||
fn(vec, vec, arg);
|
||||
a[i] = vec.x;
|
||||
a[i + 1] = vec.y;
|
||||
a[i + 2] = vec.z;
|
||||
}
|
||||
|
||||
return a;
|
||||
};
|
||||
})();
|
||||
|
||||
/**
|
||||
* Get the angle between two 3D vectors.
|
||||
* @param {vec3} a The first operand.
|
||||
* @param {vec3} b The second operand.
|
||||
* @returns {Number} The angle in radians.
|
||||
* @ignore
|
||||
*/
|
||||
vec3._angle = (function () {
|
||||
let tempA = vec3.create(0, 0, 0);
|
||||
let tempB = vec3.create(0, 0, 0);
|
||||
|
||||
return function (a, b) {
|
||||
vec3.copy(tempA, a);
|
||||
vec3.copy(tempB, b);
|
||||
|
||||
vec3.normalize(tempA, tempA);
|
||||
vec3.normalize(tempB, tempB);
|
||||
|
||||
let cosine = vec3.dot(tempA, tempB);
|
||||
|
||||
if (cosine > 1.0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cosine < -1.0) {
|
||||
return Math.PI;
|
||||
}
|
||||
|
||||
return Math.acos(cosine);
|
||||
};
|
||||
})();
|
||||
|
||||
export default vec3;
|
||||
@@ -0,0 +1,853 @@
|
||||
/// <reference path="./fwd-decls" />
|
||||
|
||||
import { EPSILON, random } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 3-dimensional vector.
|
||||
*
|
||||
* x, y, z is alias of the first, second, third component of vector, respectively.
|
||||
*/
|
||||
export default class vec3 {
|
||||
public static UNIT_X = { x: 1, y: 0, z: 0 };
|
||||
public static UNIT_Y = { x: 0, y: 1, z: 0 };
|
||||
public static UNIT_Z = { x: 0, y: 0, z: 1 };
|
||||
public static ZERO = { x: 0, y: 0, z: 0 };
|
||||
public static ONE = { x: 1, y: 1, z: 1 };
|
||||
public static NEG_ONE = { x: -1, y: -1, z: -1 };
|
||||
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param x - Value assigned to x component.
|
||||
* @param y - Value assigned to y component.
|
||||
* @param z - Value assigned to z component.
|
||||
* @return The newly created vector.
|
||||
*/
|
||||
public static create (x = 0, y = 0, z = 0) {
|
||||
return new vec3(x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a zero vector.
|
||||
*
|
||||
* @return The newly created vector.
|
||||
*/
|
||||
public static zero<Out extends IVec3Like> (out: Out) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
out.z = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a vector.
|
||||
*
|
||||
* @param a - Vector to clone.
|
||||
* @return The newly created vector.
|
||||
*/
|
||||
public static clone (a: vec3) {
|
||||
return new vec3(a.x, a.y, a.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a vector into another.
|
||||
*
|
||||
* @param out - The vector to modified.
|
||||
* @param a - The specified vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static copy<Out extends IVec3Like> (out: Out, a: vec3) {
|
||||
out.x = a.x;
|
||||
out.y = a.y;
|
||||
out.z = a.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the components of a vector to the given values.
|
||||
*
|
||||
* @param out - The vector to modified.
|
||||
* @param x - Value set to x component.
|
||||
* @param y - Value set to y component.
|
||||
* @param z - Value set to z component.
|
||||
* @return out.
|
||||
*/
|
||||
public static set<Out extends IVec3Like> (out: Out, x: number, y: number, z: number) {
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
out.z = z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static add<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
out.x = a.x + b.x;
|
||||
out.y = a.y + b.y;
|
||||
out.z = a.z + b.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static subtract<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
out.x = a.x - b.x;
|
||||
out.y = a.y - b.y;
|
||||
out.z = a.z - b.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.subtract}.
|
||||
*/
|
||||
public static sub<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
return vec3.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs multiply on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiply<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
out.x = a.x * b.x;
|
||||
out.y = a.y * b.y;
|
||||
out.z = a.z * b.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.multiply}.
|
||||
*/
|
||||
public static mul<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
return vec3.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs division on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static divide<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
out.x = a.x / b.x;
|
||||
out.y = a.y / b.y;
|
||||
out.z = a.z / b.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.divide}.
|
||||
*/
|
||||
public static div<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
return vec3.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.ceil on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to perform operation.
|
||||
* @return out.
|
||||
*/
|
||||
public static ceil<Out extends IVec3Like> (out: Out, a: vec3) {
|
||||
out.x = Math.ceil(a.x);
|
||||
out.y = Math.ceil(a.y);
|
||||
out.z = Math.ceil(a.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.floor on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to perform operation.
|
||||
* @return out.
|
||||
*/
|
||||
public static floor<Out extends IVec3Like> (out: Out, a: vec3) {
|
||||
out.x = Math.floor(a.x);
|
||||
out.y = Math.floor(a.y);
|
||||
out.z = Math.floor(a.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static min<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
out.x = Math.min(a.x, b.x);
|
||||
out.y = Math.min(a.y, b.y);
|
||||
out.z = Math.min(a.z, b.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static max<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
out.x = Math.max(a.x, b.x);
|
||||
out.y = Math.max(a.y, b.y);
|
||||
out.z = Math.max(a.z, b.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.round on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to perform operation.
|
||||
* @return out.
|
||||
*/
|
||||
public static round<Out extends IVec3Like> (out: Out, a: vec3) {
|
||||
out.x = Math.round(a.x);
|
||||
out.y = Math.round(a.y);
|
||||
out.z = Math.round(a.z);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a vector with a number.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to scale.
|
||||
* @param b - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static scale<Out extends IVec3Like> (out: Out, a: vec3, b: number) {
|
||||
out.x = a.x * b;
|
||||
out.y = a.y * b;
|
||||
out.z = a.z * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors after scaling the second operand by a number.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param scale - The scale number before adding.
|
||||
* @return out.
|
||||
*/
|
||||
public static scaleAndAdd<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, scale: number) {
|
||||
out.x = a.x + b.x * scale;
|
||||
out.y = a.y + b.y * scale;
|
||||
out.z = a.z + b.z * scale;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the euclidian distance between two vectors.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return Distance between a and b.
|
||||
*/
|
||||
public static distance (a: vec3, b: vec3) {
|
||||
const x = b.x - a.x;
|
||||
const y = b.y - a.y;
|
||||
const z = b.z - a.z;
|
||||
return Math.sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.distance}.
|
||||
*/
|
||||
public static dist (a: vec3, b: vec3) {
|
||||
return vec3.distance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared euclidian distance between two vectors.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return Squared distance between a and b.
|
||||
*/
|
||||
public static squaredDistance (a: vec3, b: vec3) {
|
||||
const x = b.x - a.x;
|
||||
const y = b.y - a.y;
|
||||
const z = b.z - a.z;
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.squaredDistance}.
|
||||
*/
|
||||
public static sqrDist (a: vec3, b: vec3) {
|
||||
return vec3.squaredDistance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the length of a vector.
|
||||
*
|
||||
* @param a - The vector.
|
||||
* @return Length of the vector.
|
||||
*/
|
||||
public static magnitude (a: vec3) {
|
||||
const { x, y, z } = a;
|
||||
return Math.sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.magnitude}.
|
||||
*/
|
||||
public static mag (a: vec3) {
|
||||
return vec3.magnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared length of a vector.
|
||||
*
|
||||
* @param a - The vector.
|
||||
* @return Squared length of the vector.
|
||||
*/
|
||||
public static squaredMagnitude (a: vec3) {
|
||||
const { x, y, z } = a;
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec3.squaredMagnitude}
|
||||
*/
|
||||
public static sqrMag (a: vec3) {
|
||||
return vec3.squaredMagnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negates each component of a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to negate.
|
||||
* @return out.
|
||||
*/
|
||||
public static negate<Out extends IVec3Like> (out: Out, a: vec3) {
|
||||
out.x = -a.x;
|
||||
out.y = -a.y;
|
||||
out.z = -a.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts the components of a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to invert.
|
||||
* @return out.
|
||||
*/
|
||||
public static inverse<Out extends IVec3Like> (out: Out, a: vec3) {
|
||||
out.x = 1.0 / a.x;
|
||||
out.y = 1.0 / a.y;
|
||||
out.z = 1.0 / a.z;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely inverts the components of a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to invert.
|
||||
* @return out.
|
||||
*/
|
||||
public static inverseSafe<Out extends IVec3Like> (out: Out, a: vec3) {
|
||||
const { x, y, z } = a;
|
||||
|
||||
if (Math.abs(x) < EPSILON) {
|
||||
out.x = 0;
|
||||
} else {
|
||||
out.x = 1.0 / x;
|
||||
}
|
||||
|
||||
if (Math.abs(y) < EPSILON) {
|
||||
out.y = 0;
|
||||
} else {
|
||||
out.y = 1.0 / y;
|
||||
}
|
||||
|
||||
if (Math.abs(z) < EPSILON) {
|
||||
out.z = 0;
|
||||
} else {
|
||||
out.z = 1.0 / z;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to normalize.
|
||||
* @return out.
|
||||
*/
|
||||
public static normalize<Out extends IVec3Like> (out: Out, a: vec3) {
|
||||
const { x, y, z } = a;
|
||||
|
||||
let len = x * x + y * y + z * z;
|
||||
if (len > 0) {
|
||||
// TODO: evaluate use of glm_invsqrt here?
|
||||
len = 1 / Math.sqrt(len);
|
||||
out.x = x * len;
|
||||
out.y = y * len;
|
||||
out.z = z * len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of two vectors.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return Dot product of a and b.
|
||||
*/
|
||||
public static dot (a: vec3, b: vec3) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the cross product of two vectors.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static cross<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
const { x: ax, y: ay, z: az } = a;
|
||||
const { x: bx, y: by, z: bz } = b;
|
||||
|
||||
out.x = ay * bz - az * by;
|
||||
out.y = az * bx - ax * bz;
|
||||
out.z = ax * by - ay * bx;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two vectors.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out.
|
||||
*/
|
||||
public static lerp<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, t: number) {
|
||||
const { x: ax, y: ay, z: az } = a;
|
||||
out.x = ax + t * (b.x - ax);
|
||||
out.y = ay + t * (b.y - ay);
|
||||
out.z = az + t * (b.z - az);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a hermite interpolation with two control points.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param c - The third operand.
|
||||
* @param d - The fourth operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out.
|
||||
*/
|
||||
public static hermite<Out extends IVec3Like> (
|
||||
out: Out, a: vec3, b: vec3, c: vec3, d: vec3, t: number) {
|
||||
const factorTimes2 = t * t;
|
||||
const factor1 = factorTimes2 * (2 * t - 3) + 1;
|
||||
const factor2 = factorTimes2 * (t - 2) + t;
|
||||
const factor3 = factorTimes2 * (t - 1);
|
||||
const factor4 = factorTimes2 * (3 - 2 * t);
|
||||
|
||||
out.x = a.x * factor1 + b.x * factor2 + c.x * factor3 + d.x * factor4;
|
||||
out.y = a.y * factor1 + b.y * factor2 + c.y * factor3 + d.y * factor4;
|
||||
out.z = a.z * factor1 + b.z * factor2 + c.z * factor3 + d.z * factor4;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a bezier interpolation with two control points.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param c - The third operand.
|
||||
* @param d - The fourth operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out.
|
||||
*/
|
||||
public static bezier<Out extends IVec3Like> (
|
||||
out: Out, a: vec3, b: vec3, c: vec3, d: vec3, t: number) {
|
||||
const inverseFactor = 1 - t;
|
||||
const inverseFactorTimesTwo = inverseFactor * inverseFactor;
|
||||
const factorTimes2 = t * t;
|
||||
const factor1 = inverseFactorTimesTwo * inverseFactor;
|
||||
const factor2 = 3 * t * inverseFactorTimesTwo;
|
||||
const factor3 = 3 * factorTimes2 * inverseFactor;
|
||||
const factor4 = factorTimes2 * t;
|
||||
|
||||
out.x = a.x * factor1 + b.x * factor2 + c.x * factor3 + d.x * factor4;
|
||||
out.y = a.y * factor1 + b.y * factor2 + c.y * factor3 + d.y * factor4;
|
||||
out.z = a.z * factor1 + b.z * factor2 + c.z * factor3 + d.z * factor4;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random vector uniformly distributed on a sphere centered at the origin.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned.
|
||||
* @return out.
|
||||
*/
|
||||
public static random<Out extends IVec3Like> (out: Out, scale: number) {
|
||||
scale = scale || 1.0;
|
||||
|
||||
const phi = random() * 2.0 * Math.PI;
|
||||
const theta = Math.acos(random() * 2 - 1);
|
||||
|
||||
out.x = Math.sin(theta) * Math.cos(phi) * scale;
|
||||
out.y = Math.sin(theta) * Math.sin(phi) * scale;
|
||||
out.z = Math.cos(theta) * scale;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a point vector with a 4x4 matrix,
|
||||
* i.e. 4th vector component is implicitly '1'.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param {mat4} m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformMat4<Out extends IVec3Like> (out: Out, a: vec3, m: IMat4Like) {
|
||||
const { x, y, z } = a;
|
||||
let rhw = m.m03 * x + m.m07 * y + m.m11 * z + m.m15;
|
||||
rhw = rhw ? 1 / rhw : 1;
|
||||
out.x = (m.m00 * x + m.m04 * y + m.m08 * z + m.m12) * rhw;
|
||||
out.y = (m.m01 * x + m.m05 * y + m.m09 * z + m.m13) * rhw;
|
||||
out.z = (m.m02 * x + m.m06 * y + m.m10 * z + m.m14) * rhw;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a normal vector with a 4x4 matrix,
|
||||
* i.e. 4th vector component is implicitly '0'.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param {mat4} m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformMat4Normal<Out extends IVec3Like> (out: Out, a: vec3, m: IMat4Like) {
|
||||
const { x, y, z } = a;
|
||||
let rhw = m.m03 * x + m.m07 * y + m.m11 * z;
|
||||
rhw = rhw ? 1 / rhw : 1;
|
||||
out.x = (m.m00 * x + m.m04 * y + m.m08 * z) * rhw;
|
||||
out.y = (m.m01 * x + m.m05 * y + m.m09 * z) * rhw;
|
||||
out.z = (m.m02 * x + m.m06 * y + m.m10 * z) * rhw;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 3x3 matrix.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param {mat3} m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformMat3<Out extends IVec3Like> (out: Out, a: vec3, m: IMat3Like) {
|
||||
const { x, y, z } = a;
|
||||
out.x = x * m.m00 + y * m.m03 + z * m.m06;
|
||||
out.y = x * m.m01 + y * m.m04 + z * m.m07;
|
||||
out.z = x * m.m02 + y * m.m05 + z * m.m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a quaternion.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param {quat} q - The quaternion.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformQuat<Out extends IVec3Like> (out: Out, a: vec3, q: IQuatLike) {
|
||||
// benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
|
||||
|
||||
const { x, y, z } = a;
|
||||
const { x: qx, y: qy, z: qz, w: qw } = q;
|
||||
|
||||
// calculate quat * vec
|
||||
const ix = qw * x + qy * z - qz * y;
|
||||
const iy = qw * y + qz * x - qx * z;
|
||||
const iz = qw * z + qx * y - qy * x;
|
||||
const iw = -qx * x - qy * y - qz * z;
|
||||
|
||||
// calculate result * inverse quat
|
||||
out.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
|
||||
out.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
|
||||
out.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a 3D vector around the x-axis.
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The point to rotate.
|
||||
* @param b - The origin of the rotation.
|
||||
* @param c - The angle of rotation.
|
||||
* @return out.
|
||||
*/
|
||||
public static rotateX<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, c: number) {
|
||||
// Translate point to the origin
|
||||
const px = a.x - b.x;
|
||||
const py = a.y - b.y;
|
||||
const pz = a.z - b.z;
|
||||
|
||||
// perform rotation
|
||||
const rx = px;
|
||||
const ry = py * Math.cos(c) - pz * Math.sin(c);
|
||||
const rz = py * Math.sin(c) + pz * Math.cos(c);
|
||||
|
||||
// translate to correct position
|
||||
out.x = rx + b.x;
|
||||
out.y = ry + b.y;
|
||||
out.z = rz + b.z;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a 3D vector around the y-axis.
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The point to rotate.
|
||||
* @param b - The origin of the rotation.
|
||||
* @param c - The angle of rotation.
|
||||
* @return out.
|
||||
*/
|
||||
public static rotateY<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, c: number) {
|
||||
// Translate point to the origin
|
||||
const px = a.x - b.x;
|
||||
const py = a.y - b.y;
|
||||
const pz = a.z - b.z;
|
||||
|
||||
// perform rotation
|
||||
const rx = pz * Math.sin(c) + px * Math.cos(c);
|
||||
const ry = py;
|
||||
const rz = pz * Math.cos(c) - px * Math.sin(c);
|
||||
|
||||
// translate to correct position
|
||||
out.x = rx + b.x;
|
||||
out.y = ry + b.y;
|
||||
out.z = rz + b.z;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates a 3D vector around the z-axis.
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The point to rotate.
|
||||
* @param b - The origin of the rotation.
|
||||
* @param c - The angle of rotation.
|
||||
* @return out.
|
||||
*/
|
||||
public static rotateZ<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, c: number) {
|
||||
// Translate point to the origin
|
||||
const px = a.x - b.x;
|
||||
const py = a.y - b.y;
|
||||
const pz = a.z - b.z;
|
||||
|
||||
// perform rotation
|
||||
const rx = px * Math.cos(c) - py * Math.sin(c);
|
||||
const ry = px * Math.sin(c) + py * Math.cos(c);
|
||||
const rz = pz;
|
||||
|
||||
// translate to correct position
|
||||
out.x = rx + b.x;
|
||||
out.y = ry + b.y;
|
||||
out.z = rz + b.z;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a vector.
|
||||
*
|
||||
* @param a - The vector.
|
||||
* @return - String representation of this vector.
|
||||
*/
|
||||
public static str (a: vec3) {
|
||||
return `vec3(${a.x}, ${a.y}, ${a.z})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a vector into array.
|
||||
*
|
||||
* @param out - Array to store result.
|
||||
* @param v - The vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static array<Out extends IWritableArrayLike<number>> (out: Out, v: vec3) {
|
||||
out[0] = v.x;
|
||||
out[1] = v.y;
|
||||
out[2] = v.z;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are equal. (Compared using ===)
|
||||
*
|
||||
* @param a - The first vector.
|
||||
* @param b - The second vector.
|
||||
* @return True if the vectors are equal, false otherwise.
|
||||
*/
|
||||
public static exactEquals (a: vec3, b: vec3) {
|
||||
return a.x === b.x && a.y === b.y && a.z === b.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are approximately equal.
|
||||
*
|
||||
* @param a The first vector.
|
||||
* @param b The second vector.
|
||||
* @return True if the vectors are approximately equal, false otherwise.
|
||||
*/
|
||||
public static equals (a: vec3, b: vec3) {
|
||||
const { x: a0, y: a1, z: a2 } = a;
|
||||
const { x: b0, y: b1, z: b2 } = b;
|
||||
return (
|
||||
Math.abs(a0 - b0) <=
|
||||
EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <=
|
||||
EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <=
|
||||
EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the angle between two vectors.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return - The angle in radians.
|
||||
*/
|
||||
public static angle (a: vec3, b: vec3) {
|
||||
vec3.normalize(tmpVec3A, a);
|
||||
vec3.normalize(tmpVec3B, b);
|
||||
const cosine = vec3.dot(tmpVec3A, tmpVec3B);
|
||||
if (cosine > 1.0) {
|
||||
return 0;
|
||||
}
|
||||
if (cosine < -1.0) {
|
||||
return Math.PI;
|
||||
}
|
||||
return Math.acos(cosine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects a vector onto a plane represented by its normal.
|
||||
* @param out The result vector.
|
||||
* @param a The vector.
|
||||
* @param n The plane's normal.
|
||||
*/
|
||||
public static projectOnPlane<Out extends IVec3Like> (out: Out, a: vec3, n: vec3) {
|
||||
return vec3.sub(out, a, vec3.project(out, a, n));
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects a vector onto another vector.
|
||||
* @param out The result vector.
|
||||
* @param a The vector to project.
|
||||
* @param b The vector onto which the projection performs.
|
||||
*/
|
||||
public static project<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
|
||||
const sqrLen = vec3.squaredMagnitude(b);
|
||||
if (sqrLen < 0.000001) {
|
||||
return vec3.set(out, 0, 0, 0);
|
||||
} else {
|
||||
return vec3.scale(out, b, vec3.dot(a, b) / sqrLen);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The x component.
|
||||
*/
|
||||
public x: number;
|
||||
|
||||
/**
|
||||
* The y component.
|
||||
*/
|
||||
public y: number;
|
||||
|
||||
/**
|
||||
* The z component.
|
||||
*/
|
||||
public z: number;
|
||||
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param x - Value assigned to x component.
|
||||
* @param y - Value assigned to y component.
|
||||
* @param z - Value assigned to z component.
|
||||
*/
|
||||
constructor (x = 0, y = 0, z = 0) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
}
|
||||
|
||||
const tmpVec3A = vec3.create();
|
||||
const tmpVec3B = vec3.create();
|
||||
@@ -1,715 +0,0 @@
|
||||
import { EPSILON, random } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 4-dimensional vector.
|
||||
*
|
||||
* x, y, z, w is alias of the first, second, third, fourth component of vector, respectively.
|
||||
*/
|
||||
class vec4 {
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param {number} x - Value assigned to x component.
|
||||
* @param {number} y - Value assigned to y component.
|
||||
* @param {number} z - Value assigned to z component.
|
||||
* @param {number} w - Value assigned to w component.
|
||||
*/
|
||||
constructor(x = 0, y = 0, z = 0, w = 1) {
|
||||
/**
|
||||
* The x component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.x = x;
|
||||
|
||||
/**
|
||||
* The y component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.y = y;
|
||||
|
||||
/**
|
||||
* The z component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.z = z;
|
||||
|
||||
/**
|
||||
* The w component.
|
||||
* @type {number}
|
||||
* */
|
||||
this.w = w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a vector, with components specified separately.
|
||||
*
|
||||
* @param {number} x - Value assigned to x component.
|
||||
* @param {number} y - Value assigned to y component.
|
||||
* @param {number} z - Value assigned to z component.
|
||||
* @param {number} w - Value assigned to w component.
|
||||
* @return {vec4} The newly created vector.
|
||||
*/
|
||||
static create(x = 0, y = 0, z = 0, w = 1) {
|
||||
return new vec4(x, y, z, w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a zero vector.
|
||||
*
|
||||
* @return {vec4} The newly created vector.
|
||||
*/
|
||||
static zero(out) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
out.z = 0;
|
||||
out.w = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a vector.
|
||||
*
|
||||
* @param {vec4} a - Vector to clone.
|
||||
* @returns {vec4} The newly created vector.
|
||||
*/
|
||||
static clone(a) {
|
||||
return new vec4(a.x, a.y, a.z, a.w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a vector into another.
|
||||
*
|
||||
* @param {vec4} out - The vector to modified.
|
||||
* @param {vec4} a - The specified vector.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static copy(out, a) {
|
||||
out.x = a.x;
|
||||
out.y = a.y;
|
||||
out.z = a.z;
|
||||
out.w = a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the components of a vector to the given values.
|
||||
*
|
||||
* @param {vec4} out - The vector to modified.
|
||||
* @param {Number} x - Value set to x component.
|
||||
* @param {Number} y - Value set to y component.
|
||||
* @param {Number} z - Value set to z component.
|
||||
* @param {Number} w - Value set to w component.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static set(out, x, y, z, w) {
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
out.z = z;
|
||||
out.w = w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static add(out, a, b) {
|
||||
out.x = a.x + b.x;
|
||||
out.y = a.y + b.y;
|
||||
out.z = a.z + b.z;
|
||||
out.w = a.w + b.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static subtract(out, a, b) {
|
||||
out.x = a.x - b.x;
|
||||
out.y = a.y - b.y;
|
||||
out.z = a.z - b.z;
|
||||
out.w = a.w - b.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.subtract}.
|
||||
*/
|
||||
static sub(out, a, b) {
|
||||
return vec4.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs multiply on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static multiply(out, a, b) {
|
||||
out.x = a.x * b.x;
|
||||
out.y = a.y * b.y;
|
||||
out.z = a.z * b.z;
|
||||
out.w = a.w * b.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.multiply}.
|
||||
*/
|
||||
static mul(out, a, b) {
|
||||
return vec4.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs division on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static divide(out, a, b) {
|
||||
out.x = a.x / b.x;
|
||||
out.y = a.y / b.y;
|
||||
out.z = a.z / b.z;
|
||||
out.w = a.w / b.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.divide}.
|
||||
*/
|
||||
static div(out, a, b) {
|
||||
return vec4.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.ceil on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to perform operation.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static ceil(out, a) {
|
||||
out.x = Math.ceil(a.x);
|
||||
out.y = Math.ceil(a.y);
|
||||
out.z = Math.ceil(a.z);
|
||||
out.w = Math.ceil(a.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.floor on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to perform operation.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static floor(out, a) {
|
||||
out.x = Math.floor(a.x);
|
||||
out.y = Math.floor(a.y);
|
||||
out.z = Math.floor(a.z);
|
||||
out.w = Math.floor(a.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static min(out, a, b) {
|
||||
out.x = Math.min(a.x, b.x);
|
||||
out.y = Math.min(a.y, b.y);
|
||||
out.z = Math.min(a.z, b.z);
|
||||
out.w = Math.min(a.w, b.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static max(out, a, b) {
|
||||
out.x = Math.max(a.x, b.x);
|
||||
out.y = Math.max(a.y, b.y);
|
||||
out.z = Math.max(a.z, b.z);
|
||||
out.w = Math.max(a.w, b.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.round on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to perform operation.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static round(out, a) {
|
||||
out.x = Math.round(a.x);
|
||||
out.y = Math.round(a.y);
|
||||
out.z = Math.round(a.z);
|
||||
out.w = Math.round(a.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a vector with a number.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to scale.
|
||||
* @param {number} b - The scale number.
|
||||
* @returns {vec4} out.
|
||||
* */
|
||||
static scale(out, a, b) {
|
||||
out.x = a.x * b;
|
||||
out.y = a.y * b;
|
||||
out.z = a.z * b;
|
||||
out.w = a.w * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors after scaling the second operand by a number.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @param {number} scale - The scale number before adding.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static scaleAndAdd(out, a, b, scale) {
|
||||
out.x = a.x + (b.x * scale);
|
||||
out.y = a.y + (b.y * scale);
|
||||
out.z = a.z + (b.z * scale);
|
||||
out.w = a.w + (b.w * scale);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the euclidian distance between two vectors.
|
||||
*
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @returns {number} Distance between a and b.
|
||||
*/
|
||||
static distance(a, b) {
|
||||
let x = b.x - a.x,
|
||||
y = b.y - a.y,
|
||||
z = b.z - a.z,
|
||||
w = b.w - a.w;
|
||||
return Math.sqrt(x * x + y * y + z * z + w * w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.distance}.
|
||||
*/
|
||||
static dist(a, b) {
|
||||
return vec4.distance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared euclidian distance between two vectors.
|
||||
*
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @returns {number} Squared distance between a and b.
|
||||
*/
|
||||
static squaredDistance(a, b) {
|
||||
let x = b.x - a.x,
|
||||
y = b.y - a.y,
|
||||
z = b.z - a.z,
|
||||
w = b.w - a.w;
|
||||
return x * x + y * y + z * z + w * w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.squaredDistance}.
|
||||
*/
|
||||
static sqrDist(a, b) {
|
||||
return vec4.squaredDistance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the length of a vector.
|
||||
*
|
||||
* @param {vec4} a - The vector.
|
||||
* @returns {Number} Length of the vector.
|
||||
*/
|
||||
static magnitude(a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z,
|
||||
w = a.w;
|
||||
return Math.sqrt(x * x + y * y + z * z + w * w);
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec4.magnitude}.
|
||||
*/
|
||||
static mag(a) {
|
||||
return vec4.magnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared length of a vector.
|
||||
*
|
||||
* @param {vec4} a - The vector.
|
||||
* @returns {Number} Squared length of the vector.
|
||||
*/
|
||||
static squaredMagnitude(a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z,
|
||||
w = a.w;
|
||||
return x * x + y * y + z * z + w * w;
|
||||
}
|
||||
|
||||
/**
|
||||
*Alias of {@link vec4.squaredMagnitude}
|
||||
*/
|
||||
static sqrMag(a) {
|
||||
return vec4.squaredMagnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negates each component of a vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to negate.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static negate(out, a) {
|
||||
out.x = -a.x;
|
||||
out.y = -a.y;
|
||||
out.z = -a.z;
|
||||
out.w = -a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts the components of a vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to invert.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static inverse(out, a) {
|
||||
out.x = 1.0 / a.x;
|
||||
out.y = 1.0 / a.y;
|
||||
out.z = 1.0 / a.z;
|
||||
out.w = 1.0 / a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely inverts the components of a vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to invert.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static inverseSafe(out, a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z,
|
||||
w = a.w;
|
||||
|
||||
if (Math.abs(x) < EPSILON) {
|
||||
out.x = 0;
|
||||
} else {
|
||||
out.x = 1.0 / x;
|
||||
}
|
||||
|
||||
if (Math.abs(y) < EPSILON) {
|
||||
out.y = 0;
|
||||
} else {
|
||||
out.y = 1.0 / y;
|
||||
}
|
||||
|
||||
if (Math.abs(z) < EPSILON) {
|
||||
out.z = 0;
|
||||
} else {
|
||||
out.z = 1.0 / z;
|
||||
}
|
||||
|
||||
if (Math.abs(w) < EPSILON) {
|
||||
out.w = 0;
|
||||
} else {
|
||||
out.w = 1.0 / w;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a vector.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to normalize.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static normalize(out, a) {
|
||||
let x = a.x,
|
||||
y = a.y,
|
||||
z = a.z,
|
||||
w = a.w;
|
||||
let len = x * x + y * y + z * z + w * w;
|
||||
if (len > 0) {
|
||||
len = 1 / Math.sqrt(len);
|
||||
out.x = x * len;
|
||||
out.y = y * len;
|
||||
out.z = z * len;
|
||||
out.w = w * len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of two vectors.
|
||||
*
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @returns {Number} Dot product of a and b.
|
||||
*/
|
||||
static dot(a, b) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two vectors.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - The first operand.
|
||||
* @param {vec4} b - The second operand.
|
||||
* @param {Number} t - The interpolation coefficient.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static lerp(out, a, b, t) {
|
||||
let ax = a.x,
|
||||
ay = a.y,
|
||||
az = a.z,
|
||||
aw = a.w;
|
||||
out.x = ax + t * (b.x - ax);
|
||||
out.y = ay + t * (b.y - ay);
|
||||
out.z = az + t * (b.z - az);
|
||||
out.w = aw + t * (b.w - aw);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random vector uniformly distributed on a sphere centered at the origin.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {Number} [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static random(out, scale) {
|
||||
scale = scale || 1.0;
|
||||
|
||||
let phi = random() * 2.0 * Math.PI;
|
||||
let theta = Math.acos(random() * 2 - 1);
|
||||
|
||||
out.x = Math.sin(theta) * Math.cos(phi) * scale;
|
||||
out.y = Math.sin(theta) * Math.sin(phi) * scale;
|
||||
out.z = Math.cos(theta) * scale;
|
||||
out.w = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 4x4 matrix.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to transform.
|
||||
* @param {mat4} m - The matrix.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static transformMat4(out, a, m) {
|
||||
let x = a.x, y = a.y, z = a.z, w = a.w;
|
||||
out.x = m.m00 * x + m.m04 * y + m.m08 * z + m.m12 * w;
|
||||
out.y = m.m01 * x + m.m05 * y + m.m09 * z + m.m13 * w;
|
||||
out.z = m.m02 * x + m.m06 * y + m.m10 * z + m.m14 * w;
|
||||
out.w = m.m03 * x + m.m07 * y + m.m11 * z + m.m15 * w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a quaternion.
|
||||
*
|
||||
* @param {vec4} out - Vector to store result.
|
||||
* @param {vec4} a - Vector to transform.
|
||||
* @param {quat} q - The quaternion.
|
||||
* @returns {vec4} out.
|
||||
*/
|
||||
static transformQuat(out, a, q) {
|
||||
let x = a.x, y = a.y, z = a.z;
|
||||
let qx = q.x, qy = q.y, qz = q.z, qw = q.w;
|
||||
|
||||
// calculate quat * vec
|
||||
let ix = qw * x + qy * z - qz * y;
|
||||
let iy = qw * y + qz * x - qx * z;
|
||||
let iz = qw * z + qx * y - qy * x;
|
||||
let iw = -qx * x - qy * y - qz * z;
|
||||
|
||||
// calculate result * inverse quat
|
||||
out.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
|
||||
out.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
|
||||
out.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
|
||||
out.w = a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a vector.
|
||||
*
|
||||
* @param {vec4} a - The vector.
|
||||
* @returns {String} - String representation of this vector.
|
||||
*/
|
||||
static str(a) {
|
||||
return `vec4(${a.x}, ${a.y}, ${a.z}, ${a.w})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a vector into array.
|
||||
*
|
||||
* @param {Array} out - Array to store result.
|
||||
* @param {vec4} v - The vector.
|
||||
* @returns {Array} out.
|
||||
*/
|
||||
static array(out, v) {
|
||||
out[0] = v.x;
|
||||
out[1] = v.y;
|
||||
out[2] = v.z;
|
||||
out[3] = v.w;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are equal. (Compared using ===)
|
||||
*
|
||||
* @param {vec4} a - The first vector.
|
||||
* @param {vec4} b - The second vector.
|
||||
* @returns {Boolean} True if the vectors are equal, false otherwise.
|
||||
*/
|
||||
static exactEquals(a, b) {
|
||||
return a.x === b.x && a.y === b.y && a.z === b.z && a.w === b.w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are approximately equal.
|
||||
*
|
||||
* @param {vec4} a The first vector.
|
||||
* @param {vec4} b The second vector.
|
||||
* @returns {Boolean} True if the vectors are approximately equal, false otherwise.
|
||||
*/
|
||||
static equals(a, b) {
|
||||
let a0 = a.x, a1 = a.y, a2 = a.z, a3 = a.w;
|
||||
let b0 = b.x, b1 = b.y, b2 = b.z, b3 = b.w;
|
||||
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs some operation over an array of vec4s.
|
||||
*
|
||||
* @param {Array} a the array of vectors to iterate over.
|
||||
* @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed.
|
||||
* @param {Number} offset Number of elements to skip at the beginning of the array.
|
||||
* @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array.
|
||||
* @param {Function} fn Function to call for each vector in the array.
|
||||
* @param {Object} [arg] additional argument to pass to fn.
|
||||
* @returns {Array} a.
|
||||
*/
|
||||
static forEach(a, stride, offset, count, fn, arg) {
|
||||
return vec4._forEach(a, stride, offset, count, fn, arg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform some operation over an array of vec4s.
|
||||
*
|
||||
* @param {Array} a the array of vectors to iterate over.
|
||||
* @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed.
|
||||
* @param {Number} offset Number of elements to skip at the beginning of the array.
|
||||
* @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array.
|
||||
* @param {Function} fn Function to call for each vector in the array.
|
||||
* @param {Object} [arg] additional argument to pass to fn.
|
||||
* @returns {Array} a.
|
||||
* @ignore
|
||||
*/
|
||||
vec4._forEach = (function () {
|
||||
let vec = vec4.create(0, 0, 0, 0);
|
||||
|
||||
return function (a, stride, offset, count, fn, arg) {
|
||||
let i, l;
|
||||
if (!stride) {
|
||||
stride = 4;
|
||||
}
|
||||
|
||||
if (!offset) {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
if (count) {
|
||||
l = Math.min((count * stride) + offset, a.length);
|
||||
} else {
|
||||
l = a.length;
|
||||
}
|
||||
|
||||
for (i = offset; i < l; i += stride) {
|
||||
vec.x = a[i];
|
||||
vec.y = a[i + 1];
|
||||
vec.z = a[i + 2];
|
||||
vec.w = a[i + 3];
|
||||
fn(vec, vec, arg);
|
||||
a[i] = vec.x;
|
||||
a[i + 1] = vec.y;
|
||||
a[i + 2] = vec.z;
|
||||
a[i + 3] = vec.w;
|
||||
}
|
||||
|
||||
return a;
|
||||
};
|
||||
})();
|
||||
|
||||
export default vec4;
|
||||
@@ -0,0 +1,640 @@
|
||||
/// <reference path="./fwd-decls" />
|
||||
|
||||
import { EPSILON, random } from './utils';
|
||||
|
||||
/**
|
||||
* Mathematical 4-dimensional vector.
|
||||
*
|
||||
* x, y, z, w is alias of the first, second, third, fourth component of vector, respectively.
|
||||
*/
|
||||
export default class vec4 {
|
||||
|
||||
/**
|
||||
* Create a vector, with components specified separately.
|
||||
*
|
||||
* @param x - Value assigned to x component.
|
||||
* @param y - Value assigned to y component.
|
||||
* @param z - Value assigned to z component.
|
||||
* @param w - Value assigned to w component.
|
||||
* @return The newly created vector.
|
||||
*/
|
||||
public static create (x = 0, y = 0, z = 0, w = 1) {
|
||||
return new vec4(x, y, z, w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a zero vector.
|
||||
*
|
||||
* @return The newly created vector.
|
||||
*/
|
||||
public static zero (out: vec4) {
|
||||
out.x = 0;
|
||||
out.y = 0;
|
||||
out.z = 0;
|
||||
out.w = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a vector.
|
||||
*
|
||||
* @param a - Vector to clone.
|
||||
* @return The newly created vector.
|
||||
*/
|
||||
public static clone (a: vec4) {
|
||||
return new vec4(a.x, a.y, a.z, a.w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content of a vector into another.
|
||||
*
|
||||
* @param out - The vector to modified.
|
||||
* @param a - The specified vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static copy<Out extends IVec4Like> (out: Out, a: vec4) {
|
||||
out.x = a.x;
|
||||
out.y = a.y;
|
||||
out.z = a.z;
|
||||
out.w = a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the components of a vector to the given values.
|
||||
*
|
||||
* @param out - The vector to modified.
|
||||
* @param x - Value set to x component.
|
||||
* @param y - Value set to y component.
|
||||
* @param z - Value set to z component.
|
||||
* @param w - Value set to w component.
|
||||
* @return out.
|
||||
*/
|
||||
public static set<Out extends IVec4Like> (out: Out, x: number, y: number, z: number, w: number) {
|
||||
out.x = x;
|
||||
out.y = y;
|
||||
out.z = z;
|
||||
out.w = w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static add<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
|
||||
out.x = a.x + b.x;
|
||||
out.y = a.y + b.y;
|
||||
out.z = a.z + b.z;
|
||||
out.w = a.w + b.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two vectors.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static subtract<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
|
||||
out.x = a.x - b.x;
|
||||
out.y = a.y - b.y;
|
||||
out.z = a.z - b.z;
|
||||
out.w = a.w - b.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.subtract}.
|
||||
*/
|
||||
public static sub<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
|
||||
return vec4.subtract(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs multiply on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static multiply<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
|
||||
out.x = a.x * b.x;
|
||||
out.y = a.y * b.y;
|
||||
out.z = a.z * b.z;
|
||||
out.w = a.w * b.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.multiply}.
|
||||
*/
|
||||
public static mul<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
|
||||
return vec4.multiply(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs division on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static divide<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
|
||||
out.x = a.x / b.x;
|
||||
out.y = a.y / b.y;
|
||||
out.z = a.z / b.z;
|
||||
out.w = a.w / b.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.divide}.
|
||||
*/
|
||||
public static div<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
|
||||
return vec4.divide(out, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.ceil on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to perform operation.
|
||||
* @return out.
|
||||
*/
|
||||
public static ceil<Out extends IVec4Like> (out: Out, a: vec4) {
|
||||
out.x = Math.ceil(a.x);
|
||||
out.y = Math.ceil(a.y);
|
||||
out.z = Math.ceil(a.z);
|
||||
out.w = Math.ceil(a.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.floor on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to perform operation.
|
||||
* @return out.
|
||||
*/
|
||||
public static floor<Out extends IVec4Like> (out: Out, a: vec4) {
|
||||
out.x = Math.floor(a.x);
|
||||
out.y = Math.floor(a.y);
|
||||
out.z = Math.floor(a.z);
|
||||
out.w = Math.floor(a.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static min<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
|
||||
out.x = Math.min(a.x, b.x);
|
||||
out.y = Math.min(a.y, b.y);
|
||||
out.z = Math.min(a.z, b.z);
|
||||
out.w = Math.min(a.w, b.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.min on each component of two vectors respectively.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return out.
|
||||
*/
|
||||
public static max<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
|
||||
out.x = Math.max(a.x, b.x);
|
||||
out.y = Math.max(a.y, b.y);
|
||||
out.z = Math.max(a.z, b.z);
|
||||
out.w = Math.max(a.w, b.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs Math.round on each component of a vector.
|
||||
*
|
||||
* It doesn't matter that any amount of these parameters refer to same vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to perform operation.
|
||||
* @return out.
|
||||
*/
|
||||
public static round<Out extends IVec4Like> (out: Out, a: vec4) {
|
||||
out.x = Math.round(a.x);
|
||||
out.y = Math.round(a.y);
|
||||
out.z = Math.round(a.z);
|
||||
out.w = Math.round(a.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a vector with a number.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to scale.
|
||||
* @param b - The scale number.
|
||||
* @return out.
|
||||
*/
|
||||
public static scale<Out extends IVec4Like> (out: Out, a: vec4, b: number) {
|
||||
out.x = a.x * b;
|
||||
out.y = a.y * b;
|
||||
out.z = a.z * b;
|
||||
out.w = a.w * b;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two vectors after scaling the second operand by a number.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param scale - The scale number before adding.
|
||||
* @return out.
|
||||
*/
|
||||
public static scaleAndAdd<Out extends IVec4Like> (out: Out, a: vec4, b: vec4, scale: number) {
|
||||
out.x = a.x + (b.x * scale);
|
||||
out.y = a.y + (b.y * scale);
|
||||
out.z = a.z + (b.z * scale);
|
||||
out.w = a.w + (b.w * scale);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the euclidian distance between two vectors.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return Distance between a and b.
|
||||
*/
|
||||
public static distance (a: vec4, b: vec4) {
|
||||
const x = b.x - a.x;
|
||||
const y = b.y - a.y;
|
||||
const z = b.z - a.z;
|
||||
const w = b.w - a.w;
|
||||
return Math.sqrt(x * x + y * y + z * z + w * w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.distance}.
|
||||
*/
|
||||
public static dist (a: vec4, b: vec4) {
|
||||
return vec4.distance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared euclidian distance between two vectors.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return Squared distance between a and b.
|
||||
*/
|
||||
public static squaredDistance (a: vec4, b: vec4) {
|
||||
const x = b.x - a.x;
|
||||
const y = b.y - a.y;
|
||||
const z = b.z - a.z;
|
||||
const w = b.w - a.w;
|
||||
return x * x + y * y + z * z + w * w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.squaredDistance}.
|
||||
*/
|
||||
public static sqrDist (a: vec4, b: vec4) {
|
||||
return vec4.squaredDistance(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the length of a vector.
|
||||
*
|
||||
* @param a - The vector.
|
||||
* @return Length of the vector.
|
||||
*/
|
||||
public static magnitude (a: vec4) {
|
||||
const { x, y, z, w } = a;
|
||||
return Math.sqrt(x * x + y * y + z * z + w * w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.magnitude}.
|
||||
*/
|
||||
public static mag (a: vec4) {
|
||||
return vec4.magnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the squared length of a vector.
|
||||
*
|
||||
* @param a - The vector.
|
||||
* @return Squared length of the vector.
|
||||
*/
|
||||
public static squaredMagnitude (a: vec4) {
|
||||
const { x, y, z, w } = a;
|
||||
return x * x + y * y + z * z + w * w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of {@link vec4.squaredMagnitude}
|
||||
*/
|
||||
public static sqrMag (a: vec4) {
|
||||
return vec4.squaredMagnitude(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negates each component of a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to negate.
|
||||
* @return out.
|
||||
*/
|
||||
public static negate<Out extends IVec4Like> (out: Out, a: vec4) {
|
||||
out.x = -a.x;
|
||||
out.y = -a.y;
|
||||
out.z = -a.z;
|
||||
out.w = -a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts the components of a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to invert.
|
||||
* @return out.
|
||||
*/
|
||||
public static inverse<Out extends IVec4Like> (out: Out, a: vec4) {
|
||||
out.x = 1.0 / a.x;
|
||||
out.y = 1.0 / a.y;
|
||||
out.z = 1.0 / a.z;
|
||||
out.w = 1.0 / a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely inverts the components of a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to invert.
|
||||
* @return out.
|
||||
*/
|
||||
public static inverseSafe<Out extends IVec4Like> (out: Out, a: vec4) {
|
||||
const { x, y, z, w } = a;
|
||||
|
||||
if (Math.abs(x) < EPSILON) {
|
||||
out.x = 0;
|
||||
} else {
|
||||
out.x = 1.0 / x;
|
||||
}
|
||||
|
||||
if (Math.abs(y) < EPSILON) {
|
||||
out.y = 0;
|
||||
} else {
|
||||
out.y = 1.0 / y;
|
||||
}
|
||||
|
||||
if (Math.abs(z) < EPSILON) {
|
||||
out.z = 0;
|
||||
} else {
|
||||
out.z = 1.0 / z;
|
||||
}
|
||||
|
||||
if (Math.abs(w) < EPSILON) {
|
||||
out.w = 0;
|
||||
} else {
|
||||
out.w = 1.0 / w;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a vector.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to normalize.
|
||||
* @return out.
|
||||
*/
|
||||
public static normalize<Out extends IVec4Like> (out: Out, a: vec4) {
|
||||
const { x, y, z, w } = a;
|
||||
let len = x * x + y * y + z * z + w * w;
|
||||
if (len > 0) {
|
||||
len = 1 / Math.sqrt(len);
|
||||
out.x = x * len;
|
||||
out.y = y * len;
|
||||
out.z = z * len;
|
||||
out.w = w * len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of two vectors.
|
||||
*
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @return Dot product of a and b.
|
||||
*/
|
||||
public static dot (a: vec4, b: vec4) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a linear interpolation between two vectors.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - The first operand.
|
||||
* @param b - The second operand.
|
||||
* @param t - The interpolation coefficient.
|
||||
* @return out.
|
||||
*/
|
||||
public static lerp<Out extends IVec4Like> (out: Out, a: vec4, b: vec4, t: number) {
|
||||
const { x: ax, y: ay, z: az, w: aw } = a;
|
||||
out.x = ax + t * (b.x - ax);
|
||||
out.y = ay + t * (b.y - ay);
|
||||
out.z = az + t * (b.z - az);
|
||||
out.w = aw + t * (b.w - aw);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random vector uniformly distributed on a sphere centered at the origin.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned.
|
||||
* @return out.
|
||||
*/
|
||||
public static random<Out extends IVec4Like> (out: Out, scale: number) {
|
||||
scale = scale || 1.0;
|
||||
|
||||
const phi = random() * 2.0 * Math.PI;
|
||||
const theta = Math.acos(random() * 2 - 1);
|
||||
|
||||
out.x = Math.sin(theta) * Math.cos(phi) * scale;
|
||||
out.y = Math.sin(theta) * Math.sin(phi) * scale;
|
||||
out.z = Math.cos(theta) * scale;
|
||||
out.w = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a 4x4 matrix.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param m - The matrix.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformMat4<Out extends IVec4Like> (out: Out, a: vec4, m: IMat4Like) {
|
||||
const { x, y, z, w } = a;
|
||||
out.x = m.m00 * x + m.m04 * y + m.m08 * z + m.m12 * w;
|
||||
out.y = m.m01 * x + m.m05 * y + m.m09 * z + m.m13 * w;
|
||||
out.z = m.m02 * x + m.m06 * y + m.m10 * z + m.m14 * w;
|
||||
out.w = m.m03 * x + m.m07 * y + m.m11 * z + m.m15 * w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a vector with a quaternion.
|
||||
*
|
||||
* @param out - Vector to store result.
|
||||
* @param a - Vector to transform.
|
||||
* @param q - The quaternion.
|
||||
* @return out.
|
||||
*/
|
||||
public static transformQuat<Out extends IVec4Like> (out: Out, a: vec4, q: IQuatLike) {
|
||||
const { x, y, z } = a;
|
||||
const { x: qx, y: qy, z: qz, w: qw } = q;
|
||||
|
||||
// calculate quat * vec
|
||||
const ix = qw * x + qy * z - qz * y;
|
||||
const iy = qw * y + qz * x - qx * z;
|
||||
const iz = qw * z + qx * y - qy * x;
|
||||
const iw = -qx * x - qy * y - qz * z;
|
||||
|
||||
// calculate result * inverse quat
|
||||
out.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
|
||||
out.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
|
||||
out.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
|
||||
out.w = a.w;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation of a vector.
|
||||
*
|
||||
* @param a - The vector.
|
||||
* @return - String representation of this vector.
|
||||
*/
|
||||
public static str (a: vec4) {
|
||||
return `vec4(${a.x}, ${a.y}, ${a.z}, ${a.w})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store components of a vector into array.
|
||||
*
|
||||
* @param out - Array to store result.
|
||||
* @param v - The vector.
|
||||
* @return out.
|
||||
*/
|
||||
public static array<Out extends IWritableArrayLike<number>> (out: Out, v: vec4) {
|
||||
out[0] = v.x;
|
||||
out[1] = v.y;
|
||||
out[2] = v.z;
|
||||
out[3] = v.w;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are equal. (Compared using ===)
|
||||
*
|
||||
* @param a - The first vector.
|
||||
* @param b - The second vector.
|
||||
* @return True if the vectors are equal, false otherwise.
|
||||
*/
|
||||
public static exactEquals (a: vec4, b: vec4) {
|
||||
return a.x === b.x && a.y === b.y && a.z === b.z && a.w === b.w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified vectors are approximately equal.
|
||||
*
|
||||
* @param a The first vector.
|
||||
* @param b The second vector.
|
||||
* @return True if the vectors are approximately equal, false otherwise.
|
||||
*/
|
||||
public static equals (a: vec4, b: vec4) {
|
||||
const { x: a0, y: a1, z: a2, w: a3 } = a;
|
||||
const { x: b0, y: b1, z: b2, w: b3 } = a;
|
||||
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
||||
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
||||
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
||||
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)));
|
||||
}
|
||||
|
||||
/**
|
||||
* The x component.
|
||||
*/
|
||||
public x: number;
|
||||
|
||||
/**
|
||||
* The y component.
|
||||
*/
|
||||
public y: number;
|
||||
|
||||
/**
|
||||
* The z component.
|
||||
*/
|
||||
public z: number;
|
||||
|
||||
/**
|
||||
* The w component.
|
||||
*/
|
||||
public w: number;
|
||||
|
||||
/**
|
||||
* Creates a vector, with components specified separately.
|
||||
*
|
||||
* @param x - Value assigned to x component.
|
||||
* @param y - Value assigned to y component.
|
||||
* @param z - Value assigned to z component.
|
||||
* @param w - Value assigned to w component.
|
||||
*/
|
||||
constructor (x = 0, y = 0, z = 0, w = 1) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,7 @@ export function _loadNewPack (uuid, packUuid, callback) {
|
||||
cc.errorID(4916, uuid);
|
||||
return callback(err);
|
||||
}
|
||||
var res = self._doLoadNewPack(uuid, packUuid, packJson);
|
||||
var res = _doLoadNewPack(uuid, packUuid, packJson);
|
||||
if (res) {
|
||||
callback(null, res);
|
||||
}
|
||||
@@ -158,7 +158,7 @@ export function load (item, callback) {
|
||||
}
|
||||
|
||||
if (Array.isArray(packUuid)) {
|
||||
packUuid = this._selectLoadedPack(packUuid);
|
||||
packUuid = _selectLoadedPack(packUuid);
|
||||
}
|
||||
|
||||
var unpackerData = globalUnpackers[packUuid];
|
||||
@@ -180,7 +180,7 @@ export function load (item, callback) {
|
||||
unpackerData = globalUnpackers[packUuid] = new UnpackerData();
|
||||
unpackerData.state = PackState.Downloading;
|
||||
}
|
||||
this._loadNewPack(uuid, packUuid, callback);
|
||||
_loadNewPack(uuid, packUuid, callback);
|
||||
}
|
||||
// Return null to let caller know it's loading asynchronously
|
||||
return null;
|
||||
|
||||
@@ -52,5 +52,5 @@ let renderer = {
|
||||
};
|
||||
Object.assign(renderer, enums);
|
||||
|
||||
export default renderer;
|
||||
export { renderer };
|
||||
cc.renderer = renderer;
|
||||
|
||||
@@ -109,15 +109,15 @@ export class Camera {
|
||||
return this._farClip;
|
||||
}
|
||||
|
||||
public get matView(): number {
|
||||
public get matView(): mat4 {
|
||||
return this._matView;
|
||||
}
|
||||
|
||||
public get matProj(): number {
|
||||
public get matProj(): mat4 {
|
||||
return this._matProj;
|
||||
}
|
||||
|
||||
public get matViewProj(): number {
|
||||
public get matViewProj(): mat4 {
|
||||
return this._matViewProj;
|
||||
}
|
||||
|
||||
|
||||
@@ -194,8 +194,8 @@ class Node extends BaseNode {
|
||||
vec3.set(this._lpos, val, y, z);
|
||||
}
|
||||
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,8 +226,8 @@ class Node extends BaseNode {
|
||||
}
|
||||
this._eulerDirty = true;
|
||||
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,8 +241,8 @@ class Node extends BaseNode {
|
||||
this._eulerDirty = false;
|
||||
quat.fromEuler(this._lrot, x, y, z);
|
||||
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -271,8 +271,8 @@ class Node extends BaseNode {
|
||||
vec3.set(this._lscale, val, y, z);
|
||||
}
|
||||
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -310,8 +310,8 @@ class Node extends BaseNode {
|
||||
vec3.copy(this._lpos, this._pos);
|
||||
}
|
||||
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -349,8 +349,8 @@ class Node extends BaseNode {
|
||||
}
|
||||
this._eulerDirty = true;
|
||||
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -370,8 +370,8 @@ class Node extends BaseNode {
|
||||
quat.copy(this._lrot, this._rot);
|
||||
}
|
||||
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -407,8 +407,8 @@ class Node extends BaseNode {
|
||||
vec3.copy(this._lscale, this._scale);
|
||||
}
|
||||
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -29,15 +29,15 @@
|
||||
import cc from './predefine';
|
||||
|
||||
// LOAD ENGINE CORE
|
||||
import './cocos/core';
|
||||
import './cocos/scene-graph';
|
||||
import './cocos/renderer';
|
||||
import './cocos/gfx';
|
||||
export * from './cocos/core';
|
||||
export * from './cocos/scene-graph';
|
||||
export * from './cocos/renderer';
|
||||
|
||||
import './cocos/assets';
|
||||
import './cocos/load-pipeline';
|
||||
import './cocos/components';
|
||||
import './cocos/animation';
|
||||
import './cocos/3d';
|
||||
export * from './cocos/assets';
|
||||
export * from './cocos/load-pipeline';
|
||||
export * from './cocos/components';
|
||||
export * from './cocos/animation';
|
||||
export * from './cocos/3d';
|
||||
export * from './cocos/gfx';
|
||||
|
||||
export default cc;
|
||||
// export default cc;
|
||||
|
||||
@@ -63,6 +63,8 @@
|
||||
"rollup-plugin-json": "^3.1.0",
|
||||
"rollup-plugin-node-resolve": "^3.4.0",
|
||||
"rollup-watch": "^4.3.1",
|
||||
"tslint": "^5.12.0",
|
||||
"typescript": "^3.2.2",
|
||||
"uglify-js": "^3.4.9",
|
||||
"vconsole": "^3.2.0",
|
||||
"vinyl": "1.1.0",
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"defaultSeverity": "error",
|
||||
"extends": [
|
||||
"tslint:recommended"
|
||||
],
|
||||
"jsRules": {},
|
||||
"rules": {
|
||||
"quotemark": [ true, "single" ],
|
||||
"no-console": false,
|
||||
"space-before-function-paren": true,
|
||||
"max-classes-per-file": false
|
||||
},
|
||||
"rulesDirectory": []
|
||||
}
|
||||
Reference in New Issue
Block a user