mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-12-01 22:02:45 +01:00
apply batched mesh to all things
This commit is contained in:
21 files changed
+844
-892
No files matched your search
@@ -1,19 +1,14 @@
|
||||
import {
|
||||
invalidate,
|
||||
useThree,
|
||||
type ObjectMap,
|
||||
type ThreeEvent,
|
||||
} from "@react-three/fiber";
|
||||
import { invalidate, useThree, type ThreeEvent } from "@react-three/fiber";
|
||||
import type { QuicklimeEvent } from "quicklime";
|
||||
import { useRef } from "react";
|
||||
import type { Texture } from "three";
|
||||
import { useEffect, useMemo, useRef } from "react";
|
||||
import type { Object3D, Texture } from "three";
|
||||
import { Group, Mesh, MeshStandardMaterial, Vector2 } from "three";
|
||||
import { applyPitchYawLimits } from "../core/blitz/applyPitchYawLimits";
|
||||
import { hasEquipment } from "../core/blitzkit/hasEquipment";
|
||||
import { modelTransformEvent } from "../core/blitzkit/modelTransform";
|
||||
import { controlsEnabledEvent } from "../core/controlsEnabled";
|
||||
import { useBatched } from "../hooks/useBatched";
|
||||
import { useModel } from "../hooks/useModel";
|
||||
import { useBatchGroups } from "../hooks/useBatchGroups";
|
||||
import { useModelNodes } from "../hooks/useModelNodes";
|
||||
import { useSkin, type Skin } from "../hooks/useSkin";
|
||||
import { useSkinAnimation } from "../hooks/useSkinAnimation";
|
||||
import { useTankModelDefinition } from "../hooks/useTankModelDefinition";
|
||||
@@ -22,17 +17,24 @@ import { Duel } from "../stores/duel";
|
||||
import { Tankopedia } from "../stores/tankopedia";
|
||||
import { TankopediaDisplay } from "../stores/tankopediaPersistent/constants";
|
||||
import { ModelTankWrapper } from "./Armor/components/ModelTankWrapper";
|
||||
import { Batch } from "./Batch";
|
||||
import { transitionEvent } from "./Tankopedia/HeroSection/components/TankSandbox/components/Lighting";
|
||||
|
||||
interface SectionProps {
|
||||
map: ObjectMap;
|
||||
nodes: Object3D[];
|
||||
skins: Skin[];
|
||||
// dragging scrolls the track textures
|
||||
scrollTracks?: boolean;
|
||||
// dragging turns the turret
|
||||
yaw?: boolean;
|
||||
// dragging raises and lowers the gun
|
||||
pitch?: boolean;
|
||||
}
|
||||
|
||||
// emissive parts fade with the lights instead of glowing in the dark
|
||||
function fadeEmissive(materials: MeshStandardMaterial[]) {
|
||||
const emissiveMaterials = materials.filter(
|
||||
(material) => material.emissiveMap
|
||||
(material) => material.emissiveMap,
|
||||
);
|
||||
|
||||
function applyIntensity(intensity: number) {
|
||||
@@ -53,29 +55,74 @@ function fadeEmissive(materials: MeshStandardMaterial[]) {
|
||||
};
|
||||
}
|
||||
|
||||
function Tracks({ map, skins }: SectionProps) {
|
||||
const Batched = useBatched(map, true, skins);
|
||||
function Section({ nodes, skins, scrollTracks, yaw, pitch }: SectionProps) {
|
||||
const turret = Duel.use((state) => state.protagonist.turret);
|
||||
const gun = Duel.use((state) => state.protagonist.gun);
|
||||
|
||||
const tankModelDefinition = useTankModelDefinition();
|
||||
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
||||
const gunModelDefinition = turretModelDefinition.guns[gun.id];
|
||||
|
||||
const { groups, parts, materials } = useBatchGroups(nodes, skins);
|
||||
|
||||
const position = useRef(new Vector2());
|
||||
const delta = useRef(new Vector2());
|
||||
|
||||
const pitchAngle = useRef(0);
|
||||
const yawAngle = useRef(0);
|
||||
const textures = useRef(new Set<Texture | null>());
|
||||
|
||||
const canvas = useThree((state) => state.gl.domElement);
|
||||
const rotates = yaw || pitch;
|
||||
|
||||
function handlePointerMove(event: PointerEvent) {
|
||||
delta.current.set(event.clientX, event.clientY).sub(position.current);
|
||||
position.current.set(event.clientX, event.clientY);
|
||||
|
||||
const deltaX = delta.current.x / window.innerWidth;
|
||||
const deltaY = delta.current.y / window.innerHeight;
|
||||
if (scrollTracks) {
|
||||
const offset =
|
||||
delta.current.x / window.innerWidth +
|
||||
delta.current.y / window.innerHeight;
|
||||
|
||||
const offset = deltaX + deltaY;
|
||||
for (const texture of textures.current) {
|
||||
if (!texture) continue;
|
||||
texture.offset.y += offset;
|
||||
}
|
||||
|
||||
for (const texture of textures.current) {
|
||||
if (!texture) continue;
|
||||
texture.offset.y += offset;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
invalidate();
|
||||
if (rotates) {
|
||||
const hasImprovedVerticalStabilizer = hasEquipment(
|
||||
122,
|
||||
Duel.state.protagonist.tank.equipment_preset,
|
||||
Duel.state.protagonist.equipmentMatrix,
|
||||
);
|
||||
const hasDownImprovedVerticalStabilizer = hasEquipment(
|
||||
124,
|
||||
Duel.state.protagonist.tank.equipment_preset,
|
||||
Duel.state.protagonist.equipmentMatrix,
|
||||
);
|
||||
const boundingRect = canvas.getBoundingClientRect();
|
||||
const pitchDelta = pitch
|
||||
? delta.current.y * (Math.PI / boundingRect.height)
|
||||
: 0;
|
||||
const yawDelta = yaw
|
||||
? delta.current.x * (Math.PI / boundingRect.width)
|
||||
: 0;
|
||||
|
||||
[pitchAngle.current, yawAngle.current] = applyPitchYawLimits(
|
||||
pitchAngle.current - pitchDelta,
|
||||
yawAngle.current + yawDelta,
|
||||
gunModelDefinition.pitch!,
|
||||
turretModelDefinition.yaw,
|
||||
hasImprovedVerticalStabilizer,
|
||||
hasDownImprovedVerticalStabilizer,
|
||||
);
|
||||
modelTransformEvent.dispatch({
|
||||
pitch: pitchAngle.current,
|
||||
yaw: yawAngle.current,
|
||||
});
|
||||
}
|
||||
}
|
||||
function handlePointerUp() {
|
||||
controlsEnabledEvent.dispatch(true);
|
||||
@@ -84,218 +131,64 @@ function Tracks({ map, skins }: SectionProps) {
|
||||
window.removeEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
function handlePointerDown(event: ThreeEvent<PointerEvent>) {
|
||||
if (Tankopedia.state.display !== TankopediaDisplay.Model) {
|
||||
// tracks only scroll while looking at the model, not the armor
|
||||
if (scrollTracks && Tankopedia.state.display !== TankopediaDisplay.Model) {
|
||||
return;
|
||||
}
|
||||
|
||||
const material = (event.object as Mesh).material as MeshStandardMaterial;
|
||||
|
||||
textures.current = new Set([
|
||||
material.map,
|
||||
material.aoMap,
|
||||
material.normalMap,
|
||||
material.roughnessMap,
|
||||
material.metalnessMap,
|
||||
]);
|
||||
|
||||
position.current.set(event.clientX, event.clientY);
|
||||
event.stopPropagation();
|
||||
|
||||
if (!scrollTracks && !rotates) return;
|
||||
|
||||
if (scrollTracks) {
|
||||
const material = (event.object as Mesh).material as MeshStandardMaterial;
|
||||
|
||||
textures.current = new Set([
|
||||
material.map,
|
||||
material.aoMap,
|
||||
material.normalMap,
|
||||
material.roughnessMap,
|
||||
material.metalnessMap,
|
||||
]);
|
||||
}
|
||||
|
||||
if (rotates) {
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.shot = undefined;
|
||||
draft.highlightArmor = undefined;
|
||||
});
|
||||
|
||||
pitchAngle.current = modelTransformEvent.last!.pitch;
|
||||
yawAngle.current = modelTransformEvent.last!.yaw;
|
||||
}
|
||||
|
||||
position.current.set(event.clientX, event.clientY);
|
||||
controlsEnabledEvent.dispatch(false);
|
||||
|
||||
window.addEventListener("pointermove", handlePointerMove);
|
||||
window.addEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
|
||||
return (
|
||||
<Batched
|
||||
onMaterials={fadeEmissive}
|
||||
onPointerDown={handlePointerDown}
|
||||
slicer={(node) => {
|
||||
return node.name.startsWith("chassis_track_");
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function Hull({ map, skins }: SectionProps) {
|
||||
const Batched = useBatched(map, true, skins);
|
||||
|
||||
function handlePointerDown(event: ThreeEvent<PointerEvent>) {
|
||||
event.stopPropagation();
|
||||
}
|
||||
useEffect(() => fadeEmissive(materials), [materials]);
|
||||
|
||||
return (
|
||||
<Batched
|
||||
onMaterials={fadeEmissive}
|
||||
onPointerDown={handlePointerDown}
|
||||
slicer={(node) => {
|
||||
const isHull = node.name === "hull";
|
||||
const isWheel = node.name.startsWith("chassis_wheel_");
|
||||
<group onPointerDown={handlePointerDown}>
|
||||
{groups.map(({ material, items }) => (
|
||||
<Batch
|
||||
key={material.uuid}
|
||||
items={items}
|
||||
material={material}
|
||||
castShadow
|
||||
receiveShadow
|
||||
/>
|
||||
))}
|
||||
|
||||
return isHull || isWheel;
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function Turret({ map, skins }: SectionProps) {
|
||||
const turret = Duel.use((state) => state.protagonist.turret);
|
||||
const gun = Duel.use((state) => state.protagonist.gun);
|
||||
|
||||
const tankModelDefinition = useTankModelDefinition();
|
||||
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
||||
const gunModelDefinition = turretModelDefinition.guns[gun.id];
|
||||
|
||||
const Batched = useBatched(map, true, skins);
|
||||
|
||||
const position = useRef(new Vector2());
|
||||
const delta = useRef(new Vector2());
|
||||
|
||||
const canvas = useThree((state) => state.gl.domElement);
|
||||
|
||||
function onPointerDown(event: ThreeEvent<PointerEvent>) {
|
||||
event.stopPropagation();
|
||||
|
||||
position.current.set(event.clientX, event.clientY);
|
||||
|
||||
controlsEnabledEvent.dispatch(false);
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.shot = undefined;
|
||||
draft.highlightArmor = undefined;
|
||||
});
|
||||
window.addEventListener("pointermove", handlePointerMove);
|
||||
window.addEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
function handlePointerMove(event: PointerEvent) {
|
||||
const hasImprovedVerticalStabilizer = hasEquipment(
|
||||
122,
|
||||
Duel.state.protagonist.tank.equipment_preset,
|
||||
Duel.state.protagonist.equipmentMatrix
|
||||
);
|
||||
const hasDownImprovedVerticalStabilizer = hasEquipment(
|
||||
124,
|
||||
Duel.state.protagonist.tank.equipment_preset,
|
||||
Duel.state.protagonist.equipmentMatrix
|
||||
);
|
||||
const boundingRect = canvas.getBoundingClientRect();
|
||||
|
||||
delta.current.set(event.clientX, event.clientY).sub(position.current);
|
||||
position.current.set(event.clientX, event.clientY);
|
||||
|
||||
const [pitch, yaw] = applyPitchYawLimits(
|
||||
modelTransformEvent.last!.pitch,
|
||||
modelTransformEvent.last!.yaw +
|
||||
delta.current.x * (Math.PI / boundingRect.width),
|
||||
gunModelDefinition.pitch!,
|
||||
turretModelDefinition.yaw,
|
||||
hasImprovedVerticalStabilizer,
|
||||
hasDownImprovedVerticalStabilizer
|
||||
);
|
||||
modelTransformEvent.dispatch({ pitch, yaw });
|
||||
}
|
||||
function handlePointerUp() {
|
||||
controlsEnabledEvent.dispatch(true);
|
||||
window.removeEventListener("pointermove", handlePointerMove);
|
||||
window.removeEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
|
||||
return (
|
||||
<Batched
|
||||
onMaterials={fadeEmissive}
|
||||
onPointerDown={onPointerDown}
|
||||
slicer={(node) => {
|
||||
const isCurrentTurret =
|
||||
node.name ===
|
||||
`turret_${turretModelDefinition.model_id
|
||||
.toString()
|
||||
.padStart(2, "0")}`;
|
||||
|
||||
return isCurrentTurret;
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function Gun({ map, skins }: SectionProps) {
|
||||
const Batched = useBatched(map, true, skins);
|
||||
|
||||
const turret = Duel.use((state) => state.protagonist.turret);
|
||||
const gun = Duel.use((state) => state.protagonist.gun);
|
||||
|
||||
const tankModelDefinition = useTankModelDefinition();
|
||||
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
||||
const gunModelDefinition = turretModelDefinition.guns[gun.id];
|
||||
|
||||
const position = useRef(new Vector2());
|
||||
const delta = useRef(new Vector2());
|
||||
const pitch = useRef(0);
|
||||
const yaw = useRef(0);
|
||||
|
||||
const canvas = useThree((state) => state.gl.domElement);
|
||||
|
||||
function handlePointerMove(event: PointerEvent) {
|
||||
const hasImprovedVerticalStabilizer = hasEquipment(
|
||||
122,
|
||||
Duel.state.protagonist.tank.equipment_preset,
|
||||
Duel.state.protagonist.equipmentMatrix
|
||||
);
|
||||
const hasDownImprovedVerticalStabilizer = hasEquipment(
|
||||
124,
|
||||
Duel.state.protagonist.tank.equipment_preset,
|
||||
Duel.state.protagonist.equipmentMatrix
|
||||
);
|
||||
const boundingRect = canvas.getBoundingClientRect();
|
||||
delta.current.set(event.clientX, event.clientY).sub(position.current);
|
||||
position.current.set(event.clientX, event.clientY);
|
||||
|
||||
[pitch.current, yaw.current] = applyPitchYawLimits(
|
||||
pitch.current - delta.current.y * (Math.PI / boundingRect.height),
|
||||
yaw.current + delta.current.x * (Math.PI / boundingRect.width),
|
||||
gunModelDefinition.pitch!,
|
||||
turretModelDefinition.yaw,
|
||||
hasImprovedVerticalStabilizer,
|
||||
hasDownImprovedVerticalStabilizer
|
||||
);
|
||||
modelTransformEvent.dispatch({ pitch: pitch.current, yaw: yaw.current });
|
||||
}
|
||||
function handlePointerUp() {
|
||||
controlsEnabledEvent.dispatch(true);
|
||||
window.removeEventListener("pointermove", handlePointerMove);
|
||||
window.removeEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
function onPointerDown(event: ThreeEvent<PointerEvent>) {
|
||||
event.stopPropagation();
|
||||
|
||||
controlsEnabledEvent.dispatch(false);
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.shot = undefined;
|
||||
draft.highlightArmor = undefined;
|
||||
});
|
||||
|
||||
position.current.set(event.clientX, event.clientY);
|
||||
pitch.current = modelTransformEvent.last!.pitch;
|
||||
yaw.current = modelTransformEvent.last!.yaw;
|
||||
|
||||
window.addEventListener("pointermove", handlePointerMove);
|
||||
window.addEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
|
||||
return (
|
||||
<Batched
|
||||
onMaterials={fadeEmissive}
|
||||
onPointerDown={onPointerDown}
|
||||
slicer={(node) => {
|
||||
const isCurrentMantlet =
|
||||
node.name ===
|
||||
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}_mask`;
|
||||
const isCurrentGun =
|
||||
node.name ===
|
||||
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}`;
|
||||
|
||||
return isCurrentGun || isCurrentMantlet;
|
||||
}}
|
||||
/>
|
||||
{parts.map(({ part, matrix }) => (
|
||||
<group key={part.uuid} matrix={matrix} matrixAutoUpdate={false}>
|
||||
<primitive object={part} />
|
||||
</group>
|
||||
))}
|
||||
</group>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -309,24 +202,29 @@ export function TankModel() {
|
||||
const turretContainer = useRef<Group>(null!);
|
||||
const gunContainer = useRef<Group>(null!);
|
||||
|
||||
const { gltf } = useModel(protagonist.tank.id);
|
||||
const nodes = useModelNodes();
|
||||
const skinGltf = useSkin(protagonist.tank.id);
|
||||
const attachmentGltf = useSkin(protagonist.tank.id, "attachment");
|
||||
const skins = [skinGltf, attachmentGltf].filter((skin) => skin !== undefined);
|
||||
const skins = useMemo(
|
||||
() => [skinGltf, attachmentGltf].filter((skin) => skin !== undefined),
|
||||
[skinGltf, attachmentGltf],
|
||||
);
|
||||
const hullNodes = useMemo(() => [...nodes.hull, ...nodes.wheels], [nodes]);
|
||||
const gunNodes = useMemo(() => [...nodes.gun, ...nodes.mask], [nodes]);
|
||||
|
||||
useTankTransform(track, turret, turretContainer, gunContainer);
|
||||
useSkinAnimation(skins);
|
||||
|
||||
return (
|
||||
<ModelTankWrapper ref={hullContainer}>
|
||||
<Tracks map={gltf} skins={skins} />
|
||||
<Hull map={gltf} skins={skins} />
|
||||
<Section nodes={nodes.tracks} skins={skins} scrollTracks />
|
||||
<Section nodes={hullNodes} skins={skins} />
|
||||
|
||||
<group ref={turretContainer}>
|
||||
<Turret map={gltf} skins={skins} />
|
||||
<Section nodes={nodes.turret} skins={skins} yaw />
|
||||
|
||||
<group ref={gunContainer}>
|
||||
<Gun map={gltf} skins={skins} />
|
||||
<Section nodes={gunNodes} skins={skins} yaw pitch />
|
||||
</group>
|
||||
</group>
|
||||
</ModelTankWrapper>
|
||||
|
||||
Reference in new issue
Block a user