mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-12-01 05:43:22 +01:00
smart shadow updates
This commit is contained in:
11 files changed
+110
-15
No files matched your search
@@ -24,6 +24,7 @@ import {
|
|||||||
} from "../../../core/blitzkit/modelTransform";
|
} from "../../../core/blitzkit/modelTransform";
|
||||||
import { defaultEqualizer } from "../../../core/blitzkit/tankToDuelMember";
|
import { defaultEqualizer } from "../../../core/blitzkit/tankToDuelMember";
|
||||||
import { discardClippingPlane } from "../../../core/three/discardClippingPlane";
|
import { discardClippingPlane } from "../../../core/three/discardClippingPlane";
|
||||||
|
import { sceneChangeEvent } from "../../../core/three/sceneChange";
|
||||||
import { useEquipment } from "../../../hooks/useEquipment";
|
import { useEquipment } from "../../../hooks/useEquipment";
|
||||||
import { Duel } from "../../../stores/duel";
|
import { Duel } from "../../../stores/duel";
|
||||||
import { Tankopedia } from "../../../stores/tankopedia";
|
import { Tankopedia } from "../../../stores/tankopedia";
|
||||||
@@ -154,6 +155,13 @@ export function StaticArmorSceneComponent({
|
|||||||
[hasEnhancedArmor, equalize],
|
[hasEnhancedArmor, equalize],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// these cast shadows
|
||||||
|
useEffect(() => {
|
||||||
|
sceneChangeEvent.dispatch();
|
||||||
|
|
||||||
|
return () => sceneChangeEvent.dispatch();
|
||||||
|
}, [node]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function handleHighlightArmor(selectedName?: string) {
|
function handleHighlightArmor(selectedName?: string) {
|
||||||
if (selectedName === undefined) {
|
if (selectedName === undefined) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { invalidate } from "@react-three/fiber";
|
import { invalidate } from "@react-three/fiber";
|
||||||
import { useEffect, useMemo, type ComponentProps } from "react";
|
import { useEffect, useMemo, type ComponentProps } from "react";
|
||||||
import { BatchedMesh, Color, type Matrix4, type Mesh } from "three";
|
import { BatchedMesh, Color, type Matrix4, type Mesh } from "three";
|
||||||
|
import { sceneChangeEvent } from "../../core/three/sceneChange";
|
||||||
|
|
||||||
export interface BatchItem {
|
export interface BatchItem {
|
||||||
mesh: Mesh;
|
mesh: Mesh;
|
||||||
@@ -32,6 +33,10 @@ export function Batch({ items, ...props }: BatchProps) {
|
|||||||
const batch = new BatchedMesh(items.length, vertices, indices);
|
const batch = new BatchedMesh(items.length, vertices, indices);
|
||||||
const color = new Color();
|
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) {
|
for (const { mesh, matrix, thickness } of items) {
|
||||||
const instanceId = batch.addInstance(batch.addGeometry(mesh.geometry));
|
const instanceId = batch.addInstance(batch.addGeometry(mesh.geometry));
|
||||||
|
|
||||||
@@ -49,9 +54,11 @@ export function Batch({ items, ...props }: BatchProps) {
|
|||||||
if (!batch) return;
|
if (!batch) return;
|
||||||
|
|
||||||
invalidate();
|
invalidate();
|
||||||
|
sceneChangeEvent.dispatch();
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
batch.dispose();
|
batch.dispose();
|
||||||
|
sceneChangeEvent.dispatch();
|
||||||
};
|
};
|
||||||
}, [batch]);
|
}, [batch]);
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
type ModelTransformEventData,
|
type ModelTransformEventData,
|
||||||
} from "../../../core/blitzkit/modelTransform";
|
} from "../../../core/blitzkit/modelTransform";
|
||||||
import { controlsEnabledEvent } from "../../../core/controlsEnabled";
|
import { controlsEnabledEvent } from "../../../core/controlsEnabled";
|
||||||
|
import { sceneChangeEvent } from "../../../core/three/sceneChange";
|
||||||
import { useModel } from "../../../hooks/useModel";
|
import { useModel } from "../../../hooks/useModel";
|
||||||
import { Mixer } from "../../../stores/mixer";
|
import { Mixer } from "../../../stores/mixer";
|
||||||
|
|
||||||
@@ -82,6 +83,8 @@ export function Model() {
|
|||||||
window.removeEventListener("pointerup", handlePointerUp);
|
window.removeEventListener("pointerup", handlePointerUp);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => sceneChangeEvent.dispatch(), [hull, turret, gun]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function handleModelTransform(
|
function handleModelTransform(
|
||||||
event: QuicklimeEvent<ModelTransformEventData>,
|
event: QuicklimeEvent<ModelTransformEventData>,
|
||||||
|
|||||||
+11
-13
@@ -1,5 +1,4 @@
|
|||||||
import { blueDark, orangeDark } from "@radix-ui/colors";
|
import { blueDark, orangeDark } from "@radix-ui/colors";
|
||||||
import { ContactShadows } from "@react-three/drei";
|
|
||||||
import { invalidate, useFrame } from "@react-three/fiber";
|
import { invalidate, useFrame } from "@react-three/fiber";
|
||||||
import { clamp, times } from "lodash-es";
|
import { clamp, times } from "lodash-es";
|
||||||
import { Quicklime } from "quicklime";
|
import { Quicklime } from "quicklime";
|
||||||
@@ -12,9 +11,12 @@ import {
|
|||||||
} from "react";
|
} from "react";
|
||||||
import { HemisphereLight, SpotLight, type Group } from "three";
|
import { HemisphereLight, SpotLight, type Group } from "three";
|
||||||
import { degToRad, lerp } from "three/src/math/MathUtils.js";
|
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 { Tankopedia } from "../../../../../../stores/tankopedia";
|
||||||
import { TankopediaPersistent } from "../../../../../../stores/tankopediaPersistent";
|
import { TankopediaPersistent } from "../../../../../../stores/tankopediaPersistent";
|
||||||
import { TankopediaDisplay } from "../../../../../../stores/tankopediaPersistent/constants";
|
import { TankopediaDisplay } from "../../../../../../stores/tankopediaPersistent/constants";
|
||||||
|
import { TankopediaContactShadows } from "../../../../../TankopediaContactShadows";
|
||||||
|
|
||||||
const ANGLE = degToRad(15);
|
const ANGLE = degToRad(15);
|
||||||
const REVEAL_ANIMATION_TIME = 3;
|
const REVEAL_ANIMATION_TIME = 3;
|
||||||
@@ -62,6 +64,10 @@ export function Lighting({ hasPbr }: Props) {
|
|||||||
|
|
||||||
const [animate, setAnimate] = useState(true);
|
const [animate, setAnimate] = useState(true);
|
||||||
|
|
||||||
|
useShadowsOnDemand();
|
||||||
|
|
||||||
|
useEffect(() => sceneChangeEvent.dispatch(), [highGraphics]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
Tankopedia.mutate((draft) => {
|
Tankopedia.mutate((draft) => {
|
||||||
draft.revealed = true;
|
draft.revealed = true;
|
||||||
@@ -113,17 +119,7 @@ export function Lighting({ hasPbr }: Props) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ContactShadows
|
<TankopediaContactShadows />
|
||||||
width={1.5}
|
|
||||||
height={1.5}
|
|
||||||
near={0}
|
|
||||||
far={1}
|
|
||||||
opacity={2 ** 1}
|
|
||||||
blur={2 ** 1}
|
|
||||||
resolution={2 ** 9}
|
|
||||||
depthWrite={false}
|
|
||||||
renderOrder={1}
|
|
||||||
/>
|
|
||||||
|
|
||||||
{animate && (
|
{animate && (
|
||||||
<Animator
|
<Animator
|
||||||
@@ -167,7 +163,7 @@ export function Lighting({ hasPbr }: Props) {
|
|||||||
intensity={intensity}
|
intensity={intensity}
|
||||||
userData={{ intensity }}
|
userData={{ intensity }}
|
||||||
penumbra={1}
|
penumbra={1}
|
||||||
castShadow={highGraphics && index === 0}
|
castShadow={highGraphics}
|
||||||
decay={1}
|
decay={1}
|
||||||
color="#ffffff"
|
color="#ffffff"
|
||||||
angle={0}
|
angle={0}
|
||||||
@@ -227,6 +223,8 @@ function Animator({
|
|||||||
}
|
}
|
||||||
|
|
||||||
transitionEvent.dispatch(t);
|
transitionEvent.dispatch(t);
|
||||||
|
// spotlight angles shape their shadows
|
||||||
|
sceneChangeEvent.dispatch();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useFrame(({ invalidate }) => {
|
useFrame(({ invalidate }) => {
|
||||||
|
|||||||
+4
-2
@@ -50,8 +50,6 @@ export function SceneProps() {
|
|||||||
"#include <dithering_fragment>",
|
"#include <dithering_fragment>",
|
||||||
|
|
||||||
`
|
`
|
||||||
#include <dithering_fragment>
|
|
||||||
|
|
||||||
vec3 scaled = vWorldPosition / size;
|
vec3 scaled = vWorldPosition / size;
|
||||||
float radius = length(scaled.xz);
|
float radius = length(scaled.xz);
|
||||||
|
|
||||||
@@ -63,6 +61,9 @@ export function SceneProps() {
|
|||||||
float y = x * x;
|
float y = x * x;
|
||||||
|
|
||||||
gl_FragColor.rgb *= y;
|
gl_FragColor.rgb *= y;
|
||||||
|
|
||||||
|
// after the falloff, so its gradient doesn't band
|
||||||
|
#include <dithering_fragment>
|
||||||
`,
|
`,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -81,6 +82,7 @@ export function SceneProps() {
|
|||||||
<meshStandardMaterial
|
<meshStandardMaterial
|
||||||
color={mauveDark.mauve6}
|
color={mauveDark.mauve6}
|
||||||
roughness={1}
|
roughness={1}
|
||||||
|
dithering
|
||||||
onBeforeCompile={handleBeforeCompile}
|
onBeforeCompile={handleBeforeCompile}
|
||||||
/>
|
/>
|
||||||
</mesh>
|
</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 { invalidate, useFrame } from "@react-three/fiber";
|
||||||
|
import { sceneChangeEvent } from "../core/three/sceneChange";
|
||||||
import type { Skin } from "./useSkin";
|
import type { Skin } from "./useSkin";
|
||||||
|
|
||||||
export function useSkinAnimation(skins: Skin[]) {
|
export function useSkinAnimation(skins: Skin[]) {
|
||||||
@@ -8,6 +9,7 @@ export function useSkinAnimation(skins: Skin[]) {
|
|||||||
|
|
||||||
mixer.update(delta);
|
mixer.update(delta);
|
||||||
invalidate();
|
invalidate();
|
||||||
|
sceneChangeEvent.dispatch();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ import { type RefObject, useEffect } from "react";
|
|||||||
import { Euler, Group, Vector3 } from "three";
|
import { Euler, Group, Vector3 } from "three";
|
||||||
import { degToRad } from "three/src/math/MathUtils.js";
|
import { degToRad } from "three/src/math/MathUtils.js";
|
||||||
import { correctZYTuple } from "../core/blitz/correctZYTuple";
|
import { correctZYTuple } from "../core/blitz/correctZYTuple";
|
||||||
|
import { sceneChangeEvent } from "../core/three/sceneChange";
|
||||||
import {
|
import {
|
||||||
modelTransformEvent,
|
modelTransformEvent,
|
||||||
type ModelTransformEventData,
|
type ModelTransformEventData,
|
||||||
@@ -96,6 +97,7 @@ export function useTankTransform(
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleModelTransformInner();
|
handleModelTransformInner();
|
||||||
|
sceneChangeEvent.dispatch();
|
||||||
|
|
||||||
modelTransformEvent.on(handleModelTransform);
|
modelTransformEvent.on(handleModelTransform);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user