auto-git:

[change] src/rendering/precomputed-shader-sky-environment-cache.ts
 [change] src/rendering/quantized-environment-blend-cache.ts
 [change] src/rendering/world-background-renderer.ts
 [change] src/rendering/world-shader-sky.ts
 [change] src/runtime-three/runtime-host.ts
This commit is contained in:
2026-04-22 17:57:03 +02:00
parent 7c73d9bc86
commit 23d0be0d2c
7 changed files with 69 additions and 63 deletions

View File

@@ -106,14 +106,14 @@ class RendererShaderSkyEnvironmentTextureBuilder {
}
}
export class PrecomputedShaderSkyEnvironmentCache
implements WorldShaderSkyEnvironmentTextureResolver
{
export class PrecomputedShaderSkyEnvironmentCache implements WorldShaderSkyEnvironmentTextureResolver {
private currentKey: string | null = null;
private readonly phaseEntries = createEmptyPhaseEntries();
private disposed = false;
constructor(private readonly options: PrecomputedShaderSkyEnvironmentCacheOptions) {}
constructor(
private readonly options: PrecomputedShaderSkyEnvironmentCacheOptions
) {}
syncPhaseTextures(states: WorldShaderSkyEnvironmentPhaseStates) {
if (this.disposed) {
@@ -132,9 +132,7 @@ export class PrecomputedShaderSkyEnvironmentCache
for (const phase of SHADER_SKY_PHASES) {
const state = states[phase];
nextEntries[phase] =
state === null
? null
: this.options.buildEnvironmentTexture(state);
state === null ? null : this.options.buildEnvironmentTexture(state);
}
} catch (error) {
disposePhaseEntries(nextEntries);
@@ -166,7 +164,8 @@ export class PrecomputedShaderSkyEnvironmentCache
return null;
}
const baseTexture = this.phaseEntries[phaseBlend.basePhase]?.texture ?? null;
const baseTexture =
this.phaseEntries[phaseBlend.basePhase]?.texture ?? null;
if (baseTexture === null) {
return null;
@@ -188,8 +187,7 @@ export class PrecomputedShaderSkyEnvironmentCache
baseTexture,
overlayTexture,
phaseBlend.blendAmount
) ??
(phaseBlend.blendAmount >= 0.5 ? overlayTexture : baseTexture)
) ?? (phaseBlend.blendAmount >= 0.5 ? overlayTexture : baseTexture)
);
}

View File

@@ -109,20 +109,16 @@ function resolveTextureDimensions(texture: Texture): {
height?: number;
}
| undefined;
const sourceData = texture.source.data as
| {
width?: number;
height?: number;
}
| null;
const sourceData = texture.source.data as {
width?: number;
height?: number;
} | null;
return {
width: Math.max(
1,
Math.floor(
image?.width ??
sourceData?.width ??
DEFAULT_BLEND_RENDER_TARGET_WIDTH
image?.width ?? sourceData?.width ?? DEFAULT_BLEND_RENDER_TARGET_WIDTH
)
),
height: Math.max(
@@ -186,9 +182,7 @@ export function createQuantizedEnvironmentBlendCacheKey(
return `${getOrderedPairCacheKey(baseTexture, overlayTexture)}@${bucketIndex}`;
}
export class QuantizedEnvironmentBlendCache
implements WorldEnvironmentBlendTextureResolver
{
export class QuantizedEnvironmentBlendCache implements WorldEnvironmentBlendTextureResolver {
private readonly bucketCount: number;
private readonly buildBlendTexture: QuantizedEnvironmentBlendCacheOptions["buildBlendTexture"];
private readonly disposeBuildResources: (() => void) | undefined;
@@ -208,9 +202,7 @@ export class QuantizedEnvironmentBlendCache
constructor(options: QuantizedEnvironmentBlendCacheOptions) {
this.bucketCount = Math.max(
1,
Math.floor(
options.bucketCount ?? DEFAULT_QUANTIZED_BLEND_BUCKET_COUNT
)
Math.floor(options.bucketCount ?? DEFAULT_QUANTIZED_BLEND_BUCKET_COUNT)
);
this.buildBlendTexture = options.buildBlendTexture;
this.disposeBuildResources = options.disposeBuildResources;
@@ -359,7 +351,11 @@ export class QuantizedEnvironmentBlendCache
}
private schedulePendingBuilds() {
if (this.disposed || this.buildScheduled || this.pendingBuilds.length === 0) {
if (
this.disposed ||
this.buildScheduled ||
this.pendingBuilds.length === 0
) {
return;
}

View File

@@ -756,8 +756,7 @@ function applyShaderSkyStateToMaterial(
material.uniforms.uCloudSoftness.value = state.clouds.softness;
material.uniforms.uCloudScale.value = state.clouds.scale;
material.uniforms.uCloudHeight.value = state.clouds.height;
material.uniforms.uCloudHeightVariation.value =
state.clouds.heightVariation;
material.uniforms.uCloudHeightVariation.value = state.clouds.heightVariation;
material.uniforms.uCloudTint.value.set(state.clouds.tintHex);
material.uniforms.uCloudOpacity.value = state.clouds.opacity;
material.uniforms.uCloudOpacityRandomness.value =
@@ -904,7 +903,9 @@ export class WorldBackgroundRenderer {
}
applyShaderSkyStateToMaterial(this.shaderSkyMaterial, shaderSkyState);
this.syncEnvironmentCaptureState(showShaderBackground ? shaderSkyState : null);
this.syncEnvironmentCaptureState(
showShaderBackground ? shaderSkyState : null
);
this.shaderMesh.visible = showShaderBackground;
this.gradientMesh.visible = !showShaderBackground && !showImageBackground;
this.imageMesh.visible = showImageBackground;

View File

@@ -98,7 +98,10 @@ function resolveTimeWindowMidpoint(
startTimeOfDayHours: number,
endTimeOfDayHours: number
): number {
const wrappedEndTime = wrapTimeForward(endTimeOfDayHours, startTimeOfDayHours);
const wrappedEndTime = wrapTimeForward(
endTimeOfDayHours,
startTimeOfDayHours
);
return normalizeTimeOfDayHours(
startTimeOfDayHours + (wrappedEndTime - startTimeOfDayHours) / 2
@@ -423,10 +426,8 @@ export function resolveWorldShaderSkyEnvironmentPhaseStates(
};
}
const dawnHalfDuration =
Math.max(timeSettings.dawnDurationHours, 0.001) / 2;
const duskHalfDuration =
Math.max(timeSettings.duskDurationHours, 0.001) / 2;
const dawnHalfDuration = Math.max(timeSettings.dawnDurationHours, 0.001) / 2;
const duskHalfDuration = Math.max(timeSettings.duskDurationHours, 0.001) / 2;
const dawnStart = timeSettings.sunriseTimeOfDayHours - dawnHalfDuration;
const dawnEnd = timeSettings.sunriseTimeOfDayHours + dawnHalfDuration;
const duskStart = timeSettings.sunsetTimeOfDayHours - duskHalfDuration;
@@ -549,9 +550,9 @@ export function createWorldShaderSkyEnvironmentPhaseCacheKey(
export function resolveWorldShaderSkyEnvironmentPhaseBlend(
state: Pick<WorldShaderSkyRenderState, "time">
): WorldShaderSkyEnvironmentPhaseBlend | null {
const activePhases = (
["day", "dawn", "dusk", "night"] as const
).filter((phase) => state.time.phaseWeights[phase] > 1e-4);
const activePhases = (["day", "dawn", "dusk", "night"] as const).filter(
(phase) => state.time.phaseWeights[phase] > 1e-4
);
if (activePhases.length === 0) {
return null;

View File

@@ -180,7 +180,10 @@ import {
buildRuntimeNpcCollider,
createRuntimeNpcFromDefinition
} from "./runtime-scene-build";
import { resolvePlayerStartLookInput, resolvePlayerStartPauseInput } from "./player-input-bindings";
import {
resolvePlayerStartLookInput,
resolvePlayerStartPauseInput
} from "./player-input-bindings";
interface CachedMaterialTexture {
signature: string;
@@ -584,7 +587,8 @@ export class RuntimeHost {
desiredCameraPosition,
radius
) ?? { ...desiredCameraPosition },
isCameraDrivenExternally: () => this.resolveActiveRuntimeCameraRig() !== null,
isCameraDrivenExternally: () =>
this.resolveActiveRuntimeCameraRig() !== null,
getCameraYawRadians: () => {
this.camera.getWorldDirection(this.cameraForward);
return Math.atan2(this.cameraForward.x, this.cameraForward.z);
@@ -1215,7 +1219,8 @@ export class RuntimeHost {
const target = rig.target;
const activeNpc =
this.runtimeScene.npcDefinitions.find(
(candidate) => candidate.actorId === target.actorId && candidate.active
(candidate) =>
candidate.actorId === target.actorId && candidate.active
) ??
this.runtimeScene.npcDefinitions.find(
(candidate) => candidate.actorId === target.actorId
@@ -1259,12 +1264,15 @@ export class RuntimeHost {
if (this.activeCameraRigOverrideEntityId !== null) {
return (
cameraRigs.find(
(candidate) => candidate.entityId === this.activeCameraRigOverrideEntityId
(candidate) =>
candidate.entityId === this.activeCameraRigOverrideEntityId
) ?? null
);
}
const eligibleCameraRigs = cameraRigs.filter((candidate) => candidate.defaultActive);
const eligibleCameraRigs = cameraRigs.filter(
(candidate) => candidate.defaultActive
);
if (eligibleCameraRigs.length === 0) {
return null;
@@ -1277,10 +1285,7 @@ export class RuntimeHost {
)[0]!;
}
private updateRuntimeCameraRigLookState(
rig: RuntimeCameraRig,
dt: number
) {
private updateRuntimeCameraRigLookState(rig: RuntimeCameraRig, dt: number) {
if (this.runtimeScene === null) {
return;
}
@@ -4514,8 +4519,7 @@ export class RuntimeHost {
deltaY * CAMERA_RIG_POINTER_LOOK_SENSITIVITY,
(-this.activeRuntimeCameraRig.lookAround.pitchLimitDegrees * Math.PI) /
180,
(this.activeRuntimeCameraRig.lookAround.pitchLimitDegrees * Math.PI) /
180
(this.activeRuntimeCameraRig.lookAround.pitchLimitDegrees * Math.PI) / 180
);
event.preventDefault();
event.stopImmediatePropagation();

View File

@@ -735,14 +735,12 @@ export class ViewportHost {
}
}
);
this.shaderSkyEnvironmentBlendCache = createRendererQuantizedPmremBlendCache(
this.renderer,
{
this.shaderSkyEnvironmentBlendCache =
createRendererQuantizedPmremBlendCache(this.renderer, {
onTextureReady: () => {
this.applyWorld();
}
}
);
});
this.shaderSkyEnvironmentCache =
createRendererPrecomputedShaderSkyEnvironmentCache(
this.renderer,

View File

@@ -36,16 +36,24 @@ describe("resolveWorldEnvironmentState", () => {
environmentIntensity: 0.4
};
const earlyTwilight = resolveWorldEnvironmentState(world.background, dayTexture, {
texture: nightTexture,
opacity: 0.25,
environmentIntensity: 0.8
});
const lateTwilight = resolveWorldEnvironmentState(world.background, dayTexture, {
texture: nightTexture,
opacity: 0.75,
environmentIntensity: 0.8
});
const earlyTwilight = resolveWorldEnvironmentState(
world.background,
dayTexture,
{
texture: nightTexture,
opacity: 0.25,
environmentIntensity: 0.8
}
);
const lateTwilight = resolveWorldEnvironmentState(
world.background,
dayTexture,
{
texture: nightTexture,
opacity: 0.75,
environmentIntensity: 0.8
}
);
expect(earlyTwilight.texture).toBe(dayTexture);
expect(earlyTwilight.intensity).toBeCloseTo(0.5);