Amp-Thread-ID: https://ampcode.com/threads/T-01a02485-5574-707c-bff4-5668d83bee8a Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
32 lines
967 B
TypeScript
32 lines
967 B
TypeScript
import { Mesh, type MeshOptions } from "../Mesh";
|
|
import type { Scene } from "../Scene";
|
|
|
|
export type PlaneOptions = Omit<MeshOptions, "geometryId" | "vertexData"> & {
|
|
size?: number;
|
|
width?: number;
|
|
depth?: number;
|
|
};
|
|
|
|
function dimension(value: number, name: string) {
|
|
if (!Number.isFinite(value) || value <= 0) throw new RangeError(name);
|
|
return value / 2;
|
|
}
|
|
|
|
/** A horizontal, upward-facing plane centered on its transform. */
|
|
export class Plane extends Mesh {
|
|
constructor(scene: Scene, options: PlaneOptions = {}) {
|
|
const { size = 1, width = size, depth = size, ...meshOptions } = options;
|
|
const x = dimension(width, "width");
|
|
const z = dimension(depth, "depth");
|
|
super(scene, {
|
|
...meshOptions,
|
|
vertexData: {
|
|
positions: [-x, 0, -z, x, 0, -z, x, 0, z, -x, 0, z],
|
|
normals: [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0],
|
|
uvs: [0, 0, 1, 0, 1, 1, 0, 1],
|
|
indices: [0, 2, 1, 0, 3, 2],
|
|
},
|
|
});
|
|
}
|
|
}
|