perf: faster still
This commit is contained in:
parent
ef5029da1d
commit
67cd54fe33
8 changed files with 280 additions and 203 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { Vec3 } from "../math/Vec3"
|
||||
import type { Mesh } from "./Mesh"
|
||||
import { STRIDE, type Mesh } from "./Mesh"
|
||||
|
||||
const TAU = Math.PI * 2
|
||||
|
||||
|
|
@ -95,13 +95,15 @@ export namespace Tree {
|
|||
const axis = Vec3.normalize(Vec3.sub(b, a))
|
||||
const [u, v] = basis(axis)
|
||||
const len = Vec3.length(Vec3.sub(b, a))
|
||||
const start = mesh.vertices.length
|
||||
const start = mesh.verts.length / STRIDE
|
||||
for (let i = 0; i <= sides; i++) {
|
||||
const angle = (i / sides) * TAU
|
||||
const dir = Vec3.add(Vec3.scale(u, Math.cos(angle)), Vec3.scale(v, Math.sin(angle)))
|
||||
const dx = u.x * Math.cos(angle) + v.x * Math.sin(angle)
|
||||
const dy = u.y * Math.cos(angle) + v.y * Math.sin(angle)
|
||||
const dz = u.z * Math.cos(angle) + v.z * Math.sin(angle)
|
||||
const s = i / sides
|
||||
mesh.vertices.push({ pos: Vec3.add(a, Vec3.scale(dir, ra)), uv: { x: s * 1.5, y: 0 } })
|
||||
mesh.vertices.push({ pos: Vec3.add(b, Vec3.scale(dir, rb)), uv: { x: s * 1.5, y: len * 0.5 } })
|
||||
mesh.verts.push(a.x + dx * ra, a.y + dy * ra, a.z + dz * ra, s * 1.5, 0)
|
||||
mesh.verts.push(b.x + dx * rb, b.y + dy * rb, b.z + dz * rb, s * 1.5, len * 0.5)
|
||||
}
|
||||
for (let i = 0; i < sides; i++) {
|
||||
const p = start + i * 2
|
||||
|
|
@ -111,17 +113,15 @@ export namespace Tree {
|
|||
|
||||
/** A cone standing on a base ring, apex `height` above it (one spruce tier). */
|
||||
function cone(mesh: Mesh, base: Vec3, height: number, radius: number, sides: number): void {
|
||||
const start = mesh.vertices.length
|
||||
mesh.vertices.push({ pos: { x: base.x, y: base.y + height, z: base.z }, uv: { x: 0.5, y: 0 } })
|
||||
const start = mesh.verts.length / STRIDE
|
||||
mesh.verts.push(base.x, base.y + height, base.z, 0.5, 0)
|
||||
for (let i = 0; i <= sides; i++) {
|
||||
const angle = (i / sides) * TAU
|
||||
mesh.vertices.push({
|
||||
pos: { x: base.x + Math.cos(angle) * radius, y: base.y, z: base.z + Math.sin(angle) * radius },
|
||||
uv: { x: (i / sides) * 2, y: 1 },
|
||||
})
|
||||
mesh.verts.push(base.x + Math.cos(angle) * radius, base.y, base.z + Math.sin(angle) * radius, (i / sides) * 2, 1)
|
||||
}
|
||||
for (let i = 0; i < sides; i++) {
|
||||
mesh.indices.push(start, start + 1 + i, start + 2 + i)
|
||||
// Wound so the outer surface faces out, matching the backface-cull sign.
|
||||
mesh.indices.push(start, start + 2 + i, start + 1 + i)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -130,7 +130,7 @@ export namespace Tree {
|
|||
function blob(mesh: Mesh, center: Vec3, radius: number, rand: () => number): void {
|
||||
const seg = 5
|
||||
const rings = 3
|
||||
const start = mesh.vertices.length
|
||||
const start = mesh.verts.length / STRIDE
|
||||
for (let r = 0; r <= rings; r++) {
|
||||
const phi = (r / rings) * Math.PI
|
||||
const cy = Math.cos(phi)
|
||||
|
|
@ -138,14 +138,13 @@ export namespace Tree {
|
|||
const scale = radius * (0.85 + rand() * 0.3)
|
||||
for (let s = 0; s <= seg; s++) {
|
||||
const theta = (s / seg) * TAU
|
||||
mesh.vertices.push({
|
||||
pos: {
|
||||
x: center.x + cr * Math.cos(theta) * scale,
|
||||
y: center.y + cy * scale,
|
||||
z: center.z + cr * Math.sin(theta) * scale,
|
||||
},
|
||||
uv: { x: (s / seg) * 2, y: (r / rings) * 2 },
|
||||
})
|
||||
mesh.verts.push(
|
||||
center.x + cr * Math.cos(theta) * scale,
|
||||
center.y + cy * scale,
|
||||
center.z + cr * Math.sin(theta) * scale,
|
||||
(s / seg) * 2,
|
||||
(r / rings) * 2,
|
||||
)
|
||||
}
|
||||
}
|
||||
const row = seg + 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue