2026-03-31 03:04:15 +02:00
|
|
|
import {
|
|
|
|
|
AmbientLight,
|
2026-04-01 00:05:29 +02:00
|
|
|
AnimationClip,
|
|
|
|
|
AnimationMixer,
|
2026-04-05 02:30:08 +02:00
|
|
|
BufferGeometry,
|
2026-03-31 03:04:15 +02:00
|
|
|
DirectionalLight,
|
2026-04-06 08:26:53 +02:00
|
|
|
Euler,
|
2026-03-31 03:04:15 +02:00
|
|
|
Group,
|
2026-04-07 06:15:02 +02:00
|
|
|
FogExp2,
|
2026-04-01 04:05:19 +02:00
|
|
|
LoopOnce,
|
|
|
|
|
LoopRepeat,
|
2026-04-07 06:34:54 +02:00
|
|
|
Matrix4,
|
2026-04-06 09:16:30 +02:00
|
|
|
Material,
|
2026-03-31 03:04:15 +02:00
|
|
|
Mesh,
|
2026-04-06 09:16:30 +02:00
|
|
|
MeshBasicMaterial,
|
2026-03-31 03:04:15 +02:00
|
|
|
MeshStandardMaterial,
|
|
|
|
|
PerspectiveCamera,
|
2026-03-31 20:06:54 +02:00
|
|
|
PointLight,
|
|
|
|
|
Quaternion,
|
2026-03-31 03:04:15 +02:00
|
|
|
Scene,
|
2026-04-06 09:16:30 +02:00
|
|
|
ShaderMaterial,
|
2026-03-31 06:46:27 +02:00
|
|
|
Vector3,
|
2026-03-31 20:06:54 +02:00
|
|
|
SpotLight,
|
2026-04-07 06:34:54 +02:00
|
|
|
UniformsLib,
|
|
|
|
|
UniformsUtils,
|
|
|
|
|
WebGLRenderTarget,
|
2026-03-31 03:04:15 +02:00
|
|
|
WebGLRenderer
|
|
|
|
|
} from "three";
|
2026-04-02 20:51:55 +02:00
|
|
|
import { EffectComposer } from "postprocessing";
|
2026-03-31 03:04:15 +02:00
|
|
|
|
2026-03-31 17:40:12 +02:00
|
|
|
import { createModelInstanceRenderGroup, disposeModelInstance } from "../assets/model-instance-rendering";
|
|
|
|
|
import type { LoadedModelAsset } from "../assets/gltf-model-import";
|
2026-03-31 20:06:54 +02:00
|
|
|
import type { LoadedImageAsset } from "../assets/image-assets";
|
2026-04-02 19:39:55 +02:00
|
|
|
import type { LoadedAudioAsset } from "../assets/audio-assets";
|
2026-03-31 17:40:12 +02:00
|
|
|
import type { ProjectAssetRecord } from "../assets/project-assets";
|
2026-04-05 02:30:08 +02:00
|
|
|
import { buildBoxBrushDerivedMeshData } from "../geometry/box-brush-mesh";
|
2026-03-31 03:04:15 +02:00
|
|
|
import { createStarterMaterialSignature, createStarterMaterialTexture } from "../materials/starter-material-textures";
|
2026-04-02 20:51:55 +02:00
|
|
|
import {
|
|
|
|
|
applyAdvancedRenderingLightShadowFlags,
|
|
|
|
|
applyAdvancedRenderingRenderableShadowFlags,
|
|
|
|
|
configureAdvancedRenderingRenderer,
|
2026-04-06 08:26:27 +02:00
|
|
|
createAdvancedRenderingComposer,
|
2026-04-06 08:44:52 +02:00
|
|
|
resolveBoxVolumeRenderPaths,
|
|
|
|
|
type ResolvedBoxVolumeRenderPaths
|
2026-04-02 20:51:55 +02:00
|
|
|
} from "../rendering/advanced-rendering";
|
2026-04-07 06:15:02 +02:00
|
|
|
import {
|
|
|
|
|
collectWaterContactPatches,
|
|
|
|
|
createWaterContactPatchAxisUniformValue,
|
|
|
|
|
createWaterContactPatchShapeUniformValue,
|
|
|
|
|
createWaterContactPatchUniformValue,
|
|
|
|
|
createWaterMaterial
|
|
|
|
|
} from "../rendering/water-material";
|
2026-04-07 06:34:54 +02:00
|
|
|
import { updatePlanarReflectionCamera } from "../rendering/planar-reflection";
|
2026-04-02 20:51:55 +02:00
|
|
|
import {
|
|
|
|
|
areAdvancedRenderingSettingsEqual,
|
|
|
|
|
cloneAdvancedRenderingSettings,
|
|
|
|
|
type AdvancedRenderingSettings
|
2026-04-02 20:58:35 +02:00
|
|
|
} from "../document/world-settings";
|
2026-03-31 03:04:15 +02:00
|
|
|
|
|
|
|
|
import { FirstPersonNavigationController } from "./first-person-navigation-controller";
|
2026-04-06 08:25:50 +02:00
|
|
|
import type { FirstPersonTelemetry, NavigationController, RuntimeControllerContext, RuntimePlayerVolumeState } from "./navigation-controller";
|
2026-04-04 07:51:38 +02:00
|
|
|
import { RapierCollisionWorld } from "./rapier-collision-world";
|
2026-03-31 06:46:27 +02:00
|
|
|
import { RuntimeInteractionSystem, type RuntimeInteractionDispatcher, type RuntimeInteractionPrompt } from "./runtime-interaction-system";
|
2026-04-02 19:39:55 +02:00
|
|
|
import { RuntimeAudioSystem } from "./runtime-audio-system";
|
2026-03-31 03:04:15 +02:00
|
|
|
import { OrbitVisitorNavigationController } from "./orbit-visitor-navigation-controller";
|
2026-04-07 06:15:02 +02:00
|
|
|
import { resolveUnderwaterFogState } from "./underwater-fog";
|
2026-03-31 20:06:54 +02:00
|
|
|
import type {
|
|
|
|
|
RuntimeBoxBrushInstance,
|
|
|
|
|
RuntimeLocalLightCollection,
|
|
|
|
|
RuntimeNavigationMode,
|
|
|
|
|
RuntimeSceneDefinition,
|
|
|
|
|
RuntimeTeleportTarget
|
|
|
|
|
} from "./runtime-scene-build";
|
2026-03-31 03:04:15 +02:00
|
|
|
|
|
|
|
|
interface CachedMaterialTexture {
|
|
|
|
|
signature: string;
|
|
|
|
|
texture: ReturnType<typeof createStarterMaterialTexture>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 20:07:11 +02:00
|
|
|
interface LocalLightRenderObjects {
|
|
|
|
|
group: Group;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 18:04:29 +02:00
|
|
|
interface RuntimeWaterContactUniformBinding {
|
|
|
|
|
brush: RuntimeBoxBrushInstance;
|
|
|
|
|
uniform: { value: import("three").Vector4[] };
|
2026-04-07 04:58:48 +02:00
|
|
|
axisUniform: { value: import("three").Vector2[] };
|
2026-04-07 06:15:02 +02:00
|
|
|
shapeUniform: { value: number[] };
|
2026-04-06 20:52:05 +02:00
|
|
|
staticContactPatches: ReturnType<typeof collectWaterContactPatches>;
|
2026-04-07 06:34:54 +02:00
|
|
|
reflectionTextureUniform: { value: import("three").Texture | null } | null;
|
|
|
|
|
reflectionMatrixUniform: { value: Matrix4 } | null;
|
|
|
|
|
reflectionEnabledUniform: { value: number } | null;
|
|
|
|
|
reflectionRenderTarget: WebGLRenderTarget | null;
|
2026-04-06 18:04:29 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:04:15 +02:00
|
|
|
const FALLBACK_FACE_COLOR = 0x747d89;
|
|
|
|
|
|
|
|
|
|
export class RuntimeHost {
|
|
|
|
|
private readonly scene = new Scene();
|
|
|
|
|
private readonly camera = new PerspectiveCamera(70, 1, 0.05, 1000);
|
2026-03-31 06:46:27 +02:00
|
|
|
private readonly cameraForward = new Vector3();
|
2026-04-06 08:25:50 +02:00
|
|
|
private readonly volumeOffset = new Vector3();
|
|
|
|
|
private readonly volumeInverseRotation = new Quaternion();
|
2026-03-31 06:25:22 +02:00
|
|
|
private readonly domElement: HTMLCanvasElement;
|
2026-03-31 03:04:15 +02:00
|
|
|
private readonly ambientLight = new AmbientLight();
|
|
|
|
|
private readonly sunLight = new DirectionalLight();
|
2026-03-31 20:06:54 +02:00
|
|
|
private readonly localLightGroup = new Group();
|
2026-03-31 03:04:15 +02:00
|
|
|
private readonly brushGroup = new Group();
|
2026-03-31 17:40:12 +02:00
|
|
|
private readonly modelGroup = new Group();
|
2026-03-31 03:04:15 +02:00
|
|
|
private readonly firstPersonController = new FirstPersonNavigationController();
|
|
|
|
|
private readonly orbitVisitorController = new OrbitVisitorNavigationController();
|
2026-03-31 06:17:09 +02:00
|
|
|
private readonly interactionSystem = new RuntimeInteractionSystem();
|
2026-04-02 19:39:55 +02:00
|
|
|
private readonly audioSystem = new RuntimeAudioSystem(this.scene, this.camera, null);
|
2026-04-07 06:15:02 +02:00
|
|
|
private readonly underwaterSceneFog = new FogExp2("#2c6f8d", 0.03);
|
2026-04-07 06:34:54 +02:00
|
|
|
private readonly waterReflectionCamera = new PerspectiveCamera();
|
2026-04-06 09:16:30 +02:00
|
|
|
private readonly brushMeshes = new Map<string, Mesh<BufferGeometry, Material[]>>();
|
|
|
|
|
private volumeTime = 0;
|
|
|
|
|
private readonly volumeAnimatedMaterials: ShaderMaterial[] = [];
|
2026-04-06 17:27:03 +02:00
|
|
|
private readonly volumeAnimatedUniforms: Array<{ value: number }> = [];
|
2026-04-06 18:04:29 +02:00
|
|
|
private readonly runtimeWaterContactUniforms: RuntimeWaterContactUniformBinding[] = [];
|
2026-03-31 20:06:54 +02:00
|
|
|
private readonly localLightObjects = new Map<string, Group>();
|
2026-03-31 17:40:12 +02:00
|
|
|
private readonly modelRenderObjects = new Map<string, Group>();
|
2026-03-31 03:04:15 +02:00
|
|
|
private readonly materialTextureCache = new Map<string, CachedMaterialTexture>();
|
2026-04-01 00:05:33 +02:00
|
|
|
private readonly animationMixers = new Map<string, AnimationMixer>();
|
|
|
|
|
private readonly instanceAnimationClips = new Map<string, AnimationClip[]>();
|
2026-03-31 03:04:15 +02:00
|
|
|
private readonly controllerContext: RuntimeControllerContext;
|
2026-03-31 06:25:22 +02:00
|
|
|
private readonly renderer: WebGLRenderer | null;
|
2026-03-31 17:40:12 +02:00
|
|
|
private runtimeScene: RuntimeSceneDefinition | null = null;
|
2026-04-04 07:51:38 +02:00
|
|
|
private collisionWorld: RapierCollisionWorld | null = null;
|
|
|
|
|
private collisionWorldRequestId = 0;
|
2026-03-31 20:06:54 +02:00
|
|
|
private currentWorld: RuntimeSceneDefinition["world"] | null = null;
|
2026-04-02 20:52:00 +02:00
|
|
|
private currentAdvancedRenderingSettings: AdvancedRenderingSettings | null = null;
|
|
|
|
|
private advancedRenderingComposer: EffectComposer | null = null;
|
2026-03-31 17:40:12 +02:00
|
|
|
private projectAssets: Record<string, ProjectAssetRecord> = {};
|
|
|
|
|
private loadedModelAssets: Record<string, LoadedModelAsset> = {};
|
2026-03-31 20:06:54 +02:00
|
|
|
private loadedImageAssets: Record<string, LoadedImageAsset> = {};
|
2026-03-31 03:04:15 +02:00
|
|
|
private resizeObserver: ResizeObserver | null = null;
|
|
|
|
|
private animationFrame = 0;
|
|
|
|
|
private previousFrameTime = 0;
|
|
|
|
|
private container: HTMLElement | null = null;
|
|
|
|
|
private activeController: NavigationController | null = null;
|
|
|
|
|
private runtimeMessageHandler: ((message: string | null) => void) | null = null;
|
|
|
|
|
private firstPersonTelemetryHandler: ((telemetry: FirstPersonTelemetry | null) => void) | null = null;
|
2026-03-31 06:46:27 +02:00
|
|
|
private interactionPromptHandler: ((prompt: RuntimeInteractionPrompt | null) => void) | null = null;
|
2026-03-31 03:04:15 +02:00
|
|
|
private currentRuntimeMessage: string | null = null;
|
|
|
|
|
private currentFirstPersonTelemetry: FirstPersonTelemetry | null = null;
|
2026-03-31 06:46:27 +02:00
|
|
|
private currentInteractionPrompt: RuntimeInteractionPrompt | null = null;
|
2026-03-31 03:04:15 +02:00
|
|
|
|
2026-03-31 06:25:22 +02:00
|
|
|
constructor(options: { enableRendering?: boolean } = {}) {
|
|
|
|
|
const enableRendering = options.enableRendering ?? true;
|
|
|
|
|
|
2026-03-31 03:04:15 +02:00
|
|
|
this.scene.add(this.ambientLight);
|
|
|
|
|
this.scene.add(this.sunLight);
|
2026-03-31 20:06:54 +02:00
|
|
|
this.scene.add(this.localLightGroup);
|
2026-03-31 03:04:15 +02:00
|
|
|
this.scene.add(this.brushGroup);
|
2026-03-31 17:40:12 +02:00
|
|
|
this.scene.add(this.modelGroup);
|
2026-04-06 20:52:05 +02:00
|
|
|
this.renderer = enableRendering ? new WebGLRenderer({ antialias: false, alpha: true }) : null;
|
2026-03-31 06:25:22 +02:00
|
|
|
this.domElement = this.renderer?.domElement ?? document.createElement("canvas");
|
|
|
|
|
|
|
|
|
|
if (this.renderer !== null) {
|
|
|
|
|
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
|
|
|
|
|
this.renderer.setClearAlpha(0);
|
|
|
|
|
} else {
|
|
|
|
|
this.domElement.className = "runner-canvas__surface";
|
|
|
|
|
}
|
2026-03-31 03:04:15 +02:00
|
|
|
|
|
|
|
|
this.controllerContext = {
|
|
|
|
|
camera: this.camera,
|
2026-03-31 06:25:22 +02:00
|
|
|
domElement: this.domElement,
|
2026-03-31 03:04:15 +02:00
|
|
|
getRuntimeScene: () => {
|
|
|
|
|
if (this.runtimeScene === null) {
|
|
|
|
|
throw new Error("Runtime scene has not been loaded.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.runtimeScene;
|
|
|
|
|
},
|
2026-04-04 07:51:38 +02:00
|
|
|
resolveFirstPersonMotion: (feetPosition, motion, shape) => this.collisionWorld?.resolveFirstPersonMotion(feetPosition, motion, shape) ?? null,
|
2026-04-06 08:25:50 +02:00
|
|
|
resolvePlayerVolumeState: (feetPosition) => this.resolvePlayerVolumeState(feetPosition),
|
2026-03-31 03:04:15 +02:00
|
|
|
setRuntimeMessage: (message) => {
|
|
|
|
|
if (message === this.currentRuntimeMessage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.currentRuntimeMessage = message;
|
|
|
|
|
this.runtimeMessageHandler?.(message);
|
|
|
|
|
},
|
|
|
|
|
setFirstPersonTelemetry: (telemetry) => {
|
|
|
|
|
this.currentFirstPersonTelemetry = telemetry;
|
|
|
|
|
this.firstPersonTelemetryHandler?.(telemetry);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 08:25:50 +02:00
|
|
|
private resolvePlayerVolumeState(feetPosition: { x: number; y: number; z: number }): RuntimePlayerVolumeState {
|
|
|
|
|
if (this.runtimeScene === null) {
|
|
|
|
|
return {
|
|
|
|
|
inWater: false,
|
|
|
|
|
inFog: false
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const inWater = this.runtimeScene.volumes.water.some((volume) => this.isPointInsideOrientedVolume(feetPosition, volume));
|
|
|
|
|
const inFog = this.runtimeScene.volumes.fog.some((volume) => this.isPointInsideOrientedVolume(feetPosition, volume));
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
inWater,
|
|
|
|
|
inFog
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private isPointInsideOrientedVolume(
|
|
|
|
|
point: { x: number; y: number; z: number },
|
|
|
|
|
volume: { center: { x: number; y: number; z: number }; rotationDegrees: { x: number; y: number; z: number }; size: { x: number; y: number; z: number } }
|
|
|
|
|
): boolean {
|
|
|
|
|
this.volumeOffset.set(point.x - volume.center.x, point.y - volume.center.y, point.z - volume.center.z);
|
|
|
|
|
|
|
|
|
|
this.volumeInverseRotation
|
|
|
|
|
.setFromEuler(
|
|
|
|
|
new Euler(
|
|
|
|
|
(volume.rotationDegrees.x * Math.PI) / 180,
|
|
|
|
|
(volume.rotationDegrees.y * Math.PI) / 180,
|
|
|
|
|
(volume.rotationDegrees.z * Math.PI) / 180,
|
|
|
|
|
"XYZ"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.invert();
|
|
|
|
|
|
|
|
|
|
this.volumeOffset.applyQuaternion(this.volumeInverseRotation);
|
|
|
|
|
|
|
|
|
|
const halfX = volume.size.x * 0.5;
|
|
|
|
|
const halfY = volume.size.y * 0.5;
|
|
|
|
|
const halfZ = volume.size.z * 0.5;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
Math.abs(this.volumeOffset.x) <= halfX &&
|
|
|
|
|
Math.abs(this.volumeOffset.y) <= halfY &&
|
|
|
|
|
Math.abs(this.volumeOffset.z) <= halfZ
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:04:15 +02:00
|
|
|
mount(container: HTMLElement) {
|
|
|
|
|
this.container = container;
|
2026-03-31 06:25:22 +02:00
|
|
|
container.appendChild(this.domElement);
|
2026-03-31 06:46:27 +02:00
|
|
|
this.domElement.addEventListener("click", this.handleRuntimeClick);
|
2026-04-02 19:39:55 +02:00
|
|
|
this.domElement.addEventListener("pointerdown", this.handleRuntimePointerDown);
|
2026-03-31 03:04:15 +02:00
|
|
|
this.resize();
|
|
|
|
|
|
|
|
|
|
this.resizeObserver = new ResizeObserver(() => {
|
|
|
|
|
this.resize();
|
|
|
|
|
});
|
|
|
|
|
this.resizeObserver.observe(container);
|
|
|
|
|
|
|
|
|
|
this.previousFrameTime = performance.now();
|
|
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadScene(runtimeScene: RuntimeSceneDefinition) {
|
|
|
|
|
this.runtimeScene = runtimeScene;
|
2026-03-31 20:07:11 +02:00
|
|
|
this.currentWorld = runtimeScene.world;
|
2026-03-31 06:17:09 +02:00
|
|
|
this.interactionSystem.reset();
|
2026-03-31 06:46:27 +02:00
|
|
|
this.setInteractionPrompt(null);
|
2026-03-31 20:07:11 +02:00
|
|
|
this.applyWorld();
|
|
|
|
|
this.rebuildLocalLights(runtimeScene.localLights);
|
2026-03-31 03:04:15 +02:00
|
|
|
this.rebuildBrushMeshes(runtimeScene.brushes);
|
2026-04-04 08:37:23 +02:00
|
|
|
this.rebuildModelInstances(runtimeScene.modelInstances);
|
2026-04-04 15:53:34 +02:00
|
|
|
void this.rebuildCollisionWorld(runtimeScene.colliders, runtimeScene.playerCollider);
|
2026-04-02 19:39:55 +02:00
|
|
|
this.audioSystem.loadScene(runtimeScene);
|
2026-03-31 17:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 20:07:11 +02:00
|
|
|
updateAssets(
|
|
|
|
|
projectAssets: Record<string, ProjectAssetRecord>,
|
|
|
|
|
loadedModelAssets: Record<string, LoadedModelAsset>,
|
2026-04-02 19:39:55 +02:00
|
|
|
loadedImageAssets: Record<string, LoadedImageAsset>,
|
|
|
|
|
loadedAudioAssets: Record<string, LoadedAudioAsset>
|
2026-03-31 20:07:11 +02:00
|
|
|
) {
|
2026-03-31 17:40:12 +02:00
|
|
|
this.projectAssets = projectAssets;
|
|
|
|
|
this.loadedModelAssets = loadedModelAssets;
|
2026-03-31 20:07:11 +02:00
|
|
|
this.loadedImageAssets = loadedImageAssets;
|
|
|
|
|
|
|
|
|
|
if (this.currentWorld !== null) {
|
|
|
|
|
this.applyWorld();
|
|
|
|
|
}
|
2026-03-31 17:40:12 +02:00
|
|
|
|
|
|
|
|
if (this.runtimeScene !== null) {
|
2026-04-04 08:37:23 +02:00
|
|
|
this.rebuildModelInstances(this.runtimeScene.modelInstances);
|
2026-03-31 17:40:12 +02:00
|
|
|
}
|
2026-04-02 19:39:55 +02:00
|
|
|
|
|
|
|
|
this.audioSystem.updateAssets(projectAssets, loadedAudioAssets);
|
2026-03-31 03:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setNavigationMode(mode: RuntimeNavigationMode) {
|
|
|
|
|
if (this.runtimeScene === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextController = mode === "firstPerson" ? this.firstPersonController : this.orbitVisitorController;
|
|
|
|
|
|
|
|
|
|
if (this.activeController?.id === nextController.id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.activeController === this.firstPersonController && this.currentFirstPersonTelemetry !== null && nextController === this.orbitVisitorController) {
|
|
|
|
|
this.orbitVisitorController.setFocusPoint(this.currentFirstPersonTelemetry.feetPosition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.activeController?.deactivate(this.controllerContext);
|
2026-03-31 06:17:31 +02:00
|
|
|
this.interactionSystem.reset();
|
2026-03-31 06:46:27 +02:00
|
|
|
this.setInteractionPrompt(null);
|
2026-03-31 03:04:15 +02:00
|
|
|
this.activeController = nextController;
|
|
|
|
|
this.activeController.activate(this.controllerContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setRuntimeMessageHandler(handler: ((message: string | null) => void) | null) {
|
|
|
|
|
this.runtimeMessageHandler = handler;
|
2026-04-02 19:39:55 +02:00
|
|
|
this.audioSystem.setRuntimeMessageHandler(handler);
|
2026-03-31 03:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setFirstPersonTelemetryHandler(handler: ((telemetry: FirstPersonTelemetry | null) => void) | null) {
|
|
|
|
|
this.firstPersonTelemetryHandler = handler;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 06:46:27 +02:00
|
|
|
setInteractionPromptHandler(handler: ((prompt: RuntimeInteractionPrompt | null) => void) | null) {
|
|
|
|
|
this.interactionPromptHandler = handler;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:04:15 +02:00
|
|
|
dispose() {
|
|
|
|
|
if (this.animationFrame !== 0) {
|
|
|
|
|
cancelAnimationFrame(this.animationFrame);
|
|
|
|
|
this.animationFrame = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.activeController?.deactivate(this.controllerContext);
|
|
|
|
|
this.activeController = null;
|
2026-03-31 06:46:27 +02:00
|
|
|
this.setInteractionPrompt(null);
|
2026-03-31 03:04:15 +02:00
|
|
|
this.resizeObserver?.disconnect();
|
|
|
|
|
this.resizeObserver = null;
|
2026-03-31 20:07:11 +02:00
|
|
|
this.clearLocalLights();
|
2026-03-31 03:04:15 +02:00
|
|
|
this.clearBrushMeshes();
|
2026-03-31 17:40:12 +02:00
|
|
|
this.clearModelInstances();
|
2026-04-04 07:51:38 +02:00
|
|
|
this.collisionWorldRequestId += 1;
|
|
|
|
|
this.clearCollisionWorld();
|
2026-04-02 19:39:55 +02:00
|
|
|
this.audioSystem.dispose();
|
2026-04-02 20:52:22 +02:00
|
|
|
this.advancedRenderingComposer?.dispose();
|
|
|
|
|
this.advancedRenderingComposer = null;
|
|
|
|
|
this.currentAdvancedRenderingSettings = null;
|
2026-04-07 06:15:02 +02:00
|
|
|
this.scene.fog = null;
|
2026-04-02 20:52:22 +02:00
|
|
|
if (this.renderer !== null) {
|
|
|
|
|
this.renderer.autoClear = true;
|
|
|
|
|
}
|
2026-03-31 03:04:15 +02:00
|
|
|
|
|
|
|
|
for (const cachedTexture of this.materialTextureCache.values()) {
|
|
|
|
|
cachedTexture.texture.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.materialTextureCache.clear();
|
2026-04-06 18:04:29 +02:00
|
|
|
this.renderer?.forceContextLoss();
|
2026-03-31 06:25:22 +02:00
|
|
|
this.renderer?.dispose();
|
2026-03-31 06:46:27 +02:00
|
|
|
this.domElement.removeEventListener("click", this.handleRuntimeClick);
|
2026-04-02 19:39:55 +02:00
|
|
|
this.domElement.removeEventListener("pointerdown", this.handleRuntimePointerDown);
|
2026-03-31 03:04:15 +02:00
|
|
|
|
2026-03-31 06:25:22 +02:00
|
|
|
if (this.container !== null && this.container.contains(this.domElement)) {
|
|
|
|
|
this.container.removeChild(this.domElement);
|
2026-03-31 03:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.container = null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 20:07:11 +02:00
|
|
|
private applyWorld() {
|
|
|
|
|
if (this.currentWorld === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const world = this.currentWorld;
|
|
|
|
|
this.ambientLight.color.set(world.ambientLight.colorHex);
|
|
|
|
|
this.ambientLight.intensity = world.ambientLight.intensity;
|
|
|
|
|
this.sunLight.color.set(world.sunLight.colorHex);
|
|
|
|
|
this.sunLight.intensity = world.sunLight.intensity;
|
2026-03-31 03:04:15 +02:00
|
|
|
this.sunLight.position
|
|
|
|
|
.set(
|
2026-03-31 20:07:11 +02:00
|
|
|
world.sunLight.direction.x,
|
|
|
|
|
world.sunLight.direction.y,
|
|
|
|
|
world.sunLight.direction.z
|
2026-03-31 03:04:15 +02:00
|
|
|
)
|
|
|
|
|
.normalize()
|
|
|
|
|
.multiplyScalar(18);
|
2026-03-31 20:07:11 +02:00
|
|
|
|
|
|
|
|
if (world.background.mode === "image") {
|
2026-03-31 23:14:41 +02:00
|
|
|
const texture = this.loadedImageAssets[world.background.assetId]?.texture ?? null;
|
|
|
|
|
this.scene.background = texture;
|
|
|
|
|
this.scene.environment = texture;
|
|
|
|
|
this.scene.environmentIntensity = world.background.environmentIntensity;
|
2026-04-02 20:52:12 +02:00
|
|
|
} else {
|
|
|
|
|
this.scene.background = null;
|
|
|
|
|
this.scene.environment = null;
|
|
|
|
|
this.scene.environmentIntensity = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.renderer !== null) {
|
|
|
|
|
configureAdvancedRenderingRenderer(this.renderer, world.advancedRendering);
|
|
|
|
|
this.syncAdvancedRenderingComposer(world.advancedRendering);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.applyShadowState();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 15:53:34 +02:00
|
|
|
private async rebuildCollisionWorld(colliders: RuntimeSceneDefinition["colliders"], playerShape: RuntimeSceneDefinition["playerCollider"]) {
|
2026-04-04 07:51:38 +02:00
|
|
|
const requestId = ++this.collisionWorldRequestId;
|
|
|
|
|
|
|
|
|
|
this.clearCollisionWorld();
|
|
|
|
|
|
|
|
|
|
try {
|
2026-04-04 15:53:34 +02:00
|
|
|
const nextCollisionWorld = await RapierCollisionWorld.create(colliders, playerShape);
|
2026-04-04 07:51:38 +02:00
|
|
|
|
|
|
|
|
if (requestId !== this.collisionWorldRequestId) {
|
|
|
|
|
nextCollisionWorld.dispose();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.collisionWorld = nextCollisionWorld;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (requestId !== this.collisionWorldRequestId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const message = error instanceof Error ? error.message : "Runner collision initialization failed.";
|
|
|
|
|
this.currentRuntimeMessage = `Runner collision initialization failed: ${message}`;
|
|
|
|
|
this.runtimeMessageHandler?.(this.currentRuntimeMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private clearCollisionWorld() {
|
|
|
|
|
this.collisionWorld?.dispose();
|
|
|
|
|
this.collisionWorld = null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 20:52:12 +02:00
|
|
|
private syncAdvancedRenderingComposer(settings: AdvancedRenderingSettings) {
|
|
|
|
|
if (this.renderer === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const shouldUseComposer = settings.enabled;
|
|
|
|
|
const settingsChanged =
|
|
|
|
|
this.currentAdvancedRenderingSettings === null ||
|
|
|
|
|
!areAdvancedRenderingSettingsEqual(this.currentAdvancedRenderingSettings, settings);
|
|
|
|
|
|
|
|
|
|
if (!shouldUseComposer) {
|
|
|
|
|
if (this.advancedRenderingComposer !== null) {
|
|
|
|
|
this.advancedRenderingComposer.dispose();
|
|
|
|
|
this.advancedRenderingComposer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.currentAdvancedRenderingSettings = null;
|
|
|
|
|
this.renderer.autoClear = true;
|
2026-03-31 20:07:11 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 20:52:12 +02:00
|
|
|
if (this.advancedRenderingComposer !== null && !settingsChanged) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.advancedRenderingComposer !== null) {
|
|
|
|
|
this.advancedRenderingComposer.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.advancedRenderingComposer = createAdvancedRenderingComposer(this.renderer, this.scene, this.camera, settings);
|
|
|
|
|
this.currentAdvancedRenderingSettings = cloneAdvancedRenderingSettings(settings);
|
|
|
|
|
this.renderer.autoClear = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyShadowState() {
|
|
|
|
|
if (this.currentWorld === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const advancedRendering = this.currentWorld.advancedRendering;
|
|
|
|
|
const shadowsEnabled = advancedRendering.enabled && advancedRendering.shadows.enabled;
|
|
|
|
|
|
|
|
|
|
applyAdvancedRenderingLightShadowFlags(this.sunLight, advancedRendering);
|
|
|
|
|
|
|
|
|
|
for (const renderGroup of this.localLightObjects.values()) {
|
|
|
|
|
applyAdvancedRenderingLightShadowFlags(renderGroup, advancedRendering);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const mesh of this.brushMeshes.values()) {
|
|
|
|
|
applyAdvancedRenderingRenderableShadowFlags(mesh, shadowsEnabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const renderGroup of this.modelRenderObjects.values()) {
|
|
|
|
|
applyAdvancedRenderingRenderableShadowFlags(renderGroup, shadowsEnabled);
|
|
|
|
|
}
|
2026-03-31 20:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private rebuildLocalLights(localLights: RuntimeLocalLightCollection) {
|
|
|
|
|
this.clearLocalLights();
|
|
|
|
|
|
|
|
|
|
for (const pointLight of localLights.pointLights) {
|
|
|
|
|
const renderObjects = this.createPointLightRuntimeObjects(pointLight);
|
|
|
|
|
this.localLightGroup.add(renderObjects.group);
|
|
|
|
|
this.localLightObjects.set(pointLight.entityId, renderObjects.group);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const spotLight of localLights.spotLights) {
|
|
|
|
|
const renderObjects = this.createSpotLightRuntimeObjects(spotLight);
|
|
|
|
|
this.localLightGroup.add(renderObjects.group);
|
|
|
|
|
this.localLightObjects.set(spotLight.entityId, renderObjects.group);
|
|
|
|
|
}
|
2026-04-02 20:52:12 +02:00
|
|
|
|
|
|
|
|
this.applyShadowState();
|
2026-03-31 03:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 20:07:32 +02:00
|
|
|
private createPointLightRuntimeObjects(pointLight: RuntimeLocalLightCollection["pointLights"][number]): LocalLightRenderObjects {
|
|
|
|
|
const group = new Group();
|
|
|
|
|
const light = new PointLight(pointLight.colorHex, pointLight.intensity, pointLight.distance);
|
|
|
|
|
|
|
|
|
|
group.position.set(pointLight.position.x, pointLight.position.y, pointLight.position.z);
|
|
|
|
|
light.position.set(0, 0, 0);
|
|
|
|
|
group.add(light);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
group
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private createSpotLightRuntimeObjects(spotLight: RuntimeLocalLightCollection["spotLights"][number]): LocalLightRenderObjects {
|
|
|
|
|
const group = new Group();
|
|
|
|
|
const light = new SpotLight(
|
|
|
|
|
spotLight.colorHex,
|
|
|
|
|
spotLight.intensity,
|
|
|
|
|
spotLight.distance,
|
|
|
|
|
(spotLight.angleDegrees * Math.PI) / 180,
|
|
|
|
|
0.18,
|
|
|
|
|
1
|
|
|
|
|
);
|
|
|
|
|
const direction = new Vector3(spotLight.direction.x, spotLight.direction.y, spotLight.direction.z).normalize();
|
|
|
|
|
const orientation = new Quaternion().setFromUnitVectors(new Vector3(0, 1, 0), direction);
|
|
|
|
|
|
|
|
|
|
group.position.set(spotLight.position.x, spotLight.position.y, spotLight.position.z);
|
|
|
|
|
group.quaternion.copy(orientation);
|
|
|
|
|
light.position.set(0, 0, 0);
|
|
|
|
|
light.target.position.set(0, 1, 0);
|
|
|
|
|
group.add(light);
|
|
|
|
|
group.add(light.target);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
group
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:04:15 +02:00
|
|
|
private rebuildBrushMeshes(brushes: RuntimeBoxBrushInstance[]) {
|
|
|
|
|
this.clearBrushMeshes();
|
2026-04-06 08:44:52 +02:00
|
|
|
const volumeRenderPaths: ResolvedBoxVolumeRenderPaths =
|
|
|
|
|
this.currentWorld === null ? { fog: "performance", water: "performance" } : resolveBoxVolumeRenderPaths(this.currentWorld.advancedRendering);
|
2026-03-31 03:04:15 +02:00
|
|
|
|
|
|
|
|
for (const brush of brushes) {
|
2026-04-05 02:30:08 +02:00
|
|
|
const geometry = buildBoxBrushDerivedMeshData(brush).geometry;
|
2026-04-06 20:52:05 +02:00
|
|
|
const staticContactPatches = brush.volume.mode === "water" ? this.collectRuntimeStaticWaterContactPatches(brush) : [];
|
2026-04-07 06:36:05 +02:00
|
|
|
const contactPatches =
|
|
|
|
|
brush.volume.mode === "water"
|
|
|
|
|
? this.mergeRuntimeWaterContactPatches(brush, staticContactPatches, this.collectRuntimePlayerWaterContactPatches(brush))
|
|
|
|
|
: [];
|
2026-03-31 03:04:15 +02:00
|
|
|
|
|
|
|
|
const materials = [
|
2026-04-06 20:55:10 +02:00
|
|
|
this.createFaceMaterial(brush, "posX", brush.faces.posX.material, volumeRenderPaths, contactPatches, staticContactPatches),
|
|
|
|
|
this.createFaceMaterial(brush, "negX", brush.faces.negX.material, volumeRenderPaths, contactPatches, staticContactPatches),
|
|
|
|
|
this.createFaceMaterial(brush, "posY", brush.faces.posY.material, volumeRenderPaths, contactPatches, staticContactPatches),
|
|
|
|
|
this.createFaceMaterial(brush, "negY", brush.faces.negY.material, volumeRenderPaths, contactPatches, staticContactPatches),
|
|
|
|
|
this.createFaceMaterial(brush, "posZ", brush.faces.posZ.material, volumeRenderPaths, contactPatches, staticContactPatches),
|
|
|
|
|
this.createFaceMaterial(brush, "negZ", brush.faces.negZ.material, volumeRenderPaths, contactPatches, staticContactPatches)
|
2026-03-31 03:04:15 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const mesh = new Mesh(geometry, materials);
|
|
|
|
|
mesh.position.set(brush.center.x, brush.center.y, brush.center.z);
|
2026-04-04 19:27:06 +02:00
|
|
|
mesh.rotation.set(
|
|
|
|
|
(brush.rotationDegrees.x * Math.PI) / 180,
|
|
|
|
|
(brush.rotationDegrees.y * Math.PI) / 180,
|
|
|
|
|
(brush.rotationDegrees.z * Math.PI) / 180
|
|
|
|
|
);
|
2026-03-31 03:04:15 +02:00
|
|
|
this.brushGroup.add(mesh);
|
|
|
|
|
this.brushMeshes.set(brush.id, mesh);
|
|
|
|
|
}
|
2026-04-02 20:52:12 +02:00
|
|
|
|
|
|
|
|
this.applyShadowState();
|
2026-03-31 03:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 08:36:51 +02:00
|
|
|
private rebuildModelInstances(modelInstances: RuntimeSceneDefinition["modelInstances"]) {
|
2026-03-31 17:40:12 +02:00
|
|
|
this.clearModelInstances();
|
|
|
|
|
|
|
|
|
|
for (const modelInstance of modelInstances) {
|
|
|
|
|
const asset = this.projectAssets[modelInstance.assetId];
|
|
|
|
|
const loadedAsset = this.loadedModelAssets[modelInstance.assetId];
|
|
|
|
|
const renderGroup = createModelInstanceRenderGroup(
|
|
|
|
|
{
|
|
|
|
|
id: modelInstance.instanceId,
|
|
|
|
|
kind: "modelInstance",
|
|
|
|
|
assetId: modelInstance.assetId,
|
|
|
|
|
name: modelInstance.name,
|
|
|
|
|
position: modelInstance.position,
|
|
|
|
|
rotationDegrees: modelInstance.rotationDegrees,
|
2026-04-04 07:57:32 +02:00
|
|
|
scale: modelInstance.scale,
|
|
|
|
|
collision: {
|
|
|
|
|
mode: "none",
|
|
|
|
|
visible: false
|
|
|
|
|
}
|
2026-03-31 17:40:12 +02:00
|
|
|
},
|
|
|
|
|
asset,
|
|
|
|
|
loadedAsset,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
this.modelGroup.add(renderGroup);
|
|
|
|
|
this.modelRenderObjects.set(modelInstance.instanceId, renderGroup);
|
2026-04-01 00:07:25 +02:00
|
|
|
|
|
|
|
|
if (loadedAsset?.animations && loadedAsset.animations.length > 0) {
|
|
|
|
|
const mixer = new AnimationMixer(renderGroup);
|
|
|
|
|
this.animationMixers.set(modelInstance.instanceId, mixer);
|
|
|
|
|
this.instanceAnimationClips.set(modelInstance.instanceId, loadedAsset.animations);
|
|
|
|
|
|
|
|
|
|
if (modelInstance.animationAutoplay === true && modelInstance.animationClipName) {
|
|
|
|
|
const clip = AnimationClip.findByName(loadedAsset.animations, modelInstance.animationClipName);
|
|
|
|
|
if (clip) {
|
|
|
|
|
mixer.clipAction(clip).play();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-31 17:40:12 +02:00
|
|
|
}
|
2026-04-02 20:52:12 +02:00
|
|
|
|
|
|
|
|
this.applyShadowState();
|
2026-03-31 17:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 08:26:27 +02:00
|
|
|
private createFaceMaterial(
|
|
|
|
|
brush: RuntimeBoxBrushInstance,
|
|
|
|
|
faceId: "posX" | "negX" | "posY" | "negY" | "posZ" | "negZ",
|
|
|
|
|
material: RuntimeBoxBrushInstance["faces"]["posX"]["material"],
|
2026-04-06 17:27:03 +02:00
|
|
|
volumeRenderPaths: { fog: "performance" | "quality"; water: "performance" | "quality" },
|
2026-04-06 20:55:10 +02:00
|
|
|
contactPatches: ReturnType<typeof collectWaterContactPatches>,
|
|
|
|
|
staticContactPatches: ReturnType<typeof collectWaterContactPatches>
|
2026-04-06 09:17:19 +02:00
|
|
|
): Material {
|
2026-04-06 08:26:27 +02:00
|
|
|
if (brush.volume.mode === "water") {
|
|
|
|
|
const baseOpacity = Math.max(0.05, Math.min(1, brush.volume.water.surfaceOpacity));
|
2026-04-06 17:27:03 +02:00
|
|
|
const waterMaterial = createWaterMaterial({
|
|
|
|
|
colorHex: brush.volume.water.colorHex,
|
|
|
|
|
surfaceOpacity: brush.volume.water.surfaceOpacity,
|
|
|
|
|
waveStrength: brush.volume.water.waveStrength,
|
2026-04-06 09:17:19 +02:00
|
|
|
opacity: faceId === "posY" ? Math.min(1, baseOpacity + 0.18) : baseOpacity * 0.5,
|
2026-04-06 17:27:03 +02:00
|
|
|
quality: volumeRenderPaths.water === "quality",
|
|
|
|
|
wireframe: false,
|
|
|
|
|
isTopFace: faceId === "posY",
|
|
|
|
|
time: this.volumeTime,
|
|
|
|
|
halfSize: {
|
|
|
|
|
x: brush.size.x * 0.5,
|
|
|
|
|
z: brush.size.z * 0.5
|
|
|
|
|
},
|
2026-04-07 06:35:08 +02:00
|
|
|
contactPatches,
|
|
|
|
|
reflection: {
|
|
|
|
|
texture: null,
|
|
|
|
|
enabled: faceId === "posY" && this.getWaterReflectionMode() !== "none"
|
|
|
|
|
}
|
2026-04-06 08:26:27 +02:00
|
|
|
});
|
2026-04-06 17:27:03 +02:00
|
|
|
|
|
|
|
|
if (waterMaterial.animationUniform !== null) {
|
|
|
|
|
this.volumeAnimatedUniforms.push(waterMaterial.animationUniform);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 04:58:48 +02:00
|
|
|
if (faceId === "posY" && waterMaterial.contactPatchesUniform !== null && waterMaterial.contactPatchAxesUniform !== null) {
|
2026-04-06 18:04:29 +02:00
|
|
|
this.runtimeWaterContactUniforms.push({
|
|
|
|
|
brush,
|
2026-04-06 20:52:05 +02:00
|
|
|
uniform: waterMaterial.contactPatchesUniform,
|
2026-04-07 04:58:48 +02:00
|
|
|
axisUniform: waterMaterial.contactPatchAxesUniform,
|
2026-04-07 06:15:02 +02:00
|
|
|
shapeUniform: waterMaterial.contactPatchShapesUniform ?? { value: [] },
|
2026-04-07 06:35:08 +02:00
|
|
|
staticContactPatches,
|
|
|
|
|
reflectionTextureUniform: waterMaterial.reflectionTextureUniform,
|
|
|
|
|
reflectionMatrixUniform: waterMaterial.reflectionMatrixUniform,
|
|
|
|
|
reflectionEnabledUniform: waterMaterial.reflectionEnabledUniform,
|
|
|
|
|
reflectionRenderTarget: this.getWaterReflectionMode() !== "none" ? this.createWaterReflectionRenderTarget() : null
|
2026-04-06 18:04:29 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 17:27:03 +02:00
|
|
|
return waterMaterial.material;
|
2026-04-06 08:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (brush.volume.mode === "fog") {
|
2026-04-06 09:17:19 +02:00
|
|
|
if (volumeRenderPaths.fog === "quality") {
|
2026-04-06 09:18:20 +02:00
|
|
|
return this.createFogQualityMaterial(brush.volume.fog);
|
2026-04-06 09:17:19 +02:00
|
|
|
}
|
|
|
|
|
// Performance fallback: simple transparent material
|
|
|
|
|
const densityOpacity = Math.max(0.06, Math.min(0.72, brush.volume.fog.density * 0.8 + 0.08));
|
|
|
|
|
return new MeshBasicMaterial({
|
2026-04-06 08:26:27 +02:00
|
|
|
color: brush.volume.fog.colorHex,
|
|
|
|
|
transparent: true,
|
|
|
|
|
opacity: densityOpacity,
|
|
|
|
|
depthWrite: false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:04:15 +02:00
|
|
|
if (material === null) {
|
|
|
|
|
return new MeshStandardMaterial({
|
|
|
|
|
color: FALLBACK_FACE_COLOR,
|
|
|
|
|
roughness: 0.9,
|
|
|
|
|
metalness: 0.05
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new MeshStandardMaterial({
|
|
|
|
|
color: 0xffffff,
|
|
|
|
|
map: this.getOrCreateTexture(material),
|
|
|
|
|
roughness: 0.92,
|
|
|
|
|
metalness: 0.02
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 09:17:19 +02:00
|
|
|
// Soft edge-faded fog shader with slow drift animation — quality mode only.
|
2026-04-06 09:18:04 +02:00
|
|
|
private createFogQualityMaterial(fog: { colorHex: string; density: number }): ShaderMaterial {
|
|
|
|
|
const hex = fog.colorHex.replace("#", "");
|
2026-04-06 09:17:19 +02:00
|
|
|
const cr = parseInt(hex.substring(0, 2), 16) / 255;
|
|
|
|
|
const cg = parseInt(hex.substring(2, 4), 16) / 255;
|
|
|
|
|
const cb = parseInt(hex.substring(4, 6), 16) / 255;
|
|
|
|
|
|
|
|
|
|
const vertexShader = /* glsl */ `
|
|
|
|
|
varying vec2 vUv;
|
2026-04-07 06:15:02 +02:00
|
|
|
#include <fog_pars_vertex>
|
2026-04-06 09:17:19 +02:00
|
|
|
void main() {
|
|
|
|
|
vUv = uv;
|
2026-04-07 06:15:02 +02:00
|
|
|
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
|
|
|
|
gl_Position = projectionMatrix * mvPosition;
|
|
|
|
|
#include <fog_vertex>
|
2026-04-06 09:17:19 +02:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const fragmentShader = /* glsl */ `
|
2026-04-07 06:35:08 +02:00
|
|
|
uniform vec3 volumeFogColor;
|
|
|
|
|
uniform float volumeFogDensity;
|
2026-04-06 09:17:19 +02:00
|
|
|
uniform float time;
|
|
|
|
|
varying vec2 vUv;
|
2026-04-07 06:15:02 +02:00
|
|
|
#include <fog_pars_fragment>
|
2026-04-06 09:17:19 +02:00
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
// Soft fade: distance from the center of each face in UV space
|
|
|
|
|
vec2 dist = abs(vUv - 0.5) * 2.0; // 0 at center, 1 at edge
|
|
|
|
|
float edgeFade = 1.0 - smoothstep(0.4, 1.0, max(dist.x, dist.y));
|
|
|
|
|
|
|
|
|
|
// Slow drifting noise to break up the flat look
|
|
|
|
|
float drift = sin(vUv.x * 4.5 + time * 0.28) * sin(vUv.y * 3.2 + time * 0.22);
|
|
|
|
|
float variation = 0.82 + drift * 0.18;
|
|
|
|
|
|
2026-04-07 06:35:08 +02:00
|
|
|
float alpha = volumeFogDensity * edgeFade * variation;
|
2026-04-06 09:17:19 +02:00
|
|
|
alpha = clamp(alpha, 0.0, 0.88);
|
|
|
|
|
|
|
|
|
|
// Edge scatter brightening
|
2026-04-07 06:35:08 +02:00
|
|
|
vec3 color = mix(volumeFogColor, vec3(1.0), (1.0 - edgeFade) * 0.09);
|
2026-04-06 09:17:19 +02:00
|
|
|
gl_FragColor = vec4(color, alpha);
|
2026-04-07 06:15:02 +02:00
|
|
|
#include <fog_fragment>
|
2026-04-06 09:17:19 +02:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2026-04-07 06:35:08 +02:00
|
|
|
const uniforms = UniformsUtils.merge([
|
|
|
|
|
UniformsLib.fog,
|
|
|
|
|
{
|
|
|
|
|
volumeFogColor: { value: [cr, cg, cb] },
|
|
|
|
|
volumeFogDensity: { value: Math.min(0.9, fog.density + 0.12) },
|
|
|
|
|
time: { value: this.volumeTime }
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|
2026-04-06 09:17:19 +02:00
|
|
|
const mat = new ShaderMaterial({
|
|
|
|
|
vertexShader,
|
|
|
|
|
fragmentShader,
|
2026-04-07 06:35:08 +02:00
|
|
|
uniforms,
|
2026-04-06 09:17:19 +02:00
|
|
|
transparent: true,
|
|
|
|
|
depthWrite: false,
|
2026-04-07 06:15:02 +02:00
|
|
|
fog: true,
|
2026-04-06 09:17:19 +02:00
|
|
|
side: 2 // THREE.DoubleSide — fog is visible from inside too
|
|
|
|
|
});
|
|
|
|
|
this.volumeAnimatedMaterials.push(mat);
|
|
|
|
|
return mat;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 06:15:02 +02:00
|
|
|
private updateUnderwaterSceneFog() {
|
|
|
|
|
const fogState =
|
|
|
|
|
this.activeController === this.firstPersonController
|
|
|
|
|
? resolveUnderwaterFogState(this.runtimeScene, this.currentFirstPersonTelemetry)
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if (fogState === null) {
|
|
|
|
|
if (this.scene.fog !== null) {
|
|
|
|
|
this.scene.fog = null;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.underwaterSceneFog.color.set(fogState.colorHex);
|
|
|
|
|
this.underwaterSceneFog.density = fogState.density;
|
|
|
|
|
|
|
|
|
|
if (this.scene.fog !== this.underwaterSceneFog) {
|
|
|
|
|
this.scene.fog = this.underwaterSceneFog;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 06:35:21 +02:00
|
|
|
private getWaterReflectionMode() {
|
|
|
|
|
if (this.currentWorld === null || !this.currentWorld.advancedRendering.enabled || this.currentWorld.advancedRendering.waterPath !== "quality") {
|
|
|
|
|
return "none" as const;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.currentWorld.advancedRendering.waterReflectionMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private createWaterReflectionRenderTarget() {
|
2026-04-07 06:42:24 +02:00
|
|
|
const canvasWidth = this.container?.clientWidth ?? this.domElement.width;
|
|
|
|
|
const canvasHeight = this.container?.clientHeight ?? this.domElement.height;
|
|
|
|
|
const width = Math.max(128, Math.round(Math.max(canvasWidth, 512) * 0.5));
|
|
|
|
|
const height = Math.max(128, Math.round(Math.max(canvasHeight, 512) * 0.5));
|
2026-04-07 06:35:21 +02:00
|
|
|
return new WebGLRenderTarget(width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private resizeWaterReflectionTargets() {
|
2026-04-07 06:42:24 +02:00
|
|
|
const canvasWidth = this.container?.clientWidth ?? this.domElement.width;
|
|
|
|
|
const canvasHeight = this.container?.clientHeight ?? this.domElement.height;
|
|
|
|
|
const width = Math.max(128, Math.round(Math.max(canvasWidth, 512) * 0.5));
|
|
|
|
|
const height = Math.max(128, Math.round(Math.max(canvasHeight, 512) * 0.5));
|
2026-04-07 06:35:21 +02:00
|
|
|
|
|
|
|
|
for (const binding of this.runtimeWaterContactUniforms) {
|
|
|
|
|
binding.reflectionRenderTarget?.setSize(width, height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateRuntimeWaterReflections() {
|
|
|
|
|
if (this.renderer === null || this.runtimeScene === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const reflectionMode = this.getWaterReflectionMode();
|
|
|
|
|
|
|
|
|
|
for (const binding of this.runtimeWaterContactUniforms) {
|
|
|
|
|
if (
|
|
|
|
|
reflectionMode === "none" ||
|
|
|
|
|
binding.reflectionRenderTarget === null ||
|
|
|
|
|
binding.reflectionTextureUniform === null ||
|
|
|
|
|
binding.reflectionMatrixUniform === null ||
|
|
|
|
|
binding.reflectionEnabledUniform === null
|
|
|
|
|
) {
|
|
|
|
|
if (binding.reflectionEnabledUniform !== null) {
|
|
|
|
|
binding.reflectionEnabledUniform.value = 0;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const canRenderReflection = updatePlanarReflectionCamera(
|
|
|
|
|
binding.brush,
|
|
|
|
|
this.camera,
|
|
|
|
|
this.waterReflectionCamera,
|
|
|
|
|
binding.reflectionMatrixUniform.value
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!canRenderReflection) {
|
|
|
|
|
binding.reflectionEnabledUniform.value = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hiddenWaterMeshes: Array<{ mesh: Mesh<BufferGeometry, Material[]>; visible: boolean }> = [];
|
|
|
|
|
for (const runtimeBrush of this.runtimeScene.brushes) {
|
|
|
|
|
if (runtimeBrush.volume.mode !== "water") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mesh = this.brushMeshes.get(runtimeBrush.id);
|
|
|
|
|
if (mesh === undefined) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hiddenWaterMeshes.push({ mesh, visible: mesh.visible });
|
|
|
|
|
mesh.visible = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const previousModelGroupVisibility = this.modelGroup.visible;
|
|
|
|
|
if (reflectionMode === "world") {
|
|
|
|
|
this.modelGroup.visible = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const previousAutoClear = this.renderer.autoClear;
|
|
|
|
|
const previousRenderTarget = this.renderer.getRenderTarget();
|
|
|
|
|
this.renderer.autoClear = true;
|
|
|
|
|
this.renderer.setRenderTarget(binding.reflectionRenderTarget);
|
|
|
|
|
this.renderer.clear();
|
|
|
|
|
this.renderer.render(this.scene, this.waterReflectionCamera);
|
|
|
|
|
this.renderer.setRenderTarget(previousRenderTarget);
|
|
|
|
|
this.renderer.autoClear = previousAutoClear;
|
|
|
|
|
this.modelGroup.visible = previousModelGroupVisibility;
|
|
|
|
|
|
|
|
|
|
for (const hiddenWaterMesh of hiddenWaterMeshes) {
|
|
|
|
|
hiddenWaterMesh.mesh.visible = hiddenWaterMesh.visible;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
binding.reflectionTextureUniform.value = binding.reflectionRenderTarget.texture;
|
|
|
|
|
binding.reflectionEnabledUniform.value = 0.36;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:04:15 +02:00
|
|
|
private getOrCreateTexture(material: NonNullable<RuntimeBoxBrushInstance["faces"]["posX"]["material"]>) {
|
|
|
|
|
const signature = createStarterMaterialSignature(material);
|
|
|
|
|
const cachedTexture = this.materialTextureCache.get(material.id);
|
|
|
|
|
|
|
|
|
|
if (cachedTexture !== undefined && cachedTexture.signature === signature) {
|
|
|
|
|
return cachedTexture.texture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cachedTexture?.texture.dispose();
|
|
|
|
|
|
|
|
|
|
const texture = createStarterMaterialTexture(material);
|
|
|
|
|
this.materialTextureCache.set(material.id, {
|
|
|
|
|
signature,
|
|
|
|
|
texture
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 20:07:37 +02:00
|
|
|
private clearLocalLights() {
|
|
|
|
|
for (const renderGroup of this.localLightObjects.values()) {
|
|
|
|
|
this.localLightGroup.remove(renderGroup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.localLightObjects.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:04:15 +02:00
|
|
|
private clearBrushMeshes() {
|
|
|
|
|
for (const mesh of this.brushMeshes.values()) {
|
|
|
|
|
this.brushGroup.remove(mesh);
|
|
|
|
|
mesh.geometry.dispose();
|
|
|
|
|
|
|
|
|
|
for (const material of mesh.material) {
|
|
|
|
|
material.dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.brushMeshes.clear();
|
2026-04-06 09:16:39 +02:00
|
|
|
this.volumeAnimatedMaterials.length = 0;
|
2026-04-06 17:27:49 +02:00
|
|
|
this.volumeAnimatedUniforms.length = 0;
|
2026-04-07 06:36:13 +02:00
|
|
|
for (const binding of this.runtimeWaterContactUniforms) {
|
|
|
|
|
binding.reflectionRenderTarget?.dispose();
|
|
|
|
|
}
|
2026-04-06 18:04:29 +02:00
|
|
|
this.runtimeWaterContactUniforms.length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private createPlayerWaterContactBounds() {
|
|
|
|
|
if (this.runtimeScene === null || this.currentFirstPersonTelemetry === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const feetPosition = this.currentFirstPersonTelemetry.feetPosition;
|
|
|
|
|
const playerShape = this.runtimeScene.playerCollider;
|
|
|
|
|
|
|
|
|
|
switch (playerShape.mode) {
|
|
|
|
|
case "capsule":
|
|
|
|
|
return {
|
|
|
|
|
min: {
|
|
|
|
|
x: feetPosition.x - playerShape.radius,
|
|
|
|
|
y: feetPosition.y,
|
|
|
|
|
z: feetPosition.z - playerShape.radius
|
|
|
|
|
},
|
|
|
|
|
max: {
|
|
|
|
|
x: feetPosition.x + playerShape.radius,
|
|
|
|
|
y: feetPosition.y + playerShape.height,
|
|
|
|
|
z: feetPosition.z + playerShape.radius
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
case "box":
|
|
|
|
|
return {
|
|
|
|
|
min: {
|
|
|
|
|
x: feetPosition.x - playerShape.size.x * 0.5,
|
|
|
|
|
y: feetPosition.y,
|
|
|
|
|
z: feetPosition.z - playerShape.size.z * 0.5
|
|
|
|
|
},
|
|
|
|
|
max: {
|
|
|
|
|
x: feetPosition.x + playerShape.size.x * 0.5,
|
|
|
|
|
y: feetPosition.y + playerShape.size.y,
|
|
|
|
|
z: feetPosition.z + playerShape.size.z * 0.5
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
case "none":
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 20:52:05 +02:00
|
|
|
private collectRuntimeStaticWaterContactPatches(brush: RuntimeBoxBrushInstance) {
|
2026-04-07 04:58:48 +02:00
|
|
|
const contactBounds: Parameters<typeof collectWaterContactPatches>[1] = [];
|
|
|
|
|
|
2026-04-07 05:44:04 +02:00
|
|
|
const runtimeBrushesById = new Map((this.runtimeScene?.brushes ?? []).map((runtimeBrush) => [runtimeBrush.id, runtimeBrush]));
|
|
|
|
|
|
|
|
|
|
for (const collider of this.runtimeScene?.colliders ?? []) {
|
|
|
|
|
if (collider.source === "brush") {
|
|
|
|
|
const otherBrush = runtimeBrushesById.get(collider.brushId);
|
|
|
|
|
|
|
|
|
|
if (otherBrush === undefined || otherBrush.id === brush.id || otherBrush.volume.mode !== "none") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contactBounds.push({
|
|
|
|
|
kind: "triangleMesh",
|
|
|
|
|
vertices: collider.vertices,
|
|
|
|
|
indices: collider.indices,
|
|
|
|
|
transform: {
|
|
|
|
|
position: collider.center,
|
|
|
|
|
rotationDegrees: collider.rotationDegrees,
|
|
|
|
|
scale: {
|
|
|
|
|
x: 1,
|
|
|
|
|
y: 1,
|
|
|
|
|
z: 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-04-07 04:58:48 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 05:44:04 +02:00
|
|
|
if (collider.kind === "trimesh") {
|
|
|
|
|
contactBounds.push({
|
|
|
|
|
kind: "triangleMesh",
|
|
|
|
|
vertices: collider.vertices,
|
|
|
|
|
indices: collider.indices,
|
2026-04-07 05:50:55 +02:00
|
|
|
mergeProfile: "aggressive",
|
2026-04-07 05:44:04 +02:00
|
|
|
transform: collider.transform
|
|
|
|
|
});
|
2026-04-07 04:58:48 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contactBounds.push({
|
|
|
|
|
min: collider.worldBounds.min,
|
|
|
|
|
max: collider.worldBounds.max
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-06 20:52:05 +02:00
|
|
|
|
|
|
|
|
return collectWaterContactPatches(
|
|
|
|
|
{
|
|
|
|
|
center: brush.center,
|
|
|
|
|
rotationDegrees: brush.rotationDegrees,
|
|
|
|
|
size: brush.size
|
|
|
|
|
},
|
2026-04-07 06:36:05 +02:00
|
|
|
contactBounds,
|
2026-04-07 06:55:15 +02:00
|
|
|
this.getRuntimeWaterFoamContactLimit(brush)
|
2026-04-06 20:52:05 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private collectRuntimePlayerWaterContactPatches(brush: RuntimeBoxBrushInstance) {
|
2026-04-06 18:04:29 +02:00
|
|
|
const playerBounds = this.createPlayerWaterContactBounds();
|
|
|
|
|
|
2026-04-06 20:52:05 +02:00
|
|
|
if (playerBounds === null) {
|
|
|
|
|
return [];
|
2026-04-06 18:04:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return collectWaterContactPatches(
|
|
|
|
|
{
|
|
|
|
|
center: brush.center,
|
|
|
|
|
rotationDegrees: brush.rotationDegrees,
|
|
|
|
|
size: brush.size
|
|
|
|
|
},
|
2026-04-07 06:36:05 +02:00
|
|
|
[playerBounds],
|
2026-04-07 06:55:15 +02:00
|
|
|
this.getRuntimeWaterFoamContactLimit(brush)
|
2026-04-06 18:04:29 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 06:55:15 +02:00
|
|
|
private getRuntimeWaterFoamContactLimit(brush: RuntimeBoxBrushInstance) {
|
|
|
|
|
return brush.volume.mode === "water" ? brush.volume.water.foamContactLimit : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 20:52:05 +02:00
|
|
|
private mergeRuntimeWaterContactPatches(
|
2026-04-07 06:36:05 +02:00
|
|
|
brush: RuntimeBoxBrushInstance,
|
2026-04-06 20:52:05 +02:00
|
|
|
staticContactPatches: ReturnType<typeof collectWaterContactPatches>,
|
|
|
|
|
dynamicContactPatches: ReturnType<typeof collectWaterContactPatches>
|
|
|
|
|
) {
|
2026-04-07 06:55:15 +02:00
|
|
|
return [...dynamicContactPatches, ...staticContactPatches].slice(0, this.getRuntimeWaterFoamContactLimit(brush));
|
2026-04-06 20:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 18:04:29 +02:00
|
|
|
private updateRuntimeWaterContactUniforms() {
|
|
|
|
|
for (const binding of this.runtimeWaterContactUniforms) {
|
2026-04-07 06:36:05 +02:00
|
|
|
const mergedPatches = this.mergeRuntimeWaterContactPatches(
|
|
|
|
|
binding.brush,
|
|
|
|
|
binding.staticContactPatches,
|
|
|
|
|
this.collectRuntimePlayerWaterContactPatches(binding.brush)
|
|
|
|
|
);
|
2026-04-07 04:58:48 +02:00
|
|
|
binding.uniform.value = createWaterContactPatchUniformValue(mergedPatches);
|
|
|
|
|
binding.axisUniform.value = createWaterContactPatchAxisUniformValue(mergedPatches);
|
2026-04-07 06:15:02 +02:00
|
|
|
binding.shapeUniform.value = createWaterContactPatchShapeUniformValue(mergedPatches);
|
2026-04-06 18:04:29 +02:00
|
|
|
}
|
2026-03-31 03:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 17:40:12 +02:00
|
|
|
private clearModelInstances() {
|
2026-04-01 00:07:30 +02:00
|
|
|
for (const mixer of this.animationMixers.values()) {
|
|
|
|
|
mixer.stopAllAction();
|
|
|
|
|
}
|
|
|
|
|
this.animationMixers.clear();
|
|
|
|
|
this.instanceAnimationClips.clear();
|
|
|
|
|
|
2026-03-31 17:40:12 +02:00
|
|
|
for (const renderGroup of this.modelRenderObjects.values()) {
|
|
|
|
|
this.modelGroup.remove(renderGroup);
|
|
|
|
|
disposeModelInstance(renderGroup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.modelRenderObjects.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:04:15 +02:00
|
|
|
private resize() {
|
|
|
|
|
if (this.container === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const width = this.container.clientWidth;
|
|
|
|
|
const height = this.container.clientHeight;
|
|
|
|
|
|
|
|
|
|
if (width === 0 || height === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.camera.aspect = width / height;
|
|
|
|
|
this.camera.updateProjectionMatrix();
|
2026-03-31 06:25:22 +02:00
|
|
|
this.domElement.width = width;
|
|
|
|
|
this.domElement.height = height;
|
|
|
|
|
this.renderer?.setSize(width, height, false);
|
2026-04-02 20:52:22 +02:00
|
|
|
this.advancedRenderingComposer?.setSize(width, height);
|
2026-04-07 06:36:13 +02:00
|
|
|
this.resizeWaterReflectionTargets();
|
2026-03-31 03:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private render = () => {
|
|
|
|
|
this.animationFrame = window.requestAnimationFrame(this.render);
|
|
|
|
|
|
|
|
|
|
const now = performance.now();
|
|
|
|
|
const dt = Math.min((now - this.previousFrameTime) / 1000, 1 / 20);
|
|
|
|
|
this.previousFrameTime = now;
|
|
|
|
|
|
|
|
|
|
this.activeController?.update(dt);
|
2026-04-02 19:39:55 +02:00
|
|
|
this.audioSystem.updateListenerTransform();
|
2026-03-31 06:17:09 +02:00
|
|
|
|
2026-04-06 09:16:39 +02:00
|
|
|
this.volumeTime += dt;
|
|
|
|
|
for (const mat of this.volumeAnimatedMaterials) {
|
|
|
|
|
(mat.uniforms["time"] as { value: number }).value = this.volumeTime;
|
|
|
|
|
}
|
2026-04-06 17:27:44 +02:00
|
|
|
for (const uniform of this.volumeAnimatedUniforms) {
|
|
|
|
|
uniform.value = this.volumeTime;
|
|
|
|
|
}
|
2026-04-06 09:16:39 +02:00
|
|
|
|
2026-04-01 00:07:34 +02:00
|
|
|
for (const mixer of this.animationMixers.values()) {
|
|
|
|
|
mixer.update(dt);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 06:17:09 +02:00
|
|
|
if (this.runtimeScene !== null && this.activeController === this.firstPersonController && this.currentFirstPersonTelemetry !== null) {
|
2026-03-31 06:46:27 +02:00
|
|
|
this.interactionSystem.updatePlayerPosition(this.currentFirstPersonTelemetry.feetPosition, this.runtimeScene, this.createInteractionDispatcher());
|
|
|
|
|
this.camera.getWorldDirection(this.cameraForward);
|
|
|
|
|
this.setInteractionPrompt(
|
|
|
|
|
this.interactionSystem.resolveClickInteractionPrompt(
|
|
|
|
|
this.currentFirstPersonTelemetry.eyePosition,
|
|
|
|
|
{
|
|
|
|
|
x: this.cameraForward.x,
|
|
|
|
|
y: this.cameraForward.y,
|
|
|
|
|
z: this.cameraForward.z
|
|
|
|
|
},
|
|
|
|
|
this.runtimeScene
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
this.setInteractionPrompt(null);
|
2026-03-31 06:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 18:04:29 +02:00
|
|
|
if (this.runtimeWaterContactUniforms.length > 0) {
|
|
|
|
|
this.updateRuntimeWaterContactUniforms();
|
2026-04-07 06:36:13 +02:00
|
|
|
this.updateRuntimeWaterReflections();
|
2026-04-06 18:04:29 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-07 06:15:02 +02:00
|
|
|
this.updateUnderwaterSceneFog();
|
|
|
|
|
|
2026-04-02 20:52:22 +02:00
|
|
|
if (this.advancedRenderingComposer !== null) {
|
|
|
|
|
this.advancedRenderingComposer.render(dt);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 06:25:22 +02:00
|
|
|
this.renderer?.render(this.scene, this.camera);
|
2026-03-31 03:04:15 +02:00
|
|
|
};
|
2026-03-31 06:17:09 +02:00
|
|
|
|
|
|
|
|
private applyTeleportPlayerAction(target: RuntimeTeleportTarget) {
|
|
|
|
|
this.firstPersonController.teleportTo(target.position, target.yawDegrees);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyToggleBrushVisibilityAction(brushId: string, visible: boolean | undefined) {
|
|
|
|
|
const mesh = this.brushMeshes.get(brushId);
|
|
|
|
|
|
|
|
|
|
if (mesh === undefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mesh.visible = visible ?? !mesh.visible;
|
|
|
|
|
}
|
2026-03-31 06:46:27 +02:00
|
|
|
|
2026-04-01 04:05:09 +02:00
|
|
|
private applyPlayAnimationAction(instanceId: string, clipName: string, loop: boolean | undefined) {
|
2026-04-01 00:05:01 +02:00
|
|
|
const mixer = this.animationMixers.get(instanceId);
|
|
|
|
|
const clips = this.instanceAnimationClips.get(instanceId);
|
|
|
|
|
|
|
|
|
|
if (!mixer || !clips) {
|
|
|
|
|
console.warn(`playAnimation: no mixer for instance ${instanceId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 00:05:37 +02:00
|
|
|
const clip = AnimationClip.findByName(clips, clipName);
|
2026-04-01 00:05:01 +02:00
|
|
|
|
|
|
|
|
if (!clip) {
|
|
|
|
|
console.warn(`playAnimation: clip "${clipName}" not found on instance ${instanceId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 04:05:09 +02:00
|
|
|
// LoopRepeat is the three.js default; LoopOnce plays the clip a single time then stops.
|
|
|
|
|
const action = mixer.clipAction(clip);
|
2026-04-01 04:05:25 +02:00
|
|
|
action.loop = loop === false ? LoopOnce : LoopRepeat;
|
2026-04-01 04:05:09 +02:00
|
|
|
action.clampWhenFinished = loop === false;
|
2026-04-01 00:05:01 +02:00
|
|
|
mixer.stopAllAction();
|
2026-04-01 04:05:09 +02:00
|
|
|
action.reset().play();
|
2026-04-01 00:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyStopAnimationAction(instanceId: string) {
|
|
|
|
|
const mixer = this.animationMixers.get(instanceId);
|
|
|
|
|
|
|
|
|
|
if (!mixer) {
|
|
|
|
|
console.warn(`stopAnimation: no mixer for instance ${instanceId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mixer.stopAllAction();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 06:46:27 +02:00
|
|
|
private createInteractionDispatcher(): RuntimeInteractionDispatcher {
|
|
|
|
|
return {
|
|
|
|
|
teleportPlayer: (target) => {
|
|
|
|
|
this.applyTeleportPlayerAction(target);
|
|
|
|
|
},
|
|
|
|
|
toggleBrushVisibility: (brushId, visible) => {
|
|
|
|
|
this.applyToggleBrushVisibilityAction(brushId, visible);
|
2026-04-01 00:04:28 +02:00
|
|
|
},
|
2026-04-01 04:05:13 +02:00
|
|
|
playAnimation: (instanceId, clipName, loop) => {
|
|
|
|
|
this.applyPlayAnimationAction(instanceId, clipName, loop);
|
2026-04-01 00:04:28 +02:00
|
|
|
},
|
|
|
|
|
stopAnimation: (instanceId) => {
|
|
|
|
|
this.applyStopAnimationAction(instanceId);
|
2026-04-02 19:39:55 +02:00
|
|
|
},
|
|
|
|
|
playSound: (soundEmitterId, link) => {
|
|
|
|
|
this.audioSystem.playSound(soundEmitterId, link);
|
|
|
|
|
},
|
|
|
|
|
stopSound: (soundEmitterId) => {
|
|
|
|
|
this.audioSystem.stopSound(soundEmitterId);
|
2026-03-31 06:46:27 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setInteractionPrompt(prompt: RuntimeInteractionPrompt | null) {
|
|
|
|
|
if (
|
|
|
|
|
this.currentInteractionPrompt?.sourceEntityId === prompt?.sourceEntityId &&
|
|
|
|
|
this.currentInteractionPrompt?.prompt === prompt?.prompt &&
|
|
|
|
|
this.currentInteractionPrompt?.distance === prompt?.distance &&
|
|
|
|
|
this.currentInteractionPrompt?.range === prompt?.range
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.currentInteractionPrompt = prompt;
|
|
|
|
|
this.interactionPromptHandler?.(prompt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleRuntimeClick = () => {
|
2026-04-02 19:39:55 +02:00
|
|
|
this.audioSystem.handleUserGesture();
|
|
|
|
|
|
2026-03-31 06:46:27 +02:00
|
|
|
if (this.runtimeScene === null || this.activeController !== this.firstPersonController || this.currentInteractionPrompt === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.interactionSystem.dispatchClickInteraction(this.currentInteractionPrompt.sourceEntityId, this.runtimeScene, this.createInteractionDispatcher());
|
|
|
|
|
};
|
2026-04-02 19:39:55 +02:00
|
|
|
|
|
|
|
|
private handleRuntimePointerDown = () => {
|
|
|
|
|
this.audioSystem.handleUserGesture();
|
|
|
|
|
};
|
2026-03-31 03:04:15 +02:00
|
|
|
}
|