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
79 lines
1.8 KiB
TypeScript
79 lines
1.8 KiB
TypeScript
/**
|
|
* @category 3d/primitive
|
|
*/
|
|
|
|
import { Vec3 } from '../../core/math';
|
|
import { IGeometry, IGeometryOptions } from './define';
|
|
|
|
/**
|
|
* @zh
|
|
* 球参数选项。
|
|
*/
|
|
interface ISphereOptions extends IGeometryOptions {
|
|
segments: number;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 生成一个球。
|
|
* @param radius 球半径。
|
|
* @param options 参数选项。
|
|
*/
|
|
export default function (radius = 0.5, opts: RecursivePartial<ISphereOptions> = {}): IGeometry {
|
|
const segments = opts.segments !== undefined ? opts.segments : 32;
|
|
|
|
// lat === latitude
|
|
// lon === longitude
|
|
|
|
const positions: number[] = [];
|
|
const normals: number[] = [];
|
|
const uvs: number[] = [];
|
|
const indices: number[] = [];
|
|
const minPos = new Vec3(-radius, -radius, -radius);
|
|
const maxPos = new Vec3(radius, radius, radius);
|
|
const boundingRadius = radius;
|
|
|
|
for (let lat = 0; lat <= segments; ++lat) {
|
|
const theta = lat * Math.PI / segments;
|
|
const sinTheta = Math.sin(theta);
|
|
const cosTheta = -Math.cos(theta);
|
|
|
|
for (let lon = 0; lon <= segments; ++lon) {
|
|
const phi = lon * 2 * Math.PI / segments - Math.PI / 2.0;
|
|
const sinPhi = Math.sin(phi);
|
|
const cosPhi = Math.cos(phi);
|
|
|
|
const x = sinPhi * sinTheta;
|
|
const y = cosTheta;
|
|
const z = cosPhi * sinTheta;
|
|
const u = lon / segments;
|
|
const v = lat / segments;
|
|
|
|
positions.push(x * radius, y * radius, z * radius);
|
|
normals.push(x, y, z);
|
|
uvs.push(u, v);
|
|
|
|
if ((lat < segments) && (lon < segments)) {
|
|
const seg1 = segments + 1;
|
|
const a = seg1 * lat + lon;
|
|
const b = seg1 * (lat + 1) + lon;
|
|
const c = seg1 * (lat + 1) + lon + 1;
|
|
const d = seg1 * lat + lon + 1;
|
|
|
|
indices.push(a, d, b);
|
|
indices.push(d, c, b);
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
positions,
|
|
indices,
|
|
normals,
|
|
uvs,
|
|
minPos,
|
|
maxPos,
|
|
boundingRadius,
|
|
};
|
|
}
|