mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-09-24 09:47:55 +00:00
初始化
This commit is contained in:
166
engine/cocos2d/core/geom-utils/aabb.ts
Normal file
166
engine/cocos2d/core/geom-utils/aabb.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import Vec3 from '../value-types/vec3';
|
||||
import Mat3 from '../value-types/mat3';
|
||||
import enums from './enums';
|
||||
|
||||
let _v3_tmp = new Vec3();
|
||||
let _v3_tmp2 = new Vec3();
|
||||
let _m3_tmp = new Mat3();
|
||||
|
||||
// https://zeuxcg.org/2010/10/17/aabb-from-obb-with-component-wise-abs/
|
||||
let transform_extent_m4 = function (out, extent, m4) {
|
||||
let _m3_tmpm = _m3_tmp.m, m4m = m4.m;
|
||||
_m3_tmpm[0] = Math.abs(m4m[0]); _m3_tmpm[1] = Math.abs(m4m[1]); _m3_tmpm[2] = Math.abs(m4m[2]);
|
||||
_m3_tmpm[3] = Math.abs(m4m[4]); _m3_tmpm[4] = Math.abs(m4m[5]); _m3_tmpm[5] = Math.abs(m4m[6]);
|
||||
_m3_tmpm[6] = Math.abs(m4m[8]); _m3_tmpm[7] = Math.abs(m4m[9]); _m3_tmpm[8] = Math.abs(m4m[10]);
|
||||
Vec3.transformMat3(out, extent, _m3_tmp);
|
||||
};
|
||||
|
||||
/**
|
||||
* Aabb
|
||||
* @class geomUtils.Aabb
|
||||
*/
|
||||
export default class aabb {
|
||||
|
||||
/**
|
||||
* create a new aabb
|
||||
* @method create
|
||||
* @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 {geomUtils.Aabb}
|
||||
*/
|
||||
public static create (px, py, pz, w, h, l) {
|
||||
return new aabb(px, py, pz, w, h, l);
|
||||
}
|
||||
|
||||
/**
|
||||
* clone a new aabb
|
||||
* @method clone
|
||||
* @param {geomUtils.Aabb} a the source aabb
|
||||
* @return {geomUtils.Aabb}
|
||||
*/
|
||||
public static clone (a) {
|
||||
return new aabb(a.center.x, a.center.y, a.center.z,
|
||||
a.halfExtents.x, a.halfExtents.y, a.halfExtents.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* copy the values from one aabb to another
|
||||
* @method copy
|
||||
* @param {geomUtils.Aabb} out the receiving aabb
|
||||
* @param {geomUtils.Aabb} a the source aabb
|
||||
* @return {geomUtils.Aabb}
|
||||
*/
|
||||
public static copy (out, a) {
|
||||
Vec3.copy(out.center, a.center);
|
||||
Vec3.copy(out.halfExtents, a.halfExtents);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a new aabb from two corner points
|
||||
* @method fromPoints
|
||||
* @param {geomUtils.Aabb} out the receiving aabb
|
||||
* @param {Vec3} minPos lower corner position of the aabb
|
||||
* @param {Vec3} maxPos upper corner position of the aabb
|
||||
* @return {geomUtils.Aabb}
|
||||
*/
|
||||
public static fromPoints (out, minPos, maxPos) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the components of a aabb to the given values
|
||||
* @method set
|
||||
* @param {geomUtils.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 {geomUtils.Aabb} out
|
||||
*/
|
||||
public static set (out, px, py, pz, w, h, l) {
|
||||
Vec3.set(out.center, px, py, pz);
|
||||
Vec3.set(out.halfExtents, w, h, l);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @property {Vec3} center
|
||||
*/
|
||||
center: Vec3;
|
||||
/**
|
||||
* @property {Vec3} halfExtents
|
||||
*/
|
||||
halfExtents: Vec3;
|
||||
/**
|
||||
* @property {number} _type
|
||||
*/
|
||||
_type: number;
|
||||
|
||||
constructor (px: number, py: number, pz: number, w: number, h: number, l: number) {
|
||||
this._type = enums.SHAPE_AABB;
|
||||
this.center = new Vec3(px, py, pz);
|
||||
this.halfExtents = new Vec3(w, h, l);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the bounding points of this shape
|
||||
* @method getBoundary
|
||||
* @param {Vec3} minPos
|
||||
* @param {Vec3} maxPos
|
||||
*/
|
||||
getBoundary (minPos, maxPos) {
|
||||
Vec3.sub(minPos, this.center, this.halfExtents);
|
||||
Vec3.add(maxPos, this.center, this.halfExtents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform this shape
|
||||
* @method transform
|
||||
* @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 {geomUtils.Aabb} [out] the target shape
|
||||
*/
|
||||
transform (m, pos, rot, scale, out) {
|
||||
if (!out) out = this;
|
||||
Vec3.transformMat4(out.center, this.center, m);
|
||||
transform_extent_m4(out.halfExtents, this.halfExtents, m);
|
||||
}
|
||||
}
|
150
engine/cocos2d/core/geom-utils/distance.ts
Normal file
150
engine/cocos2d/core/geom-utils/distance.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { Vec3 } from '../value-types';
|
||||
import aabb from './aabb';
|
||||
import obb from './obb';
|
||||
import plane from './plane';
|
||||
const X = new Vec3();
|
||||
const Y = new Vec3();
|
||||
const Z = new Vec3();
|
||||
const d = new Vec3();
|
||||
const min = new Vec3();
|
||||
const max = new Vec3();
|
||||
const u = new Array(3);
|
||||
const e = new Array(3);
|
||||
|
||||
/**
|
||||
* Some helpful utilities
|
||||
* @module cc.geomUtils
|
||||
*/
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* the distance between a point and a plane
|
||||
* !#zh
|
||||
* 计算点和平面之间的距离。
|
||||
* @method point_plane
|
||||
* @param {Vec3} point
|
||||
* @param {Plane} plane
|
||||
* @return {Number} Distance
|
||||
*/
|
||||
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
|
||||
* !#zh
|
||||
* 计算平面上最接近给定点的点。
|
||||
* @method pt_point_plane
|
||||
* @param {Vec3} out Closest point
|
||||
* @param {Vec3} point Given point
|
||||
* @param {Plane} plane
|
||||
* @return {Vec3} Closest point
|
||||
*/
|
||||
export function pt_point_plane (out: Vec3, point: Vec3, plane_: plane) {
|
||||
const t = point_plane(point, plane_);
|
||||
return Vec3.subtract(out, point, Vec3.multiplyScalar(out, plane_.n, t));
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* the closest point on aabb to a given point
|
||||
* !#zh
|
||||
* 计算 aabb 上最接近给定点的点。
|
||||
* @method pt_point_aabb
|
||||
* @param {Vec3} out Closest point.
|
||||
* @param {Vec3} point Given point.
|
||||
* @param {Aabb} aabb Align the axis around the box.
|
||||
* @return {Vec3} Closest point.
|
||||
*/
|
||||
export function pt_point_aabb (out: Vec3, point: Vec3, aabb_: aabb): Vec3 {
|
||||
Vec3.copy(out, point);
|
||||
Vec3.subtract(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 > 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
|
||||
* !#zh
|
||||
* 计算 obb 上最接近给定点的点。
|
||||
* @method pt_point_obb
|
||||
* @param {Vec3} out Closest point
|
||||
* @param {Vec3} point Given point
|
||||
* @param {Obb} obb Direction box
|
||||
* @return {Vec3} closest point
|
||||
*/
|
||||
export function pt_point_obb (out: Vec3, point: Vec3, obb_: obb): Vec3 {
|
||||
let obbm = obb_.orientation.m;
|
||||
Vec3.set(X, obbm[0], obbm[1], obbm[2]);
|
||||
Vec3.set(Y, obbm[3], obbm[4], obbm[5]);
|
||||
Vec3.set(Z, obbm[6], obbm[7], obbm[8]);
|
||||
|
||||
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.subtract(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);
|
||||
|
||||
// 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]);
|
||||
|
||||
// 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;
|
||||
}
|
||||
return out;
|
||||
}
|
89
engine/cocos2d/core/geom-utils/enums.ts
Normal file
89
engine/cocos2d/core/geom-utils/enums.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* !#en Shape type.
|
||||
* @enum geomUtils.enums
|
||||
*/
|
||||
export default {
|
||||
/**
|
||||
* !#en Ray.
|
||||
* !#zh 射线。
|
||||
* @property {Number} SHAPE_RAY
|
||||
* @default 1 << 0
|
||||
*/
|
||||
SHAPE_RAY: (1 << 0),
|
||||
/**
|
||||
* !#en Line.
|
||||
* !#zh 直线。
|
||||
* @property {Number} SHAPE_LINE
|
||||
* @default 2
|
||||
*/
|
||||
SHAPE_LINE: (1 << 1),
|
||||
/**
|
||||
* !#en Sphere.
|
||||
* !#zh 球。
|
||||
* @property {Number} SHAPE_SPHERE
|
||||
* @default 4
|
||||
*/
|
||||
SHAPE_SPHERE: (1 << 2),
|
||||
/**
|
||||
* !#en Aabb.
|
||||
* !#zh 包围盒。
|
||||
* @property {Number} SHAPE_AABB
|
||||
*/
|
||||
SHAPE_AABB: (1 << 3),
|
||||
/**
|
||||
* !#en Obb.
|
||||
* !#zh 有向包围盒。
|
||||
* @property {Number} SHAPE_OBB
|
||||
*/
|
||||
SHAPE_OBB: (1 << 4),
|
||||
/**
|
||||
* !#en Plane.
|
||||
* !#zh 平面。
|
||||
* @property {Number} SHAPE_PLANE
|
||||
*/
|
||||
SHAPE_PLANE: (1 << 5),
|
||||
/**
|
||||
* !#en Triangle.
|
||||
* !#zh 三角形。
|
||||
* @property {Number} SHAPE_TRIANGLE
|
||||
*/
|
||||
SHAPE_TRIANGLE: (1 << 6),
|
||||
/**
|
||||
* !#en Frustum.
|
||||
* !#zh 平截头体。
|
||||
* @property {Number} SHAPE_FRUSTUM
|
||||
*/
|
||||
SHAPE_FRUSTUM: (1 << 7),
|
||||
/**
|
||||
* !#en frustum accurate.
|
||||
* !#zh 平截头体。
|
||||
* @property {Number} SHAPE_FRUSTUM_ACCURATE
|
||||
*/
|
||||
SHAPE_FRUSTUM_ACCURATE: (1 << 8),
|
||||
};
|
||||
|
214
engine/cocos2d/core/geom-utils/frustum.ts
Normal file
214
engine/cocos2d/core/geom-utils/frustum.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { Mat4, Vec3 } from '../value-types';
|
||||
import enums from './enums';
|
||||
import plane from './plane';
|
||||
|
||||
const _v = new Array(8);
|
||||
_v[0] = new Vec3(1, 1, 1);
|
||||
_v[1] = new Vec3(-1, 1, 1);
|
||||
_v[2] = new Vec3(-1, -1, 1);
|
||||
_v[3] = new Vec3(1, -1, 1);
|
||||
_v[4] = new Vec3(1, 1, -1);
|
||||
_v[5] = new Vec3(-1, 1, -1);
|
||||
_v[6] = new Vec3(-1, -1, -1);
|
||||
_v[7] = new Vec3(1, -1, -1);
|
||||
|
||||
/**
|
||||
* !#en frustum
|
||||
* !#zh 平截头体
|
||||
* @class geomUtils.Frustum
|
||||
*/
|
||||
export default class frustum {
|
||||
|
||||
/**
|
||||
* Set whether to use accurate intersection testing function on this frustum
|
||||
* @property {boolean} accurate
|
||||
*/
|
||||
set accurate (b: boolean) {
|
||||
this._type = b ? enums.SHAPE_FRUSTUM_ACCURATE : enums.SHAPE_FRUSTUM;
|
||||
}
|
||||
|
||||
public static createOrtho = (() => {
|
||||
const _temp_v3 = new Vec3();
|
||||
return (out: frustum, width: number, height: number, near: number, far: number, transform: Mat4) => {
|
||||
const halfWidth = width / 2;
|
||||
const halfHeight = height / 2;
|
||||
Vec3.set(_temp_v3, halfWidth, halfHeight, near);
|
||||
Vec3.transformMat4(out.vertices[0], _temp_v3, transform);
|
||||
Vec3.set(_temp_v3, -halfWidth, halfHeight, near);
|
||||
Vec3.transformMat4(out.vertices[1], _temp_v3, transform);
|
||||
Vec3.set(_temp_v3, -halfWidth, -halfHeight, near);
|
||||
Vec3.transformMat4(out.vertices[2], _temp_v3, transform);
|
||||
Vec3.set(_temp_v3, halfWidth, -halfHeight, near);
|
||||
Vec3.transformMat4(out.vertices[3], _temp_v3, transform);
|
||||
Vec3.set(_temp_v3, halfWidth, halfHeight, far);
|
||||
Vec3.transformMat4(out.vertices[4], _temp_v3, transform);
|
||||
Vec3.set(_temp_v3, -halfWidth, halfHeight, far);
|
||||
Vec3.transformMat4(out.vertices[5], _temp_v3, transform);
|
||||
Vec3.set(_temp_v3, -halfWidth, -halfHeight, far);
|
||||
Vec3.transformMat4(out.vertices[6], _temp_v3, transform);
|
||||
Vec3.set(_temp_v3, halfWidth, -halfHeight, far);
|
||||
Vec3.transformMat4(out.vertices[7], _temp_v3, transform);
|
||||
|
||||
plane.fromPoints(out.planes[0], out.vertices[1], out.vertices[6], out.vertices[5]);
|
||||
plane.fromPoints(out.planes[1], out.vertices[3], out.vertices[4], out.vertices[7]);
|
||||
plane.fromPoints(out.planes[2], out.vertices[6], out.vertices[3], out.vertices[7]);
|
||||
plane.fromPoints(out.planes[3], out.vertices[0], out.vertices[5], out.vertices[4]);
|
||||
plane.fromPoints(out.planes[4], out.vertices[2], out.vertices[0], out.vertices[3]);
|
||||
plane.fromPoints(out.planes[0], out.vertices[7], out.vertices[5], out.vertices[6]);
|
||||
};
|
||||
})();
|
||||
|
||||
/**
|
||||
* create a new frustum
|
||||
* @method create
|
||||
* @static
|
||||
* @return {Frustum}
|
||||
*/
|
||||
public static create () {
|
||||
return new frustum();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a frustum
|
||||
* @method clone
|
||||
* @param {Frustum} f
|
||||
* @static
|
||||
* @return {Frustum}
|
||||
*/
|
||||
public static clone (f: frustum): frustum {
|
||||
return frustum.copy(new frustum(), f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the values from one frustum to another
|
||||
* @method copy
|
||||
* @param {Frustum} out
|
||||
* @param {Frustum} f
|
||||
* @return {Frustum}
|
||||
*/
|
||||
public static copy (out: frustum, f: frustum): frustum {
|
||||
out._type = f._type;
|
||||
for (let i = 0; i < 6; ++i) {
|
||||
plane.copy(out.planes[i], f.planes[i]);
|
||||
}
|
||||
for (let i = 0; i < 8; ++i) {
|
||||
Vec3.copy(out.vertices[i], f.vertices[i]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @property {Plane[]} planes
|
||||
*/
|
||||
public planes: plane[];
|
||||
/**
|
||||
* @property {Vec3[]} planes
|
||||
*/
|
||||
public vertices: Vec3[];
|
||||
private _type: number;
|
||||
|
||||
constructor () {
|
||||
this._type = enums.SHAPE_FRUSTUM;
|
||||
this.planes = new Array(6);
|
||||
for (let i = 0; i < 6; ++i) {
|
||||
this.planes[i] = plane.create(0, 0, 0, 0);
|
||||
}
|
||||
this.vertices = new Array(8);
|
||||
for (let i = 0; i < 8; ++i) {
|
||||
this.vertices[i] = new Vec3();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Update the frustum information according to the given transform matrix.
|
||||
* Note that the resulting planes are not normalized under normal mode.
|
||||
* @method update
|
||||
* @param {Mat4} m the view-projection matrix
|
||||
* @param {Mat4} inv the inverse view-projection matrix
|
||||
*/
|
||||
public update (m: Mat4, inv: Mat4) {
|
||||
// RTR4, ch. 22.14.1, p. 983
|
||||
// extract frustum planes from view-proj matrix.
|
||||
|
||||
let mm = m.m;
|
||||
|
||||
// left plane
|
||||
Vec3.set(this.planes[0].n, mm[3] + mm[0], mm[7] + mm[4], mm[11] + mm[8]);
|
||||
this.planes[0].d = -(mm[15] + mm[12]);
|
||||
// right plane
|
||||
Vec3.set(this.planes[1].n, mm[3] - mm[0], mm[7] - mm[4], mm[11] - mm[8]);
|
||||
this.planes[1].d = -(mm[15] - mm[12]);
|
||||
// bottom plane
|
||||
Vec3.set(this.planes[2].n, mm[3] + mm[1], mm[7] + mm[5], mm[11] + mm[9]);
|
||||
this.planes[2].d = -(mm[15] + mm[13]);
|
||||
// top plane
|
||||
Vec3.set(this.planes[3].n, mm[3] - mm[1], mm[7] - mm[5], mm[11] - mm[9]);
|
||||
this.planes[3].d = -(mm[15] - mm[13]);
|
||||
// near plane
|
||||
Vec3.set(this.planes[4].n, mm[3] + mm[2], mm[7] + mm[6], mm[11] + mm[10]);
|
||||
this.planes[4].d = -(mm[15] + mm[14]);
|
||||
// far plane
|
||||
Vec3.set(this.planes[5].n, mm[3] - mm[2], mm[7] - mm[6], mm[11] - mm[10]);
|
||||
this.planes[5].d = -(mm[15] - mm[14]);
|
||||
|
||||
if (this._type !== enums.SHAPE_FRUSTUM_ACCURATE) { return; }
|
||||
|
||||
// normalize planes
|
||||
for (let i = 0; i < 6; i++) {
|
||||
const pl = this.planes[i];
|
||||
const invDist = 1 / pl.n.length();
|
||||
Vec3.multiplyScalar(pl.n, pl.n, invDist);
|
||||
pl.d *= invDist;
|
||||
}
|
||||
|
||||
// update frustum vertices
|
||||
for (let i = 0; i < 8; i++) {
|
||||
Vec3.transformMat4(this.vertices[i], _v[i], inv);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en transform by matrix
|
||||
* @method transform
|
||||
* @param {Mat4} mat
|
||||
*/
|
||||
public transform (mat: Mat4) {
|
||||
if (this._type !== enums.SHAPE_FRUSTUM_ACCURATE) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < 8; i++) {
|
||||
Vec3.transformMat4(this.vertices[i], this.vertices[i], mat);
|
||||
}
|
||||
plane.fromPoints(this.planes[0], this.vertices[1], this.vertices[5], this.vertices[6]);
|
||||
plane.fromPoints(this.planes[1], this.vertices[3], this.vertices[7], this.vertices[4]);
|
||||
plane.fromPoints(this.planes[2], this.vertices[6], this.vertices[7], this.vertices[3]);
|
||||
plane.fromPoints(this.planes[3], this.vertices[0], this.vertices[4], this.vertices[5]);
|
||||
plane.fromPoints(this.planes[4], this.vertices[2], this.vertices[3], this.vertices[0]);
|
||||
plane.fromPoints(this.planes[0], this.vertices[7], this.vertices[6], this.vertices[5]);
|
||||
}
|
||||
}
|
38
engine/cocos2d/core/geom-utils/index.ts
Normal file
38
engine/cocos2d/core/geom-utils/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
export { default as enums } from './enums';
|
||||
export { default as Triangle } from './triangle';
|
||||
export { default as Aabb } from './aabb';
|
||||
export { default as Ray } from './ray';
|
||||
export { default as intersect } from './intersect';
|
||||
export { default as Sphere } from './sphere';
|
||||
export { default as Obb } from './obb';
|
||||
export { default as Frustum } from './frustum';
|
||||
export { default as Line } from './line';
|
||||
export { default as Plane } from './plane';
|
||||
export * from './distance';
|
||||
|
||||
cc.geomUtils = module.exports;
|
1201
engine/cocos2d/core/geom-utils/intersect.ts
Normal file
1201
engine/cocos2d/core/geom-utils/intersect.ts
Normal file
File diff suppressed because it is too large
Load Diff
192
engine/cocos2d/core/geom-utils/line.ts
Normal file
192
engine/cocos2d/core/geom-utils/line.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
import { Vec3 } from '../value-types';
|
||||
import enums from './enums';
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* line
|
||||
* !#zh
|
||||
* 直线
|
||||
* @class geomUtils.Line
|
||||
*/
|
||||
export default class line {
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a new line
|
||||
* !#zh
|
||||
* 创建一个新的 line。
|
||||
* @method create
|
||||
* @param {Number} sx The x part of the starting point.
|
||||
* @param {Number} sy The y part of the starting point.
|
||||
* @param {Number} sz The z part of the starting point.
|
||||
* @param {Number} ex The x part of the end point.
|
||||
* @param {Number} ey The y part of the end point.
|
||||
* @param {Number} ez The z part of the end point.
|
||||
* @return {Line}
|
||||
*/
|
||||
public static create (sx: number, sy: number, sz: number, ex: number, ey: number, ez: number) {
|
||||
return new line(sx, sy, sz, ex, ey, ez);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Creates a new line initialized with values from an existing line
|
||||
* !#zh
|
||||
* 克隆一个新的 line。
|
||||
* @method clone
|
||||
* @param {Line} a The source of cloning.
|
||||
* @return {Line} The cloned object.
|
||||
*/
|
||||
public static clone (a: line) {
|
||||
return new line(
|
||||
a.s.x, a.s.y, a.s.z,
|
||||
a.e.x, a.e.y, a.e.z,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Copy the values from one line to another
|
||||
* !#zh
|
||||
* 复制一个线的值到另一个。
|
||||
* @method copy
|
||||
* @param {Line} out The object that accepts the action.
|
||||
* @param {Line} a The source of the copy.
|
||||
* @return {Line} The object that accepts the action.
|
||||
*/
|
||||
public static copy (out: line, a: line) {
|
||||
Vec3.copy(out.s, a.s);
|
||||
Vec3.copy(out.e, a.e);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a line from two points
|
||||
* !#zh
|
||||
* 用两个点创建一个线。
|
||||
* @method fromPoints
|
||||
* @param {Line} out The object that accepts the action.
|
||||
* @param {Vec3} start The starting point.
|
||||
* @param {Vec3} end At the end.
|
||||
* @return {Line} out The object that accepts the action.
|
||||
*/
|
||||
public static fromPoints (out: line, start: Vec3, end: Vec3) {
|
||||
Vec3.copy(out.s, start);
|
||||
Vec3.copy(out.e, end);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Set the components of a Vec3 to the given values
|
||||
* !#zh
|
||||
* 将给定线的属性设置为给定值。
|
||||
* @method set
|
||||
* @param {Line} out The object that accepts the action.
|
||||
* @param {Number} sx The x part of the starting point.
|
||||
* @param {Number} sy The y part of the starting point.
|
||||
* @param {Number} sz The z part of the starting point.
|
||||
* @param {Number} ex The x part of the end point.
|
||||
* @param {Number} ey The y part of the end point.
|
||||
* @param {Number} ez The z part of the end point.
|
||||
* @return {Line} out The object that accepts the action.
|
||||
*/
|
||||
public static set (out: line, sx: number, sy: number, sz: number, ex: number, ey: number, ez: number) {
|
||||
out.s.x = sx;
|
||||
out.s.y = sy;
|
||||
out.s.z = sz;
|
||||
out.e.x = ex;
|
||||
out.e.y = ey;
|
||||
out.e.z = ez;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Calculate the length of the line.
|
||||
* !#zh
|
||||
* 计算线的长度。
|
||||
* @method len
|
||||
* @param {Line} a The line to calculate.
|
||||
* @return {Number} Length.
|
||||
*/
|
||||
public static len (a: line) {
|
||||
return Vec3.distance(a.s, a.e);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Start points.
|
||||
* !#zh
|
||||
* 起点。
|
||||
* @property {Vec3} s
|
||||
*/
|
||||
public s: Vec3;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* End points.
|
||||
* !#zh
|
||||
* 终点。
|
||||
* @property {Vec3} e
|
||||
*/
|
||||
public e: Vec3;
|
||||
|
||||
private _type: number;
|
||||
|
||||
/**
|
||||
* !#en Construct a line.
|
||||
* !#zh 构造一条线。
|
||||
* @constructor
|
||||
* @param {Number} sx The x part of the starting point.
|
||||
* @param {Number} sy The y part of the starting point.
|
||||
* @param {Number} sz The z part of the starting point.
|
||||
* @param {Number} ex The x part of the end point.
|
||||
* @param {Number} ey The y part of the end point.
|
||||
* @param {Number} ez The z part of the end point.
|
||||
*/
|
||||
constructor (sx = 0, sy = 0, sz = 0, ex = 0, ey = 0, ez = -1) {
|
||||
this._type = enums.SHAPE_LINE;
|
||||
this.s = new Vec3(sx, sy, sz);
|
||||
this.e = new Vec3(ex, ey, ez);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Calculate the length of the line.
|
||||
* !#zh
|
||||
* 计算线的长度。
|
||||
* @method length
|
||||
* @return {Number} Length.
|
||||
*/
|
||||
public length () {
|
||||
return Vec3.distance(this.s, this.e);
|
||||
}
|
||||
}
|
283
engine/cocos2d/core/geom-utils/obb.ts
Normal file
283
engine/cocos2d/core/geom-utils/obb.ts
Normal file
@@ -0,0 +1,283 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { Mat3, Mat4, Quat, Vec3 } from '../value-types';
|
||||
import enums from './enums';
|
||||
|
||||
const _v3_tmp = new Vec3();
|
||||
const _v3_tmp2 = new Vec3();
|
||||
const _m3_tmp = new Mat3();
|
||||
|
||||
// https://zeuxcg.org/2010/10/17/aabb-from-obb-with-component-wise-abs/
|
||||
const transform_extent_m3 = (out: Vec3, extent: Vec3, m3: Mat3) => {
|
||||
let m3_tmpm = _m3_tmp.m, m3m = m3.m;
|
||||
m3_tmpm[0] = Math.abs(m3m[0]); m3_tmpm[1] = Math.abs(m3m[1]); m3_tmpm[2] = Math.abs(m3m[2]);
|
||||
m3_tmpm[3] = Math.abs(m3m[3]); m3_tmpm[4] = Math.abs(m3m[4]); m3_tmpm[5] = Math.abs(m3m[5]);
|
||||
m3_tmpm[6] = Math.abs(m3m[6]); m3_tmpm[7] = Math.abs(m3m[7]); m3_tmpm[8] = Math.abs(m3m[8]);
|
||||
Vec3.transformMat3(out, extent, _m3_tmp);
|
||||
};
|
||||
|
||||
/**
|
||||
* !#en obb
|
||||
* !#zh
|
||||
* 基础几何 方向包围盒。
|
||||
* @class geomUtils.Obb
|
||||
*/
|
||||
export default class obb {
|
||||
|
||||
/**
|
||||
* !#zh
|
||||
* 获取形状的类型。
|
||||
* @property {number} type
|
||||
* @readonly
|
||||
*/
|
||||
get type () {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a new obb
|
||||
* !#zh
|
||||
* 创建一个新的 obb 实例。
|
||||
* @method create
|
||||
* @param {Number} cx X coordinates of the shape relative to the origin.
|
||||
* @param {Number} cy Y coordinates of the shape relative to the origin.
|
||||
* @param {Number} cz Z coordinates of the shape relative to the origin.
|
||||
* @param {Number} hw Obb is half the width.
|
||||
* @param {Number} hh Obb is half the height.
|
||||
* @param {Number} hl Obb is half the Length.
|
||||
* @param {Number} ox_1 Direction matrix parameter.
|
||||
* @param {Number} ox_2 Direction matrix parameter.
|
||||
* @param {Number} ox_3 Direction matrix parameter.
|
||||
* @param {Number} oy_1 Direction matrix parameter.
|
||||
* @param {Number} oy_2 Direction matrix parameter.
|
||||
* @param {Number} oy_3 Direction matrix parameter.
|
||||
* @param {Number} oz_1 Direction matrix parameter.
|
||||
* @param {Number} oz_2 Direction matrix parameter.
|
||||
* @param {Number} oz_3 Direction matrix parameter.
|
||||
* @return {Obb} Direction Box.
|
||||
*/
|
||||
public static create (
|
||||
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(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
|
||||
* !#zh
|
||||
* 克隆一个 obb。
|
||||
* @method clone
|
||||
* @param {Obb} a The target of cloning.
|
||||
* @returns {Obb} New object cloned.
|
||||
*/
|
||||
public static clone (a: obb) {
|
||||
let aom = a.orientation.m;
|
||||
return new obb(a.center.x, a.center.y, a.center.z,
|
||||
a.halfExtents.x, a.halfExtents.y, a.halfExtents.z,
|
||||
aom[0], aom[1], aom[2],
|
||||
aom[3], aom[4], aom[5],
|
||||
aom[6], aom[7], aom[8]);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* copy the values from one obb to another
|
||||
* !#zh
|
||||
* 将从一个 obb 的值复制到另一个 obb。
|
||||
* @method copy
|
||||
* @param {Obb} out Obb that accepts the operation.
|
||||
* @param {Obb} a Obb being copied.
|
||||
* @return {Obb} out Obb that accepts the operation.
|
||||
*/
|
||||
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);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a new obb from two corner points
|
||||
* !#zh
|
||||
* 用两个点创建一个新的 obb。
|
||||
* @method fromPoints
|
||||
* @param {Obb} out Obb that accepts the operation.
|
||||
* @param {Vec3} minPos The smallest point of obb.
|
||||
* @param {Vec3} maxPos Obb's maximum point.
|
||||
* @returns {Obb} out Obb that accepts the operation.
|
||||
*/
|
||||
public static fromPoints (out: obb, minPos: Vec3, maxPos: Vec3): obb {
|
||||
Vec3.multiplyScalar(out.center, Vec3.add(_v3_tmp, minPos, maxPos), 0.5);
|
||||
Vec3.multiplyScalar(out.halfExtents, Vec3.subtract(_v3_tmp2, maxPos, minPos), 0.5);
|
||||
Mat3.identity(out.orientation);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Set the components of a obb to the given values
|
||||
* !#zh
|
||||
* 将给定 obb 的属性设置为给定的值。
|
||||
* @method set
|
||||
* @param {Number} cx X coordinates of the shape relative to the origin.
|
||||
* @param {Number} cy Y coordinates of the shape relative to the origin.
|
||||
* @param {Number} cz Z coordinates of the shape relative to the origin.
|
||||
* @param {Number} hw Obb is half the width.
|
||||
* @param {Number} hh Obb is half the height.
|
||||
* @param {Number} hl Obb is half the Length.
|
||||
* @param {Number} ox_1 Direction matrix parameter.
|
||||
* @param {Number} ox_2 Direction matrix parameter.
|
||||
* @param {Number} ox_3 Direction matrix parameter.
|
||||
* @param {Number} oy_1 Direction matrix parameter.
|
||||
* @param {Number} oy_2 Direction matrix parameter.
|
||||
* @param {Number} oy_3 Direction matrix parameter.
|
||||
* @param {Number} oz_1 Direction matrix parameter.
|
||||
* @param {Number} oz_2 Direction matrix parameter.
|
||||
* @param {Number} oz_3 Direction matrix parameter.
|
||||
* @return {Obb} out
|
||||
*/
|
||||
public static set (
|
||||
out: obb,
|
||||
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): 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* The center of the local coordinate.
|
||||
* !#zh
|
||||
* 本地坐标的中心点。
|
||||
* @property {Vec3} center
|
||||
*/
|
||||
public center: Vec3;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Half the length, width, and height.
|
||||
* !#zh
|
||||
* 长宽高的一半。
|
||||
* @property {Vec3} halfExtents
|
||||
*/
|
||||
public halfExtents: Vec3;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Direction matrix.
|
||||
* !#zh
|
||||
* 方向矩阵。
|
||||
* @property {Mat3} orientation
|
||||
*/
|
||||
public orientation: Mat3;
|
||||
|
||||
protected _type: number;
|
||||
|
||||
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 = new Vec3(cx, cy, cz);
|
||||
this.halfExtents = new Vec3(hw, hh, hl);
|
||||
this.orientation = new Mat3(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
|
||||
* !#zh
|
||||
* 获取 obb 的最小点和最大点。
|
||||
* @method getBoundary
|
||||
* @param {Vec3} minPos
|
||||
* @param {Vec3} maxPos
|
||||
*/
|
||||
public getBoundary (minPos: Vec3, maxPos: Vec3) {
|
||||
transform_extent_m3(_v3_tmp, this.halfExtents, this.orientation);
|
||||
Vec3.subtract(minPos, this.center, _v3_tmp);
|
||||
Vec3.add(maxPos, this.center, _v3_tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Transform this shape
|
||||
* !#zh
|
||||
* 将 out 根据这个 obb 的数据进行变换。
|
||||
* @method transform
|
||||
* @param {Mat4} m The transformation matrix.
|
||||
* @param {Vec3} pos The position part of the transformation.
|
||||
* @param {Quat} rot The rotating part of the transformation.
|
||||
* @param {Vec3} scale The scaling part of the transformation.
|
||||
* @param {Obb} out Target of transformation.
|
||||
*/
|
||||
public transform (m: Mat4, pos: Vec3, rot: Quat, scale: Vec3, out: obb) {
|
||||
Vec3.transformMat4(out.center, this.center, m);
|
||||
// parent shape doesn't contain rotations for now
|
||||
Mat3.fromQuat(out.orientation, rot);
|
||||
Vec3.multiply(out.halfExtents, this.halfExtents, scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Transform out based on this obb data.
|
||||
* !#zh
|
||||
* 将 out 根据这个 obb 的数据进行变换。
|
||||
* @method translateAndRotate
|
||||
* @param {Mat4} m The transformation matrix.
|
||||
* @param {Quat} rot The rotating part of the transformation.
|
||||
* @param {Obb} out Target of transformation.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Scale out based on this obb data.
|
||||
* !#zh
|
||||
* 将 out 根据这个 obb 的数据进行缩放。
|
||||
* @method setScale
|
||||
* @param {Vec3} scale Scale value.
|
||||
* @param {Obb} out Scaled target.
|
||||
*/
|
||||
public setScale (scale: Vec3, out: obb) {
|
||||
Vec3.multiply(out.halfExtents, this.halfExtents, scale);
|
||||
}
|
||||
}
|
221
engine/cocos2d/core/geom-utils/plane.ts
Normal file
221
engine/cocos2d/core/geom-utils/plane.ts
Normal file
@@ -0,0 +1,221 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { Mat4, Vec3, Vec4 } from '../value-types';
|
||||
import enums from './enums';
|
||||
|
||||
const v1 = new Vec3(0, 0, 0);
|
||||
const v2 = new Vec3(0, 0, 0);
|
||||
const temp_mat = cc.mat4();
|
||||
const temp_vec4 = cc.v4();
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* plane。
|
||||
* !#zh
|
||||
* 平面。
|
||||
* @class geomUtils.Plane
|
||||
*/
|
||||
export default class plane {
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a new plane
|
||||
* !#zh
|
||||
* 创建一个新的 plane。
|
||||
* @method create
|
||||
* @param {Number} nx The x part of the normal component.
|
||||
* @param {Number} ny The y part of the normal component.
|
||||
* @param {Number} nz The z part of the normal component.
|
||||
* @param {Number} d Distance from the origin.
|
||||
* @return {Plane}
|
||||
*/
|
||||
public static create (nx: number, ny: number, nz: number, d: number) {
|
||||
return new plane(nx, ny, nz, d);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* clone a new plane
|
||||
* !#zh
|
||||
* 克隆一个新的 plane。
|
||||
* @method clone
|
||||
* @param {Plane} p The source of cloning.
|
||||
* @return {Plane} The cloned object.
|
||||
*/
|
||||
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
|
||||
* !#zh
|
||||
* 复制一个平面的值到另一个。
|
||||
* @method copy
|
||||
* @param {Plane} out The object that accepts the action.
|
||||
* @param {Plane} p The source of the copy.
|
||||
* @return {Plane} The object that accepts the action.
|
||||
*/
|
||||
public static copy (out: plane, p: plane) {
|
||||
Vec3.copy(out.n, p.n);
|
||||
out.d = p.d;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a plane from three points
|
||||
* !#zh
|
||||
* 用三个点创建一个平面。
|
||||
* @method fromPoints
|
||||
* @param {Plane} out The object that accepts the action.
|
||||
* @param {Vec3} a Point a。
|
||||
* @param {Vec3} b Point b。
|
||||
* @param {Vec3} c Point c。
|
||||
* @return {Plane} out The object that accepts the action.
|
||||
*/
|
||||
public static fromPoints (out: plane, a: Vec3, b: Vec3, c: Vec3) {
|
||||
Vec3.subtract(v1, b, a);
|
||||
Vec3.subtract(v2, c, a);
|
||||
|
||||
Vec3.normalize(out.n, Vec3.cross(out.n, v1, v2));
|
||||
out.d = Vec3.dot(out.n, a);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Set the components of a plane to the given values
|
||||
* !#zh
|
||||
* 将给定平面的属性设置为给定值。
|
||||
* @method set
|
||||
* @param {Plane} out The object that accepts the action.
|
||||
* @param {Number} nx The x part of the normal component.
|
||||
* @param {Number} ny The y part of the normal component.
|
||||
* @param {Number} nz The z part of the normal component.
|
||||
* @param {Number} d Distance from the origin.
|
||||
* @return {Plane} out The object that accepts the action.
|
||||
*/
|
||||
public static set (out: plane, nx: number, ny: number, nz: number, d: number) {
|
||||
out.n.x = nx;
|
||||
out.n.y = ny;
|
||||
out.n.z = nz;
|
||||
out.d = d;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create plane from normal and point
|
||||
* !#zh
|
||||
* 用一条法线和一个点创建平面。
|
||||
* @method fromNormalAndPoint
|
||||
* @param {Plane} out The object that accepts the action.
|
||||
* @param {Vec3} normal The normal of a plane.
|
||||
* @param {Vec3} point A point on the plane.
|
||||
* @return {Plane} out The object that accepts the action.
|
||||
*/
|
||||
public static fromNormalAndPoint (out: plane, normal: Vec3, point: Vec3) {
|
||||
Vec3.copy(out.n, normal);
|
||||
out.d = Vec3.dot(normal, point);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* normalize a plane
|
||||
* !#zh
|
||||
* 归一化一个平面。
|
||||
* @method normalize
|
||||
* @param {Plane} out The object that accepts the action.
|
||||
* @param {Plane} a Source data for operations.
|
||||
* @return {Plane} out The object that accepts the action.
|
||||
*/
|
||||
public static normalize (out: plane, a: plane) {
|
||||
const len = a.n.len();
|
||||
Vec3.normalize(out.n, a.n);
|
||||
if (len > 0) {
|
||||
out.d = a.d / len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* A normal vector.
|
||||
* !#zh
|
||||
* 法线向量。
|
||||
* @property {Vec3} n
|
||||
*/
|
||||
public n: Vec3;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* The distance from the origin to the plane.
|
||||
* !#zh
|
||||
* 原点到平面的距离。
|
||||
* @property {number} d
|
||||
*/
|
||||
public d: number;
|
||||
|
||||
private _type: number;
|
||||
|
||||
/**
|
||||
* !#en Construct a plane.
|
||||
* !#zh 构造一个平面。
|
||||
* @constructor
|
||||
* @param {Number} nx The x part of the normal component.
|
||||
* @param {Number} ny The y part of the normal component.
|
||||
* @param {Number} nz The z part of the normal component.
|
||||
* @param {Number} d Distance from the origin.
|
||||
*/
|
||||
constructor (nx = 0, ny = 1, nz = 0, d = 0) {
|
||||
this._type = enums.SHAPE_PLANE;
|
||||
this.n = new Vec3(nx, ny, nz);
|
||||
this.d = d;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Transform a plane.
|
||||
* !#zh
|
||||
* 变换一个平面。
|
||||
* @method transform
|
||||
* @param {Mat4} 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;
|
||||
}
|
||||
}
|
181
engine/cocos2d/core/geom-utils/ray.ts
Normal file
181
engine/cocos2d/core/geom-utils/ray.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { Vec3 } from '../value-types';
|
||||
import enums from './enums';
|
||||
import { IVec3Like } from '../value-types/math';
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* ray
|
||||
* !#zh
|
||||
* 射线。
|
||||
* @class geomUtils.Ray
|
||||
*/
|
||||
export default class ray {
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a new ray
|
||||
* !#zh
|
||||
* 创建一条射线。
|
||||
* @method create
|
||||
* @param {number} ox The x part of the starting point.
|
||||
* @param {number} oy The y part of the starting point.
|
||||
* @param {number} oz The z part of the starting point.
|
||||
* @param {number} dx X in the direction.
|
||||
* @param {number} dy Y in the direction.
|
||||
* @param {number} dz Z in the direction.
|
||||
* @return {Ray}
|
||||
*/
|
||||
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
|
||||
* !#zh
|
||||
* 从一条射线克隆出一条新的射线。
|
||||
* @method clone
|
||||
* @param {Ray} a Clone target
|
||||
* @return {Ray} Clone result
|
||||
*/
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Copy the values from one ray to another
|
||||
* !#zh
|
||||
* 将从一个 ray 的值复制到另一个 ray。
|
||||
* @method copy
|
||||
* @param {Ray} out Accept the ray of the operation.
|
||||
* @param {Ray} a Copied ray.
|
||||
* @return {Ray} out Accept the ray of the operation.
|
||||
*/
|
||||
public static copy (out: ray, a: ray): ray {
|
||||
Vec3.copy(out.o, a.o);
|
||||
Vec3.copy(out.d, a.d);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a ray from two points
|
||||
* !#zh
|
||||
* 用两个点创建一条射线。
|
||||
* @method fromPoints
|
||||
* @param {Ray} out Receive the operating ray.
|
||||
* @param {Vec3} origin Origin of ray
|
||||
* @param {Vec3} target A point on a ray.
|
||||
* @return {Ray} out Receive the operating ray.
|
||||
*/
|
||||
public static fromPoints (out: ray, origin: Vec3, target: Vec3): ray {
|
||||
Vec3.copy(out.o, origin);
|
||||
Vec3.normalize(out.d, Vec3.subtract(out.d, target, origin));
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Set the components of a ray to the given values
|
||||
* !#zh
|
||||
* 将给定射线的属性设置为给定的值。
|
||||
* @method set
|
||||
* @param {Ray} out Receive the operating ray.
|
||||
* @param {number} ox The x part of the starting point.
|
||||
* @param {number} oy The y part of the starting point.
|
||||
* @param {number} oz The z part of the starting point.
|
||||
* @param {number} dx X in the direction.
|
||||
* @param {number} dy Y in the direction.
|
||||
* @param {number} dz Z in the direction.
|
||||
* @return {Ray} out Receive the operating ray.
|
||||
*/
|
||||
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;
|
||||
out.d.x = dx;
|
||||
out.d.y = dy;
|
||||
out.d.z = dz;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Start point.
|
||||
* !#zh
|
||||
* 起点。
|
||||
* @property {Vec3} o
|
||||
*/
|
||||
public o: Vec3;
|
||||
|
||||
/**
|
||||
* !#e
|
||||
* Direction
|
||||
* !#zh
|
||||
* 方向。
|
||||
* @property {Vec3} d
|
||||
*/
|
||||
public d: Vec3;
|
||||
|
||||
private _type: number;
|
||||
|
||||
/**
|
||||
* !#en Construct a ray.
|
||||
* !#zh 构造一条射线。
|
||||
* @constructor
|
||||
* @param {number} ox The x part of the starting point.
|
||||
* @param {number} oy The y part of the starting point.
|
||||
* @param {number} oz The z part of the starting point.
|
||||
* @param {number} dx X in the direction.
|
||||
* @param {number} dy Y in the direction.
|
||||
* @param {number} dz Z in the direction.
|
||||
*/
|
||||
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 = new Vec3(ox, oy, oz);
|
||||
this.d = new Vec3(dx, dy, dz);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Compute hit.
|
||||
* @method computeHit
|
||||
* @param {IVec3Like} out
|
||||
* @param {number} distance
|
||||
*/
|
||||
public computeHit (out: IVec3Like, distance: number) {
|
||||
Vec3.normalize(out, this.d)
|
||||
Vec3.scaleAndAdd(out, this.o, out, distance);
|
||||
}
|
||||
}
|
236
engine/cocos2d/core/geom-utils/sphere.ts
Normal file
236
engine/cocos2d/core/geom-utils/sphere.ts
Normal file
@@ -0,0 +1,236 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { Mat4, Quat, Vec3 } from '../value-types';
|
||||
import enums from './enums';
|
||||
|
||||
const _v3_tmp = new Vec3();
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Sphere.
|
||||
* !#zh
|
||||
* 轴对齐球。
|
||||
* @class geomUtils.Sphere
|
||||
*/
|
||||
export default class sphere {
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a new sphere
|
||||
* !#zh
|
||||
* 创建一个新的 sphere 实例。
|
||||
* @method create
|
||||
* @param cx X coordinates of the shape relative to the origin.
|
||||
* @param cy Y coordinates of the shape relative to the origin.
|
||||
* @param cz Z coordinates of the shape relative to the origin.
|
||||
* @param r Radius of sphere
|
||||
* @return {Sphere} Returns a 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
|
||||
* !#zh
|
||||
* 克隆一个新的 sphere 实例。
|
||||
* @method clone
|
||||
* @param {Sphere} p The target of cloning.
|
||||
* @return {Sphere} The cloned instance.
|
||||
*/
|
||||
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
|
||||
* !#zh
|
||||
* 将从一个 sphere 的值复制到另一个 sphere。
|
||||
* @method copy
|
||||
* @param {Sphere} out Accept the sphere of operations.
|
||||
* @param {Sphere} a Sphere being copied.
|
||||
* @return {Sphere} out Accept the sphere of operations.
|
||||
*/
|
||||
public static copy (out: sphere, p: sphere): sphere {
|
||||
Vec3.copy(out.center, p.center);
|
||||
out.radius = p.radius;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* create a new bounding sphere from two corner points
|
||||
* !#zh
|
||||
* 从两个点创建一个新的 sphere。
|
||||
* @method fromPoints
|
||||
* @param out - Accept the sphere of operations.
|
||||
* @param minPos - The smallest point of sphere.
|
||||
* @param maxPos - The maximum point of sphere.
|
||||
* @returns {Sphere} out Accept the sphere of operations.
|
||||
*/
|
||||
public static fromPoints (out: sphere, minPos: Vec3, maxPos: Vec3): sphere {
|
||||
Vec3.multiplyScalar(out.center, Vec3.add(_v3_tmp, minPos, maxPos), 0.5);
|
||||
out.radius = Vec3.subtract(_v3_tmp, maxPos, minPos).len() * 0.5;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Set the components of a sphere to the given values
|
||||
* !#zh 将球体的属性设置为给定的值。
|
||||
* @method set
|
||||
* @param {Sphere} out Accept the sphere of operations.
|
||||
* @param cx X coordinates of the shape relative to the origin.
|
||||
* @param cy Y coordinates of the shape relative to the origin.
|
||||
* @param cz Z coordinates of the shape relative to the origin.
|
||||
* @param {number} r Radius.
|
||||
* @return {Sphere} out Accept the sphere of operations.
|
||||
*/
|
||||
public static set (out: sphere, cx: number, cy: number, cz: number, r: number): sphere {
|
||||
out.center.x = cx;
|
||||
out.center.y = cy;
|
||||
out.center.z = cz;
|
||||
out.radius = r;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* The center of the local coordinate.
|
||||
* !#zh
|
||||
* 本地坐标的中心点。
|
||||
* @property {Vec3} center
|
||||
*/
|
||||
public center: Vec3;
|
||||
|
||||
/**
|
||||
* !#zh
|
||||
* 半径。
|
||||
* @property {number} radius
|
||||
*/
|
||||
public radius: number;
|
||||
|
||||
protected _type: number;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Construct a sphere.
|
||||
* !#zh
|
||||
* 构造一个球。
|
||||
* @constructor
|
||||
* @param cx The x-coordinate of the sphere's world coordinates.
|
||||
* @param cy The y-coordinate of the sphere's world coordinates.
|
||||
* @param cz The z-coordinate of the sphere's world coordinates.
|
||||
* @param {number} r Radius.
|
||||
*/
|
||||
constructor (cx: number = 0, cy: number = 0, cz: number = 0, r: number = 1) {
|
||||
this._type = enums.SHAPE_SPHERE;
|
||||
this.center = new Vec3(cx, cy, cz);
|
||||
this.radius = r;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Clone.
|
||||
* !#zh
|
||||
* 获得克隆。
|
||||
* @method clone
|
||||
*/
|
||||
public clone () {
|
||||
return sphere.clone(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Copy sphere
|
||||
* !#zh
|
||||
* 拷贝对象。
|
||||
* @method copy
|
||||
* @param a Copy target.
|
||||
*/
|
||||
public copy (a: sphere) {
|
||||
return sphere.copy(this, a);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Get the bounding points of this shape
|
||||
* !#zh
|
||||
* 获取此形状的边界点。
|
||||
* @method getBoundary
|
||||
* @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);
|
||||
Vec3.set(maxPos, this.center.x + this.radius, this.center.y + this.radius, this.center.z + this.radius);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Transform this shape
|
||||
* !#zh
|
||||
* 将 out 根据这个 sphere 的数据进行变换。
|
||||
* @method transform
|
||||
* @param m The transformation matrix.
|
||||
* @param pos The position part of the transformation.
|
||||
* @param rot The rotating part of the transformation.
|
||||
* @param scale The scaling part of the transformation.
|
||||
* @param out The target of the transformation.
|
||||
*/
|
||||
public transform (m: Mat4, pos: Vec3, rot: Quat, scale: Vec3, out: sphere) {
|
||||
Vec3.transformMat4(out.center, this.center, m);
|
||||
out.radius = this.radius * scale.maxAxis();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#zh
|
||||
* 将 out 根据这个 sphere 的数据进行变换。
|
||||
* @translateAndRotate
|
||||
* @param m The transformation matrix.
|
||||
* @param rot The rotating part of the transformation.
|
||||
* @param out The target of the transformation.
|
||||
*/
|
||||
public translateAndRotate (m: Mat4, rot: Quat, out: sphere){
|
||||
Vec3.transformMat4(out.center, this.center, m);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Scale out based on the sphere data.
|
||||
* !#zh
|
||||
* 将 out 根据这个 sphere 的数据进行缩放。
|
||||
* @method setScale
|
||||
* @param scale Scale value
|
||||
* @param out Scale target
|
||||
*/
|
||||
public setScale (scale: Vec3, out: sphere) {
|
||||
out.radius = this.radius * scale.maxAxis();
|
||||
}
|
||||
}
|
167
engine/cocos2d/core/geom-utils/triangle.ts
Normal file
167
engine/cocos2d/core/geom-utils/triangle.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import Vec3 from '../value-types/vec3';
|
||||
import enums from './enums';
|
||||
|
||||
/**
|
||||
* Triangle
|
||||
* @class geomUtils.Triangle
|
||||
*/
|
||||
export default class triangle {
|
||||
|
||||
/**
|
||||
* create a new triangle
|
||||
* @method create
|
||||
* @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 {geomUtils.Triangle}
|
||||
*/
|
||||
public static create (ax, ay, az, bx, by, bz, cx, cy, cz) {
|
||||
return new triangle(ax, ay, az, bx, by, bz, cx, cy, cz);
|
||||
}
|
||||
|
||||
/**
|
||||
* clone a new triangle
|
||||
* @method clone
|
||||
* @param {geomUtils.Triangle} t the source plane
|
||||
* @return {geomUtils.Triangle}
|
||||
*/
|
||||
public static clone (t) {
|
||||
return new triangle(
|
||||
t.a.x, t.a.y, t.a.z,
|
||||
t.b.x, t.b.y, t.b.z,
|
||||
t.c.x, t.c.y, t.c.z
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* copy the values from one triangle to another
|
||||
* @method copy
|
||||
* @param {geomUtils.Triangle} out the receiving triangle
|
||||
* @param {geomUtils.Triangle} t the source triangle
|
||||
* @return {geomUtils.Triangle}
|
||||
*/
|
||||
public static copy (out, t) {
|
||||
Vec3.copy(out.a, t.a);
|
||||
Vec3.copy(out.b, t.b);
|
||||
Vec3.copy(out.c, t.c);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a triangle from three points
|
||||
* @method fromPoints
|
||||
* @param {geomUtils.Triangle} out the receiving triangle
|
||||
* @param {Vec3} a
|
||||
* @param {Vec3} b
|
||||
* @param {Vec3} c
|
||||
* @return {geomUtils.Triangle}
|
||||
*/
|
||||
public static fromPoints (out, a, b, c) {
|
||||
Vec3.copy(out.a, a);
|
||||
Vec3.copy(out.b, b);
|
||||
Vec3.copy(out.c, c);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the components of a triangle to the given values
|
||||
*
|
||||
* @method set
|
||||
* @param {geomUtils.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}
|
||||
*/
|
||||
public static set (out, ax, ay, az, bx, by, bz, cx, cy, cz) {
|
||||
out.a.x = ax;
|
||||
out.a.y = ay;
|
||||
out.a.z = az;
|
||||
|
||||
out.b.x = bx;
|
||||
out.b.y = by;
|
||||
out.b.z = bz;
|
||||
|
||||
out.c.x = cx;
|
||||
out.c.y = cy;
|
||||
out.c.z = cz;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @property {Vec3} a
|
||||
*/
|
||||
a: Vec3;
|
||||
/**
|
||||
* @property {Vec3} b
|
||||
*/
|
||||
b: Vec3;
|
||||
/**
|
||||
* @property {Vec3} c
|
||||
*/
|
||||
c: Vec3;
|
||||
|
||||
/**
|
||||
* geometry type
|
||||
*/
|
||||
_type: number;
|
||||
|
||||
/**
|
||||
* create a new triangle
|
||||
* @constructor
|
||||
* @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
|
||||
*/
|
||||
constructor (ax: number, ay: number, az: number, bx: number, by: number, bz: number, cx: number, cy: number, cz: number) {
|
||||
this.a = new Vec3(ax, ay, az);
|
||||
this.b = new Vec3(bx, by, bz);
|
||||
this.c = new Vec3(cx, cy, cz);
|
||||
this._type = enums.SHAPE_TRIANGLE;;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user