update joints after culling (#4641)

* fix vmath return type

* more docs

* update joints after culling
This commit is contained in:
Yun Hsiao Wu
2019-06-17 17:33:01 +08:00
committed by Weiliang Wang
parent 373ac1ca09
commit dc45d5e0ce
21 changed files with 468 additions and 469 deletions
+4 -4
View File
@@ -64,12 +64,12 @@ export class AudioClip extends Asset {
this._audio = clip;
if (clip) {
let ctor: any;
if (clip instanceof AudioBuffer) {
ctor = AudioPlayerWeb;
this._loadMode = AudioType.WEB_AUDIO;
} else if (CC_WECHATGAME) {
if (CC_WECHATGAME) {
ctor = AudioPlayerWX;
this._loadMode = AudioType.WX_GAME_AUDIO;
} else if (clip instanceof AudioBuffer) {
ctor = AudioPlayerWeb;
this._loadMode = AudioType.WEB_AUDIO;
} else {
ctor = AudioPlayerDOM;
this._loadMode = AudioType.DOM_AUDIO;
+1 -5
View File
@@ -26,6 +26,7 @@
import { Asset } from '../../assets/asset';
import { ccclass, property } from '../../core/data/class-decorator';
import { Vec3 } from '../../core/value-types';
import { vec3 } from '../../core/vmath';
import { GFXBuffer } from '../../gfx/buffer';
import { GFXAttributeName, GFXBufferUsageBit, GFXFormat, GFXFormatInfos, GFXFormatType, GFXMemoryUsageBit, GFXPrimitiveMode } from '../../gfx/define';
import { GFXDevice } from '../../gfx/device';
@@ -33,7 +34,6 @@ import { IGFXAttribute } from '../../gfx/input-assembler';
import { BufferBlob } from '../misc/buffer-blob';
import { IBufferView } from './utils/buffer-view';
import { postLoadMesh } from './utils/mesh-utils';
import { vec3 } from '../../core/vmath';
function getIndexStrideCtor (stride: number) {
switch (stride) {
@@ -499,9 +499,6 @@ export class Mesh extends Asset {
vbView.set(srcVBView);
// if (bundle.view.stride === dstBundle.view.stride) {
// vbView.set(dstVBView, bundle.view.length);
// } else {
srcAttrOffset = 0;
for (const attr of bundle.attributes) {
dstVBOffset = 0;
@@ -525,7 +522,6 @@ export class Mesh extends Asset {
}
srcAttrOffset += GFXFormatInfos[attr.format].size;
}
// }
vertexBundles[i] = {
attributes: bundle.attributes,
+3 -2
View File
@@ -33,8 +33,8 @@ import { GFXDevice } from '../../gfx/device';
import { Node } from '../../scene-graph';
import { Mesh } from '../assets';
import { Skeleton } from '../assets/skeleton';
import { mapBuffer } from '../misc/utils';
import { LCA, SkinningModelComponent } from './skinning-model-component';
import { LCA, mapBuffer } from '../misc/utils';
import { SkinningModelComponent } from './skinning-model-component';
const _vec2 = new Vec2();
@@ -321,6 +321,7 @@ export class AvatarModelComponent extends SkinningModelComponent {
skeleton.joints = skeleton.joints.map((_, idx, arr) => arr[idxMap[idx]]);
skeleton.bindposes = skeleton.bindposes.map((_, idx, arr) => arr[idxMap[idx]]);
// apply
// @ts-ignore
super.skeleton = skeleton;
}
@@ -35,8 +35,8 @@ import { Node } from '../../scene-graph';
import { Material, Mesh } from '../assets';
import { IMeshStruct } from '../assets/mesh';
import { Skeleton } from '../assets/skeleton';
import { mapBuffer } from '../misc/utils';
import { LCA, SkinningModelComponent } from './skinning-model-component';
import { LCA, mapBuffer } from '../misc/utils';
import { SkinningModelComponent } from './skinning-model-component';
const repeat = (n: number) => n - Math.floor(n);
const batch_id: IGFXAttribute = { name: GFXAttributeName.ATTR_BATCH_ID, format: GFXFormat.R32F, isNormalized: false };
+15 -207
View File
@@ -22,80 +22,15 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator';
import { Mat4, Quat, Vec3 } from '../../core/value-types';
import { quat, vec3 } from '../../core/vmath';
import { GFXDevice } from '../../gfx/device';
import { JointUniformCapacity } from '../../pipeline/define';
import { selectJointsMediumType, SkinningModel } from '../../renderer/models/skinning-model';
import { Node } from '../../scene-graph/node';
import { Mesh } from '../assets';
import { Material } from '../assets/material';
import { Skeleton } from '../assets/skeleton';
import { builtinResMgr } from '../builtin';
import { aabb } from '../geom-utils';
import { calculateBoneSpaceBounds } from '../misc/utils';
import { ModelComponent } from './model-component';
const v3_1 = new Vec3();
const qt_1 = new Quat();
const v3_2 = new Vec3();
const m4_1 = new Mat4();
const ab_1 = new aabb();
class Joint {
public node: Node;
public position: Vec3 = new Vec3();
public rotation: Quat = new Quat();
public scale: Vec3 = new Vec3(1, 1, 1);
public parent: Joint | null = null;
protected _lastUpdate = -1;
constructor (node: Node) { this.node = node; }
public update () {
const totalFrames = cc.director.totalFrames;
if (this._lastUpdate >= totalFrames) { return; }
this._lastUpdate = totalFrames;
const parent = this.parent;
if (parent) {
parent.update();
vec3.multiply(this.position, this.node.position, parent.scale);
vec3.transformQuat(this.position, this.position, parent.rotation);
vec3.add(this.position, this.position, parent.position);
quat.multiply(this.rotation, parent.rotation, this.node.rotation);
vec3.multiply(this.scale, parent.scale, this.node.scale);
}
}
}
class JointManager {
public static get (node: Node, root: Node) {
let joint = JointManager._joints.get(node);
if (joint) { return joint; }
joint = new Joint(node);
if (node !== root && node.parent) { joint.parent = JointManager.get(node.parent, root); }
JointManager._joints.set(node, joint);
return joint;
}
protected static _joints: Map<Node, Joint> = new Map();
}
// get the lowest common ancestor
const _path: Node[] = [];
export const LCA = (a: Node, b: Node) => {
if (a === b) { return a; }
let cur: Node | null = b;
_path.length = 0;
while (cur) { _path.push(cur); cur = cur.parent; }
cur = a;
while (cur) {
if (_path.find((n) => n === cur)) { return cur; }
cur = cur.parent;
}
return null;
};
/**
* @en The Skinning Model Component
* @zh 蒙皮模型组件。
@@ -116,11 +51,9 @@ export class SkinningModelComponent extends ModelComponent {
}
set skeleton (val) {
const old = this._skeleton;
this._skeleton = val;
this._onSkeletonChanged(old);
this._resetSkinningTarget();
this._bindSkeleton();
this._resetSkinningTarget();
}
/**
@@ -136,177 +69,52 @@ export class SkinningModelComponent extends ModelComponent {
this._resetSkinningTarget();
}
get model () {
return (this._model as SkinningModel);
}
@property(Skeleton)
protected _skeleton: Skeleton | null = null;
@property(Node)
protected _skinningRoot: Node | null = null;
protected _joints: Joint[] = [];
protected _boneSpaceBounds: null | Array<aabb | null> = null;
protected _jointCount = JointUniformCapacity;
public onLoad () {
this._materials.forEach((material, index) => material && this._onMaterialModified(index, material));
this._resetSkinningTarget();
if (CC_EDITOR && this.mesh && this._skeleton) {
this._boneSpaceBounds = boneSpaceBoundsManager.use(this.mesh, this._skeleton);
}
}
public update () {
if (!this._skeleton || !this._model) {
return;
}
const skinningModel = this._model as SkinningModel;
const skeleton = this._skeleton;
const len = this._jointCount;
for (let i = 0; i < len; ++i) {
const cur = this._joints[i];
cur.update();
const bindpose = skeleton.bindposes[i];
vec3.multiply(v3_1, bindpose.position, cur.scale);
vec3.transformQuat(v3_1, v3_1, cur.rotation);
vec3.add(v3_1, v3_1, cur.position);
quat.multiply(qt_1, cur.rotation, bindpose.rotation);
vec3.multiply(v3_2, cur.scale, bindpose.scale);
skinningModel.updateJointData(i, v3_1, qt_1, v3_2, i === 0);
}
skinningModel.commitJointData();
}
public calculateSkinnedBounds (out?: aabb): boolean {
if (!this._skeleton || !this._boneSpaceBounds) {
return false;
}
out = out || new aabb();
let outInitialized = false;
const len = this._joints.length;
for (let i = 0; i < len; ++i) {
const bounds = this._boneSpaceBounds[i];
const targetNode = this._joints[i].node;
if (!bounds || !targetNode) { continue; }
targetNode.getWorldMatrix(m4_1);
aabb.transform(ab_1, bounds, m4_1);
if (outInitialized) {
aabb.merge(out, out, ab_1);
} else {
outInitialized = true;
aabb.copy(out, ab_1);
}
}
return outInitialized;
}
public _updateModelParams () {
// Should bind skeleton before super create pso
this._bindSkeleton();
this._resetSkinningTarget();
super._updateModelParams();
}
protected _getModelConstructor () {
return SkinningModel;
}
protected _onMeshChanged (old: Mesh | null) {
super._onMeshChanged(old);
if (!CC_EDITOR || !this._skeleton) { return; }
if (old) { boneSpaceBoundsManager.unuse(old, this._skeleton); }
if (this.mesh) { this._boneSpaceBounds = boneSpaceBoundsManager.use(this.mesh, this._skeleton); }
}
protected _onSkeletonChanged (old: Skeleton | null) {
if (!CC_EDITOR || !this.mesh) { return; }
if (old) { boneSpaceBoundsManager.unuse(this.mesh, old); }
if (this._skeleton) { this._boneSpaceBounds = boneSpaceBoundsManager.use(this.mesh, this._skeleton); }
}
protected _onMaterialModified (index: number, material: Material | null) {
const device: GFXDevice = cc.director.root && cc.director.root.device;
const type = selectJointsMediumType(device, this._jointCount);
const joints = this._skeleton && this._skeleton.joints.length || 0;
const type = selectJointsMediumType(device, joints);
const mat = this.getMaterial(index, CC_EDITOR) || this._getBuiltinMaterial();
mat.recompileShaders({ CC_USE_SKINNING: type });
super._onMaterialModified(index, mat);
}
protected _getModelConstructor () {
return SkinningModel;
}
protected _getBuiltinMaterial () {
// classic ugly pink indicating missing material
return builtinResMgr.get<Material>('missing-skinning-material');
}
private _bindSkeleton () {
this._jointCount = this._skeleton && this._skeleton.joints.length || 0;
if (this._model) {
(this._model as SkinningModel).bindSkeleton(this._skeleton);
}
if (this._model) { (this._model as SkinningModel).bindSkeleton(this._skeleton); }
this._materials.forEach((material, index) => material && this._onMaterialModified(index, material));
}
private _resetSkinningTarget () {
this._joints.length = 0;
if (!this._skeleton || !this._skinningRoot) { return; }
const root = LCA(this.node, this._skinningRoot);
if (!root) { return; }
const rootNode = this._skinningRoot;
this._skeleton.joints.forEach((path) => {
const targetNode = rootNode.getChildByPath(path);
if (!targetNode) {
console.warn(`Skinning target ${path} not found in scene graph.`);
return;
}
this._joints.push(JointManager.get(targetNode, root));
});
if (!this._model || !this._skinningRoot) { return; }
(this._model as SkinningModel).resetSkinningTarget(this._skinningRoot);
}
}
interface ICachedBoneSpaceBounds {
bounds: Array<aabb | null>;
referenceCount: number;
}
class BoneSpaceBoundsManager {
private _cached = new Map<Mesh, Map<Skeleton, ICachedBoneSpaceBounds>>();
public use (mesh: Mesh, skeleton: Skeleton) {
let bucket = this._cached.get(mesh);
if (!bucket) {
bucket = new Map();
this._cached.set(mesh, bucket);
}
let cached = bucket.get(skeleton);
if (!cached) {
cached = {
bounds: calculateBoneSpaceBounds(mesh, skeleton),
referenceCount: 0,
};
bucket.set(skeleton, cached);
}
++cached.referenceCount;
return cached.bounds;
}
public unuse (mesh: Mesh, skeleton: Skeleton) {
const bucket = this._cached.get(mesh);
if (bucket) {
const cached = bucket.get(skeleton);
if (cached) {
--cached.referenceCount;
if (cached.referenceCount === 0) {
bucket.delete(skeleton);
}
}
if (bucket.size === 0) {
this._cached.delete(mesh);
}
}
}
}
const boneSpaceBoundsManager = new BoneSpaceBoundsManager();
+106 -30
View File
@@ -1,10 +1,12 @@
import { GFXAttributeName, GFXFormat, GFXFormatInfos, GFXFormatType, GFXPrimitiveMode, IGFXFormatInfo } from '../../gfx/define';
export { find } from '../../scene-graph/find';
import { Vec3 } from '../../core/value-types';
import { Mat4, Vec3 } from '../../core/value-types';
import { vec3 } from '../../core/vmath';
import { IGFXAttribute } from '../../gfx/input-assembler';
import { Node } from '../../scene-graph';
import { IMeshStruct, IPrimitive, IVertexBundle, Mesh } from '../assets/mesh';
import { Skeleton } from '../assets/skeleton';
import { SkinningModelComponent } from '../framework';
import { aabb } from '../geom-utils';
import { IGeometry } from '../primitive/define';
import { BufferBlob } from './buffer-blob';
@@ -34,6 +36,7 @@ export interface ICreateMeshOptions {
calculateBounds?: boolean;
}
const v3_1 = new Vec3();
export function createMesh (geometry: IGeometry, out?: Mesh, options?: ICreateMeshOptions) {
options = options || {};
// Collect attributes and calculate length of result vertex buffer.
@@ -211,16 +214,16 @@ export function createMesh (geometry: IGeometry, out?: Mesh, options?: ICreateMe
if (!minPosition && options.calculateBounds) {
minPosition = vec3.set(new vec3(), Infinity, Infinity, Infinity);
for (let iVertex = 0; iVertex < vertCount; ++iVertex) {
vec3.set(tmpVec3, geometry.positions[iVertex * 3 + 0], geometry.positions[iVertex * 3 + 1], geometry.positions[iVertex * 3 + 2]);
vec3.min(minPosition, minPosition, tmpVec3);
vec3.set(v3_1, geometry.positions[iVertex * 3 + 0], geometry.positions[iVertex * 3 + 1], geometry.positions[iVertex * 3 + 2]);
vec3.min(minPosition, minPosition, v3_1);
}
}
let maxPosition = geometry.maxPos;
if (!maxPosition && options.calculateBounds) {
maxPosition = vec3.set(new vec3(), -Infinity, -Infinity, -Infinity);
for (let iVertex = 0; iVertex < vertCount; ++iVertex) {
vec3.set(tmpVec3, geometry.positions[iVertex * 3 + 0], geometry.positions[iVertex * 3 + 1], geometry.positions[iVertex * 3 + 2]);
vec3.max(maxPosition, maxPosition, tmpVec3);
vec3.set(v3_1, geometry.positions[iVertex * 3 + 0], geometry.positions[iVertex * 3 + 1], geometry.positions[iVertex * 3 + 2]);
vec3.max(maxPosition, maxPosition, v3_1);
}
}
@@ -252,8 +255,8 @@ export function readMesh (mesh: Mesh, iPrimitive: number = 0) {
let offset = bundle.view.offset;
const { length, stride } = bundle.view;
for (const attr of bundle.attributes) {
const name = _keyMap[attr.name];
if (name) { out[name] = readBuffer(dataView, attr.format, offset, length, stride); }
const name: GFXAttributeName = _keyMap[attr.name];
if (name) { out[name] = (out[name] || []).concat(readBuffer(dataView, attr.format, offset, length, stride)); }
offset += GFXFormatInfos[attr.format].size;
}
}
@@ -334,21 +337,32 @@ export function mapBuffer (
return out;
}
const tmpVec3 = new vec3();
export function calculateBoneSpaceBounds (mesh: Mesh, skeleton: Skeleton) {
// https://gamedev.stackexchange.com/questions/43986/calculate-an-aabb-for-bone-animated-model/44135
const result = new Array<{max: vec3, min: vec3, hasValue: boolean}>(skeleton.joints.length);
// get the lowest common ancestor
const _path: Node[] = [];
export const LCA = (a: Node, b: Node) => {
if (a === b) { return a; }
let cur: Node | null = b;
_path.length = 0;
while (cur) { _path.push(cur); cur = cur.parent; }
cur = a;
while (cur) {
if (_path.find((n) => n === cur)) { return cur; }
cur = cur.parent;
}
return null;
};
const v3_2 = new Vec3();
function calculateBoneSpaceBounds (mesh: Mesh, skeleton: Skeleton) {
const result = new Array< { max: Vec3, min: Vec3, hasValue: boolean } >(skeleton.joints.length);
for (let i = 0; i < result.length; ++i) {
result[i] = {
hasValue: false,
min: new vec3(Infinity, Infinity, Infinity),
max: new vec3(-Infinity, -Infinity, -Infinity),
min: new Vec3(Infinity, Infinity, Infinity),
max: new Vec3(-Infinity, -Infinity, -Infinity),
};
}
const pos = new Vec3();
const transformedPos = new Vec3();
for (let iPrimitive = 0; iPrimitive < mesh.struct.primitives.length; ++iPrimitive) {
const joints = mesh.readAttribute(iPrimitive, GFXAttributeName.ATTR_JOINTS);
if (!joints) {
@@ -364,7 +378,7 @@ export function calculateBoneSpaceBounds (mesh: Mesh, skeleton: Skeleton) {
}
const vertexCount = Math.min(joints.length / 4, weights.length / 4, positions.length / 3);
for (let iVertex = 0; iVertex < vertexCount; ++iVertex) {
vec3.set(pos, positions[3 * iVertex + 0], positions[3 * iVertex + 1], positions[3 * iVertex + 2]);
vec3.set(v3_1, positions[3 * iVertex + 0], positions[3 * iVertex + 1], positions[3 * iVertex + 2]);
for (let i = 0; i < 4; ++i) {
const weight = weights[4 * iVertex + i];
if (weight === 0) {
@@ -372,27 +386,89 @@ export function calculateBoneSpaceBounds (mesh: Mesh, skeleton: Skeleton) {
}
const refJointIndex = joints[4 * iVertex + i];
if (refJointIndex >= skeleton.joints.length) {
// TODO debugger
continue;
}
const bindpose = skeleton.bindposes[refJointIndex];
const jointBounds = result[refJointIndex];
vec3.multiply(transformedPos, pos, bindpose.scale);
vec3.transformQuat(transformedPos, transformedPos, bindpose.rotation);
vec3.add(transformedPos, transformedPos, bindpose.position);
vec3.multiply(v3_2, v3_1, bindpose.scale);
vec3.transformQuat(v3_2, v3_2, bindpose.rotation);
vec3.add(v3_2, v3_2, bindpose.position);
jointBounds.hasValue = true;
vec3.min(jointBounds.min, jointBounds.min, transformedPos);
vec3.max(jointBounds.max, jointBounds.max, transformedPos);
vec3.min(jointBounds.min, jointBounds.min, v3_2);
vec3.max(jointBounds.max, jointBounds.max, v3_2);
}
}
}
return result.map((bounds) => {
if (bounds.hasValue) {
return aabb.fromPoints(new aabb(), bounds.min, bounds.max);
}
return null;
});
return result.map((bounds) => bounds.hasValue ? aabb.fromPoints(new aabb(), bounds.min, bounds.max) : null);
}
interface ICachedBoneSpaceBounds {
bounds: Array<aabb | null>;
referenceCount: number;
}
class BoneSpaceBoundsManager {
private _cached = new Map<Mesh, Map<Skeleton, ICachedBoneSpaceBounds>>();
public use (mesh: Mesh, skeleton: Skeleton) {
let bucket = this._cached.get(mesh);
if (!bucket) {
bucket = new Map();
this._cached.set(mesh, bucket);
}
let cached = bucket.get(skeleton);
if (!cached) {
cached = {
bounds: calculateBoneSpaceBounds(mesh, skeleton),
referenceCount: 0,
};
bucket.set(skeleton, cached);
}
++cached.referenceCount;
return cached.bounds;
}
public unuse (mesh: Mesh, skeleton: Skeleton) {
const bucket = this._cached.get(mesh);
if (bucket) {
const cached = bucket.get(skeleton);
if (cached) {
--cached.referenceCount;
if (cached.referenceCount === 0) {
bucket.delete(skeleton);
}
}
if (bucket.size === 0) {
this._cached.delete(mesh);
}
}
}
}
const boneSpaceBoundsManager = new BoneSpaceBoundsManager();
const m4_1 = new Mat4();
const ab_1 = new aabb();
const v3_3 = new Vec3();
const v3_4 = new Vec3();
export function calculateSkinnedBounds (out: aabb, comp: SkinningModelComponent) {
if (!comp.model || !comp.mesh || !comp.skeleton) { return; }
const mesh = comp.mesh;
const skeleton = comp.skeleton;
const joints = comp.model.joints;
vec3.set(v3_3, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
vec3.set(v3_4, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
const boundList = boneSpaceBoundsManager.use(mesh, skeleton);
const len = joints.length;
for (let i = 0; i < len; ++i) {
const bounds = boundList[i];
const targetNode = joints[i].node;
if (!bounds || !targetNode) { continue; }
targetNode.getWorldMatrix(m4_1);
aabb.transform(ab_1, bounds, m4_1);
ab_1.getBoundary(v3_1, v3_2);
vec3.min(v3_3, v3_3, v3_1);
vec3.max(v3_4, v3_4, v3_2);
}
aabb.fromPoints(out, v3_3, v3_4);
return true;
}
+12 -12
View File
@@ -24,7 +24,7 @@ export class color4 {
/**
* @zh 复制目标颜色
*/
public static copy (out: color4, a: color4) {
public static copy<Out extends color4> (out: Out, a: color4) {
out.r = a.r;
out.g = a.g;
out.b = a.b;
@@ -35,7 +35,7 @@ export class color4 {
/**
* @zh 设置颜色值
*/
public static set (out: color4, r: number, g: number, b: number, a: number) {
public static set<Out extends color4> (out: Out, r: number, g: number, b: number, a: number) {
out.r = r;
out.g = g;
out.b = b;
@@ -46,7 +46,7 @@ export class color4 {
/**
* @zh 根据指定整型数据设置颜色
*/
public static fromHex (out: color4, hex: number) {
public static fromHex<Out extends color4> (out: Out, hex: number) {
const r = ((hex >> 24)) / 255.0;
const g = ((hex >> 16) & 0xff) / 255.0;
const b = ((hex >> 8) & 0xff) / 255.0;
@@ -62,7 +62,7 @@ export class color4 {
/**
* @zh 逐通道颜色加法
*/
public static add (out: color4, a: color4, b: color4) {
public static add<Out extends color4> (out: Out, a: color4, b: color4) {
out.r = a.r + b.r;
out.g = a.g + b.g;
out.b = a.b + b.b;
@@ -73,7 +73,7 @@ export class color4 {
/**
* @zh 逐通道颜色减法
*/
public static subtract (out: color4, a: color4, b: color4) {
public static subtract<Out extends color4> (out: Out, a: color4, b: color4) {
out.r = a.r - b.r;
out.g = a.g - b.g;
out.b = a.b - b.b;
@@ -84,14 +84,14 @@ export class color4 {
/**
* @zh 逐通道颜色减法
*/
public static sub (out: color4, a: color4, b: color4) {
public static sub<Out extends color4> (out: Out, a: color4, b: color4) {
return color4.subtract(out, a, b);
}
/**
* @zh 逐通道颜色乘法
*/
public static multiply (out: color4, a: color4, b: color4) {
public static multiply<Out extends color4> (out: Out, a: color4, b: color4) {
out.r = a.r * b.r;
out.g = a.g * b.g;
out.b = a.b * b.b;
@@ -102,14 +102,14 @@ export class color4 {
/**
* @zh 逐通道颜色乘法
*/
public static mul (out: color4, a: color4, b: color4) {
public static mul<Out extends color4> (out: Out, a: color4, b: color4) {
return color4.multiply(out, a, b);
}
/**
* @zh 逐通道颜色除法
*/
public static divide (out: color4, a: color4, b: color4) {
public static divide<Out extends color4> (out: Out, a: color4, b: color4) {
out.r = a.r / b.r;
out.g = a.g / b.g;
out.b = a.b / b.b;
@@ -120,14 +120,14 @@ export class color4 {
/**
* @zh 逐通道颜色除法
*/
public static div (out: color4, a: color4, b: color4) {
public static div<Out extends color4> (out: Out, a: color4, b: color4) {
return color4.divide(out, a, b);
}
/**
* @zh 全通道统一缩放颜色
*/
public static scale (out: color4, a: color4, b: number) {
public static scale<Out extends color4> (out: Out, a: color4, b: number) {
out.r = a.r * b;
out.g = a.g * b;
out.b = a.b * b;
@@ -138,7 +138,7 @@ export class color4 {
/**
* @zh 逐通道颜色线性插值:A + t * (B - A)
*/
public static lerp (out: color4, a: color4, b: color4, t: number) {
public static lerp<Out extends color4> (out: Out, a: color4, b: color4, t: number) {
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);
+20 -20
View File
@@ -31,7 +31,7 @@ export class mat3 {
/**
* @zh 复制目标矩阵
*/
public static copy (out: mat3, a: mat3) {
public static copy<Out extends mat3> (out: Out, a: mat3) {
out.m00 = a.m00;
out.m01 = a.m01;
out.m02 = a.m02;
@@ -62,7 +62,7 @@ export class mat3 {
/**
* @zh 将目标赋值为单位矩阵
*/
public static identity (out: mat3) {
public static identity<Out extends mat3> (out: Out) {
out.m00 = 1;
out.m01 = 0;
out.m02 = 0;
@@ -78,7 +78,7 @@ export class mat3 {
/**
* @zh 转置矩阵
*/
public static transpose (out: mat3, a: mat3) {
public static transpose<Out extends mat3> (out: Out, a: mat3) {
// 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;
@@ -106,7 +106,7 @@ export class mat3 {
/**
* @zh 矩阵求逆
*/
public static invert (out: mat3, a: mat3) {
public static invert<Out extends mat3> (out: Out, a: mat3) {
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;
@@ -149,7 +149,7 @@ export class mat3 {
/**
* @zh 矩阵乘法
*/
public static multiply (out: mat3, a: mat3, b: mat3) {
public static multiply<Out extends mat3> (out: Out, a: mat3, b: mat3) {
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;
@@ -182,7 +182,7 @@ export class mat3 {
/**
* @zh 在给定矩阵变换基础上加入新位移变换
*/
public static translate (out: mat3, a: mat3, v: vec3) {
public static translate<Out extends mat3> (out: Out, a: mat3, v: vec3) {
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;
@@ -205,7 +205,7 @@ export class mat3 {
/**
* @zh 在给定矩阵变换基础上加入新缩放变换
*/
public static scale (out: mat3, a: mat3, v: vec3) {
public static scale<Out extends mat3> (out: Out, a: mat3, v: vec3) {
const x = v.x, y = v.y;
out.m00 = x * a.m00;
@@ -226,7 +226,7 @@ export class mat3 {
* @zh 在给定矩阵变换基础上加入新旋转变换
* @param rad 旋转弧度
*/
public static rotate (out: mat3, a: mat3, rad: number) {
public static rotate<Out extends mat3> (out: Out, a: mat3, rad: number) {
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;
@@ -251,7 +251,7 @@ export class mat3 {
/**
* @zh 根据指定四维矩阵计算三维矩阵
*/
public static fromMat4 (out: mat3, a: mat4) {
public static fromMat4<Out extends mat3> (out: Out, a: mat4) {
out.m00 = a.m00;
out.m01 = a.m01;
out.m02 = a.m02;
@@ -269,7 +269,7 @@ export class mat3 {
* @param view 视口面向的前方向,必须归一化
* @param up 视口的上方向,必须归一化,默认为 (0, 1, 0)
*/
public static fromViewUp (out: mat3, view: vec3, up?: vec3) {
public static fromViewUp<Out extends mat3> (out: Out, view: vec3, up?: vec3) {
if (vec3.sqrMag(view) < EPSILON * EPSILON) {
mat3.identity(out);
return out;
@@ -297,7 +297,7 @@ export class mat3 {
/**
* @zh 计算位移矩阵
*/
public static fromTranslation (out: mat3, v: vec3) {
public static fromTranslation<Out extends mat3> (out: Out, v: vec3) {
out.m00 = 1;
out.m01 = 0;
out.m02 = 0;
@@ -313,7 +313,7 @@ export class mat3 {
/**
* @zh 计算缩放矩阵
*/
public static fromScaling (out: mat3, v: vec3) {
public static fromScaling<Out extends mat3> (out: Out, v: vec3) {
out.m00 = v.x;
out.m01 = 0;
out.m02 = 0;
@@ -331,7 +331,7 @@ export class mat3 {
/**
* @zh 计算旋转矩阵
*/
public static fromRotation (out: mat3, rad: number) {
public static fromRotation<Out extends mat3> (out: Out, rad: number) {
const s = Math.sin(rad), c = Math.cos(rad);
out.m00 = c;
@@ -351,7 +351,7 @@ export class mat3 {
/**
* @zh 根据四元数旋转信息计算矩阵
*/
public static fromQuat (out: mat3, q: quat) {
public static fromQuat<Out extends mat3> (out: Out, q: quat) {
const x = q.x, y = q.y, z = q.z, w = q.w;
const x2 = x + x;
const y2 = y + y;
@@ -385,7 +385,7 @@ export class mat3 {
/**
* @zh 计算指定四维矩阵的逆转置三维矩阵
*/
public static inverseTransposeMat4 (out: mat3, a: mat4) {
public static inverseTransposeMat4<Out extends mat3> (out: Out, a: mat4) {
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,
@@ -455,7 +455,7 @@ export class mat3 {
/**
* @zh 逐元素矩阵加法
*/
public static add (out: mat3, a: mat3, b: mat3) {
public static add<Out extends mat3> (out: Out, a: mat3, b: mat3) {
out.m00 = a.m00 + b.m00;
out.m01 = a.m01 + b.m01;
out.m02 = a.m02 + b.m02;
@@ -471,7 +471,7 @@ export class mat3 {
/**
* @zh 逐元素矩阵减法
*/
public static subtract (out: mat3, a: mat3, b: mat3) {
public static subtract<Out extends mat3> (out: Out, a: mat3, b: mat3) {
out.m00 = a.m00 - b.m00;
out.m01 = a.m01 - b.m01;
out.m02 = a.m02 - b.m02;
@@ -487,14 +487,14 @@ export class mat3 {
/**
* @zh 逐元素矩阵减法
*/
public static sub (out: mat3, a: mat3, b: mat3) {
public static sub<Out extends mat3> (out: Out, a: mat3, b: mat3) {
return mat3.subtract(out, a, b);
}
/**
* @zh 矩阵标量乘法
*/
public static multiplyScalar (out: mat3, a: mat3, b: number) {
public static multiplyScalar<Out extends mat3> (out: Out, a: mat3, b: number) {
out.m00 = a.m00 * b;
out.m01 = a.m01 * b;
out.m02 = a.m02 * b;
@@ -510,7 +510,7 @@ export class mat3 {
/**
* @zh 逐元素矩阵标量乘加: A + B * scale
*/
public static multiplyScalarAndAdd (out: mat3, a: mat3, b: mat3, scale: number) {
public static multiplyScalarAndAdd<Out extends mat3> (out: Out, a: mat3, b: mat3, scale: number) {
out.m00 = a.m00 + (b.m00 * scale);
out.m01 = a.m01 + (b.m01 * scale);
out.m02 = a.m02 + (b.m02 * scale);
+35 -35
View File
@@ -41,7 +41,7 @@ export class mat4 {
/**
* @zh 复制目标矩阵
*/
public static copy (out: mat4, a: mat4) {
public static copy<Out extends mat4> (out: Out, a: mat4) {
out.m00 = a.m00;
out.m01 = a.m01;
out.m02 = a.m02;
@@ -81,7 +81,7 @@ export class mat4 {
/**
* @zh 将目标赋值为单位矩阵
*/
public static identity (out: mat4) {
public static identity<Out extends mat4> (out: Out) {
out.m00 = 1;
out.m01 = 0;
out.m02 = 0;
@@ -104,7 +104,7 @@ export class mat4 {
/**
* @zh 转置矩阵
*/
public static transpose (out: mat4, a: mat4) {
public static transpose<Out extends mat4> (out: Out, a: mat4) {
// 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, a03 = a.m03, a12 = a.m06, a13 = a.m07, a23 = a.m11;
@@ -144,7 +144,7 @@ export class mat4 {
/**
* @zh 矩阵求逆
*/
public static invert (out: mat4, a: mat4) {
public static invert<Out extends mat4> (out: Out, a: mat4) {
const a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03;
const a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07;
const a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11;
@@ -218,7 +218,7 @@ export class mat4 {
/**
* @zh 矩阵乘法
*/
public static multiply (out: mat4, a: mat4, b: mat4) {
public static multiply<Out extends mat4> (out: Out, a: mat4, b: mat4) {
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,
@@ -254,14 +254,14 @@ export class mat4 {
/**
* @zh 矩阵乘法
*/
public static mul (out: mat4, a: mat4, b: mat4) {
public static mul<Out extends mat4> (out: Out, a: mat4, b: mat4) {
return mat4.multiply(out, a, b);
}
/**
* @zh 在给定矩阵变换基础上加入新位移变换
*/
public static translate (out: mat4, a: mat4, v: vec3) {
public static translate<Out extends mat4> (out: Out, a: mat4, v: vec3) {
const x = v.x, y = v.y, z = v.z;
if (a === out) {
out.m12 = a.m00 * x + a.m04 * y + a.m08 * z + a.m12;
@@ -288,7 +288,7 @@ export class mat4 {
/**
* @zh 在给定矩阵变换基础上加入新缩放变换
*/
public static scale (out: mat4, a: mat4, v: vec3) {
public static scale<Out extends mat4> (out: Out, a: mat4, v: vec3) {
const x = v.x, y = v.y, z = v.z;
out.m00 = a.m00 * x;
out.m01 = a.m01 * x;
@@ -314,7 +314,7 @@ export class mat4 {
* @param rad 旋转角度
* @param axis 旋转轴
*/
public static rotate (out: mat4, a: mat4, rad: number, axis: vec3) {
public static rotate<Out extends mat4> (out: Out, a: mat4, rad: number, axis: vec3) {
let x = axis.x, y = axis.y, z = axis.z;
let len = Math.sqrt(x * x + y * y + z * z);
@@ -370,7 +370,7 @@ export class mat4 {
* @zh 在给定矩阵变换基础上加入绕 X 轴的旋转变换
* @param rad 旋转角度
*/
public static rotateX (out: mat4, a: mat4, rad: number) {
public static rotateX<Out extends mat4> (out: Out, a: mat4, rad: number) {
const s = Math.sin(rad),
c = Math.cos(rad),
a10 = a.m04,
@@ -410,7 +410,7 @@ export class mat4 {
* @zh 在给定矩阵变换基础上加入绕 Y 轴的旋转变换
* @param rad 旋转角度
*/
public static rotateY (out: mat4, a: mat4, rad: number) {
public static rotateY<Out extends mat4> (out: Out, a: mat4, rad: number) {
const s = Math.sin(rad),
c = Math.cos(rad),
a00 = a.m00,
@@ -450,7 +450,7 @@ export class mat4 {
* @zh 在给定矩阵变换基础上加入绕 Z 轴的旋转变换
* @param rad 旋转角度
*/
public static rotateZ (out: mat4, a: mat4, rad: number) {
public static rotateZ<Out extends mat4> (out: Out, a: mat4, rad: number) {
const s = Math.sin(rad),
c = Math.cos(rad),
a00 = a.m00,
@@ -490,7 +490,7 @@ export class mat4 {
/**
* @zh 计算位移矩阵
*/
public static fromTranslation (out: mat4, v: vec3) {
public static fromTranslation<Out extends mat4> (out: Out, v: vec3) {
out.m00 = 1;
out.m01 = 0;
out.m02 = 0;
@@ -513,7 +513,7 @@ export class mat4 {
/**
* @zh 计算缩放矩阵
*/
public static fromScaling (out: mat4, v: vec3) {
public static fromScaling<Out extends mat4> (out: Out, v: vec3) {
out.m00 = v.x;
out.m01 = 0;
out.m02 = 0;
@@ -536,7 +536,7 @@ export class mat4 {
/**
* @zh 计算旋转矩阵
*/
public static fromRotation (out: mat4, rad: number, axis: vec3) {
public static fromRotation<Out extends mat4> (out: Out, rad: number, axis: vec3) {
let x = axis.x, y = axis.y, z = axis.z;
let len = Math.sqrt(x * x + y * y + z * z);
@@ -576,7 +576,7 @@ export class mat4 {
/**
* @zh 计算绕 X 轴的旋转矩阵
*/
public static fromXRotation (out: mat4, rad: number) {
public static fromXRotation<Out extends mat4> (out: Out, rad: number) {
const s = Math.sin(rad), c = Math.cos(rad);
// Perform axis-specific matrix multiplication
@@ -602,7 +602,7 @@ export class mat4 {
/**
* @zh 计算绕 Y 轴的旋转矩阵
*/
public static fromYRotation (out: mat4, rad: number) {
public static fromYRotation<Out extends mat4> (out: Out, rad: number) {
const s = Math.sin(rad), c = Math.cos(rad);
// Perform axis-specific matrix multiplication
@@ -628,7 +628,7 @@ export class mat4 {
/**
* @zh 计算绕 Z 轴的旋转矩阵
*/
public static fromZRotation (out: mat4, rad: number) {
public static fromZRotation<Out extends mat4> (out: Out, rad: number) {
const s = Math.sin(rad), c = Math.cos(rad);
// Perform axis-specific matrix multiplication
@@ -654,7 +654,7 @@ export class mat4 {
/**
* @zh 根据旋转和位移信息计算矩阵
*/
public static fromRT (out: mat4, q: quat, v: vec3) {
public static fromRT<Out extends mat4> (out: Out, q: quat, v: vec3) {
const x = q.x, y = q.y, z = q.z, w = q.w;
const x2 = x + x;
const y2 = y + y;
@@ -693,7 +693,7 @@ export class mat4 {
/**
* @zh 提取矩阵的位移信息, 默认矩阵中的变换以 S->R->T 的顺序应用
*/
public static getTranslation (out: vec3, mat: mat4) {
public static getTranslation<Out extends vec3> (out: Out, mat: mat4) {
out.x = mat.m12;
out.y = mat.m13;
out.z = mat.m14;
@@ -704,7 +704,7 @@ export class mat4 {
/**
* @zh 提取矩阵的缩放信息, 默认矩阵中的变换以 S->R->T 的顺序应用
*/
public static getScaling (out: vec3, mat: mat4) {
public static getScaling<Out extends vec3> (out: Out, mat: mat4) {
const m00 = m3_1.m00 = mat.m00;
const m01 = m3_1.m01 = mat.m01;
const m02 = m3_1.m02 = mat.m02;
@@ -725,7 +725,7 @@ export class mat4 {
/**
* @zh 提取矩阵的旋转信息, 默认矩阵中的变换以 S->R->T 的顺序应用
*/
public static getRotation (out: quat, mat: mat4) {
public static getRotation<Out extends quat> (out: Out, mat: mat4) {
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
const trace = mat.m00 + mat.m05 + mat.m10;
let S = 0;
@@ -784,7 +784,7 @@ export class mat4 {
/**
* @zh 根据旋转、位移、缩放信息计算矩阵,以 S->R->T 的顺序应用
*/
public static fromRTS (out: mat4, q: quat, v: vec3, s: vec3) {
public static fromRTS<Out extends mat4> (out: Out, q: quat, v: vec3, s: vec3) {
const x = q.x, y = q.y, z = q.z, w = q.w;
const x2 = x + x;
const y2 = y + y;
@@ -830,7 +830,7 @@ export class mat4 {
* @param s 缩放值
* @param o 指定变换中心
*/
public static fromRTSOrigin (out: mat4, q: quat, v: vec3, s: vec3, o: vec3) {
public static fromRTSOrigin<Out extends mat4> (out: Out, q: quat, v: vec3, s: vec3, o: vec3) {
const x = q.x, y = q.y, z = q.z, w = q.w;
const x2 = x + x;
const y2 = y + y;
@@ -877,7 +877,7 @@ export class mat4 {
/**
* @zh 根据指定的旋转信息计算矩阵
*/
public static fromQuat (out: mat4, q: quat) {
public static fromQuat<Out extends mat4> (out: Out, q: quat) {
const x = q.x, y = q.y, z = q.z, w = q.w;
const x2 = x + x;
const y2 = y + y;
@@ -925,7 +925,7 @@ export class mat4 {
* @param near 近平面距离
* @param far 远平面距离
*/
public static frustum (out: mat4, left: number, right: number, bottom: number, top: number, near: number, far: number) {
public static frustum<Out extends mat4> (out: Out, left: number, right: number, bottom: number, top: number, near: number, far: number) {
const rl = 1 / (right - left);
const tb = 1 / (top - bottom);
const nf = 1 / (near - far);
@@ -956,7 +956,7 @@ export class mat4 {
* @param near 近平面距离
* @param far 远平面距离
*/
public static perspective (out: mat4, fovy: number, aspect: number, near: number, far: number) {
public static perspective<Out extends mat4> (out: Out, fovy: number, aspect: number, near: number, far: number) {
const f = 1.0 / Math.tan(fovy / 2);
const nf = 1 / (near - far);
@@ -988,7 +988,7 @@ export class mat4 {
* @param near 近平面距离
* @param far 远平面距离
*/
public static ortho (out: mat4, left: number, right: number, bottom: number, top: number, near: number, far: number) {
public static ortho<Out extends mat4> (out: Out, left: number, right: number, bottom: number, top: number, near: number, far: number) {
const lr = 1 / (left - right);
const bt = 1 / (bottom - top);
const nf = 1 / (near - far);
@@ -1017,7 +1017,7 @@ export class mat4 {
* @param center 目标视点
* @param up 视口上方向
*/
public static lookAt (out: mat4, eye: vec3, center: vec3, up: vec3) {
public static lookAt<Out extends mat4> (out: Out, eye: vec3, center: vec3, up: vec3) {
const eyex = eye.x;
const eyey = eye.y;
const eyez = eye.z;
@@ -1080,7 +1080,7 @@ export class mat4 {
/**
* @zh 计算逆转置矩阵
*/
public static inverseTranspose (out: mat4, a: mat4) {
public static inverseTranspose<Out extends mat4> (out: Out, a: mat4) {
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,
@@ -1158,7 +1158,7 @@ export class mat4 {
/**
* @zh 逐元素矩阵加法
*/
public static add (out: mat4, a: mat4, b: mat4) {
public static add<Out extends mat4> (out: Out, a: mat4, b: mat4) {
out.m00 = a.m00 + b.m00;
out.m01 = a.m01 + b.m01;
out.m02 = a.m02 + b.m02;
@@ -1181,7 +1181,7 @@ export class mat4 {
/**
* @zh 逐元素矩阵减法
*/
public static subtract (out: mat4, a: mat4, b: mat4) {
public static subtract<Out extends mat4> (out: Out, a: mat4, b: mat4) {
out.m00 = a.m00 - b.m00;
out.m01 = a.m01 - b.m01;
out.m02 = a.m02 - b.m02;
@@ -1204,14 +1204,14 @@ export class mat4 {
/**
* @zh 逐元素矩阵减法
*/
public static sub (out: mat4, a: mat4, b: mat4) {
public static sub<Out extends mat4> (out: Out, a: mat4, b: mat4) {
return mat4.subtract(out, a, b);
}
/**
* @zh 矩阵标量乘法
*/
public static multiplyScalar (out: mat4, a: mat4, b: number) {
public static multiplyScalar<Out extends mat4> (out: Out, a: mat4, b: number) {
out.m00 = a.m00 * b;
out.m01 = a.m01 * b;
out.m02 = a.m02 * b;
@@ -1234,7 +1234,7 @@ export class mat4 {
/**
* @zh 逐元素矩阵标量乘加: A + B * scale
*/
public static multiplyScalarAndAdd (out: mat4, a: mat4, b: mat4, scale: number) {
public static multiplyScalarAndAdd<Out extends mat4> (out: Out, a: mat4, b: mat4, scale: number) {
out.m00 = a.m00 + (b.m00 * scale);
out.m01 = a.m01 + (b.m01 * scale);
out.m02 = a.m02 + (b.m02 * scale);
+29 -29
View File
@@ -27,14 +27,14 @@ export class quat {
/**
* @zh 复制目标四元数
*/
public static copy (out: quat, a: quat) {
public static copy<Out extends quat> (out: Out, a: quat) {
return vec4.copy(out, a);
}
/**
* @zh 设置四元数值
*/
public static set (out: quat, x: number, y: number, z: number, w: number) {
public static set<Out extends quat> (out: Out, x: number, y: number, z: number, w: number) {
out.x = x;
out.y = y;
out.z = z;
@@ -45,7 +45,7 @@ export class quat {
/**
* @zh 将目标赋值为单位四元数
*/
public static identity (out: quat) {
public static identity<Out extends quat> (out: Out) {
out.x = 0;
out.y = 0;
out.z = 0;
@@ -56,7 +56,7 @@ export class quat {
/**
* @zh 设置四元数为两向量间的最短路径旋转,默认两向量都已归一化
*/
public static rotationTo (out: quat, a: vec3, b: vec3) {
public static rotationTo<Out extends quat> (out: Out, a: vec3, b: vec3) {
const dot = vec3.dot(a, b);
if (dot < -0.999999) {
vec3.cross(v3_1, vec3.UNIT_X, a);
@@ -107,7 +107,7 @@ export class quat {
/**
* @zh 四元数乘法
*/
public static multiply (out: quat, a: quat, b: quat) {
public static multiply<Out extends quat> (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;
@@ -121,14 +121,14 @@ export class quat {
/**
* @zh 四元数乘法
*/
public static mul (out: quat, a: quat, b: quat) {
public static mul<Out extends quat> (out: Out, a: quat, b: quat) {
return quat.multiply(out, a, b);
}
/**
* @zh 四元数标量乘法
*/
public static scale (out: quat, a: quat, b: number) {
public static scale<Out extends quat> (out: Out, a: quat, b: number) {
out.x = a.x * b;
out.y = a.y * b;
out.z = a.z * b;
@@ -139,7 +139,7 @@ export class quat {
/**
* @zh 四元数乘加:A + B * scale
*/
public static scaleAndAdd (out: quat, a: quat, b: quat, scale: number) {
public static scaleAndAdd<Out extends quat> (out: Out, a: quat, b: quat, scale: number) {
out.x = a.x + b.x * scale;
out.y = a.y + b.y * scale;
out.z = a.z + b.z * scale;
@@ -151,7 +151,7 @@ export class quat {
* @zh 绕 X 轴旋转指定四元数
* @param rad 旋转弧度
*/
public static rotateX (out: quat, a: quat, rad: number) {
public static rotateX<Out extends quat> (out: Out, a: quat, rad: number) {
rad *= 0.5;
const { x: ax, y: ay, z: az, w: aw } = a;
@@ -169,7 +169,7 @@ export class quat {
* @zh 绕 Y 轴旋转指定四元数
* @param rad 旋转弧度
*/
public static rotateY (out: quat, a: quat, rad: number) {
public static rotateY<Out extends quat> (out: Out, a: quat, rad: number) {
rad *= 0.5;
const { x: ax, y: ay, z: az, w: aw } = a;
@@ -187,7 +187,7 @@ export class quat {
* @zh 绕 Z 轴旋转指定四元数
* @param rad 旋转弧度
*/
public static rotateZ (out: quat, a: quat, rad: number) {
public static rotateZ<Out extends quat> (out: Out, a: quat, rad: number) {
rad *= 0.5;
const { x: ax, y: ay, z: az, w: aw } = a;
@@ -206,7 +206,7 @@ export class quat {
* @param axis 旋转轴
* @param rad 旋转弧度
*/
public static rotateAround (out: quat, rot: quat, axis: vec3, rad: number) {
public static rotateAround<Out extends quat> (out: Out, rot: quat, axis: vec3, rad: number) {
// get inv-axis (local to rot)
quat.invert(qt_1, rot);
vec3.transformQuat(v3_1, axis, qt_1);
@@ -221,7 +221,7 @@ export class quat {
* @param axis 旋转轴
* @param rad 旋转弧度
*/
public static rotateAroundLocal (out: quat, rot: quat, axis: vec3, rad: number) {
public static rotateAroundLocal<Out extends quat> (out: Out, rot: quat, axis: vec3, rad: number) {
quat.fromAxisAngle(qt_1, axis, rad);
quat.multiply(out, rot, qt_1);
return out;
@@ -230,7 +230,7 @@ export class quat {
/**
* @zh 根据 xyz 分量计算 w 分量,默认已归一化
*/
public static calculateW (out: quat, a: quat) {
public static calculateW<Out extends quat> (out: Out, a: quat) {
const { x, y, z } = a;
out.x = x;
@@ -250,7 +250,7 @@ export class quat {
/**
* @zh 逐元素线性插值: A + t * (B - A)
*/
public static lerp (out: quat, a: quat, b: quat, t: number) {
public static lerp<Out extends quat> (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);
@@ -262,7 +262,7 @@ export class quat {
/**
* @zh 四元数球面插值
*/
public static slerp (out: quat, a: quat, b: quat, t: number) {
public static slerp<Out extends quat> (out: Out, a: quat, b: quat, t: number) {
// benchmarks:
// http://jsperf.com/quaternion-slerp-implementations
@@ -307,7 +307,7 @@ export class quat {
/**
* @zh 带两个控制点的四元数球面插值
*/
public static sqlerp (out: quat, a: quat, b: quat, c: quat, d: quat, t: number) {
public static sqlerp<Out extends quat> (out: Out, a: quat, b: quat, c: quat, d: quat, t: number) {
quat.slerp(qt_1, a, d, t);
quat.slerp(qt_2, b, c, t);
quat.slerp(out, qt_1, qt_2, 2 * t * (1 - t));
@@ -317,7 +317,7 @@ export class quat {
/**
* @zh 四元数求逆
*/
public static invert (out: quat, a: quat) {
public static invert<Out extends quat> (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;
@@ -334,7 +334,7 @@ export class quat {
/**
* @zh 求共轭四元数,对单位四元数与求逆等价,但更高效
*/
public static conjugate (out: quat, a: quat) {
public static conjugate<Out extends quat> (out: Out, a: quat) {
out.x = -a.x;
out.y = -a.y;
out.z = -a.z;
@@ -375,7 +375,7 @@ export class quat {
/**
* @zh 归一化四元数
*/
public static normalize (out: quat, a: quat) {
public static normalize<Out extends quat> (out: Out, a: quat) {
const { x, y, z, w } = a;
let len = x * x + y * y + z * z + w * w;
if (len > 0) {
@@ -391,7 +391,7 @@ export class quat {
/**
* @zh 根据本地坐标轴朝向计算四元数,默认三向量都已归一化且相互垂直
*/
public static fromAxes (out: quat, xAxis: vec3, yAxis: vec3, zAxis: vec3) {
public static fromAxes<Out extends quat> (out: Out, xAxis: vec3, yAxis: vec3, zAxis: vec3) {
mat3.set(m3_1,
xAxis.x, xAxis.y, xAxis.z,
yAxis.x, yAxis.y, yAxis.z,
@@ -405,7 +405,7 @@ export class quat {
* @param view 视口面向的前方向,必须归一化
* @param up 视口的上方向,必须归一化,默认为 (0, 1, 0)
*/
public static fromViewUp (out: quat, view: vec3, up?: vec3) {
public static fromViewUp<Out extends quat> (out: Out, view: vec3, up?: vec3) {
mat3.fromViewUp(m3_1, view, up);
return quat.normalize(out, quat.fromMat3(out, m3_1));
}
@@ -413,7 +413,7 @@ export class quat {
/**
* @zh 根据旋转轴和旋转弧度计算四元数
*/
public static fromAxisAngle (out: quat, axis: vec3, rad: number) {
public static fromAxisAngle<Out extends quat> (out: Out, axis: vec3, rad: number) {
rad = rad * 0.5;
const s = Math.sin(rad);
out.x = s * axis.x;
@@ -426,7 +426,7 @@ export class quat {
/**
* @zh 根据三维矩阵信息计算四元数,注意输出四元数并未归一化
*/
public static fromMat3 (out: quat, m: mat3) {
public static fromMat3<Out extends quat> (out: Out, m: mat3) {
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
const {
@@ -476,7 +476,7 @@ export class quat {
/**
* @zh 根据欧拉角信息计算四元数
*/
public static fromEuler (out: quat, x: number, y: number, z: number) {
public static fromEuler<Out extends quat> (out: Out, x: number, y: number, z: number) {
x *= halfToRad;
y *= halfToRad;
z *= halfToRad;
@@ -499,7 +499,7 @@ export class quat {
/**
* @zh 返回定义此四元数的坐标系 X 轴向量
*/
public static toAxisX (out: vec3, q: quat) {
public static toAxisX<Out extends vec3> (out: Out, q: quat) {
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
out.x = 1.0 - fy * q.y - fz * q.z;
@@ -510,7 +510,7 @@ export class quat {
/**
* @zh 返回定义此四元数的坐标系 Y 轴向量
*/
public static toAxisY (out: vec3, q: quat) {
public static toAxisY<Out extends vec3> (out: Out, q: quat) {
const fx = 2.0 * q.x;
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
@@ -522,7 +522,7 @@ export class quat {
/**
* @zh 返回定义此四元数的坐标系 Z 轴向量
*/
public static toAxisZ (out: vec3, q: quat) {
public static toAxisZ<Out extends vec3> (out: Out, q: quat) {
const fx = 2.0 * q.x;
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
@@ -534,7 +534,7 @@ export class quat {
/**
* @zh 根据四元数计算欧拉角,返回角度在 [-180, 180] 区间内
*/
public static toEuler (out: vec3, q: quat) {
public static toEuler<Out extends vec3> (out: Out, q: quat) {
const { x, y, z, w } = q;
let heading: number = NaN;
let attitude: number = NaN;
+26 -26
View File
@@ -22,7 +22,7 @@ export class vec2 {
/**
* @zh 将目标赋值为零向量
*/
public static zero (out: vec2) {
public static zero<Out extends vec2> (out: Out) {
out.x = 0;
out.y = 0;
return out;
@@ -38,7 +38,7 @@ export class vec2 {
/**
* @zh 复制目标向量
*/
public static copy (out: vec2, a: vec2) {
public static copy<Out extends vec2> (out: Out, a: vec2) {
out.x = a.x;
out.y = a.y;
return out;
@@ -47,7 +47,7 @@ export class vec2 {
/**
* @zh 设置向量值
*/
public static set (out: vec2, x: number, y: number) {
public static set<Out extends vec2> (out: Out, x: number, y: number) {
out.x = x;
out.y = y;
return out;
@@ -56,7 +56,7 @@ export class vec2 {
/**
* @zh 逐元素向量加法
*/
public static add (out: vec2, a: vec2, b: vec2) {
public static add<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = a.x + b.x;
out.y = a.y + b.y;
return out;
@@ -65,7 +65,7 @@ export class vec2 {
/**
* @zh 逐元素向量减法
*/
public static subtract (out: vec2, a: vec2, b: vec2) {
public static subtract<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = a.x - b.x;
out.y = a.y - b.y;
return out;
@@ -74,14 +74,14 @@ export class vec2 {
/**
* @zh 逐元素向量减法
*/
public static sub (out: vec2, a: vec2, b: vec2) {
public static sub<Out extends vec2> (out: Out, a: vec2, b: vec2) {
return vec2.subtract(out, a, b);
}
/**
* @zh 逐元素向量乘法
*/
public static multiply (out: vec2, a: vec2, b: vec2) {
public static multiply<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = a.x * b.x;
out.y = a.y * b.y;
return out;
@@ -90,14 +90,14 @@ export class vec2 {
/**
* @zh 逐元素向量乘法
*/
public static mul (out: vec2, a: vec2, b: vec2) {
public static mul<Out extends vec2> (out: Out, a: vec2, b: vec2) {
return vec2.multiply(out, a, b);
}
/**
* @zh 逐元素向量除法
*/
public static divide (out: vec2, a: vec2, b: vec2) {
public static divide<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = a.x / b.x;
out.y = a.y / b.y;
return out;
@@ -106,14 +106,14 @@ export class vec2 {
/**
* @zh 逐元素向量除法
*/
public static div (out: vec2, a: vec2, b: vec2) {
public static div<Out extends vec2> (out: Out, a: vec2, b: vec2) {
return vec2.divide(out, a, b);
}
/**
* @zh 逐元素向量向上取整
*/
public static ceil (out: vec2, a: vec2) {
public static ceil<Out extends vec2> (out: Out, a: vec2) {
out.x = Math.ceil(a.x);
out.y = Math.ceil(a.y);
return out;
@@ -122,7 +122,7 @@ export class vec2 {
/**
* @zh 逐元素向量向下取整
*/
public static floor (out: vec2, a: vec2) {
public static floor<Out extends vec2> (out: Out, a: vec2) {
out.x = Math.floor(a.x);
out.y = Math.floor(a.y);
return out;
@@ -131,7 +131,7 @@ export class vec2 {
/**
* @zh 逐元素向量最小值
*/
public static min (out: vec2, a: vec2, b: vec2) {
public static min<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = Math.min(a.x, b.x);
out.y = Math.min(a.y, b.y);
return out;
@@ -140,7 +140,7 @@ export class vec2 {
/**
* @zh 逐元素向量最大值
*/
public static max (out: vec2, a: vec2, b: vec2) {
public static max<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = Math.max(a.x, b.x);
out.y = Math.max(a.y, b.y);
return out;
@@ -149,7 +149,7 @@ export class vec2 {
/**
* @zh 逐元素向量四舍五入取整
*/
public static round (out: vec2, a: vec2) {
public static round<Out extends vec2> (out: Out, a: vec2) {
out.x = Math.round(a.x);
out.y = Math.round(a.y);
return out;
@@ -158,7 +158,7 @@ export class vec2 {
/**
* @zh 向量标量乘法
*/
public static scale (out: vec2, a: vec2, b: number) {
public static scale<Out extends vec2> (out: Out, a: vec2, b: number) {
out.x = a.x * b;
out.y = a.y * b;
return out;
@@ -167,7 +167,7 @@ export class vec2 {
/**
* @zh 逐元素向量乘加: A + B * scale
*/
public static scaleAndAdd (out: vec2, a: vec2, b: vec2, scale: number) {
public static scaleAndAdd<Out extends vec2> (out: Out, a: vec2, b: vec2, scale: number) {
out.x = a.x + (b.x * scale);
out.y = a.y + (b.y * scale);
return out;
@@ -238,7 +238,7 @@ export class vec2 {
/**
* @zh 逐元素向量取负
*/
public static negate (out: vec2, a: vec2) {
public static negate<Out extends vec2> (out: Out, a: vec2) {
out.x = -a.x;
out.y = -a.y;
return out;
@@ -247,7 +247,7 @@ export class vec2 {
/**
* @zh 逐元素向量取倒数,接近 0 时返回 Infinity
*/
public static inverse (out: vec2, a: vec2) {
public static inverse<Out extends vec2> (out: Out, a: vec2) {
out.x = 1.0 / a.x;
out.y = 1.0 / a.y;
return out;
@@ -256,7 +256,7 @@ export class vec2 {
/**
* @zh 逐元素向量取倒数,接近 0 时返回 0
*/
public static inverseSafe (out: vec2, a: vec2) {
public static inverseSafe<Out extends vec2> (out: Out, a: vec2) {
const { x, y } = a;
if (Math.abs(x) < EPSILON) {
@@ -277,7 +277,7 @@ export class vec2 {
/**
* @zh 归一化向量
*/
public static normalize (out: vec2, a: vec2) {
public static normalize<Out extends vec2> (out: Out, a: vec2) {
const { x, y } = a;
let len = x * x + y * y;
if (len > 0) {
@@ -298,7 +298,7 @@ export class vec2 {
/**
* @zh 向量叉积(向量积),注意二维向量的叉积为与 Z 轴平行的三维向量
*/
public static cross (out: vec3, a: vec2, b: vec2) {
public static cross<Out extends vec3> (out: Out, a: vec2, b: vec2) {
out.x = out.y = 0;
out.z = a.x * b.y - a.y * b.x;
return out;
@@ -307,7 +307,7 @@ export class vec2 {
/**
* @zh 逐元素向量线性插值: A + t * (B - A)
*/
public static lerp (out: vec2, a: vec2, b: vec2, t: number) {
public static lerp<Out extends vec2> (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);
@@ -318,7 +318,7 @@ export class vec2 {
* @zh 生成一个在单位圆上均匀分布的随机向量
* @param scale 生成的向量长度
*/
public static random (out: vec2, scale?: number) {
public static random<Out extends vec2> (out: Out, scale?: number) {
scale = scale || 1.0;
const r = random() * 2.0 * Math.PI;
out.x = Math.cos(r) * scale;
@@ -329,7 +329,7 @@ export class vec2 {
/**
* @zh 向量与三维矩阵乘法,默认向量第三位为 1。
*/
public static transformMat3 (out: vec2, a: vec2, m: mat3) {
public static transformMat3<Out extends vec2> (out: Out, a: vec2, m: mat3) {
const { x, y } = a;
out.x = m.m00 * x + m.m03 * y + m.m06;
out.y = m.m01 * x + m.m04 * y + m.m07;
@@ -339,7 +339,7 @@ export class vec2 {
/**
* @zh 向量与四维矩阵乘法,默认向量第三位为 0,第四位为 1。
*/
public static transformMat4 (out: vec2, a: vec2, m: mat4) {
public static transformMat4<Out extends vec2> (out: Out, a: vec2, m: mat4) {
const { x, y } = a;
out.x = m.m00 * x + m.m04 * y + m.m12;
out.y = m.m01 * x + m.m05 * y + m.m13;
+33 -33
View File
@@ -25,7 +25,7 @@ export class vec3 {
/**
* @zh 将目标赋值为零向量
*/
public static zero (out: vec3) {
public static zero<Out extends vec3> (out: Out) {
out.x = 0;
out.y = 0;
out.z = 0;
@@ -42,7 +42,7 @@ export class vec3 {
/**
* @zh 复制目标向量
*/
public static copy (out: vec3, a: vec3) {
public static copy<Out extends vec3> (out: Out, a: vec3) {
out.x = a.x;
out.y = a.y;
out.z = a.z;
@@ -52,7 +52,7 @@ export class vec3 {
/**
* @zh 设置向量值
*/
public static set (out: vec3, x: number, y: number, z: number) {
public static set<Out extends vec3> (out: Out, x: number, y: number, z: number) {
out.x = x;
out.y = y;
out.z = z;
@@ -62,7 +62,7 @@ export class vec3 {
/**
* @zh 逐元素向量加法
*/
public static add (out: vec3, a: vec3, b: vec3) {
public static add<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = a.x + b.x;
out.y = a.y + b.y;
out.z = a.z + b.z;
@@ -72,7 +72,7 @@ export class vec3 {
/**
* @zh 逐元素向量减法
*/
public static subtract (out: vec3, a: vec3, b: vec3) {
public static subtract<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = a.x - b.x;
out.y = a.y - b.y;
out.z = a.z - b.z;
@@ -82,14 +82,14 @@ export class vec3 {
/**
* @zh 逐元素向量减法
*/
public static sub (out: vec3, a: vec3, b: vec3) {
public static sub<Out extends vec3> (out: Out, a: vec3, b: vec3) {
return vec3.subtract(out, a, b);
}
/**
* @zh 逐元素向量乘法
*/
public static multiply (out: vec3, a: vec3, b: vec3) {
public static multiply<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = a.x * b.x;
out.y = a.y * b.y;
out.z = a.z * b.z;
@@ -99,14 +99,14 @@ export class vec3 {
/**
* @zh 逐元素向量乘法
*/
public static mul (out: vec3, a: vec3, b: vec3) {
public static mul<Out extends vec3> (out: Out, a: vec3, b: vec3) {
return vec3.multiply(out, a, b);
}
/**
* @zh 逐元素向量除法
*/
public static divide (out: vec3, a: vec3, b: vec3) {
public static divide<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = a.x / b.x;
out.y = a.y / b.y;
out.z = a.z / b.z;
@@ -116,14 +116,14 @@ export class vec3 {
/**
* @zh 逐元素向量除法
*/
public static div (out: vec3, a: vec3, b: vec3) {
public static div<Out extends vec3> (out: Out, a: vec3, b: vec3) {
return vec3.divide(out, a, b);
}
/**
* @zh 逐元素向量向上取整
*/
public static ceil (out: vec3, a: vec3) {
public static ceil<Out extends vec3> (out: Out, a: vec3) {
out.x = Math.ceil(a.x);
out.y = Math.ceil(a.y);
out.z = Math.ceil(a.z);
@@ -133,7 +133,7 @@ export class vec3 {
/**
* @zh 逐元素向量向下取整
*/
public static floor (out: vec3, a: vec3) {
public static floor<Out extends vec3> (out: Out, a: vec3) {
out.x = Math.floor(a.x);
out.y = Math.floor(a.y);
out.z = Math.floor(a.z);
@@ -143,7 +143,7 @@ export class vec3 {
/**
* @zh 逐元素向量最小值
*/
public static min (out: vec3, a: vec3, b: vec3) {
public static min<Out extends vec3> (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);
@@ -153,7 +153,7 @@ export class vec3 {
/**
* @zh 逐元素向量最大值
*/
public static max (out: vec3, a: vec3, b: vec3) {
public static max<Out extends vec3> (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);
@@ -163,7 +163,7 @@ export class vec3 {
/**
* @zh 逐元素向量四舍五入取整
*/
public static round (out: vec3, a: vec3) {
public static round<Out extends vec3> (out: Out, a: vec3) {
out.x = Math.round(a.x);
out.y = Math.round(a.y);
out.z = Math.round(a.z);
@@ -173,7 +173,7 @@ export class vec3 {
/**
* @zh 向量标量乘法
*/
public static scale (out: vec3, a: vec3, b: number) {
public static scale<Out extends vec3> (out: Out, a: vec3, b: number) {
out.x = a.x * b;
out.y = a.y * b;
out.z = a.z * b;
@@ -183,7 +183,7 @@ export class vec3 {
/**
* @zh 逐元素向量乘加: A + B * scale
*/
public static scaleAndAdd (out: vec3, a: vec3, b: vec3, scale: number) {
public static scaleAndAdd<Out extends vec3> (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;
@@ -257,7 +257,7 @@ export class vec3 {
/**
* @zh 逐元素向量取负
*/
public static negate (out: vec3, a: vec3) {
public static negate<Out extends vec3> (out: Out, a: vec3) {
out.x = -a.x;
out.y = -a.y;
out.z = -a.z;
@@ -267,7 +267,7 @@ export class vec3 {
/**
* @zh 逐元素向量取倒数,接近 0 时返回 Infinity
*/
public static invert (out: vec3, a: vec3) {
public static invert<Out extends vec3> (out: Out, a: vec3) {
out.x = 1.0 / a.x;
out.y = 1.0 / a.y;
out.z = 1.0 / a.z;
@@ -277,7 +277,7 @@ export class vec3 {
/**
* @zh 逐元素向量取倒数,接近 0 时返回 0
*/
public static invertSafe (out: vec3, a: vec3) {
public static invertSafe<Out extends vec3> (out: Out, a: vec3) {
const { x, y, z } = a;
if (Math.abs(x) < EPSILON) {
@@ -304,7 +304,7 @@ export class vec3 {
/**
* @zh 归一化向量
*/
public static normalize (out: vec3, a: vec3) {
public static normalize<Out extends vec3> (out: Out, a: vec3) {
const { x, y, z } = a;
let len = x * x + y * y + z * z;
@@ -327,7 +327,7 @@ export class vec3 {
/**
* @zh 向量叉积(向量积)
*/
public static cross (out: vec3, a: vec3, b: vec3) {
public static cross<Out extends vec3> (out: Out, a: vec3, b: vec3) {
const { x: ax, y: ay, z: az } = a;
const { x: bx, y: by, z: bz } = b;
@@ -340,7 +340,7 @@ export class vec3 {
/**
* @zh 逐元素向量线性插值: A + t * (B - A)
*/
public static lerp (out: vec3, a: vec3, b: vec3, t: number) {
public static lerp<Out extends vec3> (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);
@@ -352,7 +352,7 @@ export class vec3 {
* @zh 生成一个在单位球体上均匀分布的随机向量
* @param scale 生成的向量长度
*/
public static random (out: vec3, scale?: number) {
public static random<Out extends vec3> (out: Out, scale?: number) {
scale = scale || 1.0;
const phi = random() * 2.0 * Math.PI;
@@ -368,7 +368,7 @@ export class vec3 {
/**
* @zh 向量与四维矩阵乘法,默认向量第四位为 1。
*/
public static transformMat4 (out: vec3, a: vec3, m: mat4) {
public static transformMat4<Out extends vec3> (out: Out, a: vec3, m: mat4) {
const { x, y, z } = a;
let rhw = m.m03 * x + m.m07 * y + m.m11 * z + m.m15;
rhw = rhw ? 1 / rhw : 1;
@@ -381,7 +381,7 @@ export class vec3 {
/**
* @zh 向量与四维矩阵乘法,默认向量第四位为 0。
*/
public static transformMat4Normal (out: vec3, a: vec3, m: mat4) {
public static transformMat4Normal<Out extends vec3> (out: Out, a: vec3, m: mat4) {
const { x, y, z } = a;
let rhw = m.m03 * x + m.m07 * y + m.m11 * z;
rhw = rhw ? 1 / rhw : 1;
@@ -394,7 +394,7 @@ export class vec3 {
/**
* @zh 向量与三维矩阵乘法
*/
public static transformMat3 (out: vec3, a: vec3, m: mat3) {
public static transformMat3<Out extends vec3> (out: Out, a: vec3, m: mat3) {
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;
@@ -405,7 +405,7 @@ export class vec3 {
/**
* @zh 向量四元数乘法
*/
public static transformQuat (out: vec3, a: vec3, q: quat) {
public static transformQuat<Out extends vec3> (out: Out, a: vec3, q: quat) {
// benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
const { x, y, z } = a;
@@ -430,7 +430,7 @@ export class vec3 {
* @param o 旋转中心
* @param a 旋转弧度
*/
public static rotateX (out: vec3, v: vec3, o: vec3, a: number) {
public static rotateX<Out extends vec3> (out: Out, v: vec3, o: vec3, a: number) {
// Translate point to the origin
const px = v.x - o.x;
const py = v.y - o.y;
@@ -457,7 +457,7 @@ export class vec3 {
* @param o 旋转中心
* @param a 旋转弧度
*/
public static rotateY (out: vec3, v: vec3, o: vec3, a: number) {
public static rotateY<Out extends vec3> (out: Out, v: vec3, o: vec3, a: number) {
// Translate point to the origin
const px = v.x - o.x;
const py = v.y - o.y;
@@ -484,7 +484,7 @@ export class vec3 {
* @param o 旋转中心
* @param a 旋转弧度
*/
public static rotateZ (out: vec3, v: vec3, o: vec3, a: number) {
public static rotateZ<Out extends vec3> (out: Out, v: vec3, o: vec3, a: number) {
// Translate point to the origin
const px = v.x - o.x;
const py = v.y - o.y;
@@ -568,7 +568,7 @@ export class vec3 {
* @param a 待投影向量
* @param n 指定平面的法线
*/
public static projectOnPlane (out: vec3, a: vec3, n: vec3) {
public static projectOnPlane<Out extends vec3> (out: Out, a: vec3, n: vec3) {
return vec3.subtract(out, a, vec3.project(out, a, n));
}
@@ -577,7 +577,7 @@ export class vec3 {
* @param a 待投影向量
* @param n 目标向量
*/
public static project (out: vec3, a: vec3, b: vec3) {
public static project<Out extends vec3> (out: Out, a: vec3, b: vec3) {
const sqrLen = vec3.squaredMagnitude(b);
if (sqrLen < 0.000001) {
return vec3.set(out, 0, 0, 0);
+25 -25
View File
@@ -21,7 +21,7 @@ export class vec4 {
/**
* @zh 将目标赋值为零向量
*/
public static zero (out: vec4) {
public static zero<Out extends vec4> (out: Out) {
out.x = 0;
out.y = 0;
out.z = 0;
@@ -39,7 +39,7 @@ export class vec4 {
/**
* @zh 复制目标向量
*/
public static copy (out: vec4, a: vec4) {
public static copy<Out extends vec4> (out: Out, a: vec4) {
out.x = a.x;
out.y = a.y;
out.z = a.z;
@@ -50,7 +50,7 @@ export class vec4 {
/**
* @zh 设置向量值
*/
public static set (out: vec4, x: number, y: number, z: number, w: number) {
public static set<Out extends vec4> (out: Out, x: number, y: number, z: number, w: number) {
out.x = x;
out.y = y;
out.z = z;
@@ -61,7 +61,7 @@ export class vec4 {
/**
* @zh 逐元素向量加法
*/
public static add (out: vec4, a: vec4, b: vec4) {
public static add<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = a.x + b.x;
out.y = a.y + b.y;
out.z = a.z + b.z;
@@ -72,7 +72,7 @@ export class vec4 {
/**
* @zh 逐元素向量减法
*/
public static subtract (out: vec4, a: vec4, b: vec4) {
public static subtract<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = a.x - b.x;
out.y = a.y - b.y;
out.z = a.z - b.z;
@@ -83,14 +83,14 @@ export class vec4 {
/**
* @zh 逐元素向量减法
*/
public static sub (out: vec4, a: vec4, b: vec4) {
public static sub<Out extends vec4> (out: Out, a: vec4, b: vec4) {
return vec4.subtract(out, a, b);
}
/**
* @zh 逐元素向量乘法
*/
public static multiply (out: vec4, a: vec4, b: vec4) {
public static multiply<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = a.x * b.x;
out.y = a.y * b.y;
out.z = a.z * b.z;
@@ -101,14 +101,14 @@ export class vec4 {
/**
* @zh 逐元素向量乘法
*/
public static mul (out: vec4, a: vec4, b: vec4) {
public static mul<Out extends vec4> (out: Out, a: vec4, b: vec4) {
return vec4.multiply(out, a, b);
}
/**
* @zh 逐元素向量除法
*/
public static divide (out: vec4, a: vec4, b: vec4) {
public static divide<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = a.x / b.x;
out.y = a.y / b.y;
out.z = a.z / b.z;
@@ -119,14 +119,14 @@ export class vec4 {
/**
* @zh 逐元素向量除法
*/
public static div (out: vec4, a: vec4, b: vec4) {
public static div<Out extends vec4> (out: Out, a: vec4, b: vec4) {
return vec4.divide(out, a, b);
}
/**
* @zh 逐元素向量向上取整
*/
public static ceil (out: vec4, a: vec4) {
public static ceil<Out extends vec4> (out: Out, a: vec4) {
out.x = Math.ceil(a.x);
out.y = Math.ceil(a.y);
out.z = Math.ceil(a.z);
@@ -137,7 +137,7 @@ export class vec4 {
/**
* @zh 逐元素向量向下取整
*/
public static floor (out: vec4, a: vec4) {
public static floor<Out extends vec4> (out: Out, a: vec4) {
out.x = Math.floor(a.x);
out.y = Math.floor(a.y);
out.z = Math.floor(a.z);
@@ -148,7 +148,7 @@ export class vec4 {
/**
* @zh 逐元素向量最小值
*/
public static min (out: vec4, a: vec4, b: vec4) {
public static min<Out extends vec4> (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);
@@ -159,7 +159,7 @@ export class vec4 {
/**
* @zh 逐元素向量最大值
*/
public static max (out: vec4, a: vec4, b: vec4) {
public static max<Out extends vec4> (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);
@@ -170,7 +170,7 @@ export class vec4 {
/**
* @zh 逐元素向量四舍五入取整
*/
public static round (out: vec4, a: vec4) {
public static round<Out extends vec4> (out: Out, a: vec4) {
out.x = Math.round(a.x);
out.y = Math.round(a.y);
out.z = Math.round(a.z);
@@ -181,7 +181,7 @@ export class vec4 {
/**
* @zh 向量标量乘法
*/
public static scale (out: vec4, a: vec4, b: number) {
public static scale<Out extends vec4> (out: Out, a: vec4, b: number) {
out.x = a.x * b;
out.y = a.y * b;
out.z = a.z * b;
@@ -192,7 +192,7 @@ export class vec4 {
/**
* @zh 逐元素向量乘加: A + B * scale
*/
public static scaleAndAdd (out: vec4, a: vec4, b: vec4, scale: number) {
public static scaleAndAdd<Out extends vec4> (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);
@@ -269,7 +269,7 @@ export class vec4 {
/**
* @zh 逐元素向量取负
*/
public static negate (out: vec4, a: vec4) {
public static negate<Out extends vec4> (out: Out, a: vec4) {
out.x = -a.x;
out.y = -a.y;
out.z = -a.z;
@@ -280,7 +280,7 @@ export class vec4 {
/**
* @zh 逐元素向量取倒数,接近 0 时返回 Infinity
*/
public static inverse (out: vec4, a: vec4) {
public static inverse<Out extends vec4> (out: Out, a: vec4) {
out.x = 1.0 / a.x;
out.y = 1.0 / a.y;
out.z = 1.0 / a.z;
@@ -291,7 +291,7 @@ export class vec4 {
/**
* @zh 逐元素向量取倒数,接近 0 时返回 0
*/
public static inverseSafe (out: vec4, a: vec4) {
public static inverseSafe<Out extends vec4> (out: Out, a: vec4) {
const { x, y, z, w } = a;
if (Math.abs(x) < EPSILON) {
@@ -324,7 +324,7 @@ export class vec4 {
/**
* @zh 归一化向量
*/
public static normalize (out: vec4, a: vec4) {
public static normalize<Out extends vec4> (out: Out, a: vec4) {
const { x, y, z, w } = a;
let len = x * x + y * y + z * z + w * w;
if (len > 0) {
@@ -347,7 +347,7 @@ export class vec4 {
/**
* @zh 逐元素向量线性插值: A + t * (B - A)
*/
public static lerp (out: vec4, a: vec4, b: vec4, t: number) {
public static lerp<Out extends vec4> (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);
@@ -360,7 +360,7 @@ export class vec4 {
* @zh 生成一个在单位球体上均匀分布的随机向量
* @param scale 生成的向量长度
*/
public static random (out: vec4, scale?: number) {
public static random<Out extends vec4> (out: Out, scale?: number) {
scale = scale || 1.0;
const phi = random() * 2.0 * Math.PI;
@@ -377,7 +377,7 @@ export class vec4 {
/**
* @zh 向量矩阵乘法
*/
public static transformMat4 (out: vec4, a: vec4, m: mat4) {
public static transformMat4<Out extends vec4> (out: Out, a: vec4, m: mat4) {
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;
@@ -389,7 +389,7 @@ export class vec4 {
/**
* @zh 向量四元数乘法
*/
public static transformQuat (out: vec4, a: vec4, q: quat) {
public static transformQuat<Out extends vec4> (out: Out, a: vec4, q: quat) {
const { x, y, z } = a;
const { x: qx, y: qy, z: qz, w: qw } = q;
+1 -1
View File
@@ -30,7 +30,7 @@ export interface IGFXCommandBufferInfo {
/**
* @zh
* GFX深度便宜。
* GFX深度偏移。
*/
export interface IGFXDepthBias {
constantFactor: number;
+4 -4
View File
@@ -1,11 +1,11 @@
export enum RenderQueue {
OPAQUE = 0,
TRANSPARENT = 1,
OVERLAY = 2
};
OVERLAY = 2,
}
export enum PassStage {
DEFAULT = 1,
FORWARD = 2,
SHADOWCAST = 4
};
SHADOWCAST = 4,
}
+91 -8
View File
@@ -1,10 +1,11 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
import { Skeleton } from '../../3d/assets/skeleton';
import { LCA } from '../../3d/misc/utils';
import { Filter, PixelFormat, WrapMode } from '../../assets/asset-enum';
import { Texture2D } from '../../assets/texture-2d';
import { Mat4, Quat, Vec3 } from '../../core/value-types';
import { mat4, quat, vec4 } from '../../core/vmath';
import { mat4, quat, vec3 } from '../../core/vmath';
import { GFXBuffer } from '../../gfx/buffer';
import { GFXBufferUsageBit, GFXFormatInfos, GFXMemoryUsageBit } from '../../gfx/define';
import { GFXDevice, GFXFeature } from '../../gfx/device';
@@ -46,15 +47,61 @@ const _jointsFormat = {
[JointsMediumType.RGBA32F]: PixelFormat.RGBA32F,
};
const m4_1 = new Mat4();
const v3_1 = new Vec3();
const v3_2 = new Vec3();
const qt_0 = new Quat();
const qt_1 = new Quat();
const qt_2 = new Quat();
const m4_1 = new Mat4();
const f4_1 = new Float32Array(4);
export class Joint {
public node: Node;
public position: Vec3 = new Vec3();
public rotation: Quat = new Quat();
public scale: Vec3 = new Vec3(1, 1, 1);
public parent: Joint | null = null;
protected _lastUpdate = -1;
constructor (node: Node) { this.node = node; }
public update () {
const totalFrames = cc.director.getTotalFrames();
if (this._lastUpdate >= totalFrames) { return; }
this._lastUpdate = totalFrames;
const parent = this.parent;
if (parent) {
parent.update();
vec3.multiply(this.position, this.node.position, parent.scale);
vec3.transformQuat(this.position, this.position, parent.rotation);
vec3.add(this.position, this.position, parent.position);
quat.multiply(this.rotation, parent.rotation, this.node.rotation);
vec3.multiply(this.scale, parent.scale, this.node.scale);
}
}
}
class JointManager {
public static get (node: Node, root: Node) {
let joint = JointManager._joints.get(node);
if (joint) { return joint; }
joint = new Joint(node);
if (node !== root && node.parent) { joint.parent = JointManager.get(node.parent, root); }
JointManager._joints.set(node, joint);
return joint;
}
protected static _joints: Map<Node, Joint> = new Map();
}
export class SkinningModel extends Model {
// change here and cc-skinning.inc to use other skinning algorithms
public updateJointData = this.updateJointDataDQS;
get joints () {
return this._joints;
}
private _skeleton: Skeleton | null = null;
private _joints: Joint[] = [];
private _jointsMedium: IJointsInfo | null = null;
constructor (scene: RenderScene, node: Node) {
@@ -65,6 +112,7 @@ export class SkinningModel extends Model {
public bindSkeleton (skeleton: Skeleton | null) {
this._destroyJointsMedium();
this._skeleton = skeleton;
if (!skeleton) { return; }
const type = selectJointsMediumType(this._device, skeleton.joints.length);
const format = _jointsFormat[type];
@@ -104,6 +152,41 @@ export class SkinningModel extends Model {
}
}
public updateUBOs () {
if (!super.updateUBOs() || !this._skeleton) { return false; }
const len = this._joints.length;
for (let i = 0; i < len; ++i) {
const cur = this._joints[i]; cur.update();
const bindpose = this._skeleton.bindposes[i];
vec3.multiply(v3_1, bindpose.position, cur.scale);
vec3.transformQuat(v3_1, v3_1, cur.rotation);
vec3.add(v3_1, v3_1, cur.position);
quat.multiply(qt_1, cur.rotation, bindpose.rotation);
vec3.multiply(v3_2, cur.scale, bindpose.scale);
this.updateJointData(i, v3_1, qt_1, v3_2, i === 0);
}
this.commitJointData();
return true;
}
public resetSkinningTarget (skinningRoot: Node) {
const root = LCA(this.node, skinningRoot);
if (!root || !this._skeleton) { return; }
this._joints.length = 0;
this._skeleton.joints.forEach((path) => {
const targetNode = skinningRoot.getChildByPath(path);
if (!targetNode) {
console.warn(`Skinning target ${path} not found in scene graph.`);
return;
}
this._joints.push(JointManager.get(targetNode, root));
});
}
// Linear Blending Skinning
protected updateJointDataLBS (idx: number, pos: Vec3, rot: Quat, scale: Vec3) {
if (!this._jointsMedium) { return; }
@@ -133,17 +216,17 @@ export class SkinningModel extends Model {
if (first) { quat.copy(qt_0, rot); }
else if (quat.dot(qt_0, rot) < 0) { quat.scale(rot, rot, -1); }
// conversion
quat.set(qt_1, pos.x, pos.y, pos.z, 0);
quat.scale(qt_1, quat.multiply(qt_1, qt_1, rot), 0.5);
quat.set(qt_2, pos.x, pos.y, pos.z, 0);
quat.scale(qt_2, quat.multiply(qt_2, qt_2, rot), 0.5);
// upload
out[base + 0] = rot.x;
out[base + 1] = rot.y;
out[base + 2] = rot.z;
out[base + 3] = rot.w;
out[base + 4] = qt_1.x;
out[base + 5] = qt_1.y;
out[base + 6] = qt_1.z;
out[base + 7] = qt_1.w;
out[base + 4] = qt_2.x;
out[base + 5] = qt_2.y;
out[base + 6] = qt_2.z;
out[base + 7] = qt_2.w;
out[base + 8] = scale.x;
out[base + 9] = scale.y;
out[base + 10] = scale.z;
-1
View File
@@ -1,4 +1,3 @@
import { vec3 } from '../../core/vmath';
import { RenderScene } from './render-scene';
export class Ambient {
+1 -2
View File
@@ -1,11 +1,10 @@
import { frustum, ray } from '../../3d/geom-utils';
import { Mat4, Rect, Vec3 } from '../../core/value-types';
import { lerp, mat4, toRadian, vec3, color4 } from '../../core/vmath';
import { color4, lerp, mat4, toRadian, vec3 } from '../../core/vmath';
import { GFXClearFlag, IGFXColor } from '../../gfx/define';
import { RenderView } from '../../pipeline/render-view';
import { Node } from '../../scene-graph/node';
import { RenderScene } from './render-scene';
import { max, min } from '../../core/vmath/bits';
export enum CameraProjection {
ORTHO,
@@ -33,19 +33,55 @@ class Customization {
}
}
/**
* @zh
* 材质在挂载到模型时的定制回调,一般用于显示相关的特殊逻辑,<br>
* 作为渲染管线的定制不够灵活时的最后备用路线,或初期快速实现思路原型的小工具。<br>
* 因为书写回调函数时不可避免地会对内部渲染流程做一部分 assumption,不建议作为商业项目的正式依赖。
* @example
* ```ts
* cc.customizationManager.register('no-frustum-culling', {
* onAttach: (m) => {
* m.worldBoundsBak = m.worldBounds;
* m.modelBoundsBak = m.modelBounds;
* m._modelBounds = m._worldBounds = null;
* },
* onDetach: (m) => {
* m._worldBounds = m.worldBoundsBak;
* m._modelBounds = m.modelBoundsBak;
* delete m.modelBoundsBak; delete m.worldBoundsBak;
* },
* });
* ```
*/
class CustomizationManager {
protected _customs: Record<string, Customization> = {};
/**
* @zh 注册新的定制回调
* @param name 此定制回调名
* @param info 具体定制回调函数
*/
public register (name: string, info: ICustomization) {
this._customs[name] = new Customization(info);
}
/**
* @zh 材质挂载到 model 时,会根据 pass 内信息调用此函数
* @param name 目标定制回调
* @param model 源模型
*/
public attach (name: string, model: Model) {
const cus = this._customs[name];
if (!cus) { console.warn(`no customization named '${name}'`); return; }
cus.attach(model);
}
/**
* @zh 在材质与 model 分离时,会根据 pass 内信息调用此函数
* @param name 目标定制回调
* @param model 源模型
*/
public detach (name: string, model: Model) {
const cus = this._customs[name];
if (!cus) { console.warn(`no customization named '${name}'`); return; }
+4 -3
View File
@@ -197,9 +197,9 @@ export class Model {
const node = this._transform;
if (!node.hasChanged) { return; }
node.updateWorldTransformFull();
if (!this._modelBounds) { return; }
if (!this._modelBounds || !this._worldBounds) { return; }
// @ts-ignore
this._modelBounds.transform(node._mat, node._pos, node._rot, node._scale, this._worldBounds!);
this._modelBounds.transform(node._mat, node._pos, node._rot, node._scale, this._worldBounds);
}
public _resetUBOUpdateFlag () {
@@ -208,7 +208,7 @@ export class Model {
public updateUBOs () {
if (this._uboUpdated) {
return;
return false;
}
this._uboUpdated = true;
// @ts-ignore
@@ -232,6 +232,7 @@ export class Model {
pso.pipelineLayout.layouts[0].update();
}
}
return true;
}
/**
+20 -20
View File
@@ -26,7 +26,7 @@ const TRANFORM_ON = 1 << 0;
* 场景树中的基本节点,基本特性有:
* * 具有层级关系
* * 持有各类组件
* * 维护空间变换(位移、旋转、缩放)信息
* * 维护空间变换(坐标、旋转、缩放)信息
*/
@ccclass('cc.Node')
export class Node extends BaseNode {
@@ -332,18 +332,18 @@ export class Node extends BaseNode {
/**
* @zh
* 设置本地位移
* @param position 目标本地位移
* 设置本地坐标
* @param position 目标本地坐标
*/
public setPosition (position: Vec3): void;
/**
* @zh
* 设置本地位移
* @param x 目标本地位移的 X 分量
* @param y 目标本地位移的 Y 分量
* @param z 目标本地位移的 Z 分量
* @param w 目标本地位移的 W 分量
* 设置本地坐标
* @param x 目标本地坐标的 X 分量
* @param y 目标本地坐标的 Y 分量
* @param z 目标本地坐标的 Z 分量
* @param w 目标本地坐标的 W 分量
*/
public setPosition (x: number, y: number, z: number): void;
@@ -364,7 +364,7 @@ export class Node extends BaseNode {
/**
* @zh
* 获取本地位移
* 获取本地坐标
* @param out 输出到此目标 vector
*/
public getPosition (out?: Vec3): Vec3 {
@@ -377,7 +377,7 @@ export class Node extends BaseNode {
/**
* @zh
* 本地位移
* 本地坐标
*/
@constget
public get position (): Readonly<Vec3> {
@@ -518,18 +518,18 @@ export class Node extends BaseNode {
/**
* @zh
* 设置世界位移
* @param position 目标世界位移
* 设置世界坐标
* @param position 目标世界坐标
*/
public setWorldPosition (position: Vec3): void;
/**
* @zh
* 设置世界位移
* @param x 目标世界位移的 X 分量
* @param y 目标世界位移的 Y 分量
* @param z 目标世界位移的 Z 分量
* @param w 目标世界位移的 W 分量
* 设置世界坐标
* @param x 目标世界坐标的 X 分量
* @param y 目标世界坐标的 Y 分量
* @param z 目标世界坐标的 Z 分量
* @param w 目标世界坐标的 W 分量
*/
public setWorldPosition (x: number, y: number, z: number): void;
@@ -559,7 +559,7 @@ export class Node extends BaseNode {
/**
* @zh
* 获取世界位移
* 获取世界坐标
* @param out 输出到此目标 vector
*/
public getWorldPosition (out?: Vec3): Vec3 {
@@ -573,7 +573,7 @@ export class Node extends BaseNode {
/**
* @zh
* 世界位移
* 世界坐标
*/
public get worldPosition (): Readonly<Vec3> {
this.updateWorldTransform();
@@ -765,7 +765,7 @@ export class Node extends BaseNode {
/**
* @zh
* 获取只包含位移和旋转的世界变换矩阵
* 获取只包含坐标和旋转的世界变换矩阵
* @param out 输出到此目标矩阵
*/
public getWorldRT (out?: Mat4): Mat4 {