lecodes-cli 0.18.2 → 0.19.0
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/dist/index.js +1240 -170
- package/package.json +3 -2
- package/runtime/scene-harness.json +1 -1
- package/runtime/sdk/compile/aspectMacro.ts +86 -0
- package/runtime/sdk/compile/assetIconMacro.ts +384 -0
- package/runtime/sdk/compile/assetMacro.ts +146 -0
- package/runtime/sdk/compile/assetName.ts +50 -0
- package/runtime/sdk/compile/bundler.ts +287 -0
- package/runtime/sdk/compile/compileProject.ts +129 -0
- package/runtime/sdk/compile/detectEntry.ts +128 -0
- package/runtime/sdk/compile/fontMacro.ts +459 -0
- package/runtime/sdk/compile/fontRegistry.ts +78 -0
- package/runtime/sdk/compile/header.ts +67 -0
- package/runtime/sdk/compile/index.ts +100 -0
- package/runtime/sdk/compile/libraryImports.ts +52 -0
- package/runtime/sdk/compile/liteMaterial.ts +247 -0
- package/runtime/sdk/compile/sceneEditor.ts +88 -0
- package/runtime/sdk/compile/serverSplit.ts +233 -0
- package/runtime/sdk/compile/serverTypes.ts +227 -0
- package/runtime/sdk/compile/sfnt.ts +98 -0
- package/runtime/sdk/compile/shaderSchema.ts +202 -0
- package/runtime/sdk/compile/shaderTargets.ts +81 -0
- package/runtime/sdk/compile/sourcemap.ts +25 -0
- package/runtime/sdk/core/Aspect.ts +65 -9
- package/runtime/sdk/core/StateMachine.ts +308 -0
- package/runtime/sdk/core/time.ts +3 -0
- package/runtime/sdk/gl/CameraPlace.ts +52 -52
- package/runtime/sdk/gl/Light.ts +12 -4
- package/runtime/sdk/gl/Lightmap.ts +87 -10
- package/runtime/sdk/gl/Material.ts +15 -0
- package/runtime/sdk/gl/Model.ts +68 -8
- package/runtime/sdk/gl/NavAgent.ts +337 -0
- package/runtime/sdk/gl/NavMesh.ts +397 -0
- package/runtime/sdk/gl/Ragdoll.ts +270 -0
- package/runtime/sdk/gl/Scene.ts +7 -0
- package/runtime/sdk/gl/Shape.ts +37 -5
- package/runtime/sdk/gl/Terrain.ts +1092 -0
- package/runtime/sdk/gl/Texture.ts +17 -0
- package/runtime/sdk/gl/Trigger.ts +45 -45
- package/runtime/sdk/gl/animation/Animator.ts +35 -2
- package/runtime/sdk/gl/animation/Layer.ts +3 -1
- package/runtime/sdk/gl/animation/Loop.ts +5 -0
- package/runtime/sdk/gl/animation/Playback.ts +4 -3
- package/runtime/sdk/gl/animation/core.ts +52 -10
- package/runtime/sdk/gl/scenarios.ts +291 -291
- package/runtime/sdk/gl/state.ts +6 -6
- package/runtime/sdk/gl/terrainMesh.ts +219 -0
- package/runtime/sdk/inject.ts +12 -1
- package/runtime/sdk/net/codec.ts +119 -0
- package/runtime/sdk/net/core.ts +384 -0
- package/runtime/sdk/net/index.ts +181 -0
- package/runtime/sdk/net/replication.ts +622 -0
- package/runtime/sdk/scene/defineScene.ts +140 -14
- package/runtime/sdk/scene/editorPlugins.ts +18 -0
- package/runtime/sdk/scene/gizmos.ts +148 -148
- package/runtime/sdk/ui/UINode.ts +4 -0
- package/runtime/sdk-types.json +1 -1
|
@@ -205,6 +205,21 @@ export class Material {
|
|
|
205
205
|
const m = new Material({ _id: _creatorUtils.fetchLocal("lightmap.filamat") } as any)
|
|
206
206
|
m.set("baseColorFactor", "#ffffffff").set("roughnessFactor", 1).set("metallicFactor", 0)
|
|
207
207
|
m.set("lightmapST", [ 1, 1, 0, 0 ]).set("ambientScale", 1).set("sunStrength", 1).set("hasNormalMap", 0)
|
|
208
|
+
m.set("probeMin", [ 0, 0, 0, 0 ]).set("probeInvSize", [ 0, 0, 0, 0 ])
|
|
209
|
+
return m
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The terrain splat material (docs/terrain-plan.md §1.5): four albedo (+ normal-map) layers blended by
|
|
213
|
+
* a control map on the terrain's own grid, per-layer `tiling` (metres per repeat) / `roughness` /
|
|
214
|
+
* `normalScale` / `triplanar`, lightmap-aware (the same `lightmap` / `lightmapST` block as
|
|
215
|
+
* `Material.lightmap`). `Terrain` builds and owns one per terrain; the uniforms are primed here so an
|
|
216
|
+
* unset layer is white and an unbaked terrain is fully lit. */
|
|
217
|
+
static terrain(): Material {
|
|
218
|
+
// Inline literal: the compiler's preload header captures the shader name from it.
|
|
219
|
+
const m = new Material({ _id: _creatorUtils.fetchLocal("terrain.filamat") } as any)
|
|
220
|
+
m.set("tiling", [ 8, 8, 8, 8 ]).set("roughness", [ 1, 1, 1, 1 ]).set("normalScale", [ 0, 0, 0, 0 ]).set("triplanar", [ 0, 0, 0, 0 ])
|
|
221
|
+
m.set("gridSize", [ 2, 2 ]).set("cellSize", 1).set("tint", "#ffffff")
|
|
222
|
+
m.set("lightmapST", [ 1, 1, 0, 0 ]).set("ambientScale", 1).set("sunStrength", 1)
|
|
208
223
|
return m
|
|
209
224
|
}
|
|
210
225
|
|
package/runtime/sdk/gl/Model.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { fetch, type FetchResponse } from "../runtime/fetch"
|
|
8
8
|
import { Node } from "./Node"
|
|
9
|
-
import { Animator } from "./animation/Animator"
|
|
9
|
+
import { Animator, _pushLod, type LodMode } from "./animation/Animator"
|
|
10
10
|
import { Material } from "./Material"
|
|
11
11
|
|
|
12
12
|
export class Model extends Node {
|
|
@@ -15,20 +15,74 @@ export class Model extends Node {
|
|
|
15
15
|
declare readonly anim: Animator
|
|
16
16
|
/** @internal loaded through the lightmap material (docs/lightmap-plan.md); clones inherit it. */
|
|
17
17
|
_lightmapped = false
|
|
18
|
+
/** @internal what `load` does without an explicit `lightmap` option: `'dynamic'` while a scene file with
|
|
19
|
+
* `env.lightmap.volume` runs (Lightmap sets it), so weapons, spawns and make() subtrees read the volume. */
|
|
20
|
+
static _lightmapDefault: false | "dynamic" = false
|
|
21
|
+
/** @internal the shadow pair, mirrored here because the bridge writes both at once. */
|
|
22
|
+
_castShadows = true
|
|
23
|
+
/** @internal */
|
|
24
|
+
_receiveShadows = true
|
|
25
|
+
/** @internal */
|
|
26
|
+
_culling = true
|
|
27
|
+
/** @internal -1 = auto (see `lod`) */
|
|
28
|
+
_lodMesh = -1
|
|
18
29
|
|
|
19
30
|
constructor(internalId: number) {
|
|
20
31
|
super(internalId)
|
|
21
32
|
this.aspect(Animator)
|
|
22
33
|
}
|
|
23
34
|
|
|
35
|
+
/** Does this model cast a real-time shadow? Unlike Mesh (one renderable) a GLB is a whole
|
|
36
|
+
* hierarchy, so the flag goes to EVERY renderable of the instance. A first-person viewmodel —
|
|
37
|
+
* arms, weapon, attachments — sets it false: it lives in front of the camera and its shadow
|
|
38
|
+
* is never wanted. */
|
|
39
|
+
get castShadows(): boolean { return this._castShadows }
|
|
40
|
+
set castShadows(v: boolean) { this._castShadows = v; this._applyShadows() }
|
|
41
|
+
|
|
42
|
+
/** Is this model lit by other casters' shadows? Same instance-wide reach as castShadows. */
|
|
43
|
+
get receiveShadows(): boolean { return this._receiveShadows }
|
|
44
|
+
set receiveShadows(v: boolean) { this._receiveShadows = v; this._applyShadows() }
|
|
45
|
+
|
|
46
|
+
/** Frustum culling for the whole instance: a model the camera cannot see skips the draw and the
|
|
47
|
+
* shadow pass. ON by default where the host keeps a skinned mesh's bounds honest — the engine refits
|
|
48
|
+
* them to the joints every frame, so a walking, kneeling or ragdolled body is never culled while on
|
|
49
|
+
* screen (`_creator.skinnedCullingSupported`); OFF (always draw) on hosts without that, where
|
|
50
|
+
* Filament would cull an animated body by its bind-pose box. `false` = always draw (a skybox-sized
|
|
51
|
+
* mesh, a debugging aid); `true` forces it on regardless of the host. */
|
|
52
|
+
get culling(): boolean { return this._culling }
|
|
53
|
+
set culling(v: boolean) { this._culling = v; _creator.setGlbCulling(this.id, v) }
|
|
54
|
+
/** Level of detail (docs/lod-plan.md). `'auto'` (default): the engine shows the `_LOD<n>` mesh level
|
|
55
|
+
* that fits the model's size on screen (`lecodes assets doctor --lod` makes them) and scales the
|
|
56
|
+
* animation rate with it; a number 0–3 pins that level for both — `0` = always full detail (a hero,
|
|
57
|
+
* a showcase), `2`/`3` = always cheap (a crowd filler). `model.anim.lod = 'full'` keeps the animation
|
|
58
|
+
* exact while the mesh still switches. No-op on hosts without the LOD pass. */
|
|
59
|
+
get lod(): LodMode { return this._lodMesh < 0 ? "auto" : (this._lodMesh as 0 | 1 | 2 | 3) }
|
|
60
|
+
set lod(v: LodMode) { this._lodMesh = v === "auto" ? -1 : Math.max(0, Math.min(3, Math.round(v))); _pushLod(this) }
|
|
61
|
+
|
|
62
|
+
/** @internal the default for a fresh instance (load / clone), decided once per host. */
|
|
63
|
+
static _cullingDefault(): boolean {
|
|
64
|
+
if (Model._cullDefault === undefined) Model._cullDefault = !!_creator.skinnedCullingSupported?.()
|
|
65
|
+
return Model._cullDefault
|
|
66
|
+
}
|
|
67
|
+
private static _cullDefault: boolean | undefined
|
|
68
|
+
|
|
69
|
+
/** @internal both flags travel together — the bridge walks the instance once. */
|
|
70
|
+
_applyShadows(): void { _creator.setGlbShadows?.(this.id, this._castShadows, this._receiveShadows) }
|
|
71
|
+
|
|
24
72
|
/** Duplicate this model — a deep copy of the GLB (meshes, skeleton, animation clips), attached to
|
|
25
73
|
* the same parent and scene and sharing this model's current transform. The clone has its own
|
|
26
|
-
* independent animation state (reach it via clone.anim). Mirrors
|
|
74
|
+
* independent animation state (reach it via clone.anim). Mirrors this model's culling flag. */
|
|
27
75
|
clone(): Model {
|
|
28
76
|
const id = _creator.cloneEntity(this.id)
|
|
29
|
-
_creator.setGlbCulling(id, false)
|
|
30
77
|
const m = new Model(id)
|
|
31
78
|
m._lightmapped = this._lightmapped
|
|
79
|
+
if (!this._culling) { m._culling = false; _creator.setGlbCulling(id, false) } // the engine default is on
|
|
80
|
+
if (this._lodMesh >= 0) { m._lodMesh = this._lodMesh; _pushLod(m) }
|
|
81
|
+
// the clone is a FRESH gltfio instance — it comes back with the asset's own shadow flags,
|
|
82
|
+
// not this model's, so a cleared flag has to be re-applied
|
|
83
|
+
m._castShadows = this._castShadows
|
|
84
|
+
m._receiveShadows = this._receiveShadows
|
|
85
|
+
if (!this._castShadows || !this._receiveShadows) m._applyShadows()
|
|
32
86
|
return m
|
|
33
87
|
}
|
|
34
88
|
|
|
@@ -36,11 +90,15 @@ export class Model extends Node {
|
|
|
36
90
|
static load(
|
|
37
91
|
source: string | FetchResponse,
|
|
38
92
|
options: {
|
|
93
|
+
/** Frustum culling — see `culling` (default: on where the host refits skinned bounds, else off). */
|
|
39
94
|
culling?: boolean
|
|
40
95
|
onProgress?: (p: { loaded: number, total?: number }) => void
|
|
41
|
-
/**
|
|
42
|
-
*
|
|
43
|
-
|
|
96
|
+
/** Baked lighting (docs/lightmap-plan.md). `true` = a STATIC: loads through the lightmap material so
|
|
97
|
+
* `Lightmap.load` can bind its atlas rect (the GLB needs TEXCOORD_1 — `lecodes assets doctor
|
|
98
|
+
* --lightmap-uv`). `'dynamic'` = a mover: the same material, lit by the level's light VOLUME instead
|
|
99
|
+
* (no UV1 needed). Omitted: `'dynamic'` while a scene file with `env.lightmap.volume` is running,
|
|
100
|
+
* else off. `false` = the standard shader. Hosts without lightmap support ignore it. */
|
|
101
|
+
lightmap?: boolean | "dynamic"
|
|
44
102
|
} = {},
|
|
45
103
|
): Promise<Model> {
|
|
46
104
|
if (typeof source === "string") {
|
|
@@ -50,13 +108,15 @@ export class Model extends Node {
|
|
|
50
108
|
})
|
|
51
109
|
}
|
|
52
110
|
return new Promise<Model>((resolve, reject) => {
|
|
53
|
-
const
|
|
111
|
+
const mode = options.lightmap ?? Model._lightmapDefault
|
|
112
|
+
const lightmapped = !!mode && !!_creator.setNextGlbLightmapped
|
|
54
113
|
if (lightmapped) _creator.setNextGlbLightmapped!(Material.idOf(Material._lightmapTemplate()))
|
|
55
114
|
_creator.createGlb((source as unknown as { _id: number })._id, (entityId: number) => {
|
|
56
115
|
if (entityId === 0) { reject(new Error("Failed to load GLB")); return }
|
|
57
116
|
const model = new Model(entityId)
|
|
58
117
|
model._lightmapped = lightmapped
|
|
59
|
-
|
|
118
|
+
const culling = options.culling ?? Model._cullingDefault()
|
|
119
|
+
if (!culling) { model._culling = false; _creator.setGlbCulling(entityId, false) }
|
|
60
120
|
resolve(model)
|
|
61
121
|
}, reject)
|
|
62
122
|
})
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
// A navmesh agent, as an aspect on a 3D Node (docs/navmesh-plan.md §5). The GAME decides where it
|
|
2
|
+
// goes and when; DetourCrowd (creator-nav, one update per fixed step for the whole crowd) does the
|
|
3
|
+
// corridor, steering, path replanning and local avoidance; this aspect applies its slot to the node:
|
|
4
|
+
//
|
|
5
|
+
// const op = new Node().aspect(NavAgent, { radius: 0.4, speed: 3.2 })
|
|
6
|
+
// scene.add(op)
|
|
7
|
+
// op.agent.goTo(player.position) // false = nothing to walk on there
|
|
8
|
+
// op.agent.on('arrive', () => engage())
|
|
9
|
+
// if (op.agent.state === 'blocked') op.agent.goTo(nav.randomPoint(op.position, 8)!)
|
|
10
|
+
//
|
|
11
|
+
// Two ways to move the node (`drive`):
|
|
12
|
+
// • 'position' (default) — the crowd owns the transform: every host including headless, no
|
|
13
|
+
// physics. The node is placed on the navmesh surface (heights are quantized to the bake's
|
|
14
|
+
// cell height, 10 cm by default).
|
|
15
|
+
// • 'controller' — the node has a CharacterController: the crowd's velocity becomes its `move`
|
|
16
|
+
// command each fixed step, the capsule collides with everything, and the crowd slot is warped
|
|
17
|
+
// back to the body when they diverge (a push-back off the mesh is corrected by Detour itself).
|
|
18
|
+
// Facing turns toward the velocity at `turnSpeed` (0 = leave the rotation alone).
|
|
19
|
+
//
|
|
20
|
+
// Without a loaded NavMesh the aspect is inert — `goTo` returns false, nothing throws — so a
|
|
21
|
+
// project stays runnable on a host without navigation.
|
|
22
|
+
|
|
23
|
+
import { Aspect } from "../core/Aspect"
|
|
24
|
+
import type { FieldMeta } from "../core/fields"
|
|
25
|
+
import { Vec3, cx, cy, cz, type Vec3Like } from "../math/vec"
|
|
26
|
+
import { CharacterController } from "./CharacterController"
|
|
27
|
+
import { NavCrowd, NavMesh, NAV_AGENT, NAV_READ } from "./NavMesh"
|
|
28
|
+
import type { Node } from "./Node"
|
|
29
|
+
|
|
30
|
+
export type NavAgentState = "idle" | "moving" | "arrived" | "blocked" | "offmesh"
|
|
31
|
+
export type NavAgentAvoidance = "off" | "low" | "medium" | "high"
|
|
32
|
+
export type NavAgentDrive = "position" | "controller"
|
|
33
|
+
|
|
34
|
+
export type NavAgentEvents = {
|
|
35
|
+
/** Reached the target (within `stopDistance`). */
|
|
36
|
+
arrive: () => void
|
|
37
|
+
/** The target cannot be reached — the agent stopped at the closest point (or nowhere near). */
|
|
38
|
+
blocked: () => void
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const AVOIDANCE: Record<NavAgentAvoidance, number> = { off: -1, low: 0, medium: 1, high: 3 }
|
|
42
|
+
const DEG2RAD = Math.PI / 180
|
|
43
|
+
const JOIN_RETRY_STEPS = 30
|
|
44
|
+
|
|
45
|
+
export class NavAgent extends Aspect<"agent", Node, NavAgentEvents> {
|
|
46
|
+
static readonly aspect = "agent"
|
|
47
|
+
/** The crowd advances first, then every agent applies its slot. */
|
|
48
|
+
static after = [ NavCrowd ]
|
|
49
|
+
|
|
50
|
+
static fields: FieldMeta<NavAgent> = {
|
|
51
|
+
radius: { min: 0.05, max: 5, step: 0.05 },
|
|
52
|
+
height: { min: 0.1, max: 10, step: 0.1 },
|
|
53
|
+
speed: { min: 0, max: 50, step: 0.1 },
|
|
54
|
+
acceleration: { min: 0.1, max: 200, step: 0.5 },
|
|
55
|
+
turnSpeed: { min: 0, max: 3600, step: 10 },
|
|
56
|
+
separation: { min: 0, max: 10, step: 0.1 },
|
|
57
|
+
stopDistance: { min: 0, max: 10, step: 0.05 },
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private _radius = 0.4
|
|
61
|
+
private _height = 1.8
|
|
62
|
+
private _speed = 3
|
|
63
|
+
private _acceleration = 8
|
|
64
|
+
private _avoidance: NavAgentAvoidance = "medium"
|
|
65
|
+
private _separation = 2
|
|
66
|
+
private _include: string[] | undefined
|
|
67
|
+
private _exclude: string[] | undefined
|
|
68
|
+
/** deg/s the node turns toward its velocity; 0 = never touch the rotation. */
|
|
69
|
+
turnSpeed = 540
|
|
70
|
+
/** How the node follows its slot. */
|
|
71
|
+
drive: NavAgentDrive = "position"
|
|
72
|
+
/** Reached = within this many metres (along the corridor) of the target. */
|
|
73
|
+
stopDistance = 0.3
|
|
74
|
+
|
|
75
|
+
/** Agent radius (metres). Defaults to the navmesh's. */
|
|
76
|
+
get radius(): number { return this._radius }
|
|
77
|
+
set radius(v: number) { this._radius = v; this._dirty = true }
|
|
78
|
+
get height(): number { return this._height }
|
|
79
|
+
set height(v: number) { this._height = v; this._dirty = true }
|
|
80
|
+
/** Top speed (m/s). */
|
|
81
|
+
get speed(): number { return this._speed }
|
|
82
|
+
set speed(v: number) { this._speed = v; this._dirty = true }
|
|
83
|
+
get acceleration(): number { return this._acceleration }
|
|
84
|
+
set acceleration(v: number) { this._acceleration = v; this._dirty = true }
|
|
85
|
+
/** Local (agent-vs-agent) avoidance quality. */
|
|
86
|
+
get avoidance(): NavAgentAvoidance { return this._avoidance }
|
|
87
|
+
set avoidance(v: NavAgentAvoidance) { this._avoidance = v; this._dirty = true }
|
|
88
|
+
/** Separation force weight (0 = none). */
|
|
89
|
+
get separation(): number { return this._separation }
|
|
90
|
+
set separation(v: number) { this._separation = v; this._dirty = true }
|
|
91
|
+
/** Area names this agent may walk (default: all). */
|
|
92
|
+
get include(): string[] | undefined { return this._include }
|
|
93
|
+
set include(v: string[] | undefined) { this._include = v; this._dirty = true }
|
|
94
|
+
/** Area names this agent never enters. */
|
|
95
|
+
get exclude(): string[] | undefined { return this._exclude }
|
|
96
|
+
set exclude(v: string[] | undefined) { this._exclude = v; this._dirty = true }
|
|
97
|
+
|
|
98
|
+
state: NavAgentState = "idle"
|
|
99
|
+
/** The current velocity after avoidance (m/s). */
|
|
100
|
+
readonly velocity = new Vec3(0, 0, 0)
|
|
101
|
+
/** The velocity the corridor asked for, before avoidance. */
|
|
102
|
+
readonly desiredVelocity = new Vec3(0, 0, 0)
|
|
103
|
+
/** Metres left along the corridor (-1 = no target). */
|
|
104
|
+
remainingDistance = -1
|
|
105
|
+
/** Where the agent is going, or null. */
|
|
106
|
+
get target(): Vec3 | null { return this._target }
|
|
107
|
+
get hasPath(): boolean { return this._target !== null }
|
|
108
|
+
|
|
109
|
+
private _crowd: NavCrowd | null = null
|
|
110
|
+
private _slot = -1
|
|
111
|
+
private _target: Vec3 | null = null
|
|
112
|
+
/** velocity-driven (`move`) rather than target-driven */
|
|
113
|
+
private _steering = false
|
|
114
|
+
private _dirty = false
|
|
115
|
+
private _faceX = 0
|
|
116
|
+
private _faceZ = -1
|
|
117
|
+
private _retryAt = 0
|
|
118
|
+
private _lastSeen = -1
|
|
119
|
+
private _still = 0
|
|
120
|
+
private _near = 0
|
|
121
|
+
private _slow = 0
|
|
122
|
+
|
|
123
|
+
onAttach(): void {
|
|
124
|
+
this.state = "idle"
|
|
125
|
+
this._join()
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
onReconfigure(): void { this._dirty = true }
|
|
129
|
+
|
|
130
|
+
onDetach(): void {
|
|
131
|
+
this._leave()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Walk to a point. False = no walkable polygon near it (or no navmesh) — the current target stays. */
|
|
135
|
+
goTo(target: Vec3Like): boolean {
|
|
136
|
+
if (this._slot < 0 && !this._join()) return false
|
|
137
|
+
const ok = _creatorNav.crowdSetTarget(this._crowd!._id, this._slot, cx(target), cy(target), cz(target))
|
|
138
|
+
if (!ok) return false
|
|
139
|
+
this._target = new Vec3(cx(target), cy(target), cz(target))
|
|
140
|
+
this._steering = false
|
|
141
|
+
this.state = "moving"
|
|
142
|
+
this._still = 0
|
|
143
|
+
this._near = 0
|
|
144
|
+
this._slow = 0
|
|
145
|
+
return true
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Drop the target and decelerate to a stop. */
|
|
149
|
+
stop(): void {
|
|
150
|
+
if (this._slot >= 0) _creatorNav.crowdResetTarget(this._crowd!._id, this._slot)
|
|
151
|
+
this._target = null
|
|
152
|
+
this._steering = false
|
|
153
|
+
this.remainingDistance = -1
|
|
154
|
+
this.state = "idle"
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Steer by velocity instead of a target — a player on WASD, a scripted push: the crowd keeps
|
|
159
|
+
* the node on the mesh and avoids the other agents, no path and no arrival. Call it every fixed
|
|
160
|
+
* step (the last velocity holds until the next call); `move(0, 0)` stops. Cancels `goTo`.
|
|
161
|
+
* False = no navmesh / off the mesh.
|
|
162
|
+
*/
|
|
163
|
+
move(vx: number, vz: number): boolean {
|
|
164
|
+
if (this._slot < 0 && !this._join()) return false
|
|
165
|
+
this._target = null
|
|
166
|
+
this.remainingDistance = -1
|
|
167
|
+
if (vx === 0 && vz === 0) {
|
|
168
|
+
if (this._steering) { _creatorNav.crowdResetTarget(this._crowd!._id, this._slot); this._steering = false }
|
|
169
|
+
this.state = "idle"
|
|
170
|
+
return true
|
|
171
|
+
}
|
|
172
|
+
_creatorNav.crowdSetVelocity(this._crowd!._id, this._slot, vx, 0, vz)
|
|
173
|
+
this._steering = true
|
|
174
|
+
this.state = "moving"
|
|
175
|
+
return true
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Teleport (a spawn, a respawn). The target, if any, is kept. */
|
|
179
|
+
warp(p: Vec3Like): void {
|
|
180
|
+
this.node.position = [ cx(p), cy(p), cz(p) ]
|
|
181
|
+
if (this._slot >= 0 && !_creatorNav.crowdWarp(this._crowd!._id, this._slot, cx(p), cy(p), cz(p))) {
|
|
182
|
+
// off the mesh now — leave the crowd and re-join when it is back on
|
|
183
|
+
this._leave()
|
|
184
|
+
this._retryAt = 0
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private params(): Float32Array {
|
|
189
|
+
const p = new Float32Array(NAV_AGENT.COUNT)
|
|
190
|
+
const mesh = this._crowd?._mesh
|
|
191
|
+
p[NAV_AGENT.RADIUS] = this._radius
|
|
192
|
+
p[NAV_AGENT.HEIGHT] = this._height
|
|
193
|
+
p[NAV_AGENT.MAX_SPEED] = this._speed
|
|
194
|
+
p[NAV_AGENT.MAX_ACCELERATION] = this._acceleration
|
|
195
|
+
p[NAV_AGENT.COLLISION_QUERY_RANGE] = 0
|
|
196
|
+
p[NAV_AGENT.PATH_OPTIMIZATION_RANGE] = 0
|
|
197
|
+
p[NAV_AGENT.SEPARATION_WEIGHT] = this._separation
|
|
198
|
+
p[NAV_AGENT.AVOIDANCE] = AVOIDANCE[this._avoidance] ?? 1
|
|
199
|
+
p[NAV_AGENT.INCLUDE_FLAGS] = mesh ? mesh.flags(this._include) : 0xffff
|
|
200
|
+
p[NAV_AGENT.EXCLUDE_FLAGS] = mesh && this._exclude ? mesh.flags(this._exclude) : 0
|
|
201
|
+
p[NAV_AGENT.ANTICIPATE_TURNS] = 1
|
|
202
|
+
p[NAV_AGENT.OPTIMIZE_VIS] = 1
|
|
203
|
+
p[NAV_AGENT.OPTIMIZE_TOPO] = 1
|
|
204
|
+
return p
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** @internal Register with the active navmesh's crowd. False = no navmesh / off the mesh. */
|
|
208
|
+
_join(): boolean {
|
|
209
|
+
const mesh = NavMesh.active
|
|
210
|
+
if (!mesh || typeof _creatorNav === "undefined") return false
|
|
211
|
+
const crowd = mesh._crowd()
|
|
212
|
+
if (!crowd._ensureCapacity()) return false
|
|
213
|
+
if (mesh._maxRadius < this._radius) mesh._maxRadius = this._radius
|
|
214
|
+
const p = this.node.worldPosition
|
|
215
|
+
const slot = _creatorNav.crowdAdd(crowd._id, p.x, p.y, p.z, this.params())
|
|
216
|
+
if (slot < 0) { this._retryAt = crowd._step + JOIN_RETRY_STEPS; return false }
|
|
217
|
+
this._crowd = crowd
|
|
218
|
+
this._slot = slot
|
|
219
|
+
this._dirty = false
|
|
220
|
+
this._lastSeen = -1
|
|
221
|
+
crowd._agents.add(this)
|
|
222
|
+
if (this._target && !_creatorNav.crowdSetTarget(crowd._id, slot, this._target.x, this._target.y, this._target.z)) this._target = null
|
|
223
|
+
return true
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** @internal The crowd was re-created (capacity grew): take a fresh slot, keep the target. */
|
|
227
|
+
_rejoin(): void {
|
|
228
|
+
const crowd = this._crowd
|
|
229
|
+
if (!crowd) return
|
|
230
|
+
const p = this.node.worldPosition
|
|
231
|
+
const slot = _creatorNav.crowdAdd(crowd._id, p.x, p.y, p.z, this.params())
|
|
232
|
+
this._slot = slot
|
|
233
|
+
if (slot >= 0 && this._target) _creatorNav.crowdSetTarget(crowd._id, slot, this._target.x, this._target.y, this._target.z)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** @internal Leave the crowd (detach, navmesh disposal). */
|
|
237
|
+
_leave(): void {
|
|
238
|
+
if (this._crowd && this._slot >= 0 && typeof _creatorNav !== "undefined" && this._crowd._id) {
|
|
239
|
+
_creatorNav.crowdRemove(this._crowd._id, this._slot)
|
|
240
|
+
}
|
|
241
|
+
this._crowd?._agents.delete(this)
|
|
242
|
+
this._crowd = null
|
|
243
|
+
this._slot = -1
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
updateFixed(dt: number): void {
|
|
247
|
+
if (this._slot < 0) {
|
|
248
|
+
// no crowd yet (the navmesh loaded after the attach, or the spawn was off the mesh): retry
|
|
249
|
+
if (NavMesh.active && (this._crowd === null ? true : this._crowd._step >= this._retryAt)) {
|
|
250
|
+
if (!this._join()) this._retryAt = (NavMesh.active._crowd()._step) + JOIN_RETRY_STEPS
|
|
251
|
+
}
|
|
252
|
+
return
|
|
253
|
+
}
|
|
254
|
+
const crowd = this._crowd!
|
|
255
|
+
if (crowd._mesh !== NavMesh.active) { this._leave(); return }
|
|
256
|
+
if (this._dirty) { this._dirty = false; _creatorNav.crowdSetParams(crowd._id, this._slot, this.params()) }
|
|
257
|
+
if (crowd._step === this._lastSeen) return // the crowd has not advanced since (a frame with no step)
|
|
258
|
+
this._lastSeen = crowd._step
|
|
259
|
+
const b = crowd._buf
|
|
260
|
+
const o = this._slot * NAV_READ.STRIDE
|
|
261
|
+
const st = b[o + NAV_READ.STATE]!
|
|
262
|
+
if (st === 0) { this._leave(); return }
|
|
263
|
+
const px = b[o + NAV_READ.POS_X]!, py = b[o + NAV_READ.POS_Y]!, pz = b[o + NAV_READ.POS_Z]!
|
|
264
|
+
const vx = b[o + NAV_READ.VEL_X]!, vy = b[o + NAV_READ.VEL_Y]!, vz = b[o + NAV_READ.VEL_Z]!
|
|
265
|
+
this.velocity.x = vx; this.velocity.y = vy; this.velocity.z = vz
|
|
266
|
+
this.desiredVelocity.x = b[o + NAV_READ.DVEL_X]!; this.desiredVelocity.y = b[o + NAV_READ.DVEL_Y]!; this.desiredVelocity.z = b[o + NAV_READ.DVEL_Z]!
|
|
267
|
+
const remaining = b[o + NAV_READ.REMAINING]!
|
|
268
|
+
this.remainingDistance = remaining
|
|
269
|
+
|
|
270
|
+
// the node
|
|
271
|
+
const node = this.node
|
|
272
|
+
if (this.drive === "controller") {
|
|
273
|
+
const cc = node.get(CharacterController)
|
|
274
|
+
if (cc) {
|
|
275
|
+
cc.move(vx, vz)
|
|
276
|
+
// the body is the truth: re-sync the slot when the capsule ended up elsewhere
|
|
277
|
+
const wp = node.worldPosition
|
|
278
|
+
const dx = wp.x - px, dz = wp.z - pz
|
|
279
|
+
if (dx * dx + dz * dz > this._radius * this._radius * 0.25) _creatorNav.crowdWarp(crowd._id, this._slot, wp.x, wp.y, wp.z)
|
|
280
|
+
} else {
|
|
281
|
+
node.position = [ px, py, pz ]
|
|
282
|
+
}
|
|
283
|
+
} else {
|
|
284
|
+
node.position = [ px, py, pz ]
|
|
285
|
+
}
|
|
286
|
+
if (this.turnSpeed > 0) this.face(vx, vz, dt)
|
|
287
|
+
|
|
288
|
+
// the state machine — arrival / blocked are decided here, in JS. A crowd never lets everyone
|
|
289
|
+
// reach the same point: the first agent stops ON it and avoidance holds the rest jostling in a
|
|
290
|
+
// ring around it — so a second spent within a few radii of the goal counts as arrived, and
|
|
291
|
+
// standing still anywhere else for 3 s (a jam, a door that closed) counts as blocked.
|
|
292
|
+
if (this._target) {
|
|
293
|
+
const ts = b[o + NAV_READ.TARGET_STATE]!
|
|
294
|
+
const partial = b[o + NAV_READ.PARTIAL]! > 0.5
|
|
295
|
+
const speed = Math.hypot(vx, vz)
|
|
296
|
+
if (ts === 1) {
|
|
297
|
+
this.finish("blocked")
|
|
298
|
+
} else if (ts === 2 && remaining >= 0 && remaining <= this.stopDistance) {
|
|
299
|
+
this.finish(partial ? "blocked" : "arrived")
|
|
300
|
+
} else {
|
|
301
|
+
// near = a few radii out (the first ring); slow-near = creeping in the outer rings of a big crowd
|
|
302
|
+
const r = this._radius
|
|
303
|
+
const valid = ts === 2 && remaining >= 0
|
|
304
|
+
this._near = valid && remaining <= this.stopDistance + r * 8 ? this._near + 1 : 0
|
|
305
|
+
this._slow = valid && remaining <= this.stopDistance + r * 16 && speed < 0.7 ? this._slow + 1 : 0
|
|
306
|
+
this._still = speed < 0.15 ? this._still + 1 : 0
|
|
307
|
+
if (this._near >= 60 || this._slow >= 120) this.finish(partial ? "blocked" : "arrived")
|
|
308
|
+
else if (this._still >= 180) this.finish("blocked")
|
|
309
|
+
else this.state = st === 3 ? "offmesh" : "moving"
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private finish(state: "arrived" | "blocked"): void {
|
|
315
|
+
_creatorNav.crowdResetTarget(this._crowd!._id, this._slot)
|
|
316
|
+
this._target = null
|
|
317
|
+
this.state = state
|
|
318
|
+
this.remainingDistance = 0
|
|
319
|
+
this.emit(state === "arrived" ? "arrive" : "blocked")
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private face(vx: number, vz: number, dt: number): void {
|
|
323
|
+
const speed = Math.hypot(vx, vz)
|
|
324
|
+
if (speed < 0.05) return
|
|
325
|
+
const a0 = Math.atan2(this._faceX, this._faceZ)
|
|
326
|
+
const a1 = Math.atan2(vx / speed, vz / speed)
|
|
327
|
+
let delta = a1 - a0
|
|
328
|
+
while (delta > Math.PI) delta -= 2 * Math.PI
|
|
329
|
+
while (delta < -Math.PI) delta += 2 * Math.PI
|
|
330
|
+
const max = this.turnSpeed * DEG2RAD * dt
|
|
331
|
+
const a = a0 + Math.max(-max, Math.min(max, delta))
|
|
332
|
+
this._faceX = Math.sin(a)
|
|
333
|
+
this._faceZ = Math.cos(a)
|
|
334
|
+
const p = this.node.position
|
|
335
|
+
this.node.lookAt([ cx(p) + this._faceX, cy(p), cz(p) + this._faceZ ], "-z")
|
|
336
|
+
}
|
|
337
|
+
}
|