mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
4bc2739786
* Vmath (#4895) * vmath refactor * value-type API change * vmath change update * conflict fix * value types change * translate modify (#4917) * value types bug fixed (#4940) * playground update (#4946) * bug fixed * bug fixed * bug fixed * bug fix * util modify * create remove & ccenum rename * export by cc.math * Rename value-types to math * util funtion deprecated warn * value-types change * color test fix * color API change
184 lines
4.5 KiB
TypeScript
184 lines
4.5 KiB
TypeScript
/**
|
|
* @category 3d/primitive
|
|
*/
|
|
|
|
import { Vec3 } from '../../core/math';
|
|
import { IGeometry, IGeometryOptions } from './define';
|
|
|
|
/**
|
|
* @zh
|
|
* 立方体参数选项。
|
|
*/
|
|
interface IBoxOptions extends RecursivePartial<IGeometryOptions> {
|
|
/**
|
|
* @en
|
|
* Box extent on X-axis.
|
|
* @zh
|
|
* 立方体宽度。
|
|
*/
|
|
width?: number;
|
|
|
|
/**
|
|
* @en
|
|
* Box extent on Y-axis.
|
|
* @zh
|
|
* 立方体高度。
|
|
*/
|
|
height?: number;
|
|
|
|
/**
|
|
* @en
|
|
* Box extent on Z-axis.
|
|
* @zh
|
|
* 立方体长度。
|
|
*/
|
|
length?: number;
|
|
|
|
/**
|
|
* @en
|
|
* Segment count on X-axis.
|
|
* @zh
|
|
* 宽度线段数。
|
|
*/
|
|
widthSegments?: number;
|
|
|
|
/**
|
|
* @en
|
|
* Segment count on Y-axis.
|
|
* @zh
|
|
* 高度线段数。
|
|
*/
|
|
heightSegments?: number;
|
|
|
|
/**
|
|
* @en
|
|
* Segment count on Z-axis.
|
|
* @zh
|
|
* 长度线段数。
|
|
*/
|
|
lengthSegments?: number;
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* This function generates a box with specified extents and centered at origin,
|
|
* but may be repositioned through `center` option).
|
|
* @zh
|
|
* 生成一个立方体,其大小是定义的范围且中心在原点。
|
|
* @param options 参数选项。
|
|
*/
|
|
export default function box (options?: IBoxOptions): IGeometry {
|
|
options = options || {};
|
|
const ws = options.widthSegments || 1;
|
|
const hs = options.heightSegments || 1;
|
|
const ls = options.lengthSegments || 1;
|
|
|
|
const hw = (options.width || 1) / 2;
|
|
const hh = (options.height || 1) / 2;
|
|
const hl = (options.length || 1) / 2;
|
|
|
|
const corners = [
|
|
Vec3.set(c0, -hw, -hh, hl),
|
|
Vec3.set(c1, hw, -hh, hl),
|
|
Vec3.set(c2, hw, hh, hl),
|
|
Vec3.set(c3, -hw, hh, hl),
|
|
Vec3.set(c4, hw, -hh, -hl),
|
|
Vec3.set(c5, -hw, -hh, -hl),
|
|
Vec3.set(c6, -hw, hh, -hl),
|
|
Vec3.set(c7, hw, hh, -hl),
|
|
];
|
|
|
|
const faceAxes = [
|
|
[2, 3, 1], // FRONT
|
|
[4, 5, 7], // BACK
|
|
[7, 6, 2], // TOP
|
|
[1, 0, 4], // BOTTOM
|
|
[1, 4, 2], // RIGHT
|
|
[5, 0, 6], // LEFT
|
|
];
|
|
|
|
const faceNormals = [
|
|
[0, 0, 1], // FRONT
|
|
[0, 0, -1], // BACK
|
|
[0, 1, 0], // TOP
|
|
[0, -1, 0], // BOTTOM
|
|
[1, 0, 0], // RIGHT
|
|
[-1, 0, 0], // LEFT
|
|
];
|
|
|
|
const positions: number[] = [];
|
|
const normals: number[] = [];
|
|
const uvs: number[] = [];
|
|
const indices: number[] = [];
|
|
const minPos = new Vec3(-hw, -hh, -hl);
|
|
const maxPos = new Vec3(hw, hh, hl);
|
|
const boundingRadius = Math.sqrt(hw * hw + hh * hh + hl * hl);
|
|
|
|
function _buildPlane (side: number, uSegments: number, vSegments: number) {
|
|
let u: number;
|
|
let v: number;
|
|
let ix: number;
|
|
let iy: number;
|
|
const offset = positions.length / 3;
|
|
const faceAxe = faceAxes[side];
|
|
const faceNormal = faceNormals[side];
|
|
|
|
for (iy = 0; iy <= vSegments; iy++) {
|
|
for (ix = 0; ix <= uSegments; ix++) {
|
|
u = ix / uSegments;
|
|
v = iy / vSegments;
|
|
|
|
Vec3.lerp(temp1, corners[faceAxe[0]], corners[faceAxe[1]], u);
|
|
Vec3.lerp(temp2, corners[faceAxe[0]], corners[faceAxe[2]], v);
|
|
Vec3.subtract(temp3, temp2, corners[faceAxe[0]]);
|
|
Vec3.add(r, temp1, temp3);
|
|
|
|
positions.push(r.x, r.y, r.z);
|
|
normals.push(faceNormal[0], faceNormal[1], faceNormal[2]);
|
|
uvs.push(u, v);
|
|
|
|
if ((ix < uSegments) && (iy < vSegments)) {
|
|
const useg1 = uSegments + 1;
|
|
const a = ix + iy * useg1;
|
|
const b = ix + (iy + 1) * useg1;
|
|
const c = (ix + 1) + (iy + 1) * useg1;
|
|
const d = (ix + 1) + iy * useg1;
|
|
|
|
indices.push(offset + a, offset + d, offset + b);
|
|
indices.push(offset + b, offset + d, offset + c);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
_buildPlane(0, ws, hs); // FRONT
|
|
_buildPlane(4, ls, hs); // RIGHT
|
|
_buildPlane(1, ws, hs); // BACK
|
|
_buildPlane(5, ls, hs); // LEFT
|
|
_buildPlane(3, ws, ls); // BOTTOM
|
|
_buildPlane(2, ws, ls); // TOP
|
|
|
|
return {
|
|
positions,
|
|
normals,
|
|
uvs,
|
|
indices,
|
|
minPos,
|
|
maxPos,
|
|
boundingRadius,
|
|
};
|
|
}
|
|
|
|
const temp1 = new Vec3();
|
|
const temp2 = new Vec3();
|
|
const temp3 = new Vec3();
|
|
const r = new Vec3();
|
|
const c0 = new Vec3();
|
|
const c1 = new Vec3();
|
|
const c2 = new Vec3();
|
|
const c3 = new Vec3();
|
|
const c4 = new Vec3();
|
|
const c5 = new Vec3();
|
|
const c6 = new Vec3();
|
|
const c7 = new Vec3();
|