mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-12-01 22:02:45 +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";
|
||||
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>,
|
||||
|
||||
+11
-13
@@ -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 }) => {
|
||||
|
||||
+4
-2
@@ -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}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user