@wildshard/shard-format 0.48.0 → 0.48.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wildshard/shard-format",
|
|
3
|
-
"version": "0.48.
|
|
3
|
+
"version": "0.48.1",
|
|
4
4
|
"description": "The raw shardfile format: the spec, the validator and a validate CLI. The public, versioned contract every other repo builds on.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ShardEntryways } from './entryways.ts';
|
|
2
2
|
import type { ShardProps } from './props.ts';
|
|
3
|
+
import { propHullBounds } from './prop-hull.ts';
|
|
3
4
|
import type { ShardWater } from './water.ts';
|
|
4
5
|
import { entryFootprints, clipEntryPolygon, obstructsEntry, type EntryVertex, type EntryFootprint } from './entryGeometry.ts';
|
|
5
6
|
import { PropExtent, visitGlbTriangles } from './shardfile-assets.ts';
|
|
@@ -51,6 +52,7 @@ export function validateEntrywayClearance(source: { entryways: ShardEntryways; p
|
|
|
51
52
|
// Hidden/inactive panels can become active, so their authored shapes still count.
|
|
52
53
|
for (const row of props.colliders) for (const shape of row.shapes) {
|
|
53
54
|
if (shape.kind === 'box') boxTriangles(shape, inspect);
|
|
55
|
+
else if (shape.kind === 'hull') { boxTriangles(propHullBounds(shape), inspect); }
|
|
54
56
|
else {
|
|
55
57
|
const dx = shape.to.x - shape.from.x, dz = shape.to.z - shape.from.z, run = Math.hypot(dx, dz) / shape.count, rise = (shape.to.y - shape.from.y) / shape.count;
|
|
56
58
|
for (let i = 0; i < shape.count; i++) {
|
|
@@ -39,7 +39,7 @@ export function validateSocketLandings(source: { entryways: ShardEntryways; terr
|
|
|
39
39
|
if (!row.initialActive || row.panel !== null) continue;
|
|
40
40
|
for (const shape of row.shapes) {
|
|
41
41
|
if (shape.kind === 'box') decks.push(...boxTop(shape));
|
|
42
|
-
else {
|
|
42
|
+
else if (shape.kind === 'treads') {
|
|
43
43
|
const dx = shape.to.x - shape.from.x, dz = shape.to.z - shape.from.z, run = Math.hypot(dx, dz) / shape.count, rise = (shape.to.y - shape.from.y) / shape.count;
|
|
44
44
|
for (let i = 0; i < shape.count; i++) {
|
|
45
45
|
const f = (i + 0.5) / shape.count, hy = Math.max(0.01, rise * (i + 1) / 2);
|
|
@@ -22,6 +22,7 @@ function boxTop(box: Box): EntryVertex[][] {
|
|
|
22
22
|
function staticTops(rows: readonly ShardProps['colliders'][number][]): EntryVertex[][] {
|
|
23
23
|
return rows.flatMap(row => row.shapes.flatMap(shape => {
|
|
24
24
|
if (shape.kind === 'box') return boxTop(shape);
|
|
25
|
+
if (shape.kind === 'hull') { return []; }
|
|
25
26
|
const dx = shape.to.x - shape.from.x, dz = shape.to.z - shape.from.z, run = Math.hypot(dx, dz) / shape.count, rise = (shape.to.y - shape.from.y) / shape.count;
|
|
26
27
|
return Array.from({ length: shape.count }, (_unused, i) => {
|
|
27
28
|
const f = (i + 0.5) / shape.count, hy = Math.max(0.01, rise * (i + 1) / 2);
|
|
@@ -24,7 +24,7 @@ export function validatePortalFloors(entries: readonly PortalLinkEntry[], source
|
|
|
24
24
|
if (!row.initialActive || row.panel !== null) throw new Error('Portal floor is not permanent');
|
|
25
25
|
for (const shape of row.shapes) {
|
|
26
26
|
if (shape.kind === 'box') result.push(...boxTop(shape));
|
|
27
|
-
else {
|
|
27
|
+
else if (shape.kind === 'treads') {
|
|
28
28
|
const dx = shape.to.x - shape.from.x, dz = shape.to.z - shape.from.z, run = Math.hypot(dx, dz) / shape.count, rise = (shape.to.y - shape.from.y) / shape.count;
|
|
29
29
|
for (let i = 0; i < shape.count; i++) {
|
|
30
30
|
const f = (i + 0.5) / shape.count, hy = Math.max(0.01, rise * (i + 1) / 2);
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/* oxlint-disable id-length -- The collision and GLB wire contracts use x, y, z, w and half extents. */
|
|
2
|
+
import { CELL_ABOVE, CELL_BELOW } from './contentBounds.ts'
|
|
3
|
+
import { glbPoint, glbTransform } from './glbTriangles.ts'
|
|
4
|
+
|
|
5
|
+
import type { GlbVertex } from './glbTriangles.ts'
|
|
6
|
+
|
|
7
|
+
type HullPose = Readonly<GlbVertex> & {
|
|
8
|
+
readonly points: readonly number[]
|
|
9
|
+
readonly yaw?: number | undefined
|
|
10
|
+
readonly rot?: (Readonly<GlbVertex> & { readonly w: number }) | undefined
|
|
11
|
+
}
|
|
12
|
+
type HullBounds = GlbVertex & {
|
|
13
|
+
readonly kind: 'box'
|
|
14
|
+
readonly hx: number
|
|
15
|
+
readonly hy: number
|
|
16
|
+
readonly hz: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const CELL_HALF_WIDTH = 250
|
|
20
|
+
const DIAMETER = 2
|
|
21
|
+
const ZERO = 0
|
|
22
|
+
const CELL_WIDTH = CELL_HALF_WIDTH * DIAMETER
|
|
23
|
+
const LOCAL_LIMIT = Math.hypot(CELL_WIDTH, CELL_ABOVE + CELL_BELOW, CELL_WIDTH)
|
|
24
|
+
const COMPONENTS = 3
|
|
25
|
+
const MIN_VERTICES = 4
|
|
26
|
+
const PLANE_EPSILON = 1e-5
|
|
27
|
+
|
|
28
|
+
function pointsInWorld(shape: HullPose): GlbVertex[] {
|
|
29
|
+
const halfYaw = (shape.yaw ?? ZERO) / DIAMETER
|
|
30
|
+
const rotation = shape.rot ?? {
|
|
31
|
+
x: 0,
|
|
32
|
+
y: Math.sin(halfYaw),
|
|
33
|
+
z: 0,
|
|
34
|
+
w: Math.cos(halfYaw),
|
|
35
|
+
}
|
|
36
|
+
const matrix = glbTransform({
|
|
37
|
+
translation: [shape.x, shape.y, shape.z],
|
|
38
|
+
rotation: [rotation.x, rotation.y, rotation.z, rotation.w],
|
|
39
|
+
})
|
|
40
|
+
const result: GlbVertex[] = []
|
|
41
|
+
for (let index = 0; index < shape.points.length; index += COMPONENTS) {
|
|
42
|
+
const [localX, localY, localZ] = shape.points.slice(
|
|
43
|
+
index,
|
|
44
|
+
index + COMPONENTS,
|
|
45
|
+
)
|
|
46
|
+
if (localX === undefined || localY === undefined || localZ === undefined) {
|
|
47
|
+
return []
|
|
48
|
+
}
|
|
49
|
+
result.push(
|
|
50
|
+
glbPoint(matrix, {
|
|
51
|
+
x: Math.fround(localX),
|
|
52
|
+
y: Math.fround(localY),
|
|
53
|
+
z: Math.fround(localZ),
|
|
54
|
+
}),
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
return result
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function insideCell(vertex: Readonly<GlbVertex>): boolean {
|
|
61
|
+
return (
|
|
62
|
+
Math.abs(vertex.x) <= CELL_HALF_WIDTH &&
|
|
63
|
+
Math.abs(vertex.z) <= CELL_HALF_WIDTH &&
|
|
64
|
+
vertex.y >= -CELL_BELOW &&
|
|
65
|
+
vertex.y <= CELL_ABOVE
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function longest(points: readonly Readonly<GlbVertex>[]): Readonly<GlbVertex> {
|
|
70
|
+
let vector = { x: 0, y: 0, z: 0 }
|
|
71
|
+
let maxLength = 0
|
|
72
|
+
for (const point of points) {
|
|
73
|
+
const length = point.x * point.x + point.y * point.y + point.z * point.z
|
|
74
|
+
if (length > maxLength) {
|
|
75
|
+
maxLength = length
|
|
76
|
+
vector = point
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return vector
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function hasVolume(
|
|
83
|
+
vertices: readonly Readonly<GlbVertex>[],
|
|
84
|
+
first: Readonly<GlbVertex>,
|
|
85
|
+
): boolean {
|
|
86
|
+
const offsets = vertices.map((point: Readonly<GlbVertex>): GlbVertex => ({
|
|
87
|
+
x: point.x - first.x,
|
|
88
|
+
y: point.y - first.y,
|
|
89
|
+
z: point.z - first.z,
|
|
90
|
+
}))
|
|
91
|
+
const axis = longest(offsets)
|
|
92
|
+
const normal = longest(
|
|
93
|
+
offsets.map((point: Readonly<GlbVertex>): GlbVertex => ({
|
|
94
|
+
x: axis.y * point.z - axis.z * point.y,
|
|
95
|
+
y: axis.z * point.x - axis.x * point.z,
|
|
96
|
+
z: axis.x * point.y - axis.y * point.x,
|
|
97
|
+
})),
|
|
98
|
+
)
|
|
99
|
+
const length = Math.hypot(normal.x, normal.y, normal.z)
|
|
100
|
+
return (
|
|
101
|
+
length > ZERO &&
|
|
102
|
+
offsets.some(
|
|
103
|
+
(point: Readonly<GlbVertex>): boolean =>
|
|
104
|
+
Math.abs(normal.x * point.x + normal.y * point.y + normal.z * point.z) >
|
|
105
|
+
PLANE_EPSILON * length,
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Require volume after rounding the local vertices to Rapier's float32 coordinates. */
|
|
111
|
+
function solidHullWithinCell(shape: HullPose): boolean {
|
|
112
|
+
const { points } = shape
|
|
113
|
+
if (
|
|
114
|
+
points.length < MIN_VERTICES * COMPONENTS ||
|
|
115
|
+
points.length % COMPONENTS !== ZERO ||
|
|
116
|
+
!points.every((value): boolean => Math.abs(value) <= LOCAL_LIMIT)
|
|
117
|
+
) {
|
|
118
|
+
return false
|
|
119
|
+
}
|
|
120
|
+
const posed = pointsInWorld(shape)
|
|
121
|
+
const [first] = posed
|
|
122
|
+
if (
|
|
123
|
+
first === undefined ||
|
|
124
|
+
!posed.every((vertex: Readonly<GlbVertex>): boolean => insideCell(vertex))
|
|
125
|
+
) {
|
|
126
|
+
return false
|
|
127
|
+
}
|
|
128
|
+
return hasVolume(posed, first)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function axisBounds(values: readonly number[]): readonly [number, number] {
|
|
132
|
+
return [Math.min(...values), Math.max(...values)]
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A conservative clearance envelope for the posed convex solid. */
|
|
136
|
+
function propHullBounds(shape: HullPose): HullBounds {
|
|
137
|
+
const points = pointsInWorld(shape)
|
|
138
|
+
const [minX, maxX] = axisBounds(
|
|
139
|
+
points.map((point: Readonly<GlbVertex>): number => point.x),
|
|
140
|
+
)
|
|
141
|
+
const [minY, maxY] = axisBounds(
|
|
142
|
+
points.map((point: Readonly<GlbVertex>): number => point.y),
|
|
143
|
+
)
|
|
144
|
+
const [minZ, maxZ] = axisBounds(
|
|
145
|
+
points.map((point: Readonly<GlbVertex>): number => point.z),
|
|
146
|
+
)
|
|
147
|
+
return {
|
|
148
|
+
kind: 'box',
|
|
149
|
+
x: (minX + maxX) / DIAMETER,
|
|
150
|
+
y: (minY + maxY) / DIAMETER,
|
|
151
|
+
z: (minZ + maxZ) / DIAMETER,
|
|
152
|
+
hx: (maxX - minX) / DIAMETER,
|
|
153
|
+
hy: (maxY - minY) / DIAMETER,
|
|
154
|
+
hz: (maxZ - minZ) / DIAMETER,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export { propHullBounds, solidHullWithinCell }
|
package/src/format/props.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { isJsonData } from './json.ts';
|
|
|
4
4
|
import { PropMaterialsSchema, propMaterialTextureRefs, validatePropMaterials } from './propMaterials.ts';
|
|
5
5
|
import { SplatTerrainSchema, validateSplatTerrain } from './splatTerrain.ts';
|
|
6
6
|
import { glbPoint, glbTransform } from './glbTriangles.ts';
|
|
7
|
+
import { solidHullWithinCell } from './prop-hull.ts';
|
|
8
|
+
import type { GlbVertex } from './glbTriangles.ts';
|
|
7
9
|
|
|
8
10
|
const ref = v.pipe(v.string(), v.regex(/^[a-f0-9]{64}$/u));
|
|
9
11
|
const id = v.pipe(v.string(), v.regex(/^[a-z][a-z0-9.-]*$/u), v.maxLength(128));
|
|
@@ -25,8 +27,25 @@ function boxWithinCell(b: v.InferOutput<typeof boxData>): boolean {
|
|
|
25
27
|
})).every(vertex => Math.abs(vertex.x) <= 250 && Math.abs(vertex.z) <= 250 && vertex.y >= -CELL_BELOW && vertex.y <= CELL_ABOVE);
|
|
26
28
|
}
|
|
27
29
|
const box = v.pipe(boxData, v.check(boxWithinCell, 'collider within cell'));
|
|
30
|
+
interface PropHull extends GlbVertex {
|
|
31
|
+
kind: 'hull';
|
|
32
|
+
points: number[];
|
|
33
|
+
yaw?: number | undefined;
|
|
34
|
+
// oxlint-disable-next-line eslint/id-length -- Quaternion components are part of the wire.
|
|
35
|
+
rot?: (GlbVertex & { w: number }) | undefined;
|
|
36
|
+
surface?: 'wood' | 'stone' | 'grass' | 'sand' | 'rock' | 'metal' | 'earth' | 'felt' | undefined;
|
|
37
|
+
}
|
|
38
|
+
const MIN_HULL_COORDS = 12;
|
|
39
|
+
const MAX_HULL_COORDS = 192;
|
|
40
|
+
const AXES_PER_VERTEX = 3;
|
|
41
|
+
const COLLIDER_UNIT = 1;
|
|
42
|
+
const hullPoints = v.pipe(v.array(finite), v.minLength(MIN_HULL_COORDS), v.maxLength(MAX_HULL_COORDS));
|
|
43
|
+
// oxlint-disable-next-line eslint/id-length -- A hull has the same cell-local pose as a prop box.
|
|
44
|
+
const hullData = v.strictObject({ kind: v.literal('hull'), x: coord, y: height, z: coord, points: hullPoints,
|
|
45
|
+
yaw: v.optional(finite), rot: v.optional(rotation), surface });
|
|
46
|
+
const hull = v.pipe(hullData, v.check<v.InferOutput<typeof hullData>, string>(solidHullWithinCell, 'solid hull within cell'));
|
|
28
47
|
const treads = v.pipe(v.strictObject({ kind: v.literal('treads'), from: point, to: point, width: extent, count: v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(256)), surface }), v.check((t) => Math.hypot(t.to.x - t.from.x, t.to.z - t.from.z) > 0 && t.to.y >= t.from.y && [t.from, t.to].every((p) => Math.abs(p.x) + t.width / 2 <= 250 && Math.abs(p.z) + t.width / 2 <= 250), 'bounded nondegenerate stair'));
|
|
29
|
-
const collider = v.strictObject({ id, panel: v.nullable(id), initialActive: v.boolean(), shapes: v.pipe(v.array(v.variant('kind', [box, treads])), v.minLength(1), v.maxLength(256)) });
|
|
48
|
+
const collider = v.strictObject({ id, panel: v.nullable(id), initialActive: v.boolean(), shapes: v.pipe(v.array(v.variant('kind', [box, treads, hull])), v.minLength(1), v.maxLength(256)) });
|
|
30
49
|
const BODY_EXTENT_MIN = 0.005;
|
|
31
50
|
const BODY_EXTENT_MAX = 8;
|
|
32
51
|
const BODY_DENSITY_MIN = 1;
|
|
@@ -62,6 +81,19 @@ function bodiesAdmitted(section: { readonly bodies?: readonly { readonly id: str
|
|
|
62
81
|
const owned = new Set(rows.map((row): string => row.panel));
|
|
63
82
|
return ids.size === rows.length && owned.size === rows.length && rows.every((row): boolean => declared.has(row.panel) && !claimed.has(row.panel));
|
|
64
83
|
}
|
|
84
|
+
type CostedShape = Readonly<{ kind: 'treads'; count: number }> | Readonly<{ kind: 'hull'; points: readonly number[] }> | Readonly<{ kind: 'box' }>;
|
|
85
|
+
const COLLIDER_BUDGET = 2048;
|
|
86
|
+
function withinColliderBudget(rows: readonly Readonly<{ shapes: readonly CostedShape[] }>[]): boolean {
|
|
87
|
+
let total = 0;
|
|
88
|
+
for (const row of rows) {
|
|
89
|
+
for (const shape of row.shapes) {
|
|
90
|
+
if (shape.kind === 'treads') { total += shape.count; }
|
|
91
|
+
else if (shape.kind === 'hull') { total += shape.points.length / AXES_PER_VERTEX; }
|
|
92
|
+
else { total += COLLIDER_UNIT; }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return total <= COLLIDER_BUDGET;
|
|
96
|
+
}
|
|
65
97
|
/** Declared self-contained GLBs: merged tile meshes, EXT_mesh_gpu_instancing lists, far proxy and script-addressable panels. */
|
|
66
98
|
export const PropsSchema: v.GenericSchema<PropsDataInput, PropsData> = v.pipe(v.unknown(), v.check(isJsonData, 'JSON-only props'), v.strictObject({ version: v.literal(1), family: id,
|
|
67
99
|
materials: v.exactOptional(PropMaterialsSchema),
|
|
@@ -76,7 +108,7 @@ export const PropsSchema: v.GenericSchema<PropsDataInput, PropsData> = v.pipe(v.
|
|
|
76
108
|
colliders: v.optional(v.pipe(v.array(collider), v.maxLength(1024)), []),
|
|
77
109
|
textures: v.pipe(v.array(v.strictObject({ model: ref, colour: ref })), v.maxLength(256)),
|
|
78
110
|
bodies: v.exactOptional(bodies),
|
|
79
|
-
}), v.check((s) => new Set(s.tiles.map((tile) => `${tile.lod}/${tile.x}/${tile.z}`)).size === s.tiles.length && s.tiles.every((tile) => tile.x < (tile.lod === 0 ? 8 : 4) && tile.z < (tile.lod === 0 ? 8 : 4)) && new Set([...s.panels, ...s.models].map((entry) => entry.id)).size === s.panels.length + s.models.length && new Set(s.textures.map((texture) => texture.model)).size === s.textures.length && new Set(s.colliders.map((solid) => solid.id)).size === s.colliders.length && s.colliders
|
|
111
|
+
}), v.check((s) => new Set(s.tiles.map((tile) => `${tile.lod}/${tile.x}/${tile.z}`)).size === s.tiles.length && s.tiles.every((tile) => tile.x < (tile.lod === 0 ? 8 : 4) && tile.z < (tile.lod === 0 ? 8 : 4)) && new Set([...s.panels, ...s.models].map((entry) => entry.id)).size === s.panels.length + s.models.length && new Set(s.textures.map((texture) => texture.model)).size === s.textures.length && new Set(s.colliders.map((solid) => solid.id)).size === s.colliders.length && withinColliderBudget(s.colliders) && s.colliders.every((solid) => solid.panel === null || s.panels.some((panel) => panel.id === solid.panel)), 'unique props addresses and ids'), v.check<PropsData, string>(bodiesAdmitted, 'touchable bodies have unique ids and each names its own panel, which no static collider claims'));
|
|
80
112
|
/** Validated data for the props renderer; no callbacks, URLs or generator code enter this section. */
|
|
81
113
|
export type ShardProps = v.InferOutput<typeof PropsSchema>;
|
|
82
114
|
/** Check every render binding against ordinary admitted file, tile and library rows before allocating content. */
|
|
@@ -148,7 +180,7 @@ interface PropsDataFields {
|
|
|
148
180
|
w: number;
|
|
149
181
|
} | undefined;
|
|
150
182
|
surface?: 'wood' | 'stone' | 'grass' | 'sand' | 'rock' | 'metal' | 'earth' | 'felt' | undefined;
|
|
151
|
-
}) | ({
|
|
183
|
+
}) | PropHull | ({
|
|
152
184
|
kind: 'treads';
|
|
153
185
|
from: {
|
|
154
186
|
x: number;
|