mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-30 05:14:06 +01:00
apply batched mesh to all things
This commit is contained in:
21 files changed
+768
-816
No files matched your search
@@ -0,0 +1,9 @@
|
|||||||
|
# BlitzKit
|
||||||
|
|
||||||
|
## Layout
|
||||||
|
|
||||||
|
- Hooks live in `src/hooks/useName.ts` (`.tsx` if they render JSX), one hook per file named after it, never inside a component's folder.
|
||||||
|
|
||||||
|
## TypeScript
|
||||||
|
|
||||||
|
- When an `if`/`else if` chain checks one value against several candidates (`x === "a"`, `x === b`, ...), use a `switch` on that value instead. Inside a loop, end each case with `continue`, and put the checks a `switch` can't express (`startsWith`, ranges, ...) as an `if` after it, not in `default`.
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
import { memo, useRef } from "react";
|
import { memo, useRef } from "react";
|
||||||
import { Group } from "three";
|
import { Group } from "three";
|
||||||
import { correctZYTuple } from "../../../core/blitz/correctZYTuple";
|
import { correctZYTuple } from "../../../core/blitz/correctZYTuple";
|
||||||
import { nameToArmorId } from "../../../core/blitzkit/nameToArmorId";
|
import { useArmorPlates } from "../../../hooks/useArmorPlates";
|
||||||
import { resolveArmor } from "../../../core/blitzkit/resolveThickness";
|
|
||||||
import { useArmor } from "../../../hooks/useArmor";
|
|
||||||
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";
|
||||||
@@ -14,12 +12,9 @@ export const PrimaryArmorScene = memo(() => {
|
|||||||
const wrapper = useRef<Group>(null);
|
const wrapper = useRef<Group>(null);
|
||||||
const turretContainer = useRef<Group>(null!);
|
const turretContainer = useRef<Group>(null!);
|
||||||
const gunContainer = useRef<Group>(null!);
|
const gunContainer = useRef<Group>(null!);
|
||||||
const tank = Duel.use((state) => state.protagonist.tank);
|
|
||||||
const track = Duel.use((state) => state.protagonist.track);
|
const track = Duel.use((state) => state.protagonist.track);
|
||||||
const turret = Duel.use((state) => state.protagonist.turret);
|
const turret = Duel.use((state) => state.protagonist.turret);
|
||||||
const gun = Duel.use((state) => state.protagonist.gun);
|
const gun = Duel.use((state) => state.protagonist.gun);
|
||||||
const armorGltf = useArmor(tank.id).gltf;
|
|
||||||
const armorNodes = Object.values(armorGltf.nodes);
|
|
||||||
const tankModelDefinition = useTankModelDefinition();
|
const tankModelDefinition = useTankModelDefinition();
|
||||||
const trackModelDefinition = tankModelDefinition.tracks[track.id];
|
const trackModelDefinition = tankModelDefinition.tracks[track.id];
|
||||||
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
||||||
@@ -31,107 +26,30 @@ export const PrimaryArmorScene = memo(() => {
|
|||||||
state.protagonist.consumables.includes(73),
|
state.protagonist.consumables.includes(73),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const hiddenState = isDynamicArmorActive ? "state_01" : "state_00";
|
||||||
|
const plates = useArmorPlates(hiddenState);
|
||||||
|
|
||||||
useTankTransform(track, turret, turretContainer, gunContainer);
|
useTankTransform(track, turret, turretContainer, gunContainer);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ModelTankWrapper ref={wrapper}>
|
<ModelTankWrapper ref={wrapper}>
|
||||||
<group position={hullOrigin}>
|
<group position={hullOrigin}>
|
||||||
{armorNodes.map((node) => {
|
<PrimaryArmorSceneComponent plates={plates.hull.primary} />
|
||||||
const isHull = node.name.startsWith("hull_");
|
|
||||||
const isVisible = isHull;
|
|
||||||
const armorId = nameToArmorId(node.name);
|
|
||||||
const { spaced, thickness } = resolveArmor(
|
|
||||||
tankModelDefinition.armor!,
|
|
||||||
armorId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
!isVisible ||
|
|
||||||
spaced ||
|
|
||||||
thickness === undefined ||
|
|
||||||
(isDynamicArmorActive && node.name.includes("state_01")) ||
|
|
||||||
(!isDynamicArmorActive && node.name.includes("state_00"))
|
|
||||||
)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<PrimaryArmorSceneComponent
|
|
||||||
key={node.uuid}
|
|
||||||
thickness={thickness}
|
|
||||||
node={node}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
<group ref={turretContainer}>
|
<group ref={turretContainer}>
|
||||||
{armorNodes.map((node) => {
|
<group position={hullOrigin}>
|
||||||
const isCurrentTurret = node.name.startsWith(
|
|
||||||
`turret_${turretModelDefinition.model_id
|
|
||||||
.toString()
|
|
||||||
.padStart(2, "0")}`,
|
|
||||||
);
|
|
||||||
const isVisible = isCurrentTurret;
|
|
||||||
const armorId = nameToArmorId(node.name);
|
|
||||||
const { spaced, thickness } = resolveArmor(
|
|
||||||
turretModelDefinition.armor!,
|
|
||||||
armorId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
!isVisible ||
|
|
||||||
spaced ||
|
|
||||||
thickness === undefined ||
|
|
||||||
(isDynamicArmorActive && node.name.includes("state_01")) ||
|
|
||||||
(!isDynamicArmorActive && node.name.includes("state_00"))
|
|
||||||
)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<group position={hullOrigin} key={node.uuid}>
|
|
||||||
<group position={turretOrigin}>
|
<group position={turretOrigin}>
|
||||||
<PrimaryArmorSceneComponent
|
<PrimaryArmorSceneComponent plates={plates.turret.primary} />
|
||||||
key={node.uuid}
|
|
||||||
thickness={thickness}
|
|
||||||
node={node}
|
|
||||||
/>
|
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
);
|
|
||||||
})}
|
|
||||||
|
|
||||||
<group ref={gunContainer}>
|
<group ref={gunContainer}>
|
||||||
{armorNodes.map((node) => {
|
<group position={hullOrigin}>
|
||||||
const isCurrentGun = node.name.startsWith(
|
|
||||||
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}`,
|
|
||||||
);
|
|
||||||
const isVisible = isCurrentGun;
|
|
||||||
const armorId = nameToArmorId(node.name);
|
|
||||||
const { spaced, thickness } = resolveArmor(
|
|
||||||
gunModelDefinition.armor!,
|
|
||||||
armorId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
!isVisible ||
|
|
||||||
spaced ||
|
|
||||||
thickness === undefined ||
|
|
||||||
(isDynamicArmorActive && node.name.includes("state_01")) ||
|
|
||||||
(!isDynamicArmorActive && node.name.includes("state_00"))
|
|
||||||
)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<group position={hullOrigin} key={node.uuid}>
|
|
||||||
<group position={turretOrigin.clone().add(gunOrigin)}>
|
<group position={turretOrigin.clone().add(gunOrigin)}>
|
||||||
<PrimaryArmorSceneComponent
|
<PrimaryArmorSceneComponent plates={plates.gun.primary} />
|
||||||
thickness={thickness}
|
|
||||||
node={node}
|
|
||||||
/>
|
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
);
|
|
||||||
})}
|
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
</ModelTankWrapper>
|
</ModelTankWrapper>
|
||||||
|
|||||||
+12
-33
@@ -9,7 +9,6 @@ import { useEffect, useRef } from "react";
|
|||||||
import {
|
import {
|
||||||
MeshBasicMaterial,
|
MeshBasicMaterial,
|
||||||
NormalBlending,
|
NormalBlending,
|
||||||
Object3D,
|
|
||||||
ShaderMaterial,
|
ShaderMaterial,
|
||||||
SubtractiveBlending,
|
SubtractiveBlending,
|
||||||
UniformsLib,
|
UniformsLib,
|
||||||
@@ -18,18 +17,19 @@ import {
|
|||||||
} from "three";
|
} from "three";
|
||||||
import { degToRad } from "three/src/math/MathUtils.js";
|
import { degToRad } from "three/src/math/MathUtils.js";
|
||||||
import { hasEquipment } from "../../../../core/blitzkit/hasEquipment";
|
import { hasEquipment } from "../../../../core/blitzkit/hasEquipment";
|
||||||
import { jsxTree } from "../../../../core/blitzkit/jsxTree";
|
import type { ArmorPlate } from "../../../../hooks/useArmorPlates";
|
||||||
|
import { usePlateItems } from "../../../../hooks/usePlateItems";
|
||||||
import { Duel } from "../../../../stores/duel";
|
import { Duel } from "../../../../stores/duel";
|
||||||
import { Tankopedia } from "../../../../stores/tankopedia";
|
import { Tankopedia } from "../../../../stores/tankopedia";
|
||||||
import { TankopediaPersistent } from "../../../../stores/tankopediaPersistent";
|
import { TankopediaPersistent } from "../../../../stores/tankopediaPersistent";
|
||||||
|
import { Batch } from "../../../Batch";
|
||||||
import { transitionEvent } from "../../../Tankopedia/HeroSection/components/TankSandbox/components/Lighting";
|
import { transitionEvent } from "../../../Tankopedia/HeroSection/components/TankSandbox/components/Lighting";
|
||||||
import fragmentShader from "./shaders/fragment.glsl?raw";
|
import fragmentShader from "./shaders/fragment.glsl?raw";
|
||||||
import vertexShader from "./shaders/vertex.glsl?raw";
|
import vertexShader from "./shaders/vertex.glsl?raw";
|
||||||
import { spacedArmorRenderTarget } from "./target";
|
import { spacedArmorRenderTarget } from "./target";
|
||||||
|
|
||||||
interface PrimaryArmorSceneComponentProps {
|
interface PrimaryArmorSceneComponentProps {
|
||||||
thickness: number;
|
plates: ArmorPlate[];
|
||||||
node: Object3D;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const excludeMaterial = new MeshBasicMaterial({
|
const excludeMaterial = new MeshBasicMaterial({
|
||||||
@@ -40,8 +40,7 @@ const date = new Date();
|
|||||||
const isHalloween = date.getMonth() === 9 && date.getDate() === 31;
|
const isHalloween = date.getMonth() === 9 && date.getDate() === 31;
|
||||||
|
|
||||||
export function PrimaryArmorSceneComponent({
|
export function PrimaryArmorSceneComponent({
|
||||||
node,
|
plates,
|
||||||
thickness,
|
|
||||||
}: PrimaryArmorSceneComponentProps) {
|
}: PrimaryArmorSceneComponentProps) {
|
||||||
const material = useRef(
|
const material = useRef(
|
||||||
new ShaderMaterial({
|
new ShaderMaterial({
|
||||||
@@ -58,7 +57,7 @@ export function PrimaryArmorSceneComponent({
|
|||||||
UniformsLib.fog,
|
UniformsLib.fog,
|
||||||
|
|
||||||
{
|
{
|
||||||
thickness: { value: null },
|
thicknessCoefficient: { value: null },
|
||||||
penetration: { value: null },
|
penetration: { value: null },
|
||||||
caliber: { value: null },
|
caliber: { value: null },
|
||||||
ricochet: { value: null },
|
ricochet: { value: null },
|
||||||
@@ -131,8 +130,8 @@ export function PrimaryArmorSceneComponent({
|
|||||||
? Duel.state.protagonist.tank.equalizer?.armor
|
? Duel.state.protagonist.tank.equalizer?.armor
|
||||||
: undefined) ?? 1;
|
: undefined) ?? 1;
|
||||||
|
|
||||||
material.current.uniforms.thickness.value =
|
material.current.uniforms.thicknessCoefficient.value =
|
||||||
thickness * (hasEnhancedArmor ? 1.04 : 1) * equalizer;
|
(hasEnhancedArmor ? 1.04 : 1) * equalizer;
|
||||||
|
|
||||||
if (!noInvalidate) invalidate();
|
if (!noInvalidate) invalidate();
|
||||||
}
|
}
|
||||||
@@ -223,32 +222,12 @@ export function PrimaryArmorSceneComponent({
|
|||||||
camera.projectionMatrixInverse;
|
camera.projectionMatrixInverse;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const items = usePlateItems(plates);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{jsxTree(node, {
|
<Batch items={items} material={excludeMaterial} renderOrder={0} />
|
||||||
mesh(_, props, key) {
|
<Batch items={items} material={material.current} renderOrder={1} />
|
||||||
return (
|
|
||||||
<mesh
|
|
||||||
{...props}
|
|
||||||
key={`${key}-exclude`}
|
|
||||||
renderOrder={0}
|
|
||||||
material={excludeMaterial}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
})}
|
|
||||||
{jsxTree(node, {
|
|
||||||
mesh(_, props, key) {
|
|
||||||
return (
|
|
||||||
<mesh
|
|
||||||
{...props}
|
|
||||||
key={`${key}-include`}
|
|
||||||
renderOrder={1}
|
|
||||||
material={material.current}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
})}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
+4
-1
@@ -4,7 +4,8 @@ varying vec3 vNormal;
|
|||||||
varying vec3 vViewPosition;
|
varying vec3 vViewPosition;
|
||||||
|
|
||||||
uniform float opacity;
|
uniform float opacity;
|
||||||
uniform float thickness;
|
uniform float thicknessCoefficient;
|
||||||
|
varying float vThickness;
|
||||||
uniform float penetration;
|
uniform float penetration;
|
||||||
uniform float caliber;
|
uniform float caliber;
|
||||||
uniform float ricochet;
|
uniform float ricochet;
|
||||||
@@ -43,6 +44,8 @@ vec3 getPenetrationColor(bool isThreeCalibersRule, bool couldHaveRicochet) {
|
|||||||
void main() {
|
void main() {
|
||||||
#include <clipping_planes_fragment>
|
#include <clipping_planes_fragment>
|
||||||
|
|
||||||
|
float thickness = vThickness * thicknessCoefficient;
|
||||||
|
|
||||||
vec2 screenCoord = gl_FragCoord.xy / resolution;
|
vec2 screenCoord = gl_FragCoord.xy / resolution;
|
||||||
vec4 spacedArmorData = texture2D(spacedArmorBuffer, screenCoord);
|
vec4 spacedArmorData = texture2D(spacedArmorBuffer, screenCoord);
|
||||||
bool isUnderSpacedArmor = spacedArmorData.a != 0.0;
|
bool isUnderSpacedArmor = spacedArmorData.a != 0.0;
|
||||||
|
|||||||
+21
-2
@@ -2,12 +2,31 @@ precision mediump float;
|
|||||||
|
|
||||||
varying vec3 vNormal;
|
varying vec3 vNormal;
|
||||||
varying vec3 vViewPosition;
|
varying vec3 vViewPosition;
|
||||||
|
varying float vThickness;
|
||||||
|
|
||||||
|
#include <batching_pars_vertex>
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
vec4 localPosition = vec4(position, 1.0);
|
||||||
|
vec3 localNormal = normal;
|
||||||
|
|
||||||
|
#ifdef USE_BATCHING
|
||||||
|
#include <batching_vertex>
|
||||||
|
|
||||||
|
localPosition = batchingMatrix * localPosition;
|
||||||
|
localNormal = normalize(mat3(batchingMatrix) * localNormal);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_BATCHING_COLOR
|
||||||
|
vThickness = getBatchingColor(getIndirectIndex(gl_DrawID)).r;
|
||||||
|
#else
|
||||||
|
vThickness = 0.0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
vec4 mvPosition = modelViewMatrix * localPosition;
|
||||||
|
|
||||||
vViewPosition = mvPosition.xyz;
|
vViewPosition = mvPosition.xyz;
|
||||||
vNormal = normalMatrix * normal;
|
vNormal = normalMatrix * localNormal;
|
||||||
|
|
||||||
gl_Position = projectionMatrix * mvPosition;
|
gl_Position = projectionMatrix * mvPosition;
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,19 @@
|
|||||||
import { memo, useRef } from "react";
|
import { memo, useMemo, useRef } from "react";
|
||||||
import { Group, Plane, Scene, Vector3 } from "three";
|
import { Group, Plane, Scene, Vector3, type Object3D } from "three";
|
||||||
import { correctZYTuple } from "../../../core/blitz/correctZYTuple";
|
import { correctZYTuple } from "../../../core/blitz/correctZYTuple";
|
||||||
import { nameToArmorId } from "../../../core/blitzkit/nameToArmorId";
|
import { type ArmorPlate, useArmorPlates } from "../../../hooks/useArmorPlates";
|
||||||
import { resolveArmor } from "../../../core/blitzkit/resolveThickness";
|
import { useModelNodes } from "../../../hooks/useModelNodes";
|
||||||
import { useArmor } from "../../../hooks/useArmor";
|
|
||||||
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 { ModelTankWrapper } from "./ModelTankWrapper";
|
import { ModelTankWrapper } from "./ModelTankWrapper";
|
||||||
import { SpacedArmorSceneComponent } from "./SpacedArmorSceneComponent";
|
import {
|
||||||
|
SpacedArmorSceneComponent,
|
||||||
|
type ExternalModuleVariant,
|
||||||
|
} from "./SpacedArmorSceneComponent";
|
||||||
|
import { SpacedArmorSubExternal } from "./SpacedArmorSceneComponent/components/SpacedArmorSubExternal";
|
||||||
|
import { SpacedArmorSubPrimary } from "./SpacedArmorSceneComponent/components/SpacedArmorSubPrimary";
|
||||||
|
import { SpacedArmorSubSpaced } from "./SpacedArmorSceneComponent/components/SpacedArmorSubSpaced";
|
||||||
|
|
||||||
export enum ArmorType {
|
export enum ArmorType {
|
||||||
Primary,
|
Primary,
|
||||||
@@ -21,18 +25,90 @@ interface SpacedArmorSceneProps {
|
|||||||
scene: Scene;
|
scene: Scene;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ArmorSectionProps {
|
||||||
|
primary: ArmorPlate[];
|
||||||
|
spaced: ArmorPlate[];
|
||||||
|
scene: Scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ArmorSection({ primary, spaced, scene }: ArmorSectionProps) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<SpacedArmorSubPrimary plates={primary} />
|
||||||
|
<SpacedArmorSubSpaced plates={spaced} />
|
||||||
|
|
||||||
|
{primary.map(({ node, thickness }) => (
|
||||||
|
<SpacedArmorSceneComponent
|
||||||
|
scene={scene}
|
||||||
|
key={node.uuid}
|
||||||
|
type={ArmorType.Primary}
|
||||||
|
thickness={thickness}
|
||||||
|
node={node}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
{spaced.map(({ node, thickness }) => (
|
||||||
|
<SpacedArmorSceneComponent
|
||||||
|
scene={scene}
|
||||||
|
key={node.uuid}
|
||||||
|
type={ArmorType.Spaced}
|
||||||
|
thickness={thickness}
|
||||||
|
node={node}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ExternalSectionProps {
|
||||||
|
nodes: Object3D[];
|
||||||
|
thickness: number;
|
||||||
|
variant: ExternalModuleVariant;
|
||||||
|
clip?: Plane;
|
||||||
|
scene: Scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ExternalSection({
|
||||||
|
nodes,
|
||||||
|
thickness,
|
||||||
|
variant,
|
||||||
|
clip,
|
||||||
|
scene,
|
||||||
|
}: ExternalSectionProps) {
|
||||||
|
const plates = useMemo(
|
||||||
|
() => nodes.map((node) => ({ node, thickness })),
|
||||||
|
[nodes, thickness],
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<SpacedArmorSubExternal
|
||||||
|
plates={plates}
|
||||||
|
thickness={thickness}
|
||||||
|
clip={clip}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{nodes.map((node) => (
|
||||||
|
<SpacedArmorSceneComponent
|
||||||
|
scene={scene}
|
||||||
|
key={node.uuid}
|
||||||
|
type={ArmorType.External}
|
||||||
|
thickness={thickness}
|
||||||
|
variant={variant}
|
||||||
|
node={node}
|
||||||
|
clip={clip}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export const SpacedArmorScene = memo<SpacedArmorSceneProps>(({ scene }) => {
|
export const SpacedArmorScene = memo<SpacedArmorSceneProps>(({ scene }) => {
|
||||||
const wrapper = useRef<Group>(null);
|
const wrapper = useRef<Group>(null);
|
||||||
const turretContainer = useRef<Group>(null!);
|
const turretContainer = useRef<Group>(null!);
|
||||||
const gunContainer = useRef<Group>(null!);
|
const gunContainer = useRef<Group>(null!);
|
||||||
const tank = Duel.use((state) => state.protagonist.tank);
|
|
||||||
const track = Duel.use((state) => state.protagonist.track);
|
const track = Duel.use((state) => state.protagonist.track);
|
||||||
const turret = Duel.use((state) => state.protagonist.turret);
|
const turret = Duel.use((state) => state.protagonist.turret);
|
||||||
const gun = Duel.use((state) => state.protagonist.gun);
|
const gun = Duel.use((state) => state.protagonist.gun);
|
||||||
const { gltf: armorGltf } = useArmor(tank.id);
|
|
||||||
const { gltf: modelGltf } = useModel(tank.id);
|
|
||||||
const armorNodes = Object.values(armorGltf.nodes);
|
|
||||||
const modelNodes = Object.values(modelGltf.nodes);
|
|
||||||
const tankModelDefinition = useTankModelDefinition();
|
const tankModelDefinition = useTankModelDefinition();
|
||||||
const trackModelDefinition = tankModelDefinition.tracks[track.id];
|
const trackModelDefinition = tankModelDefinition.tracks[track.id];
|
||||||
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
||||||
@@ -47,156 +123,63 @@ export const SpacedArmorScene = memo<SpacedArmorSceneProps>(({ scene }) => {
|
|||||||
const isDynamicArmorActive = Duel.use((state) =>
|
const isDynamicArmorActive = Duel.use((state) =>
|
||||||
state.protagonist.consumables.includes(73),
|
state.protagonist.consumables.includes(73),
|
||||||
);
|
);
|
||||||
|
const clip = useMemo(
|
||||||
|
() =>
|
||||||
|
maskOrigin === undefined
|
||||||
|
? undefined
|
||||||
|
: new Plane(new Vector3(0, 0, -1), -maskOrigin),
|
||||||
|
[maskOrigin],
|
||||||
|
);
|
||||||
|
|
||||||
|
const hiddenState = isDynamicArmorActive ? "state_00" : "state_01";
|
||||||
|
const plates = useArmorPlates(hiddenState);
|
||||||
|
const modelNodes = useModelNodes();
|
||||||
|
const hasMask = Boolean(gunModelDefinition.mask);
|
||||||
|
const trackNodes = useMemo(
|
||||||
|
() => [...modelNodes.wheels, ...modelNodes.tracks],
|
||||||
|
[modelNodes],
|
||||||
|
);
|
||||||
|
const gunNodes = useMemo(
|
||||||
|
() => (hasMask ? [...modelNodes.gun, ...modelNodes.mask] : modelNodes.gun),
|
||||||
|
[modelNodes, hasMask],
|
||||||
|
);
|
||||||
|
|
||||||
useTankTransform(track, turret, turretContainer, gunContainer);
|
useTankTransform(track, turret, turretContainer, gunContainer);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ModelTankWrapper ref={wrapper}>
|
<ModelTankWrapper ref={wrapper}>
|
||||||
<group position={hullOrigin}>
|
<group position={hullOrigin}>
|
||||||
{armorNodes.map((node) => {
|
<ArmorSection {...plates.hull} scene={scene} />
|
||||||
const isHull = node.name.startsWith("hull_");
|
|
||||||
const isVisible = isHull;
|
|
||||||
const armorId = nameToArmorId(node.name);
|
|
||||||
const { spaced, thickness } = resolveArmor(
|
|
||||||
tankModelDefinition.armor!,
|
|
||||||
armorId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
!isVisible ||
|
|
||||||
thickness === undefined ||
|
|
||||||
(isDynamicArmorActive && node.name.includes("state_00")) ||
|
|
||||||
(!isDynamicArmorActive && node.name.includes("state_01"))
|
|
||||||
)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<SpacedArmorSceneComponent
|
|
||||||
scene={scene}
|
|
||||||
key={`${node.uuid}-hull`}
|
|
||||||
type={spaced ? ArmorType.Spaced : ArmorType.Primary}
|
|
||||||
thickness={thickness}
|
|
||||||
node={node}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
{modelNodes.map((node) => {
|
<ExternalSection
|
||||||
const isWheel = node.name.startsWith("chassis_wheel_");
|
nodes={trackNodes}
|
||||||
const isTrack = node.name.startsWith("chassis_track_");
|
|
||||||
const isVisible = isWheel || isTrack;
|
|
||||||
|
|
||||||
if (!isVisible) return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<SpacedArmorSceneComponent
|
|
||||||
scene={scene}
|
|
||||||
key={`${node.uuid}-track`}
|
|
||||||
type={ArmorType.External}
|
|
||||||
thickness={trackModelDefinition.thickness}
|
thickness={trackModelDefinition.thickness}
|
||||||
variant="track"
|
variant="track"
|
||||||
node={node}
|
scene={scene}
|
||||||
/>
|
/>
|
||||||
);
|
|
||||||
})}
|
|
||||||
|
|
||||||
<group ref={turretContainer}>
|
<group ref={turretContainer}>
|
||||||
{armorNodes.map((node) => {
|
<group position={hullOrigin}>
|
||||||
const isCurrentTurret = node.name.startsWith(
|
|
||||||
`turret_${turretModelDefinition.model_id.toString().padStart(2, "0")}`,
|
|
||||||
);
|
|
||||||
const isVisible = isCurrentTurret;
|
|
||||||
const armorId = nameToArmorId(node.name);
|
|
||||||
const { spaced, thickness } = resolveArmor(
|
|
||||||
turretModelDefinition.armor!,
|
|
||||||
armorId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
!isVisible ||
|
|
||||||
thickness === undefined ||
|
|
||||||
(isDynamicArmorActive && node.name.includes("state_00")) ||
|
|
||||||
(!isDynamicArmorActive && node.name.includes("state_01"))
|
|
||||||
)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<group position={hullOrigin} key={node.uuid}>
|
|
||||||
<group position={turretOrigin}>
|
<group position={turretOrigin}>
|
||||||
<SpacedArmorSceneComponent
|
<ArmorSection {...plates.turret} scene={scene} />
|
||||||
scene={scene}
|
|
||||||
key={`${node.uuid}-turret`}
|
|
||||||
type={spaced ? ArmorType.Spaced : ArmorType.Primary}
|
|
||||||
thickness={thickness}
|
|
||||||
node={node}
|
|
||||||
/>
|
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
);
|
|
||||||
})}
|
|
||||||
|
|
||||||
<group ref={gunContainer}>
|
<group ref={gunContainer}>
|
||||||
{armorNodes.map((node) => {
|
<group position={hullOrigin}>
|
||||||
const isCurrentGun = node.name.startsWith(
|
|
||||||
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}`,
|
|
||||||
);
|
|
||||||
const isVisible = isCurrentGun;
|
|
||||||
const armorId = nameToArmorId(node.name);
|
|
||||||
const { spaced, thickness } = resolveArmor(
|
|
||||||
gunModelDefinition.armor!,
|
|
||||||
armorId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
!isVisible ||
|
|
||||||
thickness === undefined ||
|
|
||||||
(isDynamicArmorActive && node.name.includes("state_00")) ||
|
|
||||||
(!isDynamicArmorActive && node.name.includes("state_01"))
|
|
||||||
)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<group position={hullOrigin} key={`${node.uuid}-gun-spaced}`}>
|
|
||||||
<group position={turretOrigin.clone().add(gunOrigin)}>
|
<group position={turretOrigin.clone().add(gunOrigin)}>
|
||||||
<SpacedArmorSceneComponent
|
<ArmorSection {...plates.gun} scene={scene} />
|
||||||
scene={scene}
|
|
||||||
type={spaced ? ArmorType.Spaced : ArmorType.Primary}
|
|
||||||
thickness={thickness}
|
|
||||||
node={node}
|
|
||||||
/>
|
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
);
|
|
||||||
})}
|
|
||||||
|
|
||||||
{modelNodes.map((node) => {
|
<ExternalSection
|
||||||
const gunString = `gun_${gunModelDefinition.model_id
|
nodes={gunNodes}
|
||||||
.toString()
|
|
||||||
.padStart(2, "0")}`;
|
|
||||||
const isCurrentGun = gunModelDefinition.mask
|
|
||||||
? node.name.startsWith(gunString)
|
|
||||||
: node.name === gunString;
|
|
||||||
const isVisible = isCurrentGun;
|
|
||||||
|
|
||||||
if (!isVisible) return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<SpacedArmorSceneComponent
|
|
||||||
scene={scene}
|
|
||||||
key={`${node.uuid}-gun-external`}
|
|
||||||
type={ArmorType.External}
|
|
||||||
thickness={gunModelDefinition.thickness}
|
thickness={gunModelDefinition.thickness}
|
||||||
variant="gun"
|
variant="gun"
|
||||||
node={node}
|
clip={clip}
|
||||||
clip={
|
scene={scene}
|
||||||
maskOrigin === undefined
|
|
||||||
? undefined
|
|
||||||
: new Plane(new Vector3(0, 0, -1), -maskOrigin)
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
);
|
|
||||||
})}
|
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
</ModelTankWrapper>
|
</ModelTankWrapper>
|
||||||
|
|||||||
+27
-64
@@ -1,35 +1,44 @@
|
|||||||
import { resolvePenetrationCoefficient } from "@blitzkit/core";
|
import { resolvePenetrationCoefficient } from "@blitzkit/core";
|
||||||
import { useEffect } from "react";
|
import { useEffect, useMemo } from "react";
|
||||||
import {
|
import {
|
||||||
AdditiveBlending,
|
AdditiveBlending,
|
||||||
MeshBasicMaterial,
|
MeshBasicMaterial,
|
||||||
Object3D,
|
|
||||||
Plane,
|
Plane,
|
||||||
ShaderMaterial,
|
ShaderMaterial,
|
||||||
} from "three";
|
} from "three";
|
||||||
import type { ArmorUserData, ExternalModuleVariant } from "../..";
|
|
||||||
import { hasEquipment } from "../../../../../../core/blitzkit/hasEquipment";
|
import { hasEquipment } from "../../../../../../core/blitzkit/hasEquipment";
|
||||||
import { jsxTree } from "../../../../../../core/blitzkit/jsxTree";
|
import type { ArmorPlate } from "../../../../../../hooks/useArmorPlates";
|
||||||
|
import { usePlateItems } from "../../../../../../hooks/usePlateItems";
|
||||||
import { Duel } from "../../../../../../stores/duel";
|
import { Duel } from "../../../../../../stores/duel";
|
||||||
import { Tankopedia } from "../../../../../../stores/tankopedia";
|
import { Tankopedia } from "../../../../../../stores/tankopedia";
|
||||||
import { ArmorType } from "../../../SpacedArmorScene";
|
import { Batch } from "../../../../../Batch";
|
||||||
import fragmentShader from "./shaders/fragment.glsl?raw";
|
import fragmentShader from "./shaders/fragment.glsl?raw";
|
||||||
import vertexShader from "./shaders/vertex.glsl?raw";
|
import vertexShader from "./shaders/vertex.glsl?raw";
|
||||||
|
|
||||||
interface SpacedArmorSubExternalProps {
|
interface SpacedArmorSubExternalProps {
|
||||||
node: Object3D;
|
plates: ArmorPlate[];
|
||||||
thickness: number;
|
thickness: number;
|
||||||
variant: ExternalModuleVariant;
|
|
||||||
clip?: Plane;
|
clip?: Plane;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SpacedArmorSubExternal({
|
export function SpacedArmorSubExternal({
|
||||||
node,
|
plates,
|
||||||
thickness,
|
thickness,
|
||||||
variant,
|
|
||||||
clip,
|
clip,
|
||||||
}: SpacedArmorSubExternalProps) {
|
}: SpacedArmorSubExternalProps) {
|
||||||
const material = new ShaderMaterial({
|
const excludeMaterial = useMemo(
|
||||||
|
() =>
|
||||||
|
new MeshBasicMaterial({
|
||||||
|
colorWrite: false,
|
||||||
|
depthTest: true,
|
||||||
|
depthWrite: true,
|
||||||
|
...(clip ? { clippingPlanes: [clip] } : {}),
|
||||||
|
}),
|
||||||
|
[clip],
|
||||||
|
);
|
||||||
|
const material = useMemo(
|
||||||
|
() =>
|
||||||
|
new ShaderMaterial({
|
||||||
fragmentShader,
|
fragmentShader,
|
||||||
vertexShader,
|
vertexShader,
|
||||||
|
|
||||||
@@ -43,7 +52,9 @@ export function SpacedArmorSubExternal({
|
|||||||
thickness: { value: null },
|
thickness: { value: null },
|
||||||
penetration: { value: null },
|
penetration: { value: null },
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
|
[clip],
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function handleShellChange() {
|
function handleShellChange() {
|
||||||
@@ -121,62 +132,14 @@ export function SpacedArmorSubExternal({
|
|||||||
return () => {
|
return () => {
|
||||||
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
||||||
};
|
};
|
||||||
});
|
}, [material, thickness]);
|
||||||
|
|
||||||
|
const items = usePlateItems(plates);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{jsxTree(node, {
|
<Batch items={items} material={excludeMaterial} renderOrder={3} />
|
||||||
group(_, props, key) {
|
<Batch items={items} material={material} renderOrder={4} />
|
||||||
return (
|
|
||||||
<group {...props} key={`${key}-spaced-sub-external-exclude`} />
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
mesh(_, props, key) {
|
|
||||||
return (
|
|
||||||
<mesh
|
|
||||||
{...props}
|
|
||||||
onClick={() => {}}
|
|
||||||
key={`${key}-spaced-sub-external-exclude`}
|
|
||||||
renderOrder={3}
|
|
||||||
material={
|
|
||||||
new MeshBasicMaterial({
|
|
||||||
colorWrite: false,
|
|
||||||
depthTest: true,
|
|
||||||
depthWrite: true,
|
|
||||||
...(clip ? { clippingPlanes: [clip] } : {}),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
userData={
|
|
||||||
{
|
|
||||||
type: ArmorType.External,
|
|
||||||
thickness,
|
|
||||||
variant,
|
|
||||||
} satisfies ArmorUserData
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
})}
|
|
||||||
|
|
||||||
{jsxTree(node, {
|
|
||||||
group(_, props, key) {
|
|
||||||
return (
|
|
||||||
<group {...props} key={`${key}-spaced-sub-external-include`} />
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
mesh(_, props, key) {
|
|
||||||
return (
|
|
||||||
<mesh
|
|
||||||
{...props}
|
|
||||||
key={`${key}-spaced-sub-external-include`}
|
|
||||||
renderOrder={4}
|
|
||||||
material={material}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
})}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
+2
@@ -1,8 +1,10 @@
|
|||||||
precision mediump float;
|
precision mediump float;
|
||||||
|
|
||||||
|
#include <batching_pars_vertex>
|
||||||
#include <clipping_planes_pars_vertex>
|
#include <clipping_planes_pars_vertex>
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
#include <batching_vertex>
|
||||||
#include <begin_vertex>
|
#include <begin_vertex>
|
||||||
#include <project_vertex>
|
#include <project_vertex>
|
||||||
#include <clipping_planes_vertex>
|
#include <clipping_planes_vertex>
|
||||||
|
|||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
import { MeshBasicMaterial } from "three";
|
||||||
|
import type { ArmorPlate } from "../../../../../../hooks/useArmorPlates";
|
||||||
|
import { usePlateItems } from "../../../../../../hooks/usePlateItems";
|
||||||
|
import { Batch } from "../../../../../Batch";
|
||||||
|
|
||||||
|
interface SpacedArmorSubPrimaryProps {
|
||||||
|
plates: ArmorPlate[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const omitMaterial = new MeshBasicMaterial({
|
||||||
|
colorWrite: false,
|
||||||
|
depthWrite: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export function SpacedArmorSubPrimary({ plates }: SpacedArmorSubPrimaryProps) {
|
||||||
|
const items = usePlateItems(plates);
|
||||||
|
|
||||||
|
return <Batch items={items} material={omitMaterial} renderOrder={0} />;
|
||||||
|
}
|
||||||
+21
-62
@@ -1,24 +1,18 @@
|
|||||||
import { isExplosive, resolvePenetrationCoefficient } from "@blitzkit/core";
|
import { isExplosive, resolvePenetrationCoefficient } from "@blitzkit/core";
|
||||||
import { useEffect } from "react";
|
import { useEffect, useMemo } from "react";
|
||||||
import {
|
import { AdditiveBlending, MeshBasicMaterial, ShaderMaterial } from "three";
|
||||||
AdditiveBlending,
|
|
||||||
MeshBasicMaterial,
|
|
||||||
ShaderMaterial,
|
|
||||||
type Object3D,
|
|
||||||
} from "three";
|
|
||||||
import { degToRad } from "three/src/math/MathUtils.js";
|
import { degToRad } from "three/src/math/MathUtils.js";
|
||||||
import type { ArmorUserData } from "../..";
|
|
||||||
import { hasEquipment } from "../../../../../../core/blitzkit/hasEquipment";
|
import { hasEquipment } from "../../../../../../core/blitzkit/hasEquipment";
|
||||||
import { jsxTree } from "../../../../../../core/blitzkit/jsxTree";
|
import type { ArmorPlate } from "../../../../../../hooks/useArmorPlates";
|
||||||
|
import { usePlateItems } from "../../../../../../hooks/usePlateItems";
|
||||||
import { Duel } from "../../../../../../stores/duel";
|
import { Duel } from "../../../../../../stores/duel";
|
||||||
import { Tankopedia } from "../../../../../../stores/tankopedia";
|
import { Tankopedia } from "../../../../../../stores/tankopedia";
|
||||||
import { ArmorType } from "../../../SpacedArmorScene";
|
import { Batch } from "../../../../../Batch";
|
||||||
import fragmentShader from "./shaders/fragment.glsl?raw";
|
import fragmentShader from "./shaders/fragment.glsl?raw";
|
||||||
import vertexShader from "./shaders/vertex.glsl?raw";
|
import vertexShader from "./shaders/vertex.glsl?raw";
|
||||||
|
|
||||||
interface SpacedArmorSubSpacedProps {
|
interface SpacedArmorSubSpacedProps {
|
||||||
node: Object3D;
|
plates: ArmorPlate[];
|
||||||
thickness: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const depthWriteMaterial = new MeshBasicMaterial({
|
const depthWriteMaterial = new MeshBasicMaterial({
|
||||||
@@ -26,11 +20,10 @@ const depthWriteMaterial = new MeshBasicMaterial({
|
|||||||
colorWrite: false,
|
colorWrite: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
export function SpacedArmorSubSpaced({
|
export function SpacedArmorSubSpaced({ plates }: SpacedArmorSubSpacedProps) {
|
||||||
node,
|
const material = useMemo(
|
||||||
thickness,
|
() =>
|
||||||
}: SpacedArmorSubSpacedProps) {
|
new ShaderMaterial({
|
||||||
const material = new ShaderMaterial({
|
|
||||||
fragmentShader,
|
fragmentShader,
|
||||||
vertexShader,
|
vertexShader,
|
||||||
|
|
||||||
@@ -39,13 +32,15 @@ export function SpacedArmorSubSpaced({
|
|||||||
blending: AdditiveBlending,
|
blending: AdditiveBlending,
|
||||||
|
|
||||||
uniforms: {
|
uniforms: {
|
||||||
thickness: { value: null },
|
thicknessCoefficient: { value: null },
|
||||||
penetration: { value: null },
|
penetration: { value: null },
|
||||||
caliber: { value: null },
|
caliber: { value: null },
|
||||||
ricochet: { value: null },
|
ricochet: { value: null },
|
||||||
normalization: { value: null },
|
normalization: { value: null },
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function handleShellChange() {
|
function handleShellChange() {
|
||||||
@@ -74,8 +69,8 @@ export function SpacedArmorSubSpaced({
|
|||||||
? Duel.state.protagonist.tank.equalizer?.armor
|
? Duel.state.protagonist.tank.equalizer?.armor
|
||||||
: undefined) ?? 1;
|
: undefined) ?? 1;
|
||||||
|
|
||||||
material.uniforms.thickness.value =
|
material.uniforms.thicknessCoefficient.value =
|
||||||
thickness * (hasEnhancedArmor ? 1.04 : 1) * equalizer;
|
(hasEnhancedArmor ? 1.04 : 1) * equalizer;
|
||||||
}
|
}
|
||||||
function handleAntagonistEquipmentChange() {
|
function handleAntagonistEquipmentChange() {
|
||||||
const equipment = Duel.state.antagonist.equipmentMatrix;
|
const equipment = Duel.state.antagonist.equipmentMatrix;
|
||||||
@@ -131,50 +126,14 @@ export function SpacedArmorSubSpaced({
|
|||||||
return () => {
|
return () => {
|
||||||
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
||||||
};
|
};
|
||||||
});
|
}, [material]);
|
||||||
|
|
||||||
|
const items = usePlateItems(plates);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{jsxTree(node, {
|
<Batch items={items} material={material} renderOrder={2} />
|
||||||
group(_, props, key) {
|
<Batch items={items} material={depthWriteMaterial} renderOrder={5} />
|
||||||
return <group {...props} key={`${key}-spaced-sub-spaced-exclude`} />;
|
|
||||||
},
|
|
||||||
|
|
||||||
mesh(_, props, key) {
|
|
||||||
return (
|
|
||||||
<mesh
|
|
||||||
{...props}
|
|
||||||
key={`${key}-spaced-sub-spaced-exclude`}
|
|
||||||
renderOrder={2}
|
|
||||||
material={material}
|
|
||||||
onClick={() => {}}
|
|
||||||
userData={
|
|
||||||
{
|
|
||||||
type: ArmorType.Spaced,
|
|
||||||
thickness,
|
|
||||||
} satisfies ArmorUserData
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
})}
|
|
||||||
|
|
||||||
{jsxTree(node, {
|
|
||||||
group(_, props, key) {
|
|
||||||
return <group {...props} key={`${key}-spaced-sub-spaced-include`} />;
|
|
||||||
},
|
|
||||||
|
|
||||||
mesh(_, props, key) {
|
|
||||||
return (
|
|
||||||
<mesh
|
|
||||||
{...props}
|
|
||||||
key={`${key}-spaced-sub-spaced-include`}
|
|
||||||
renderOrder={5}
|
|
||||||
material={depthWriteMaterial}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
})}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
+3
-1
@@ -3,13 +3,15 @@ precision mediump float;
|
|||||||
varying vec3 vNormal;
|
varying vec3 vNormal;
|
||||||
varying vec3 vViewPosition;
|
varying vec3 vViewPosition;
|
||||||
|
|
||||||
uniform float thickness;
|
uniform float thicknessCoefficient;
|
||||||
|
varying float vThickness;
|
||||||
uniform float penetration;
|
uniform float penetration;
|
||||||
uniform float caliber;
|
uniform float caliber;
|
||||||
uniform float ricochet;
|
uniform float ricochet;
|
||||||
uniform float normalization;
|
uniform float normalization;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
float thickness = vThickness * thicknessCoefficient;
|
||||||
float angle = acos(dot(vNormal, -vViewPosition) / length(vViewPosition));
|
float angle = acos(dot(vNormal, -vViewPosition) / length(vViewPosition));
|
||||||
|
|
||||||
bool threeCalibersRule = caliber > thickness * 3.0;
|
bool threeCalibersRule = caliber > thickness * 3.0;
|
||||||
|
|||||||
+21
-2
@@ -2,12 +2,31 @@ precision mediump float;
|
|||||||
|
|
||||||
varying vec3 vNormal;
|
varying vec3 vNormal;
|
||||||
varying vec3 vViewPosition;
|
varying vec3 vViewPosition;
|
||||||
|
varying float vThickness;
|
||||||
|
|
||||||
|
#include <batching_pars_vertex>
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 vViewPosition4 = modelViewMatrix * vec4(position, 1.0);
|
vec4 localPosition = vec4(position, 1.0);
|
||||||
|
vec3 localNormal = normal;
|
||||||
|
|
||||||
|
#ifdef USE_BATCHING
|
||||||
|
#include <batching_vertex>
|
||||||
|
|
||||||
|
localPosition = batchingMatrix * localPosition;
|
||||||
|
localNormal = normalize(mat3(batchingMatrix) * localNormal);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_BATCHING_COLOR
|
||||||
|
vThickness = getBatchingColor(getIndirectIndex(gl_DrawID)).r;
|
||||||
|
#else
|
||||||
|
vThickness = 0.0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
vec4 vViewPosition4 = modelViewMatrix * localPosition;
|
||||||
|
|
||||||
vViewPosition = vViewPosition4.xyz;
|
vViewPosition = vViewPosition4.xyz;
|
||||||
vNormal = normalMatrix * normal;
|
vNormal = normalMatrix * localNormal;
|
||||||
|
|
||||||
gl_Position = projectionMatrix * vViewPosition4;
|
gl_Position = projectionMatrix * vViewPosition4;
|
||||||
}
|
}
|
||||||
+30
-45
@@ -4,7 +4,7 @@ import {
|
|||||||
resolvePenetrationCoefficient,
|
resolvePenetrationCoefficient,
|
||||||
} from "@blitzkit/core";
|
} from "@blitzkit/core";
|
||||||
import { useThree } from "@react-three/fiber";
|
import { useThree } from "@react-three/fiber";
|
||||||
import { useCallback } from "react";
|
import { useCallback, useMemo } from "react";
|
||||||
import {
|
import {
|
||||||
type Intersection,
|
type Intersection,
|
||||||
MeshBasicMaterial,
|
MeshBasicMaterial,
|
||||||
@@ -32,8 +32,6 @@ import {
|
|||||||
Tankopedia,
|
Tankopedia,
|
||||||
} from "../../../../stores/tankopedia";
|
} from "../../../../stores/tankopedia";
|
||||||
import { ArmorType } from "../SpacedArmorScene";
|
import { ArmorType } from "../SpacedArmorScene";
|
||||||
import { SpacedArmorSubExternal } from "./components/SpacedArmorSubExternal";
|
|
||||||
import { SpacedArmorSubSpaced } from "./components/SpacedArmorSubSpaced";
|
|
||||||
|
|
||||||
type SpacedArmorSceneComponentProps = {
|
type SpacedArmorSceneComponentProps = {
|
||||||
node: Object3D;
|
node: Object3D;
|
||||||
@@ -93,12 +91,14 @@ function resolvePenetrationChance(shoot: (scale: number) => Shot | null) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const omitMaterial = new MeshBasicMaterial({
|
const proxyMaterial = new MeshBasicMaterial({ visible: false });
|
||||||
colorWrite: false,
|
|
||||||
depthWrite: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Plates are drawn in batches by SpacedArmorSubPrimary, SpacedArmorSubSpaced
|
||||||
|
* and SpacedArmorSubExternal; this only renders invisible per plate proxies
|
||||||
|
* so raycasts still resolve each plate's userData. Raycasting ignores
|
||||||
|
* material visibility.
|
||||||
|
*
|
||||||
* Render orders allocations:
|
* Render orders allocations:
|
||||||
* - 0 : core omission
|
* - 0 : core omission
|
||||||
* - 1-2: spaced without depth buffer write but do test to allow additive blending
|
* - 1-2: spaced without depth buffer write but do test to allow additive blending
|
||||||
@@ -433,36 +433,36 @@ export function SpacedArmorSceneComponent({
|
|||||||
[camera],
|
[camera],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
const clippedProxyMaterial = useMemo(
|
||||||
<>
|
() =>
|
||||||
{props.type === ArmorType.Primary &&
|
clip
|
||||||
jsxTree(node, {
|
? new MeshBasicMaterial({ visible: false, clippingPlanes: [clip] })
|
||||||
mesh(_, props, key) {
|
: proxyMaterial,
|
||||||
|
[clip],
|
||||||
|
);
|
||||||
|
const userData: ArmorUserData =
|
||||||
|
props.type === ArmorType.External
|
||||||
|
? { type: ArmorType.External, thickness, variant: props.variant }
|
||||||
|
: { type: props.type, thickness };
|
||||||
|
|
||||||
|
return jsxTree(node, {
|
||||||
|
mesh(_, meshProps, key) {
|
||||||
return (
|
return (
|
||||||
<mesh
|
<mesh
|
||||||
{...props}
|
{...meshProps}
|
||||||
key={`${key}-spaced-component`}
|
key={`${key}-proxy`}
|
||||||
renderOrder={0}
|
material={clippedProxyMaterial}
|
||||||
material={omitMaterial}
|
userData={userData}
|
||||||
userData={
|
|
||||||
{
|
|
||||||
type: ArmorType.Primary,
|
|
||||||
thickness,
|
|
||||||
} satisfies ArmorUserData
|
|
||||||
}
|
|
||||||
onClick={(event) => {
|
onClick={(event) => {
|
||||||
|
// other layers only need to be in the intersections
|
||||||
|
if (props.type !== ArmorType.Primary) return;
|
||||||
|
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
|
|
||||||
const shot = shoot(event.point, event.intersections, true)!;
|
const shot = shoot(event.point, event.intersections, true)!;
|
||||||
|
|
||||||
shot.penetrationChance = resolvePenetrationChance((scale) =>
|
shot.penetrationChance = resolvePenetrationChance((scale) =>
|
||||||
shoot(
|
shoot(event.point, event.intersections, true, undefined, scale),
|
||||||
event.point,
|
|
||||||
event.intersections,
|
|
||||||
true,
|
|
||||||
undefined,
|
|
||||||
scale,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
Tankopedia.mutate((draft) => {
|
Tankopedia.mutate((draft) => {
|
||||||
@@ -472,20 +472,5 @@ export function SpacedArmorSceneComponent({
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
})}
|
});
|
||||||
|
|
||||||
{props.type === ArmorType.Spaced && (
|
|
||||||
<SpacedArmorSubSpaced node={node} thickness={thickness} />
|
|
||||||
)}
|
|
||||||
|
|
||||||
{props.type === ArmorType.External && (
|
|
||||||
<SpacedArmorSubExternal
|
|
||||||
node={node}
|
|
||||||
thickness={thickness}
|
|
||||||
variant={props.variant}
|
|
||||||
clip={clip}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import { invalidate } from "@react-three/fiber";
|
||||||
|
import { useEffect, useMemo, type ComponentProps } from "react";
|
||||||
|
import { BatchedMesh, Color, type Matrix4, type Mesh } from "three";
|
||||||
|
|
||||||
|
export interface BatchItem {
|
||||||
|
mesh: Mesh;
|
||||||
|
matrix: Matrix4;
|
||||||
|
// red channel of the instance color, read in shaders with getBatchingColor
|
||||||
|
thickness?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
type BatchProps = Omit<ComponentProps<"primitive">, "object"> & {
|
||||||
|
items: BatchItem[];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One draw call for all items. Only the geometry is memoized, on the identity
|
||||||
|
* of items; material, renderOrder, shadows, events and so on are plain props.
|
||||||
|
*/
|
||||||
|
export function Batch({ items, ...props }: BatchProps) {
|
||||||
|
const batch = useMemo(() => {
|
||||||
|
if (items.length === 0) return null;
|
||||||
|
|
||||||
|
let vertices = 0;
|
||||||
|
let indices = 0;
|
||||||
|
|
||||||
|
for (const { mesh } of items) {
|
||||||
|
vertices += mesh.geometry.attributes.position.count;
|
||||||
|
indices += mesh.geometry.index!.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
const batch = new BatchedMesh(items.length, vertices, indices);
|
||||||
|
const color = new Color();
|
||||||
|
|
||||||
|
for (const { mesh, matrix, thickness } of items) {
|
||||||
|
const instanceId = batch.addInstance(batch.addGeometry(mesh.geometry));
|
||||||
|
|
||||||
|
batch.setMatrixAt(instanceId, matrix);
|
||||||
|
|
||||||
|
if (thickness !== undefined) {
|
||||||
|
batch.setColorAt(instanceId, color.setRGB(thickness, 0, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return batch;
|
||||||
|
}, [items]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!batch) return;
|
||||||
|
|
||||||
|
invalidate();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
batch.dispose();
|
||||||
|
};
|
||||||
|
}, [batch]);
|
||||||
|
|
||||||
|
if (!batch) return null;
|
||||||
|
|
||||||
|
return <primitive object={batch} {...props} />;
|
||||||
|
}
|
||||||
@@ -1,19 +1,14 @@
|
|||||||
import {
|
import { invalidate, useThree, type ThreeEvent } from "@react-three/fiber";
|
||||||
invalidate,
|
|
||||||
useThree,
|
|
||||||
type ObjectMap,
|
|
||||||
type ThreeEvent,
|
|
||||||
} from "@react-three/fiber";
|
|
||||||
import type { QuicklimeEvent } from "quicklime";
|
import type { QuicklimeEvent } from "quicklime";
|
||||||
import { useRef } from "react";
|
import { useEffect, useMemo, useRef } from "react";
|
||||||
import type { Texture } from "three";
|
import type { Object3D, Texture } from "three";
|
||||||
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";
|
||||||
import { modelTransformEvent } from "../core/blitzkit/modelTransform";
|
import { modelTransformEvent } from "../core/blitzkit/modelTransform";
|
||||||
import { controlsEnabledEvent } from "../core/controlsEnabled";
|
import { controlsEnabledEvent } from "../core/controlsEnabled";
|
||||||
import { useBatched } from "../hooks/useBatched";
|
import { useBatchGroups } from "../hooks/useBatchGroups";
|
||||||
import { useModel } from "../hooks/useModel";
|
import { useModelNodes } from "../hooks/useModelNodes";
|
||||||
import { useSkin, type Skin } from "../hooks/useSkin";
|
import { useSkin, type Skin } from "../hooks/useSkin";
|
||||||
import { useSkinAnimation } from "../hooks/useSkinAnimation";
|
import { useSkinAnimation } from "../hooks/useSkinAnimation";
|
||||||
import { useTankModelDefinition } from "../hooks/useTankModelDefinition";
|
import { useTankModelDefinition } from "../hooks/useTankModelDefinition";
|
||||||
@@ -22,17 +17,24 @@ import { Duel } from "../stores/duel";
|
|||||||
import { Tankopedia } from "../stores/tankopedia";
|
import { Tankopedia } from "../stores/tankopedia";
|
||||||
import { TankopediaDisplay } from "../stores/tankopediaPersistent/constants";
|
import { TankopediaDisplay } from "../stores/tankopediaPersistent/constants";
|
||||||
import { ModelTankWrapper } from "./Armor/components/ModelTankWrapper";
|
import { ModelTankWrapper } from "./Armor/components/ModelTankWrapper";
|
||||||
|
import { Batch } from "./Batch";
|
||||||
import { transitionEvent } from "./Tankopedia/HeroSection/components/TankSandbox/components/Lighting";
|
import { transitionEvent } from "./Tankopedia/HeroSection/components/TankSandbox/components/Lighting";
|
||||||
|
|
||||||
interface SectionProps {
|
interface SectionProps {
|
||||||
map: ObjectMap;
|
nodes: Object3D[];
|
||||||
skins: Skin[];
|
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
|
// emissive parts fade with the lights instead of glowing in the dark
|
||||||
function fadeEmissive(materials: MeshStandardMaterial[]) {
|
function fadeEmissive(materials: MeshStandardMaterial[]) {
|
||||||
const emissiveMaterials = materials.filter(
|
const emissiveMaterials = materials.filter(
|
||||||
(material) => material.emissiveMap
|
(material) => material.emissiveMap,
|
||||||
);
|
);
|
||||||
|
|
||||||
function applyIntensity(intensity: number) {
|
function applyIntensity(intensity: number) {
|
||||||
@@ -53,22 +55,33 @@ function fadeEmissive(materials: MeshStandardMaterial[]) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function Tracks({ map, skins }: SectionProps) {
|
function Section({ nodes, skins, scrollTracks, yaw, pitch }: 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 { groups, parts, materials } = useBatchGroups(nodes, skins);
|
||||||
|
|
||||||
const position = useRef(new Vector2());
|
const position = useRef(new Vector2());
|
||||||
const delta = useRef(new Vector2());
|
const delta = useRef(new Vector2());
|
||||||
|
const pitchAngle = useRef(0);
|
||||||
|
const yawAngle = useRef(0);
|
||||||
const textures = useRef(new Set<Texture | null>());
|
const textures = useRef(new Set<Texture | null>());
|
||||||
|
|
||||||
|
const canvas = useThree((state) => state.gl.domElement);
|
||||||
|
const rotates = yaw || pitch;
|
||||||
|
|
||||||
function handlePointerMove(event: PointerEvent) {
|
function handlePointerMove(event: PointerEvent) {
|
||||||
delta.current.set(event.clientX, event.clientY).sub(position.current);
|
delta.current.set(event.clientX, event.clientY).sub(position.current);
|
||||||
position.current.set(event.clientX, event.clientY);
|
position.current.set(event.clientX, event.clientY);
|
||||||
|
|
||||||
const deltaX = delta.current.x / window.innerWidth;
|
if (scrollTracks) {
|
||||||
const deltaY = delta.current.y / window.innerHeight;
|
const offset =
|
||||||
|
delta.current.x / window.innerWidth +
|
||||||
const offset = deltaX + deltaY;
|
delta.current.y / window.innerHeight;
|
||||||
|
|
||||||
for (const texture of textures.current) {
|
for (const texture of textures.current) {
|
||||||
if (!texture) continue;
|
if (!texture) continue;
|
||||||
@@ -77,6 +90,40 @@ function Tracks({ map, skins }: SectionProps) {
|
|||||||
|
|
||||||
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() {
|
function handlePointerUp() {
|
||||||
controlsEnabledEvent.dispatch(true);
|
controlsEnabledEvent.dispatch(true);
|
||||||
|
|
||||||
@@ -84,10 +131,16 @@ function Tracks({ map, skins }: SectionProps) {
|
|||||||
window.removeEventListener("pointerup", handlePointerUp);
|
window.removeEventListener("pointerup", handlePointerUp);
|
||||||
}
|
}
|
||||||
function handlePointerDown(event: ThreeEvent<PointerEvent>) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
event.stopPropagation();
|
||||||
|
|
||||||
|
if (!scrollTracks && !rotates) return;
|
||||||
|
|
||||||
|
if (scrollTracks) {
|
||||||
const material = (event.object as Mesh).material as MeshStandardMaterial;
|
const material = (event.object as Mesh).material as MeshStandardMaterial;
|
||||||
|
|
||||||
textures.current = new Set([
|
textures.current = new Set([
|
||||||
@@ -97,205 +150,45 @@ function Tracks({ map, skins }: SectionProps) {
|
|||||||
material.roughnessMap,
|
material.roughnessMap,
|
||||||
material.metalnessMap,
|
material.metalnessMap,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
position.current.set(event.clientX, event.clientY);
|
|
||||||
event.stopPropagation();
|
|
||||||
|
|
||||||
controlsEnabledEvent.dispatch(false);
|
|
||||||
|
|
||||||
window.addEventListener("pointermove", handlePointerMove);
|
|
||||||
window.addEventListener("pointerup", handlePointerUp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
if (rotates) {
|
||||||
<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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Batched
|
|
||||||
onMaterials={fadeEmissive}
|
|
||||||
onPointerDown={handlePointerDown}
|
|
||||||
slicer={(node) => {
|
|
||||||
const isHull = node.name === "hull";
|
|
||||||
const isWheel = node.name.startsWith("chassis_wheel_");
|
|
||||||
|
|
||||||
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) => {
|
Tankopedia.mutate((draft) => {
|
||||||
draft.shot = undefined;
|
draft.shot = undefined;
|
||||||
draft.highlightArmor = undefined;
|
draft.highlightArmor = undefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
pitchAngle.current = modelTransformEvent.last!.pitch;
|
||||||
|
yawAngle.current = modelTransformEvent.last!.yaw;
|
||||||
|
}
|
||||||
|
|
||||||
position.current.set(event.clientX, event.clientY);
|
position.current.set(event.clientX, event.clientY);
|
||||||
pitch.current = modelTransformEvent.last!.pitch;
|
controlsEnabledEvent.dispatch(false);
|
||||||
yaw.current = modelTransformEvent.last!.yaw;
|
|
||||||
|
|
||||||
window.addEventListener("pointermove", handlePointerMove);
|
window.addEventListener("pointermove", handlePointerMove);
|
||||||
window.addEventListener("pointerup", handlePointerUp);
|
window.addEventListener("pointerup", handlePointerUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
useEffect(() => fadeEmissive(materials), [materials]);
|
||||||
<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;
|
return (
|
||||||
}}
|
<group onPointerDown={handlePointerDown}>
|
||||||
|
{groups.map(({ material, items }) => (
|
||||||
|
<Batch
|
||||||
|
key={material.uuid}
|
||||||
|
items={items}
|
||||||
|
material={material}
|
||||||
|
castShadow
|
||||||
|
receiveShadow
|
||||||
/>
|
/>
|
||||||
|
))}
|
||||||
|
|
||||||
|
{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 turretContainer = useRef<Group>(null!);
|
||||||
const gunContainer = useRef<Group>(null!);
|
const gunContainer = useRef<Group>(null!);
|
||||||
|
|
||||||
const { gltf } = useModel(protagonist.tank.id);
|
const nodes = useModelNodes();
|
||||||
const skinGltf = useSkin(protagonist.tank.id);
|
const skinGltf = useSkin(protagonist.tank.id);
|
||||||
const attachmentGltf = useSkin(protagonist.tank.id, "attachment");
|
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);
|
useTankTransform(track, turret, turretContainer, gunContainer);
|
||||||
useSkinAnimation(skins);
|
useSkinAnimation(skins);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ModelTankWrapper ref={hullContainer}>
|
<ModelTankWrapper ref={hullContainer}>
|
||||||
<Tracks map={gltf} skins={skins} />
|
<Section nodes={nodes.tracks} skins={skins} scrollTracks />
|
||||||
<Hull map={gltf} skins={skins} />
|
<Section nodes={hullNodes} skins={skins} />
|
||||||
|
|
||||||
<group ref={turretContainer}>
|
<group ref={turretContainer}>
|
||||||
<Turret map={gltf} skins={skins} />
|
<Section nodes={nodes.turret} skins={skins} yaw />
|
||||||
|
|
||||||
<group ref={gunContainer}>
|
<group ref={gunContainer}>
|
||||||
<Gun map={gltf} skins={skins} />
|
<Section nodes={gunNodes} skins={skins} yaw pitch />
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
</ModelTankWrapper>
|
</ModelTankWrapper>
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import { Matrix4, type Object3D } from "three";
|
||||||
|
|
||||||
|
export interface FlatNode {
|
||||||
|
object: Object3D;
|
||||||
|
// relative to the root's parent, like jsxTree renders it
|
||||||
|
matrix: Matrix4;
|
||||||
|
root: Object3D;
|
||||||
|
}
|
||||||
|
|
||||||
|
function allChildren(object: Object3D) {
|
||||||
|
return object.children;
|
||||||
|
}
|
||||||
|
|
||||||
|
// every object under the roots; children picks which ones to descend into
|
||||||
|
export function flattenNodes(roots: Object3D[], children = allChildren) {
|
||||||
|
const flat: FlatNode[] = [];
|
||||||
|
|
||||||
|
function walk(object: Object3D, parent: Matrix4, root: Object3D) {
|
||||||
|
object.updateMatrix();
|
||||||
|
|
||||||
|
const matrix = parent.clone().multiply(object.matrix);
|
||||||
|
|
||||||
|
flat.push({ object, matrix, root });
|
||||||
|
|
||||||
|
for (const child of children(object)) walk(child, matrix, root);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const root of roots) walk(root, new Matrix4(), root);
|
||||||
|
|
||||||
|
return flat;
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import type { Armor } from "@blitzkit/core";
|
||||||
|
import { useMemo } from "react";
|
||||||
|
import type { Object3D } from "three";
|
||||||
|
import { nameToArmorId } from "../core/blitzkit/nameToArmorId";
|
||||||
|
import { resolveArmor } from "../core/blitzkit/resolveThickness";
|
||||||
|
import { Duel } from "../stores/duel";
|
||||||
|
import { useArmor } from "./useArmor";
|
||||||
|
import { useTankModelDefinition } from "./useTankModelDefinition";
|
||||||
|
|
||||||
|
export interface ArmorPlate {
|
||||||
|
node: Object3D;
|
||||||
|
thickness: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SectionPlates {
|
||||||
|
primary: ArmorPlate[];
|
||||||
|
spaced: ArmorPlate[];
|
||||||
|
}
|
||||||
|
|
||||||
|
// the protagonist's hull, turret and gun plates; nodes whose name contains
|
||||||
|
// hiddenState are dynamic armor in the other state
|
||||||
|
export function useArmorPlates(hiddenState: string) {
|
||||||
|
const tank = Duel.use((state) => state.protagonist.tank);
|
||||||
|
const turret = Duel.use((state) => state.protagonist.turret);
|
||||||
|
const gun = Duel.use((state) => state.protagonist.gun);
|
||||||
|
const { gltf } = useArmor(tank.id);
|
||||||
|
const tankModelDefinition = useTankModelDefinition();
|
||||||
|
|
||||||
|
return useMemo(() => {
|
||||||
|
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
||||||
|
const gunModelDefinition = turretModelDefinition.guns[gun.id];
|
||||||
|
const nodes = Object.values(gltf.nodes);
|
||||||
|
|
||||||
|
function platesOf(prefix: string, armor: Armor) {
|
||||||
|
const plates: SectionPlates = { primary: [], spaced: [] };
|
||||||
|
|
||||||
|
for (const node of nodes) {
|
||||||
|
if (!node.name.startsWith(prefix) || node.name.includes(hiddenState)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { spaced, thickness } = resolveArmor(
|
||||||
|
armor,
|
||||||
|
nameToArmorId(node.name),
|
||||||
|
);
|
||||||
|
|
||||||
|
plates[spaced ? "spaced" : "primary"].push({ node, thickness });
|
||||||
|
}
|
||||||
|
|
||||||
|
return plates;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hullPlates = platesOf("hull_", tankModelDefinition.armor!);
|
||||||
|
const turretPlates = platesOf(
|
||||||
|
`turret_${turretModelDefinition.model_id.toString().padStart(2, "0")}`,
|
||||||
|
turretModelDefinition.armor!,
|
||||||
|
);
|
||||||
|
const gunPlates = platesOf(
|
||||||
|
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}`,
|
||||||
|
gunModelDefinition.armor!,
|
||||||
|
);
|
||||||
|
|
||||||
|
return { hull: hullPlates, turret: turretPlates, gun: gunPlates };
|
||||||
|
}, [gltf, tankModelDefinition, turret.id, gun.id, hiddenState]);
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
import { useEffect, useMemo } from "react";
|
||||||
|
import {
|
||||||
|
Mesh,
|
||||||
|
MeshStandardMaterial,
|
||||||
|
type Material,
|
||||||
|
type Matrix4,
|
||||||
|
type Object3D,
|
||||||
|
} from "three";
|
||||||
|
import type { BatchItem } from "../components/Batch";
|
||||||
|
import { prepareMaterial } from "../core/blitzkit/jsxTree";
|
||||||
|
import { nameOf } from "../core/blitzkit/skinTree";
|
||||||
|
import { flattenNodes } from "../core/three/flattenNodes";
|
||||||
|
import type { Skin } from "./useSkin";
|
||||||
|
|
||||||
|
interface BatchGroup {
|
||||||
|
material: MeshStandardMaterial;
|
||||||
|
items: BatchItem[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SkinPart {
|
||||||
|
part: Object3D;
|
||||||
|
matrix: Matrix4;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Model nodes grouped into one batch per material. Skins are layered in order,
|
||||||
|
* so an attachment's states win over the camouflage's. Their parts aren't
|
||||||
|
* batched so their bones can animate.
|
||||||
|
*/
|
||||||
|
export function useBatchGroups(nodes: Object3D[], skins: Skin[]) {
|
||||||
|
const batchGroups = useMemo(() => {
|
||||||
|
const skinMaterials = new Map<string, Material>();
|
||||||
|
const states: Record<string, string | null> = {};
|
||||||
|
|
||||||
|
for (const skin of skins) {
|
||||||
|
for (const [name, material] of skin.materials) {
|
||||||
|
skinMaterials.set(name, material);
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(states, skin.scene.userData.states);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stateChildren(object: Object3D) {
|
||||||
|
if (object.userData.state === undefined) return object.children;
|
||||||
|
|
||||||
|
const name = nameOf(object);
|
||||||
|
const state = name in states ? states[name] : object.userData.state;
|
||||||
|
|
||||||
|
return object.children.filter((child) => nameOf(child) === state);
|
||||||
|
}
|
||||||
|
|
||||||
|
const groups = new Map<Material, BatchGroup>();
|
||||||
|
const parts: SkinPart[] = [];
|
||||||
|
|
||||||
|
for (const { object, matrix } of flattenNodes(nodes, stateChildren)) {
|
||||||
|
for (const skin of skins) {
|
||||||
|
for (const part of skin.parts.get(object.name) ?? []) {
|
||||||
|
parts.push({ part, matrix });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(object instanceof Mesh)) continue;
|
||||||
|
|
||||||
|
const source =
|
||||||
|
skinMaterials.get((object.material as Material).name) ??
|
||||||
|
object.material;
|
||||||
|
|
||||||
|
if (!(source instanceof MeshStandardMaterial)) continue;
|
||||||
|
|
||||||
|
let group = groups.get(source);
|
||||||
|
|
||||||
|
if (!group) {
|
||||||
|
const material = source.clone();
|
||||||
|
|
||||||
|
prepareMaterial(material);
|
||||||
|
group = { material, items: [] };
|
||||||
|
groups.set(source, group);
|
||||||
|
}
|
||||||
|
|
||||||
|
group.items.push({ mesh: object, matrix });
|
||||||
|
}
|
||||||
|
|
||||||
|
const materials = [...groups.values()].map(({ material }) => material);
|
||||||
|
|
||||||
|
for (const { part } of parts) {
|
||||||
|
part.traverse((object) => {
|
||||||
|
if (
|
||||||
|
object instanceof Mesh &&
|
||||||
|
object.material instanceof MeshStandardMaterial
|
||||||
|
) {
|
||||||
|
materials.push(object.material);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { groups: [...groups.values()], parts, materials };
|
||||||
|
}, [nodes, skins]);
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() => () => {
|
||||||
|
for (const { material } of batchGroups.groups) material.dispose();
|
||||||
|
},
|
||||||
|
[batchGroups],
|
||||||
|
);
|
||||||
|
|
||||||
|
return batchGroups;
|
||||||
|
}
|
||||||
@@ -1,168 +0,0 @@
|
|||||||
import type { ObjectMap } from "@react-three/fiber";
|
|
||||||
import { useEffect, type ComponentProps } from "react";
|
|
||||||
import {
|
|
||||||
BatchedMesh,
|
|
||||||
Group,
|
|
||||||
Material,
|
|
||||||
Mesh,
|
|
||||||
MeshStandardMaterial,
|
|
||||||
type Object3D,
|
|
||||||
} from "three";
|
|
||||||
import { prepareMaterial } from "../core/blitzkit/jsxTree";
|
|
||||||
import { nameOf } from "../core/blitzkit/skinTree";
|
|
||||||
import type { Skin } from "./useSkin";
|
|
||||||
|
|
||||||
interface BatchedProps extends ComponentProps<"group"> {
|
|
||||||
slicer: (node: Object3D) => boolean;
|
|
||||||
mutator?: (mesh: Mesh) => void;
|
|
||||||
onMaterials?: (materials: MeshStandardMaterial[]) => (() => void) | void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* skins are layered in order, so an attachment's states win over the
|
|
||||||
* camouflage's; their parts render as they are so their bones can animate
|
|
||||||
*/
|
|
||||||
export function useBatched(
|
|
||||||
map: ObjectMap,
|
|
||||||
shadows = false,
|
|
||||||
skins: Skin[] = []
|
|
||||||
) {
|
|
||||||
return function Batched({
|
|
||||||
slicer,
|
|
||||||
mutator,
|
|
||||||
onMaterials,
|
|
||||||
...props
|
|
||||||
}: BatchedProps) {
|
|
||||||
const skinMaterials = new Map<string, Material>();
|
|
||||||
const states: Record<string, string | null> = {};
|
|
||||||
|
|
||||||
for (const skin of skins) {
|
|
||||||
for (const [name, material] of skin.materials) {
|
|
||||||
skinMaterials.set(name, material);
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.assign(states, skin.scene.userData.states);
|
|
||||||
}
|
|
||||||
|
|
||||||
const groups = new Map<
|
|
||||||
MeshStandardMaterial,
|
|
||||||
{ clonedMaterial: MeshStandardMaterial; meshes: Mesh[] }
|
|
||||||
>();
|
|
||||||
const parts: { target: Object3D; part: Object3D }[] = [];
|
|
||||||
|
|
||||||
function walk(object: Object3D) {
|
|
||||||
for (const skin of skins) {
|
|
||||||
for (const part of skin.parts.get(object.name) ?? []) {
|
|
||||||
parts.push({ target: object, part });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (object instanceof Mesh) {
|
|
||||||
const originalMaterial =
|
|
||||||
skinMaterials.get((object.material as Material).name) ??
|
|
||||||
object.material;
|
|
||||||
|
|
||||||
if (originalMaterial instanceof MeshStandardMaterial) {
|
|
||||||
let group = groups.get(originalMaterial);
|
|
||||||
|
|
||||||
if (!group) {
|
|
||||||
const clonedMaterial = originalMaterial.clone();
|
|
||||||
|
|
||||||
prepareMaterial(clonedMaterial);
|
|
||||||
group = { clonedMaterial, meshes: [] };
|
|
||||||
groups.set(originalMaterial, group);
|
|
||||||
}
|
|
||||||
|
|
||||||
group.meshes.push(object);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let children = object.children;
|
|
||||||
|
|
||||||
if (object.userData.state !== undefined) {
|
|
||||||
const name = nameOf(object);
|
|
||||||
const state = name in states ? states[name] : object.userData.state;
|
|
||||||
|
|
||||||
children = children.filter((child) => nameOf(child) === state);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const child of children) walk(child);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const node of Object.values(map.nodes)) {
|
|
||||||
if (slicer(node)) walk(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
const wrapper = new Group();
|
|
||||||
|
|
||||||
for (const { clonedMaterial: material, meshes } of groups.values()) {
|
|
||||||
let vertices = 0;
|
|
||||||
let indices = 0;
|
|
||||||
|
|
||||||
for (const mesh of meshes) {
|
|
||||||
vertices += mesh.geometry.attributes.position.count;
|
|
||||||
indices += mesh.geometry.index!.count;
|
|
||||||
}
|
|
||||||
|
|
||||||
const batched = new BatchedMesh(
|
|
||||||
meshes.length,
|
|
||||||
vertices,
|
|
||||||
indices,
|
|
||||||
material
|
|
||||||
);
|
|
||||||
|
|
||||||
for (const mesh of meshes) {
|
|
||||||
const geometryId = batched.addGeometry(mesh.geometry);
|
|
||||||
const instanceId = batched.addInstance(geometryId);
|
|
||||||
|
|
||||||
mesh.updateWorldMatrix(true, false);
|
|
||||||
batched.setMatrixAt(instanceId, mesh.matrixWorld);
|
|
||||||
|
|
||||||
if (shadows) {
|
|
||||||
batched.castShadow = true;
|
|
||||||
batched.receiveShadow = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
mutator?.(mesh);
|
|
||||||
}
|
|
||||||
|
|
||||||
wrapper.add(batched);
|
|
||||||
}
|
|
||||||
|
|
||||||
const materials = [...groups.values()].map(
|
|
||||||
({ clonedMaterial }) => clonedMaterial
|
|
||||||
);
|
|
||||||
|
|
||||||
for (const { part } of parts) {
|
|
||||||
part.traverse((object) => {
|
|
||||||
if (
|
|
||||||
object instanceof Mesh &&
|
|
||||||
object.material instanceof MeshStandardMaterial
|
|
||||||
) {
|
|
||||||
materials.push(object.material);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => onMaterials?.(materials));
|
|
||||||
|
|
||||||
return (
|
|
||||||
<group {...props}>
|
|
||||||
<primitive object={wrapper} />
|
|
||||||
{parts.map(({ target, part }) => {
|
|
||||||
target.updateWorldMatrix(true, false);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<group
|
|
||||||
key={part.uuid}
|
|
||||||
matrix={target.matrixWorld}
|
|
||||||
matrixAutoUpdate={false}
|
|
||||||
>
|
|
||||||
<primitive object={part} />
|
|
||||||
</group>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</group>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import { useMemo } from "react";
|
||||||
|
import type { Object3D } from "three";
|
||||||
|
import { Duel } from "../stores/duel";
|
||||||
|
import { useModel } from "./useModel";
|
||||||
|
import { useTankModelDefinition } from "./useTankModelDefinition";
|
||||||
|
|
||||||
|
interface ModelNodes {
|
||||||
|
hull: Object3D[];
|
||||||
|
wheels: Object3D[];
|
||||||
|
tracks: Object3D[];
|
||||||
|
turret: Object3D[];
|
||||||
|
gun: Object3D[];
|
||||||
|
mask: Object3D[];
|
||||||
|
}
|
||||||
|
|
||||||
|
// the protagonist's visual model split into its parts
|
||||||
|
export function useModelNodes() {
|
||||||
|
const tank = Duel.use((state) => state.protagonist.tank);
|
||||||
|
const turret = Duel.use((state) => state.protagonist.turret);
|
||||||
|
const gun = Duel.use((state) => state.protagonist.gun);
|
||||||
|
const { gltf } = useModel(tank.id);
|
||||||
|
const tankModelDefinition = useTankModelDefinition();
|
||||||
|
|
||||||
|
return useMemo(() => {
|
||||||
|
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
||||||
|
const gunModelDefinition = turretModelDefinition.guns[gun.id];
|
||||||
|
const turretName = `turret_${turretModelDefinition.model_id
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0")}`;
|
||||||
|
const gunName = `gun_${gunModelDefinition.model_id
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0")}`;
|
||||||
|
const maskName = `${gunName}_mask`;
|
||||||
|
const nodes: ModelNodes = {
|
||||||
|
hull: [],
|
||||||
|
wheels: [],
|
||||||
|
tracks: [],
|
||||||
|
turret: [],
|
||||||
|
gun: [],
|
||||||
|
mask: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const node of Object.values(gltf.nodes)) {
|
||||||
|
switch (node.name) {
|
||||||
|
case "hull":
|
||||||
|
nodes.hull.push(node);
|
||||||
|
continue;
|
||||||
|
|
||||||
|
case turretName:
|
||||||
|
nodes.turret.push(node);
|
||||||
|
continue;
|
||||||
|
|
||||||
|
case gunName:
|
||||||
|
nodes.gun.push(node);
|
||||||
|
continue;
|
||||||
|
|
||||||
|
case maskName:
|
||||||
|
nodes.mask.push(node);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.name.startsWith("chassis_wheel_")) {
|
||||||
|
nodes.wheels.push(node);
|
||||||
|
} else if (node.name.startsWith("chassis_track_")) {
|
||||||
|
nodes.tracks.push(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes;
|
||||||
|
}, [gltf, tankModelDefinition, turret.id, gun.id]);
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { useMemo } from "react";
|
||||||
|
import { Mesh } from "three";
|
||||||
|
import type { BatchItem } from "../components/Batch";
|
||||||
|
import { flattenNodes } from "../core/three/flattenNodes";
|
||||||
|
import type { ArmorPlate } from "./useArmorPlates";
|
||||||
|
|
||||||
|
// batch items for armor plates, each carrying its plate's thickness
|
||||||
|
export function usePlateItems(plates: ArmorPlate[]) {
|
||||||
|
return useMemo(() => {
|
||||||
|
const thicknesses = new Map(
|
||||||
|
plates.map(({ node, thickness }) => [node, thickness]),
|
||||||
|
);
|
||||||
|
|
||||||
|
return flattenNodes(plates.map(({ node }) => node)).flatMap(
|
||||||
|
({ object, matrix, root }) =>
|
||||||
|
object instanceof Mesh
|
||||||
|
? [
|
||||||
|
{
|
||||||
|
mesh: object,
|
||||||
|
matrix,
|
||||||
|
thickness: thicknesses.get(root)!,
|
||||||
|
} satisfies BatchItem,
|
||||||
|
]
|
||||||
|
: [],
|
||||||
|
);
|
||||||
|
}, [plates]);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user