smart shadow updates

This commit is contained in:
tresabhi committed 2026-11-30 22:42:12 -06:00
1 parent 3078dfeb10
commit a3e7908cd2
11 files changed
+110 -15

No files matched your search

@@ -24,6 +24,7 @@ import {
} from "../../../core/blitzkit/modelTransform";
import { defaultEqualizer } from "../../../core/blitzkit/tankToDuelMember";
import { discardClippingPlane } from "../../../core/three/discardClippingPlane";
import { sceneChangeEvent } from "../../../core/three/sceneChange";
import { useEquipment } from "../../../hooks/useEquipment";
import { Duel } from "../../../stores/duel";
import { Tankopedia } from "../../../stores/tankopedia";
@@ -154,6 +155,13 @@ export function StaticArmorSceneComponent({
[hasEnhancedArmor, equalize],
);
// these cast shadows
useEffect(() => {
sceneChangeEvent.dispatch();
return () => sceneChangeEvent.dispatch();
}, [node]);
useEffect(() => {
function handleHighlightArmor(selectedName?: string) {
if (selectedName === undefined) {
@@ -1,6 +1,7 @@
import { invalidate } from "@react-three/fiber";
import { useEffect, useMemo, type ComponentProps } from "react";
import { BatchedMesh, Color, type Matrix4, type Mesh } from "three";
import { sceneChangeEvent } from "../../core/three/sceneChange";
export interface BatchItem {
mesh: Mesh;
@@ -32,6 +33,10 @@ export function Batch({ items, ...props }: BatchProps) {
const batch = new BatchedMesh(items.length, vertices, indices);
const color = new Color();
// otherwise the draw list is re-culled, re-sorted and re-uploaded every draw
batch.perObjectFrustumCulled = false;
batch.sortObjects = false;
for (const { mesh, matrix, thickness } of items) {
const instanceId = batch.addInstance(batch.addGeometry(mesh.geometry));
@@ -49,9 +54,11 @@ export function Batch({ items, ...props }: BatchProps) {
if (!batch) return;
invalidate();
sceneChangeEvent.dispatch();
return () => {
batch.dispose();
sceneChangeEvent.dispatch();
};
}, [batch]);
@@ -9,6 +9,7 @@ import {
type ModelTransformEventData,
} from "../../../core/blitzkit/modelTransform";
import { controlsEnabledEvent } from "../../../core/controlsEnabled";
import { sceneChangeEvent } from "../../../core/three/sceneChange";
import { useModel } from "../../../hooks/useModel";
import { Mixer } from "../../../stores/mixer";
@@ -82,6 +83,8 @@ export function Model() {
window.removeEventListener("pointerup", handlePointerUp);
}, []);
useEffect(() => sceneChangeEvent.dispatch(), [hull, turret, gun]);
useEffect(() => {
function handleModelTransform(
event: QuicklimeEvent<ModelTransformEventData>,
@@ -1,5 +1,4 @@
import { blueDark, orangeDark } from "@radix-ui/colors";
import { ContactShadows } from "@react-three/drei";
import { invalidate, useFrame } from "@react-three/fiber";
import { clamp, times } from "lodash-es";
import { Quicklime } from "quicklime";
@@ -12,9 +11,12 @@ import {
} from "react";
import { HemisphereLight, SpotLight, type Group } from "three";
import { degToRad, lerp } from "three/src/math/MathUtils.js";
import { sceneChangeEvent } from "../../../../../../core/three/sceneChange";
import { useShadowsOnDemand } from "../../../../../../hooks/useShadowsOnDemand";
import { Tankopedia } from "../../../../../../stores/tankopedia";
import { TankopediaPersistent } from "../../../../../../stores/tankopediaPersistent";
import { TankopediaDisplay } from "../../../../../../stores/tankopediaPersistent/constants";
import { TankopediaContactShadows } from "../../../../../TankopediaContactShadows";
const ANGLE = degToRad(15);
const REVEAL_ANIMATION_TIME = 3;
@@ -62,6 +64,10 @@ export function Lighting({ hasPbr }: Props) {
const [animate, setAnimate] = useState(true);
useShadowsOnDemand();
useEffect(() => sceneChangeEvent.dispatch(), [highGraphics]);
useEffect(() => {
Tankopedia.mutate((draft) => {
draft.revealed = true;
@@ -113,17 +119,7 @@ export function Lighting({ hasPbr }: Props) {
return (
<>
<ContactShadows
width={1.5}
height={1.5}
near={0}
far={1}
opacity={2 ** 1}
blur={2 ** 1}
resolution={2 ** 9}
depthWrite={false}
renderOrder={1}
/>
<TankopediaContactShadows />
{animate && (
<Animator
@@ -167,7 +163,7 @@ export function Lighting({ hasPbr }: Props) {
intensity={intensity}
userData={{ intensity }}
penumbra={1}
castShadow={highGraphics && index === 0}
castShadow={highGraphics}
decay={1}
color="#ffffff"
angle={0}
@@ -227,6 +223,8 @@ function Animator({
}
transitionEvent.dispatch(t);
// spotlight angles shape their shadows
sceneChangeEvent.dispatch();
}, []);
useFrame(({ invalidate }) => {
@@ -50,8 +50,6 @@ export function SceneProps() {
"#include <dithering_fragment>",
`
#include <dithering_fragment>
vec3 scaled = vWorldPosition / size;
float radius = length(scaled.xz);
@@ -63,6 +61,9 @@ export function SceneProps() {
float y = x * x;
gl_FragColor.rgb *= y;
// after the falloff, so its gradient doesn't band
#include <dithering_fragment>
`,
);
},
@@ -81,6 +82,7 @@ export function SceneProps() {
<meshStandardMaterial
color={mauveDark.mauve6}
roughness={1}
dithering
onBeforeCompile={handleBeforeCompile}
/>
</mesh>
@@ -0,0 +1,27 @@
import { ContactShadows } from "@react-three/drei";
import { Tankopedia } from "../stores/tankopedia";
import { TankopediaDisplay } from "../stores/tankopediaPersistent/constants";
/**
* drei's ContactShadows, but only redrawn when the scene changes instead of
* every frame. drei resets its frame count on every render, so re-rendering
* with frames={1} draws it once more.
*/
export function TankopediaContactShadows() {
const display = Tankopedia.use((state) => state.display);
if (display === TankopediaDisplay.StaticArmor) return null;
return (
<ContactShadows
frames={1}
width={1.5}
height={1.5}
near={0}
far={1}
opacity={2 ** 1}
blur={2 ** 1}
resolution={2 ** 6}
/>
);
}
@@ -0,0 +1,4 @@
import { Quicklime } from "quicklime";
// something that casts or receives shadows was added, removed or changed
export const sceneChangeEvent = new Quicklime();
@@ -0,0 +1,21 @@
import { useEffect } from "react";
import { modelTransformEvent } from "../core/blitzkit/modelTransform";
import { sceneChangeEvent } from "../core/three/sceneChange";
/**
* Calls back on mount and whenever shadows may have changed: the tank moved or
* something dispatched sceneChangeEvent.
*/
export function useOnSceneChange(callback: () => void) {
useEffect(() => {
callback();
modelTransformEvent.on(callback);
sceneChangeEvent.on(callback);
return () => {
modelTransformEvent.off(callback);
sceneChangeEvent.off(callback);
};
}, [callback]);
}
@@ -0,0 +1,21 @@
import { invalidate, useThree } from "@react-three/fiber";
import { useEffect } from "react";
import { useOnSceneChange } from "./useOnSceneChange";
// shadow maps only re-render when the scene changes, not every frame
export function useShadowsOnDemand() {
const gl = useThree((state) => state.gl);
useEffect(() => {
gl.shadowMap.autoUpdate = false;
return () => {
gl.shadowMap.autoUpdate = true;
};
}, [gl]);
useOnSceneChange(() => {
gl.shadowMap.needsUpdate = true;
invalidate();
});
}
@@ -1,4 +1,5 @@
import { invalidate, useFrame } from "@react-three/fiber";
import { sceneChangeEvent } from "../core/three/sceneChange";
import type { Skin } from "./useSkin";
export function useSkinAnimation(skins: Skin[]) {
@@ -8,6 +9,7 @@ export function useSkinAnimation(skins: Skin[]) {
mixer.update(delta);
invalidate();
sceneChangeEvent.dispatch();
}
});
}
@@ -11,6 +11,7 @@ import { type RefObject, useEffect } from "react";
import { Euler, Group, Vector3 } from "three";
import { degToRad } from "three/src/math/MathUtils.js";
import { correctZYTuple } from "../core/blitz/correctZYTuple";
import { sceneChangeEvent } from "../core/three/sceneChange";
import {
modelTransformEvent,
type ModelTransformEventData,
@@ -96,6 +97,7 @@ export function useTankTransform(
}
handleModelTransformInner();
sceneChangeEvent.dispatch();
modelTransformEvent.on(handleModelTransform);