Add some documents for gemo-util (#4614)

* Add some documents for gemo-util

* Add docments for distance
This commit is contained in:
jiaxin.lai
2019-06-13 17:33:31 +08:00
committed by Weiliang Wang
parent 4a39ee361f
commit 8a86e39151
11 changed files with 727 additions and 384 deletions
@@ -290,9 +290,9 @@ class SharedRigidBody {
// 开始物理计算之前,用户脚本或引擎功能有可能改变节点的Transform,所以需要判断并进行更新
if (this._node.hasChanged) {
// scale 进行单独判断,因为目前的物理系统处理后不会改变scale的属性
if (!vec3.equals(this._prevScale, this._node._scale)) {
this._body.scaleAllShapes(this._node._scale);
vec3.copy(this._prevScale, this._node._scale);
if (!vec3.equals(this._prevScale, this._node.worldScale)) {
this._body.scaleAllShapes(this._node.worldScale);
vec3.copy(this._prevScale, this._node.worldScale);
}
this.syncPhysWithScene(this._node);
}
+116 -53
View File
@@ -15,33 +15,45 @@ const transform_extent_m4 = (out: vec3, extent: vec3, m4: mat4) => {
vec3.transformMat3(out, extent, _m3_tmp);
};
/**
* @zh
* 基础几何 轴对齐包围盒
*/
// tslint:disable-next-line: class-name
export default class aabb {
/**
* @zh
* 获取形状的类型
*/
get type () {
return this._type;
}
/**
* @en
* create a new aabb
*
* @param px - X coordinates for aabb's original point
* @param py - Y coordinates for aabb's original point
* @param pz - Z coordinates for aabb's original point
* @param w - the half of aabb width
* @param h - the half of aabb height
* @param l - the half of aabb length
* @returns the resulting aabb
* @zh
* 创建一个新的 aabb 实例
* @param px - aabb 的原点的 X 坐标
* @param py - aabb 的原点的 Y 坐标
* @param pz - aabb 的原点的 Z 坐标
* @param hw - aabb 宽度的一半
* @param hh - aabb 高度的一半
* @param hl - aabb 长度的一半
* @returns 返回新创建的 aabb 实例
*/
public static create (px?: number, py?: number, pz?: number, w?: number, h?: number, l?: number) {
return new aabb(px, py, pz, w, h, l);
public static create (px?: number, py?: number, pz?: number, hw?: number, hh?: number, hl?: number) {
return new aabb(px, py, pz, hw, hh, hl);
}
/**
* @en
* clone a new aabb
*
* @param a - the source aabb
* @returns the cloned aabb
* @zh
* 克隆一个 aabb
* @param a - 克隆的目标
* @returns 克隆出的 aabb
*/
public static clone (a: aabb) {
return new aabb(a.center.x, a.center.y, a.center.z,
@@ -49,11 +61,13 @@ export default class aabb {
}
/**
* @en
* copy the values from one aabb to another
*
* @param {aabb} out the receiving aabb
* @param {aabb} a the source aabb
* @return {aabb}
* @zh
* 将从一个 aabb 的值复制到另一个 aabb
* @param {aabb} out 接受操作的 aabb
* @param {aabb} a 被复制的 aabb
* @return {aabb} out 接受操作的 aabb
*/
public static copy (out: aabb, a: aabb): aabb {
vec3.copy(out.center, a.center);
@@ -63,39 +77,50 @@ export default class aabb {
}
/**
* @en
* create a new aabb from two corner points
*
* @param out - the receiving aabb
* @param minPos - lower corner position of the aabb
* @param maxPos - upper corner position of the aabb
* @returns the resulting aabb
* @zh
* 从两个点创建一个新的 aabb
* @param out - 接受操作的 aabb
* @param minPos - aabb 的最小点
* @param maxPos - aabb 的最大点
* @returns {aabb} out 接受操作的 aabb
*/
public static fromPoints (out: aabb, minPos: vec3, maxPos: vec3) {
public static fromPoints (out: aabb, minPos: vec3, maxPos: vec3): aabb {
vec3.scale(out.center, vec3.add(_v3_tmp, minPos, maxPos), 0.5);
vec3.scale(out.halfExtents, vec3.sub(_v3_tmp2, maxPos, minPos), 0.5);
return out;
}
/**
* @en
* Set the components of a aabb to the given values
*
* @param {aabb} out the receiving aabb
* @param {number} px X coordinates for aabb's original point
* @param {number} py Y coordinates for aabb's original point
* @param {number} pz Z coordinates for aabb's original point
* @param {number} w the half of aabb width
* @param {number} h the half of aabb height
* @param {number} l the half of aabb length
* @return {aabb} out
* @function
* @zh
* 将 aabb 的属性设置为给定的值
* @param {aabb} out 接受操作的 aabb
* @param px - aabb 的原点的 X 坐标
* @param py - aabb 的原点的 Y 坐标
* @param pz - aabb 的原点的 Z 坐标
* @param hw - aabb 宽度的一半
* @param hh - aabb 高度的一半
* @param hl - aabb 长度度的一半
* @return {aabb} out 接受操作的 aabb
*/
public static set (out: aabb, px: number, py: number, pz: number, w: number, h: number, l: number): aabb {
public static set (out: aabb, px: number, py: number, pz: number, hw: number, hh: number, hl: number): aabb {
vec3.set(out.center, px, py, pz);
vec3.set(out.halfExtents, w, h, l);
vec3.set(out.halfExtents, hw, hh, hl);
return out;
}
public static merge (out: aabb, a: aabb, b: aabb) {
/**
* @zh
* 合并两个 aabb 到 out
* @param out 接受操作的 aabb
* @param a 输入的 aabb
* @param b 输入的 aabb
* @returns {aabb} out 接受操作的 aabb
*/
public static merge (out: aabb, a: aabb, b: aabb): aabb {
vec3.sub(_v3_tmp, a.center, a.halfExtents);
vec3.sub(_v3_tmp2, b.center, b.halfExtents);
vec3.add(_v3_tmp3, a.center, a.halfExtents);
@@ -105,24 +130,46 @@ export default class aabb {
return aabb.fromPoints(out, _v3_tmp3, _v3_tmp4);
}
public static transform (out: aabb, a: aabb, matrix: mat4) {
/**
* @zh
* 变换一个 aabb 到 out 中
* @param out 接受操作的 aabb
* @param a 输入的源 aabb
* @param matrix 矩阵
* @returns {aabb} out 接受操作的 aabb
*/
public static transform (out: aabb, a: aabb, matrix: mat4): aabb {
vec3.transformMat4(out.center, a.center, matrix);
transform_extent_m4(out.halfExtents, a.halfExtents, matrix);
}
public center: vec3;
public halfExtents: vec3;
protected _type: number = enums.SHAPE_AABB;
constructor (px = 0, py = 0, pz = 0, w = 1, h = 1, l = 1) {
this.center = vec3.create(px, py, pz);
this.halfExtents = vec3.create(w, h, l);
return out;
}
/**
* @zh
* 本地坐标的中心点
*/
public center: vec3;
/**
* @zh
* 长宽高的一半
*/
public halfExtents: vec3;
protected _type: number = enums.SHAPE_AABB;
constructor (px = 0, py = 0, pz = 0, hw = 1, hh = 1, hl = 1) {
this.center = vec3.create(px, py, pz);
this.halfExtents = vec3.create(hw, hh, hl);
}
/**
* @en
* Get the bounding points of this shape
* @param {vec3} minPos
* @param {vec3} maxPos
* @zh
* 获取 aabb 的最小点和最大点
* @param {vec3} minPos 最小点
* @param {vec3} maxPos 最大点
*/
public getBoundary (minPos: vec3, maxPos: vec3) {
vec3.sub(minPos, this.center, this.halfExtents);
@@ -130,21 +177,37 @@ export default class aabb {
}
/**
* @en
* Transform this shape
* @param m - the transform matrix
* @param out - the target shape
* @zh
* 将 out 根据这个 aabb 的数据进行变换
* @param m 变换的矩阵
* @param pos 变换的位置部分
* @param rot 变换的旋转部分
* @param scale 变换的缩放部分
* @param out 变换的目标
*/
public transform (m: mat4, pos: vec3 | null, rot: quat | null, scale: vec3 | null, out: aabb) {
if (!out) { out = this; }
vec3.transformMat4(out.center, this.center, m);
transform_extent_m4(out.halfExtents, this.halfExtents, m);
}
public clone () {
/**
* @zh
* 获得克隆
* @returns {aabb}
*/
public clone (): aabb {
return aabb.clone(this);
}
public copy (a: aabb) {
/**
* @zh
* 拷贝对象
* @param a 拷贝的目标
* @returns {aabb}
*/
public copy (a: aabb): aabb {
return aabb.copy(this, a);
}
}
+79 -76
View File
@@ -2,25 +2,37 @@ import { plane } from '.';
import { vec3 } from '../../core/vmath';
import aabb from './aabb';
import obb from './obb';
const X = vec3.create();
const Y = vec3.create();
const Z = vec3.create();
const d = vec3.create();
const min = vec3.create();
const max = vec3.create();
const u = new Array(3);
const e = new Array(3);
/**
* @en
* the distance between a point and a plane
*
* @param {vec3} point
* @param {plane} plane
* @return
* @zh
* 计算点和平面之间的距离
* @param {vec3} point 点
* @param {plane} plane 平面
* @return 距离
*/
export function point_plane (point: vec3, plane_: plane) {
return vec3.dot(plane_.n, point) - plane_.d;
}
/**
* @en
* the closest point on plane to a given point
*
* @param out the result point
* @param point
* @param plane
* @return
* @zh
* 计算平面上最接近给定点的点
* @param out 最近点
* @param point 给定点
* @param plane 平面
* @return 最近点
*/
export function pt_point_plane (out: vec3, point: vec3, plane_: plane) {
const t = point_plane(point, plane_);
@@ -28,85 +40,76 @@ export function pt_point_plane (out: vec3, point: vec3, plane_: plane) {
}
/**
* @en
* the closest point on aabb to a given point
*
* @param {vec3} out the result point
* @param {vec3} point
* @param {aabb} aabb
* @return {vec3}
* @zh
* 计算 aabb 上最接近给定点的点
* @param {vec3} out 最近点
* @param {vec3} point 给定点
* @param {aabb} aabb 轴对齐包围盒
* @return {vec3} 最近点
*/
export let pt_point_aabb = (() => {
const min = vec3.create();
const max = vec3.create();
return (out: vec3, point: vec3, aabb_: aabb) => {
vec3.copy(out, point);
vec3.sub(min, aabb_.center, aabb_.halfExtents);
vec3.add(max, aabb_.center, aabb_.halfExtents);
export function pt_point_aabb (out: vec3, point: vec3, aabb_: aabb): vec3 {
vec3.copy(out, point);
vec3.sub(min, aabb_.center, aabb_.halfExtents);
vec3.add(max, aabb_.center, aabb_.halfExtents);
out.x = (out.x < min.x) ? min.x : out.x;
out.y = (out.y < min.x) ? min.y : out.y;
out.z = (out.z < min.x) ? min.z : out.z;
out.x = (out.x < min.x) ? min.x : out.x;
out.y = (out.y < min.x) ? min.y : out.y;
out.z = (out.z < min.x) ? min.z : out.z;
out.x = (out.x > max.x) ? max.x : out.x;
out.y = (out.y > max.x) ? max.y : out.y;
out.z = (out.z > max.x) ? max.z : out.z;
return out;
};
})();
out.x = (out.x > max.x) ? max.x : out.x;
out.y = (out.y > max.x) ? max.y : out.y;
out.z = (out.z > max.x) ? max.z : out.z;
return out;
}
/**
* @en
* the closest point on obb to a given point
*
* @param {vec3} out the result point
* @param {vec3} point
* @param {obb} obb
* @return {vec3}
* @zh
* 计算 obb 上最接近给定点的点
* @param {vec3} out 最近点
* @param {vec3} point 给定点
* @param {obb} obb 方向包围盒
* @return {vec3} 最近点
*/
export let pt_point_obb = (() => {
const X = vec3.create();
const Y = vec3.create();
const Z = vec3.create();
const d = vec3.create();
const u = new Array(3);
const e = new Array(3);
export function pt_point_obb (out: vec3, point: vec3, obb_: obb): vec3 {
vec3.set(X, obb_.orientation.m00, obb_.orientation.m01, obb_.orientation.m02);
vec3.set(Y, obb_.orientation.m03, obb_.orientation.m04, obb_.orientation.m05);
vec3.set(Z, obb_.orientation.m06, obb_.orientation.m07, obb_.orientation.m08);
return (out: vec3, point: vec3, obb_: obb) => {
vec3.set(X, obb_.orientation.m00, obb_.orientation.m01, obb_.orientation.m02);
vec3.set(Y, obb_.orientation.m03, obb_.orientation.m04, obb_.orientation.m05);
vec3.set(Z, obb_.orientation.m06, obb_.orientation.m07, obb_.orientation.m08);
u[0] = X;
u[1] = Y;
u[2] = Z;
e[0] = obb_.halfExtents.x;
e[1] = obb_.halfExtents.y;
e[2] = obb_.halfExtents.z;
u[0] = X;
u[1] = Y;
u[2] = Z;
e[0] = obb_.halfExtents.x;
e[1] = obb_.halfExtents.y;
e[2] = obb_.halfExtents.z;
vec3.sub(d, point, obb_.center);
vec3.sub(d, point, obb_.center);
// Start result at center of obb; make steps from there
vec3.set(out, obb_.center.x, obb_.center.y, obb_.center.z);
// Start result at center of obb; make steps from there
vec3.set(out, obb_.center.x, obb_.center.y, obb_.center.z);
// For each OBB axis...
for (let i = 0; i < 3; i++) {
// For each OBB axis...
for (let i = 0; i < 3; i++) {
// ...project d onto that axis to get the distance
// along the axis of d from the obb center
let dist = vec3.dot(d, u[i]);
// ...project d onto that axis to get the distance
// along the axis of d from the obb center
let dist = vec3.dot(d, u[i]);
// if distance farther than the obb extents, clamp to the obb
if (dist > e[i]) {
dist = e[i];
}
if (dist < -e[i]) {
dist = -e[i];
}
// Step that distance along the axis to get world coordinate
out.x += dist * u[i].x;
out.y += dist * u[i].y;
out.z += dist * u[i].z;
// if distance farther than the obb extents, clamp to the obb
if (dist > e[i]) {
dist = e[i];
}
return out;
};
})();
if (dist < -e[i]) {
dist = -e[i];
}
// Step that distance along the axis to get world coordinate
out.x += dist * u[i].x;
out.y += dist * u[i].y;
out.z += dist * u[i].z;
}
return out;
}
+2 -1
View File
@@ -59,6 +59,7 @@ const line_plane = (function () {
*
* @param {ray} ray
* @param {triangle} triangle
* @param {boolean} doubleSided
* @return {number}
*/
const ray_triangle = (function () {
@@ -68,7 +69,7 @@ const ray_triangle = (function () {
const tvec = vec3.create(0, 0, 0);
const qvec = vec3.create(0, 0, 0);
return function (ray: ray, triangle: triangle, doubleSided: number) {
return function (ray: ray, triangle: triangle, doubleSided: boolean) {
vec3.sub(ab, triangle.b, triangle.a);
vec3.sub(ac, triangle.c, triangle.a);
+69 -32
View File
@@ -1,17 +1,24 @@
import { vec3 } from '../../core/vmath';
import enums from './enums';
/**
* @zh
* 基础几何 line
*/
// tslint:disable-next-line:class-name
export default class line {
/**
* @en
* create a new line
*
* @param sx start X component
* @param sy start Y component
* @param sz start Z component
* @param ex end X component
* @param ey end Y component
* @param ez end Z component
* @zh
* 创建一个新的 line
* @param sx 起点的 x 部分
* @param sy 起点的 y 部分
* @param sz 起点的 z 部分
* @param ex 终点的 x 部分
* @param ey 终点的 y 部分
* @param ez 终点的 z 部分
* @return
*/
public static create (sx: number, sy: number, sz: number, ex: number, ey: number, ez: number) {
@@ -19,10 +26,12 @@ export default class line {
}
/**
* @en
* Creates a new line initialized with values from an existing line
*
* @param a line to clone
* @return a new line
* @zh
* 克隆一个新的 line
* @param a 克隆的来源
* @return 克隆出的对象
*/
public static clone (a: line) {
return new line(
@@ -32,11 +41,13 @@ export default class line {
}
/**
* @en
* Copy the values from one line to another
*
* @param out the receiving line
* @param a the source line
* @return out
* @zh
* 复制一个线的值到另一个
* @param out 接受操作的对象
* @param a 复制的来源
* @return 接受操作的对象
*/
public static copy (out: line, a: line) {
vec3.copy(out.s, a.s);
@@ -46,12 +57,14 @@ export default class line {
}
/**
* @en
* create a line from two points
*
* @param out the receiving line
* @param start line start
* @param end target position
* @return out
* @zh
* 用两个点创建一个线
* @param out 接受操作的对象
* @param start 起点
* @param end 终点
* @return out 接受操作的对象
*/
public static fromPoints (out: line, start: vec3, end: vec3) {
vec3.copy(out.s, start);
@@ -60,16 +73,18 @@ export default class line {
}
/**
* @en
* Set the components of a vec3 to the given values
*
* @param out the receiving vector
* @param sx start X component
* @param sy start Y component
* @param sz start Z component
* @param ex end X component
* @param ey end Y component
* @param ez end Z component
* @return out
* @zh
* 将给定线的属性设置为给定值
* @param out 接受操作的对象
* @param sx 起点的 x 部分
* @param sy 起点的 y 部分
* @param sz 起点的 z 部分
* @param ex 终点的 x 部分
* @param ey 终点的 y 部分
* @param ez 终点的 z 部分
* @return out 接受操作的对象
*/
public static set (out: line, sx: number, sy: number, sz: number, ex: number, ey: number, ez: number) {
out.s.x = sx;
@@ -83,27 +98,49 @@ export default class line {
}
/**
* create line from 2 points
*
* @param a
* @return
* @zh
* 计算线的长度
* @param a 要计算的线
* @return 长度
*/
public static magnitude (a: line) {
return vec3.distance(a.s, a.e);
}
/**
* @en
* Alias of {@link line.magnitude}.
* @zh
* line.magnitude 的别名
*/
public static mag (a: line) {
return line.magnitude(a);
}
/**
* @zh
* 起点
*/
public s: vec3;
/**
* @zh
* 终点
*/
public e: vec3;
private _type: number;
/**
* @zh
* 构造一条线
* @param sx 起点的 x 部分
* @param sy 起点的 y 部分
* @param sz 起点的 z 部分
* @param ex 终点的 x 部分
* @param ey 终点的 y 部分
* @param ez 终点的 z 部分
*/
constructor (sx = 0, sy = 0, sz = 0, ex = 0, ey = 0, ez = -1) {
this._type = enums.SHAPE_LINE;
this.s = vec3.create(sx, sy, sz);
+118 -64
View File
@@ -13,43 +13,59 @@ const transform_extent_m3 = (out: vec3, extent: vec3, m3: mat3) => {
vec3.transformMat3(out, extent, _m3_tmp);
};
/**
* @zh
* 基础几何 方向包围盒
*/
// tslint:disable-next-line:class-name
export default class obb {
/**
* @zh
* 获取形状的类型
*/
get type () {
return this._type;
}
/**
* @en
* create a new obb
*
* @param px X coordinates for obb's original point
* @param py Y coordinates for obb's original point
* @param pz Z coordinates for obb's original point
* @param hw the half of obb width
* @param hh the half of obb height
* @param hl the half of obb length
* @param ox_1 orientation matrix coefficient
* @param ox_2 orientation matrix coefficient
* @param ox_3 orientation matrix coefficient
* @param oy_1 orientation matrix coefficient
* @param oy_2 orientation matrix coefficient
* @param oy_3 orientation matrix coefficient
* @param oz_1 orientation matrix coefficient
* @param oz_2 orientation matrix coefficient
* @param oz_3 orientation matrix coefficient
* @return
* @zh
* 创建一个新的 obb 实例
* @param cx 形状的相对于原点的 X 坐标
* @param cy 形状的相对于原点的 Y 坐标
* @param cz 形状的相对于原点的 Z 坐标
* @param hw - obb 宽度的一半
* @param hh - obb 高度的一半
* @param hl - obb 长度的一半
* @param ox_1 方向矩阵参数
* @param ox_2 方向矩阵参数
* @param ox_3 方向矩阵参数
* @param oy_1 方向矩阵参数
* @param oy_2 方向矩阵参数
* @param oy_3 方向矩阵参数
* @param oz_1 方向矩阵参数
* @param oz_2 方向矩阵参数
* @param oz_3 方向矩阵参数
* @return 返回一个 obb
*/
public static create (
px: number, py: number, pz: number,
cx: number, cy: number, cz: number,
hw: number, hh: number, hl: number,
ox_1: number, ox_2: number, ox_3: number,
oy_1: number, oy_2: number, oy_3: number,
oz_1: number, oz_2: number, oz_3: number) {
return new obb(px, py, pz, hw, hh, hl, ox_1, ox_2, ox_3, oy_1, oy_2, oy_3, oz_1, oz_2, oz_3);
return new obb(cx, cy, cz, hw, hh, hl, ox_1, ox_2, ox_3, oy_1, oy_2, oy_3, oz_1, oz_2, oz_3);
}
/**
* @en
* clone a new obb
*
* @param a the source obb
* @return
* @zh
* 克隆一个 obb
* @param a 克隆的目标
* @returns 克隆出的新对象
*/
public static clone (a: obb) {
return new obb(a.center.x, a.center.y, a.center.z,
@@ -60,13 +76,15 @@ export default class obb {
}
/**
* @en
* copy the values from one obb to another
*
* @param out the receiving obb
* @param a the source obb
* @return
* @zh
* 将从一个 obb 的值复制到另一个 obb
* @param {obb} out 接受操作的 obb
* @param {obb} a 被复制的 obb
* @return {obb} out 接受操作的 obb
*/
public static copy (out: obb, a: obb) {
public static copy (out: obb, a: obb): obb {
vec3.copy(out.center, a.center);
vec3.copy(out.halfExtents, a.halfExtents);
mat3.copy(out.orientation, a.orientation);
@@ -75,14 +93,16 @@ export default class obb {
}
/**
* @en
* create a new obb from two corner points
*
* @param out the receiving obb
* @param minPos lower corner position of the obb
* @param maxPos upper corner position of the obb
* @return
* @zh
* 用两个点创建一个新的 obb
* @param out - 接受操作的 obb
* @param minPos - obb 的最小点
* @param maxPos - obb 的最大点
* @returns {obb} out 接受操作的 obb
*/
public static fromPoints (out: obb, minPos: vec3, maxPos: vec3) {
public static fromPoints (out: obb, minPos: vec3, maxPos: vec3): obb {
vec3.scale(out.center, vec3.add(_v3_tmp, minPos, maxPos), 0.5);
vec3.scale(out.halfExtents, vec3.sub(_v3_tmp2, maxPos, minPos), 0.5);
mat3.identity(out.orientation);
@@ -90,59 +110,78 @@ export default class obb {
}
/**
* @en
* Set the components of a obb to the given values
*
* @param out the receiving obb
* @param px X coordinates for obb's original point
* @param py Y coordinates for obb's original point
* @param pz Z coordinates for obb's original point
* @param hw the half of obb width
* @param hh the half of obb height
* @param hl the half of obb length
* @param ox_1 orientation matrix coefficient
* @param ox_2 orientation matrix coefficient
* @param ox_3 orientation matrix coefficient
* @param oy_1 orientation matrix coefficient
* @param oy_2 orientation matrix coefficient
* @param oy_3 orientation matrix coefficient
* @param oz_1 orientation matrix coefficient
* @param oz_2 orientation matrix coefficient
* @param oz_3 orientation matrix coefficient
* @return out
* @zh
* 将给定 obb 的属性设置为给定的值
* @param cx - obb 的原点的 X 坐标
* @param cy - obb 的原点的 Y 坐标
* @param cz - obb 的原点的 Z 坐标
* @param hw - obb 宽度的一半
* @param hh - obb 高度的一半
* @param hl - obb 长度的一半
* @param ox_1 方向矩阵参数
* @param ox_2 方向矩阵参数
* @param ox_3 方向矩阵参数
* @param oy_1 方向矩阵参数
* @param oy_2 方向矩阵参数
* @param oy_3 方向矩阵参数
* @param oz_1 方向矩阵参数
* @param oz_2 方向矩阵参数
* @param oz_3 方向矩阵参数
* @return {obb} out
*/
public static set (
out: obb,
px: number, py: number, pz: number,
cx: number, cy: number, cz: number,
hw: number, hh: number, hl: number,
ox_1: number, ox_2: number, ox_3: number,
oy_1: number, oy_2: number, oy_3: number,
oz_1: number, oz_2: number, oz_3: number) {
vec3.set(out.center, px, py, pz);
oz_1: number, oz_2: number, oz_3: number): obb {
vec3.set(out.center, cx, cy, cz);
vec3.set(out.halfExtents, hw, hh, hl);
mat3.set(out.orientation, ox_1, ox_2, ox_3, oy_1, oy_2, oy_3, oz_1, oz_2, oz_3);
return out;
}
/**
* @zh
* 本地坐标的中心点
*/
public center: vec3;
/**
* @zh
* 长宽高的一半
*/
public halfExtents: vec3;
/**
* @zh
* 方向矩阵
*/
public orientation: mat3;
private _type: number;
protected _type: number;
constructor (px = 0, py = 0, pz = 0,
constructor (cx = 0, cy = 0, cz = 0,
hw = 1, hh = 1, hl = 1,
ox_1 = 1, ox_2 = 0, ox_3 = 0,
oy_1 = 0, oy_2 = 1, oy_3 = 0,
oz_1 = 0, oz_2 = 0, oz_3 = 1) {
this._type = enums.SHAPE_OBB;
this.center = vec3.create(px, py, pz);
this.center = vec3.create(cx, cy, cz);
this.halfExtents = vec3.create(hw, hh, hl);
this.orientation = mat3.create(ox_1, ox_2, ox_3, oy_1, oy_2, oy_3, oz_1, oz_2, oz_3);
}
/**
* @en
* Get the bounding points of this shape
* @param minPos
* @param maxPos
* @zh
* 获取 obb 的最小点和最大点
* @param {vec3} minPos 最小点
* @param {vec3} maxPos 最大点
*/
public getBoundary (minPos: vec3, maxPos: vec3) {
transform_extent_m3(_v3_tmp, this.halfExtents, this.orientation);
@@ -152,11 +191,13 @@ export default class obb {
/**
* Transform this shape
* @param m the transform matrix
* @param pos the position part of the transform
* @param rot the rotation part of the transform
* @param scale the scale part of the transform
* @param [out] the target shape
* @zh
* 将 out 根据这个 obb 的数据进行变换
* @param m 变换的矩阵
* @param pos 变换的位置部分
* @param rot 变换的旋转部分
* @param scale 变换的缩放部分
* @param out 变换的目标
*/
public transform (m: mat4, pos: vec3, rot: quat, scale: vec3, out: obb) {
vec3.transformMat4(out.center, this.center, m);
@@ -165,12 +206,25 @@ export default class obb {
vec3.mul(out.halfExtents, this.halfExtents, scale);
}
/**
* @zh
* 将 out 根据这个 obb 的数据进行变换
* @param m 变换的矩阵
* @param rot 变换的旋转部分
* @param out 变换的目标
*/
public translateAndRotate (m: mat4, rot: quat, out: obb){
vec3.transformMat4(out.center, this.center, m);
// parent shape doesn't contain rotations for now
mat3.fromQuat(out.orientation, rot);
}
/**
* @zh
* 将 out 根据这个 obb 的数据进行缩放
* @param scale 缩放值
* @param out 缩放的目标
*/
public setScale (scale: vec3, out: obb) {
vec3.mul(out.halfExtents, this.halfExtents, scale);
}
+87 -48
View File
@@ -1,18 +1,27 @@
import { vec3, mat4, vec4 } from '../../core/vmath';
import { mat4, vec3, vec4 } from '../../core/vmath';
import enums from './enums';
const v1 = vec3.create(0, 0, 0);
const v2 = vec3.create(0, 0, 0);
const temp_mat = cc.mat4();
const temp_vec4 = cc.v4();
/**
* @zh
* 基础几何 plane
*/
// tslint:disable-next-line:class-name
export default class plane {
/**
* @en
* create a new plane
*
* @param nx normal X component
* @param ny normal Y component
* @param nz normal Z component
* @param d distance to the origin
* @zh
* 创建一个新的 plane
* @param nx 法向分量的 x 部分
* @param ny 法向分量的 y 部分
* @param nz 法向分量的 z 部分
* @param d 与原点的距离
* @return
*/
public static create (nx: number, ny: number, nz: number, d: number) {
@@ -20,21 +29,25 @@ export default class plane {
}
/**
* @en
* clone a new plane
*
* @param p a the source plane
* @return
* @zh
* 克隆一个新的 plane
* @param p 克隆的来源
* @return 克隆出的对象
*/
public static clone (p: plane) {
return new plane(p.n.x, p.n.y, p.n.z, p.d);
}
/**
* @en
* copy the values from one plane to another
*
* @param out the receiving plane
* @param p the source plane
* @return
* @zh
* 复制一个平面的值到另一个
* @param out 接受操作的对象
* @param p 复制的来源
* @return 接受操作的对象
*/
public static copy (out: plane, p: plane) {
vec3.copy(out.n, p.n);
@@ -44,13 +57,15 @@ export default class plane {
}
/**
* @en
* create a plane from three points
*
* @param out the receiving plane
* @param a
* @param b
* @param c
* @return
* @zh
* 用三个点创建一个平面
* @param out 接受操作的对象
* @param a 点 a
* @param b 点 b
* @param c 点 c
* @return out 接受操作的对象
*/
public static fromPoints (out: plane, a: vec3, b: vec3, c: vec3) {
vec3.sub(v1, b, a);
@@ -63,14 +78,16 @@ export default class plane {
}
/**
* @en
* Set the components of a plane to the given values
*
* @param out the receiving plane
* @param nx X component of n
* @param ny Y component of n
* @param nz Z component of n
* @param d
* @return out
* @zh
* 将给定平面的属性设置为给定值
* @param out 接受操作的对象
* @param nx 法向分量的 x 部分
* @param ny 法向分量的 y 部分
* @param nz 法向分量的 z 部分
* @param d 与原点的距离
* @return out 接受操作的对象
*/
public static set (out: plane, nx: number, ny: number, nz: number, d: number) {
out.n.x = nx;
@@ -82,12 +99,14 @@ export default class plane {
}
/**
* @en
* create plane from normal and point
*
* @param out the receiving plane
* @param normal
* @param point
* @return out
* @zh
* 用一条法线和一个点创建平面
* @param out 接受操作的对象
* @param normal 平面的法线
* @param point 平面上的一点
* @return out 接受操作的对象
*/
public static fromNormalAndPoint (out: plane, normal: vec3, point: vec3) {
vec3.copy(out.n, normal);
@@ -97,11 +116,13 @@ export default class plane {
}
/**
* @en
* normalize a plane
*
* @param out the receiving plane
* @param a plane to normalize
* @return out
* @zh
* 归一化一个平面
* @param out 接受操作的对象
* @param a 操作的源数据
* @return out 接受操作的对象
*/
public static normalize (out: plane, a: plane) {
const len = vec3.magnitude(a.n);
@@ -112,27 +133,45 @@ export default class plane {
return out;
}
public transform = (() => {
const temp_mat = cc.mat4();
const temp_vec4 = cc.v4();
return (mat: mat4) => {
mat4.invert(temp_mat, mat);
mat4.transpose(temp_mat, temp_mat);
vec4.set(temp_vec4, this.n.x, this.n.y, this.n.z, this.d);
vec4.transformMat4(temp_vec4, temp_vec4, temp_mat);
vec3.set(this.n, temp_vec4.x, temp_vec4.y, temp_vec4.z);
this.d = temp_vec4.w;
};
})();
/**
* @zh
* 法线向量
*/
public n: vec3;
/**
* @zh
* 原点到平面的距离
*/
public d: number;
private _type: number;
/**
* @zh
* 构造一个平面
* @param nx 法向分量的 x 部分
* @param ny 法向分量的 y 部分
* @param nz 法向分量的 z 部分
* @param d 与原点的距离
*/
constructor (nx = 0, ny = 1, nz = 0, d = 0) {
this._type = enums.SHAPE_PLANE;
this.n = vec3.create(nx, ny, nz);
this.d = d;
}
/**
* @zh
* 变换一个平面
* @param mat
*/
public transform (mat: mat4): void {
mat4.invert(temp_mat, mat);
mat4.transpose(temp_mat, temp_mat);
vec4.set(temp_vec4, this.n.x, this.n.y, this.n.z, this.d);
vec4.transformMat4(temp_vec4, temp_vec4, temp_mat);
vec3.set(this.n, temp_vec4.x, temp_vec4.y, temp_vec4.z);
this.d = temp_vec4.w;
}
}
+69 -35
View File
@@ -1,31 +1,39 @@
import { vec3 } from '../../core/vmath';
import enums from './enums';
/**
* @zh
* 基础几何 射线
*/
// tslint:disable-next-line:class-name
export default class ray {
/**
* @en
* create a new ray
*
* @param {number} ox origin X component
* @param {number} oy origin Y component
* @param {number} oz origin Z component
* @param {number} dx dir X component
* @param {number} dy dir Y component
* @param {number} dz dir Z component
* @return {ray}
* @zh
* 创建一条射线
* @param {number} ox 起点的 x 部分
* @param {number} oy 起点的 y 部分
* @param {number} oz 起点的 z 部分
* @param {number} dx 方向的 x 部分
* @param {number} dy 方向的 y 部分
* @param {number} dz 方向的 z 部分
* @return {ray} 射线
*/
public static create (ox = 0, oy = 0, oz = 0, dx = 0, dy = 0, dz = 1) {
public static create (ox: number = 0, oy: number = 0, oz: number = 0, dx: number = 0, dy: number = 0, dz: number = 1): ray {
return new ray(ox, oy, oz, dx, dy, dz);
}
/**
* @en
* Creates a new ray initialized with values from an existing ray
*
* @param {ray} a ray to clone
* @return {ray} a new ray
* @zh
* 从一条射线克隆出一条新的射线
* @param {ray} a 克隆的目标
* @return {ray} 克隆出的新对象
*/
public static clone (a) {
public static clone (a: ray): ray {
return new ray(
a.o.x, a.o.y, a.o.z,
a.d.x, a.d.y, a.d.z,
@@ -33,13 +41,15 @@ export default class ray {
}
/**
* @en
* Copy the values from one ray to another
*
* @param {ray} out the receiving ray
* @param {ray} a the source ray
* @return {ray} out
* @zh
* 将从一个 ray 的值复制到另一个 ray
* @param {ray} out 接受操作的 ray
* @param {ray} a 被复制的 ray
* @return {ray} out 接受操作的 ray
*/
public static copy (out, a) {
public static copy (out: ray, a: ray): ray {
vec3.copy(out.o, a.o);
vec3.copy(out.d, a.d);
@@ -47,32 +57,36 @@ export default class ray {
}
/**
* @en
* create a ray from two points
*
* @param {ray} out the receiving ray
* @param {vec3} origin ray origin
* @param {vec3} target target position
* @return {ray} out
* @zh
* 用两个点创建一条射线
* @param {ray} out 接受操作的射线
* @param {vec3} origin 射线的起点
* @param {vec3} target 射线上的一点
* @return {ray} out 接受操作的射线
*/
public static fromPoints (out, origin, target) {
public static fromPoints (out: ray, origin: vec3, target: vec3): ray {
vec3.copy(out.o, origin);
vec3.normalize(out.d, vec3.sub(out.d, target, origin));
return out;
}
/**
* @en
* Set the components of a ray to the given values
*
* @param {ray} out the receiving ray
* @param {number} ox origin X component
* @param {number} oy origin Y component
* @param {number} oz origin Z component
* @param {number} dx dir X component
* @param {number} dy dir Y component
* @param {number} dz dir Z component
* @return {ray} out
* @zh
* 将给定射线的属性设置为给定的值
* @param {ray} out 接受操作的射线
* @param {number} ox 起点的 x 部分
* @param {number} oy 起点的 y 部分
* @param {number} oz 起点的 z 部分
* @param {number} dx 方向的 x 部分
* @param {number} dy 方向的 y 部分
* @param {number} dz 方向的 z 部分
* @return {ray} out 接受操作的射线
*/
public static set (out, ox, oy, oz, dx, dy, dz) {
public static set (out: ray, ox: number, oy: number, oz: number, dx: number, dy: number, dz: number): ray {
out.o.x = ox;
out.o.y = oy;
out.o.z = oz;
@@ -82,12 +96,32 @@ export default class ray {
return out;
}
/**
* @zh
* 起点
*/
public o: vec3;
/**
* @zh
* 方向
*/
public d: vec3;
private _type: number;
constructor (ox = 0, oy = 0, oz = 0, dx = 0, dy = 0, dz = -1) {
/**
* 构造一条射线
* @param {number} ox 起点的 x 部分
* @param {number} oy 起点的 y 部分
* @param {number} oz 起点的 z 部分
* @param {number} dx 方向的 x 部分
* @param {number} dy 方向的 y 部分
* @param {number} dz 方向的 z 部分
*/
constructor (ox: number = 0, oy: number = 0, oz: number = 0,
dx: number = 0, dy: number = 0, dz: number = -1) {
this._type = enums.SHAPE_RAY;
this.o = vec3.create(ox, oy, oz);
this.d = vec3.create(dx, dy, dz);
+90 -29
View File
@@ -4,34 +4,48 @@ import enums from './enums';
const _v3_tmp = vec3.create();
function maxComponent (v: vec3) { return Math.max(Math.max(v.x, v.y), v.z); }
/**
* @zh
* 基础几何 轴对齐球
*/
// tslint:disable-next-line: class-name
export default class sphere {
/**
* @en
* create a new sphere
*
* @return {sphere}
* @zh
* 创建一个新的 sphere 实例
* @param cx 形状的相对于原点的 X 坐标
* @param cy 形状的相对于原点的 Y 坐标
* @param cz 形状的相对于原点的 Z 坐标
* @param r 球体的半径
* @return {sphere} 返回一个 sphere
*/
public static create (cx: number, cy: number, cz: number, r: number): sphere {
return new sphere(cx, cy, cz, r);
}
/**
* @en
* clone a new sphere
*
* @param {sphere} p the source sphere
* @return {sphere}
* @zh
* 克隆一个新的 sphere 实例
* @param {sphere} p 克隆的目标
* @return {sphere} 克隆出的示例
*/
public static clone (p: sphere): sphere {
return new sphere(p.center.x, p.center.y, p.center.z, p.radius);
}
/**
* @en
* copy the values from one sphere to another
*
* @param {sphere} out the receiving sphere
* @param {sphere} p the source sphere
* @return {sphere}
* @zh
* 将从一个 sphere 的值复制到另一个 sphere
* @param {sphere} out 接受操作的 sphere
* @param {sphere} a 被复制的 sphere
* @return {sphere} out 接受操作的 sphere
*/
public static copy (out: sphere, p: sphere): sphere {
vec3.copy(out.center, p.center);
@@ -41,12 +55,14 @@ export default class sphere {
}
/**
* @en
* create a new bounding sphere from two corner points
*
* @param {sphere} out the receiving sphere
* @param {vec3} minPos lower corner of the original shape
* @param {vec3} maxPos upper corner of the original shape
* @return {sphere}
* @zh
* 从两个点创建一个新的 sphere
* @param out - 接受操作的 sphere
* @param minPos - sphere 的最小点
* @param maxPos - sphere 的最大点
* @returns {sphere} out 接受操作的 sphere
*/
public static fromPoints (out: sphere, minPos: vec3, maxPos: vec3): sphere {
vec3.scale(out.center, vec3.add(_v3_tmp, minPos, maxPos), 0.5);
@@ -57,12 +73,12 @@ export default class sphere {
/**
* Set the components of a sphere to the given values
*
* @param {sphere} out the receiving sphere
* @param {number} cx X component of c
* @param {number} cy Y component of c
* @param {number} cz Z component of c
* @param {number} r
* @return {sphere} out
* @param {sphere} out 接受操作的 sphere
* @param cx 形状的相对于原点的 X 坐标
* @param cy 形状的相对于原点的 Y 坐标
* @param cz 形状的相对于原点的 Z 坐标
* @param {number} r 半径
* @return {sphere} out 接受操作的 sphere
* @function
*/
public static set (out: sphere, cx: number, cy: number, cz: number, r: number): sphere {
@@ -74,29 +90,58 @@ export default class sphere {
return out;
}
/**
* @zh
* 本地坐标的中心点
*/
public center: vec3;
/**
* @zh
* 半径
*/
public radius: number;
private _type: number;
protected _type: number;
constructor (cx = 0, cy = 0, cz = 0, r = 1) {
/**
* @zh
* 构造一个球
* @param cx 形状的相对于原点的 X 坐标
* @param cy 形状的相对于原点的 Y 坐标
* @param cz 形状的相对于原点的 Z 坐标
* @param {number} r 半径
*/
constructor (cx: number = 0, cy: number = 0, cz: number = 0, r: number = 1) {
this._type = enums.SHAPE_SPHERE;
this.center = vec3.create(cx, cy, cz);
this.radius = r;
}
/**
* @zh
* 获得克隆
*/
public clone () {
return sphere.clone(this);
}
/**
* @zh
* 拷贝对象
* @param a 拷贝的目标
*/
public copy (a: sphere) {
return sphere.copy(this, a);
}
/**
* @en
* Get the bounding points of this shape
* @param {vec3} minPos
* @param {vec3} maxPos
* @zh
* 获取此形状的边界点
* @param {vec3} minPos 最小点
* @param {vec3} maxPos 最大点
*/
public getBoundary (minPos: vec3, maxPos: vec3) {
vec3.set(minPos, this.center.x - this.radius, this.center.y - this.radius, this.center.z - this.radius);
@@ -104,22 +149,38 @@ export default class sphere {
}
/**
* @en
* Transform this shape
* @param {mat4} m the transform matrix
* @param {vec3} pos the position part of the transform
* @param {quat} rot the rotation part of the transform
* @param {vec3} scale the scale part of the transform
* @param {sphere} [out] the target shape
* @zh
* 将 out 根据这个 sphere 的数据进行变换
* @param m 变换的矩阵
* @param pos 变换的位置部分
* @param rot 变换的旋转部分
* @param scale 变换的缩放部分
* @param out 变换的目标
*/
public transform (m: mat4, pos: vec3, rot: quat, scale: vec3, out: sphere) {
vec3.transformMat4(out.center, this.center, m);
out.radius = this.radius * maxComponent(scale);
}
/**
* @zh
* 将 out 根据这个 sphere 的数据进行变换
* @param m 变换的矩阵
* @param rot 变换的旋转部分
* @param out 变换的目标
*/
public translateAndRotate (m: mat4, rot: quat, out: sphere){
vec3.transformMat4(out.center, this.center, m);
}
/**
* @zh
* 将 out 根据这个 sphere 的数据进行缩放
* @param scale 缩放值
* @param out 缩放的目标
*/
public setScale (scale: vec3, out: sphere) {
out.radius = this.radius * maxComponent(scale);
}
+91 -42
View File
@@ -1,33 +1,44 @@
import { vec3 } from '../../core/vmath';
import enums from './enums';
/**
* @zh
* 基础几何 三角形
*/
// tslint:disable-next-line:class-name
export default class triangle {
/**
* @en
* create a new triangle
*
* @param {number} ax
* @param {number} ay
* @param {number} az
* @param {number} bx
* @param {number} by
* @param {number} bz
* @param {number} cx
* @param {number} cy
* @param {number} cz
* @return {triangle}
* @zh
* 创建一个新的 triangle
* @param {number} ax a 点的 x 部分
* @param {number} ay a 点的 y 部分
* @param {number} az a 点的 z 部分
* @param {number} bx b 点的 x 部分
* @param {number} by b 点的 y 部分
* @param {number} bz b 点的 z 部分
* @param {number} cx c 点的 x 部分
* @param {number} cy c 点的 y 部分
* @param {number} cz c 点的 z 部分
* @return {triangle} 一个新的 triangle
*/
public static create (ax = 1, ay = 0, az = 0, bx = 0, by = 0, bz = 0, cx = 0, cy = 0, cz = 1) {
public static create (ax: number = 1, ay: number = 0, az: number = 0,
bx: number = 0, by: number = 0, bz: number = 0,
cx: number = 0, cy: number = 0, cz: number = 1): triangle {
return new triangle(ax, ay, az, bx, by, bz, cx, cy, cz);
}
/**
* @en
* clone a new triangle
*
* @param {triangle} t the source plane
* @return {triangle}
* @zh
* 克隆一个新的 triangle
* @param {triangle} t 克隆的目标
* @return {triangle} 克隆出的新对象
*/
public static clone (t) {
public static clone (t: triangle): triangle {
return new triangle(
t.a.x, t.a.y, t.a.z,
t.b.x, t.b.y, t.b.z,
@@ -36,13 +47,15 @@ export default class triangle {
}
/**
* @en
* copy the values from one triangle to another
*
* @param {triangle} out the receiving triangle
* @param {triangle} t the source triangle
* @return {triangle}
* @zh
* 将一个 triangle 的值复制到另一个 triangle
* @param {triangle} out 接受操作的 triangle
* @param {triangle} t 被复制的 triangle
* @return {triangle} out 接受操作的 triangle
*/
public static copy (out, t) {
public static copy (out: triangle, t: triangle): triangle {
vec3.copy(out.a, t.a);
vec3.copy(out.b, t.b);
vec3.copy(out.c, t.c);
@@ -51,15 +64,17 @@ export default class triangle {
}
/**
* @en
* Create a triangle from three points
*
* @param {triangle} out the receiving triangle
* @param {vec3} a
* @param {vec3} b
* @param {vec3} c
* @return {triangle}
* @zh
* 用三个点创建一个 triangle
* @param {triangle} out 接受操作的 triangle
* @param {vec3} a a 点
* @param {vec3} b b 点
* @param {vec3} c c 点
* @return {triangle} out 接受操作的 triangle
*/
public static fromPoints (out, a, b, c) {
public static fromPoints (out: triangle, a: vec3, b: vec3, c: vec3): triangle {
vec3.copy(out.a, a);
vec3.copy(out.b, b);
vec3.copy(out.c, c);
@@ -67,22 +82,27 @@ export default class triangle {
}
/**
* @en
* Set the components of a triangle to the given values
*
* @param {triangle} out the receiving plane
* @param {number} ax X component of a
* @param {number} ay Y component of a
* @param {number} az Z component of a
* @param {number} bx X component of b
* @param {number} by Y component of b
* @param {number} bz Z component of b
* @param {number} cx X component of c
* @param {number} cy Y component of c
* @param {number} cz Z component of c
* @return {plane}
* @zh
* 将给定三角形的属性设置为给定值
* @param {triangle} out 给定的三角形
* @param {number} ax a 点的 x 部分
* @param {number} ay a 点的 y 部分
* @param {number} az a 点的 z 部分
* @param {number} bx b 点的 x 部分
* @param {number} by b 点的 y 部分
* @param {number} bz b 点的 z 部分
* @param {number} cx c 点的 x 部分
* @param {number} cy c 点的 y 部分
* @param {number} cz c 点的 z 部分
* @return {triangle}
* @function
*/
public static set (out, ax, ay, az, bx, by, bz, cx, cy, cz) {
public static set (out: triangle,
ax: number, ay: number, az: number,
bx: number, by: number, bz: number,
cx: number, cy: number, cz: number): triangle {
out.a.x = ax;
out.a.y = ay;
out.a.z = az;
@@ -98,13 +118,42 @@ export default class triangle {
return out;
}
/**
* @zh
* 点 a
*/
public a: vec3;
/**
* @zh
* 点 b
*/
public b: vec3;
/**
* @zh
* 点 c
*/
public c: vec3;
private _type: number;
constructor (ax = 0, ay = 0, az = 0, bx = 1, by = 0, bz = 0, cx = 0, cy = 1, cz = 0) {
/**
* @zh
* 构造一个三角形
* @param {number} ax a 点的 x 部分
* @param {number} ay a 点的 y 部分
* @param {number} az a 点的 z 部分
* @param {number} bx b 点的 x 部分
* @param {number} by b 点的 y 部分
* @param {number} bz b 点的 z 部分
* @param {number} cx c 点的 x 部分
* @param {number} cy c 点的 y 部分
* @param {number} cz c 点的 z 部分
*/
constructor (ax: number = 0, ay: number = 0, az: number = 0,
bx: number = 1, by: number = 0, bz: number = 0,
cx: number = 0, cy: number = 1, cz: number = 0) {
this._type = enums.SHAPE_TRIANGLE;
this.a = vec3.create(ax, ay, az);
this.b = vec3.create(bx, by, bz);
+3 -1
View File
@@ -31,7 +31,9 @@ import * as geometry from './geom-utils';
import * as utils from './misc/utils';
import * as primitives from './primitive';
export * from './ui';
export { createRaycastResult } from './physics/instance';
// hidden
// export { createRaycastResult } from './physics/instance';
export {
utils,