mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 10:47:04 +02:00
feat: reduce rerenders
This commit is contained in:
1 parent
2eadad8612
commit
cd53136dce
2 files changed
+35
-14
No files matched your search
+31
-5
@@ -1,6 +1,6 @@
|
|||||||
import { normalizeAngleRad } from '@blitzkit/core';
|
import { normalizeAngleRad } from '@blitzkit/core';
|
||||||
import { invalidate, useThree, type ThreeEvent } from '@react-three/fiber';
|
import { invalidate, useThree, type ThreeEvent } from '@react-three/fiber';
|
||||||
import { memo, useRef } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
import { Group, Mesh, MeshStandardMaterial, Vector2 } from 'three';
|
import { Group, Mesh, MeshStandardMaterial, Vector2 } from 'three';
|
||||||
import { applyPitchYawLimits } from '../../../../../../core/blitz/applyPitchYawLimits';
|
import { applyPitchYawLimits } from '../../../../../../core/blitz/applyPitchYawLimits';
|
||||||
import { hasEquipment } from '../../../../../../core/blitzkit/hasEquipment';
|
import { hasEquipment } from '../../../../../../core/blitzkit/hasEquipment';
|
||||||
@@ -10,11 +10,14 @@ import { useModel } from '../../../../../../hooks/useModel';
|
|||||||
import { useTankModelDefinition } from '../../../../../../hooks/useTankModelDefinition';
|
import { useTankModelDefinition } from '../../../../../../hooks/useTankModelDefinition';
|
||||||
import { useTankTransform } from '../../../../../../hooks/useTankTransform';
|
import { useTankTransform } from '../../../../../../hooks/useTankTransform';
|
||||||
import { Duel } from '../../../../../../stores/duel';
|
import { Duel } from '../../../../../../stores/duel';
|
||||||
import { TankopediaEphemeral } from '../../../../../../stores/tankopediaEphemeral';
|
import {
|
||||||
|
ShootingRangeZoom,
|
||||||
|
TankopediaEphemeral,
|
||||||
|
} from '../../../../../../stores/tankopediaEphemeral';
|
||||||
import { TankopediaDisplay } from '../../../../../../stores/tankopediaPersistent/constants';
|
import { TankopediaDisplay } from '../../../../../../stores/tankopediaPersistent/constants';
|
||||||
import { ModelTankWrapper } from '../../../../../Armor/components/ModelTankWrapper';
|
import { ModelTankWrapper } from '../../../../../Armor/components/ModelTankWrapper';
|
||||||
|
|
||||||
export const TankModel = memo(() => {
|
export function TankModel() {
|
||||||
const mutateDuel = Duel.useMutation();
|
const mutateDuel = Duel.useMutation();
|
||||||
const duelStore = Duel.useStore();
|
const duelStore = Duel.useStore();
|
||||||
const protagonist = Duel.use((draft) => draft.protagonist);
|
const protagonist = Duel.use((draft) => draft.protagonist);
|
||||||
@@ -34,6 +37,18 @@ export const TankModel = memo(() => {
|
|||||||
const { gltf } = useModel(protagonist.tank.id);
|
const { gltf } = useModel(protagonist.tank.id);
|
||||||
const nodes = Object.values(gltf.nodes);
|
const nodes = Object.values(gltf.nodes);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const unsubscribeShootingRangeZoom = tankopediaEphemeralStore.subscribe(
|
||||||
|
(state) => state.shootingRangeZoom,
|
||||||
|
(zoom) => {
|
||||||
|
if (!hullContainer.current) return;
|
||||||
|
hullContainer.current.visible = zoom === ShootingRangeZoom.Arcade;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return unsubscribeShootingRangeZoom;
|
||||||
|
}, []);
|
||||||
|
|
||||||
useTankTransform(track, turret, turretContainer, gunContainer);
|
useTankTransform(track, turret, turretContainer, gunContainer);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -142,7 +157,12 @@ export const TankModel = memo(() => {
|
|||||||
function onPointerDown(event: ThreeEvent<PointerEvent>) {
|
function onPointerDown(event: ThreeEvent<PointerEvent>) {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
|
|
||||||
if (!isTurret) return;
|
if (
|
||||||
|
!isTurret ||
|
||||||
|
tankopediaEphemeralStore.getState().display ===
|
||||||
|
TankopediaDisplay.ShootingRange
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
|
||||||
position.set(event.clientX, event.clientY);
|
position.set(event.clientX, event.clientY);
|
||||||
yaw = protagonist.yaw;
|
yaw = protagonist.yaw;
|
||||||
@@ -239,6 +259,12 @@ export const TankModel = memo(() => {
|
|||||||
function onPointerDown(event: ThreeEvent<PointerEvent>) {
|
function onPointerDown(event: ThreeEvent<PointerEvent>) {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
|
|
||||||
|
if (
|
||||||
|
tankopediaEphemeralStore.getState().display ===
|
||||||
|
TankopediaDisplay.ShootingRange
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
|
||||||
mutateTankopediaEphemeral((draft) => {
|
mutateTankopediaEphemeral((draft) => {
|
||||||
draft.controlsEnabled = false;
|
draft.controlsEnabled = false;
|
||||||
});
|
});
|
||||||
@@ -315,4 +341,4 @@ export const TankModel = memo(() => {
|
|||||||
</group>
|
</group>
|
||||||
</ModelTankWrapper>
|
</ModelTankWrapper>
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
+4
-9
@@ -13,10 +13,7 @@ import { useEquipment } from '../../../../../hooks/useEquipment';
|
|||||||
import { useOnScreen } from '../../../../../hooks/useOnScreen';
|
import { useOnScreen } from '../../../../../hooks/useOnScreen';
|
||||||
import { useTankModelDefinition } from '../../../../../hooks/useTankModelDefinition';
|
import { useTankModelDefinition } from '../../../../../hooks/useTankModelDefinition';
|
||||||
import { Duel } from '../../../../../stores/duel';
|
import { Duel } from '../../../../../stores/duel';
|
||||||
import {
|
import { TankopediaEphemeral } from '../../../../../stores/tankopediaEphemeral';
|
||||||
ShootingRangeZoom,
|
|
||||||
TankopediaEphemeral,
|
|
||||||
} from '../../../../../stores/tankopediaEphemeral';
|
|
||||||
import { TankopediaDisplay } from '../../../../../stores/tankopediaPersistent/constants';
|
import { TankopediaDisplay } from '../../../../../stores/tankopediaPersistent/constants';
|
||||||
import { Armor } from '../../../../Armor';
|
import { Armor } from '../../../../Armor';
|
||||||
import { ArmorPlateDisplay } from '../../../../Armor/components/ArmorPlateDisplay';
|
import { ArmorPlateDisplay } from '../../../../Armor/components/ArmorPlateDisplay';
|
||||||
@@ -51,9 +48,6 @@ export const TankSandbox = forwardRef<HTMLCanvasElement, TankSandboxProps>(
|
|||||||
turretModelDefinition.guns[protagonistGun.gun_type!.value.base.id];
|
turretModelDefinition.guns[protagonistGun.gun_type!.value.base.id];
|
||||||
const display = TankopediaEphemeral.use((state) => state.display);
|
const display = TankopediaEphemeral.use((state) => state.display);
|
||||||
const onScreen = useOnScreen(canvas);
|
const onScreen = useOnScreen(canvas);
|
||||||
const shootingRangeZoom = TankopediaEphemeral.use(
|
|
||||||
(state) => state.shootingRangeZoom,
|
|
||||||
);
|
|
||||||
|
|
||||||
useImperativeHandle(ref, () => canvas.current!, []);
|
useImperativeHandle(ref, () => canvas.current!, []);
|
||||||
|
|
||||||
@@ -162,6 +156,8 @@ export const TankSandbox = forwardRef<HTMLCanvasElement, TankSandboxProps>(
|
|||||||
}
|
}
|
||||||
}, [display]);
|
}, [display]);
|
||||||
|
|
||||||
|
console.log('rendering tank sandbox');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Canvas
|
<Canvas
|
||||||
frameloop={
|
frameloop={
|
||||||
@@ -192,8 +188,7 @@ export const TankSandbox = forwardRef<HTMLCanvasElement, TankSandboxProps>(
|
|||||||
>
|
>
|
||||||
<Controls />
|
<Controls />
|
||||||
<SceneProps />
|
<SceneProps />
|
||||||
{display !== TankopediaDisplay.StaticArmor &&
|
{display !== TankopediaDisplay.StaticArmor && <TankModel />}
|
||||||
shootingRangeZoom === ShootingRangeZoom.Arcade && <TankModel />}
|
|
||||||
<ShotDisplay />
|
<ShotDisplay />
|
||||||
<ArmorPlateDisplay />
|
<ArmorPlateDisplay />
|
||||||
<AutoClear />
|
<AutoClear />
|
||||||
|
|||||||
Reference in new issue
Block a user