mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-12-01 13:55:01 +01:00
Merge branch 'main' into data-driven
This commit is contained in:
34 files changed
+986
-992
No files matched your search
@@ -16,9 +16,11 @@ Pages that are not migrated yet are named `_index.astro` so Astro skips them. Mi
|
||||
|
||||
- Components live flat at `src/components/Name/index.tsx` with `index.module.css` beside it. No flat `Name.tsx` files and no nested `components/Parent/components/Child` folders: a sub-component becomes its own top-level folder prefixed with its feature, e.g. `MixerScene/components/Model.tsx` becomes `components/MixerModel/index.tsx`.
|
||||
- Pages keep their styles in `_index.module.css` next to `_index.tsx`.
|
||||
- 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`.
|
||||
- No return type annotations. Put `satisfies Type` on the returned expression instead.
|
||||
- Wrap functions defined inside React components in `useCallback`.
|
||||
- Never inline a complex type (a union of object shapes, etc.) in a signature; name it as an interface or type.
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
# BlitzKit 1.33.1
|
||||
|
||||
Thank you to [HitJack](https://github.com/Hitjack007) for your contributions this update.
|
||||
|
||||
## Features
|
||||
|
||||
- Legendary camouflages in Tankopedia are now animated and loop their garage animations [HitJack]
|
||||
- Tankopedia's 3D viewer is now much faster as tank models, armor, and legendary camouflages are drawn in batches
|
||||
- Shadows are now only redrawn when something in the scene changes instead of every frame, saving battery and GPU time
|
||||
|
||||
## Fixes
|
||||
|
||||
- Unused parts of legendary camouflages are now hidden, fixing camouflages like the Maus' [HitJack]
|
||||
- Legendary camouflage parts with no LODs are no longer missing, like the Tiger II's cat and the BDT-5A's engine rings [HitJack]
|
||||
- The ground in Tankopedia is no longer transparent, so it fades out cleanly instead of blending with things behind it
|
||||
|
||||
## Technical Changes
|
||||
|
||||
- Tank and legendary camouflage models build much faster thanks to a faster DDS decoder and native CRC32 checksums [HitJack]
|
||||
- The game client is now downloaded from Wargaming Game Center by default
|
||||
- Added a zip-backed virtual file system for reading game files
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "blitzkit",
|
||||
"version": "1.33.0",
|
||||
"version": "1.33.1",
|
||||
"license": "BlitzKit License",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import { invalidate } from "@react-three/fiber";
|
||||
import { useEffect, useMemo, type ComponentProps } from "react";
|
||||
import { BatchedMesh, Color, type Matrix4, type Mesh } from "three";
|
||||
import { sceneChangeEvent } from "../../core/three/sceneChange";
|
||||
|
||||
export interface BatchItem {
|
||||
mesh: Mesh;
|
||||
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();
|
||||
|
||||
// otherwise the draw list is re-culled, re-sorted and re-uploaded every draw
|
||||
batch.perObjectFrustumCulled = false;
|
||||
batch.sortObjects = false;
|
||||
|
||||
for (const { mesh, matrix, thickness } of items) {
|
||||
const instanceId = batch.addInstance(batch.addGeometry(mesh.geometry));
|
||||
|
||||
batch.setMatrixAt(instanceId, matrix);
|
||||
|
||||
if (thickness !== undefined) {
|
||||
batch.setColorAt(instanceId, color.setRGB(thickness, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
return batch;
|
||||
}, [items]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!batch) return;
|
||||
|
||||
invalidate();
|
||||
sceneChangeEvent.dispatch();
|
||||
|
||||
return () => {
|
||||
batch.dispose();
|
||||
sceneChangeEvent.dispatch();
|
||||
};
|
||||
}, [batch]);
|
||||
|
||||
if (!batch) return null;
|
||||
|
||||
return <primitive object={batch} {...props} />;
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { blueDark, orangeDark } from "@radix-ui/colors";
|
||||
import { ContactShadows } from "@react-three/drei";
|
||||
import { invalidate, useFrame } from "@react-three/fiber";
|
||||
import { clamp, times } from "lodash-es";
|
||||
import { Quicklime } from "quicklime";
|
||||
@@ -12,9 +11,12 @@ import {
|
||||
} from "react";
|
||||
import { HemisphereLight, SpotLight, type Group } from "three";
|
||||
import { degToRad, lerp } from "three/src/math/MathUtils.js";
|
||||
import { sceneChangeEvent } from "../core/three/sceneChange";
|
||||
import { useShadowsOnDemand } from "../hooks/useShadowsOnDemand";
|
||||
import { Tankopedia } from "../stores/tankopedia";
|
||||
import { TankopediaPersistent } from "../stores/tankopediaPersistent";
|
||||
import { TankopediaDisplay } from "../stores/tankopediaPersistent/constants";
|
||||
import { TankopediaContactShadows } from "./TankopediaContactShadows";
|
||||
|
||||
const ANGLE = degToRad(15);
|
||||
const REVEAL_ANIMATION_TIME = 3;
|
||||
@@ -66,6 +68,10 @@ export function Lighting({ hasPbr, hidden = false }: Props) {
|
||||
|
||||
const [animate, setAnimate] = useState(true);
|
||||
|
||||
useShadowsOnDemand();
|
||||
|
||||
useEffect(() => sceneChangeEvent.dispatch(), [highGraphics]);
|
||||
|
||||
useEffect(() => {
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.showControls = true;
|
||||
@@ -118,17 +124,7 @@ export function Lighting({ hasPbr, hidden = false }: Props) {
|
||||
|
||||
return (
|
||||
<>
|
||||
<ContactShadows
|
||||
width={1.5}
|
||||
height={1.5}
|
||||
near={0}
|
||||
far={1}
|
||||
opacity={2 ** 1}
|
||||
blur={2 ** 1}
|
||||
resolution={2 ** 9}
|
||||
depthWrite={false}
|
||||
renderOrder={1}
|
||||
/>
|
||||
<TankopediaContactShadows />
|
||||
|
||||
{animate && (
|
||||
<Animator
|
||||
@@ -234,6 +230,8 @@ function Animator({
|
||||
}
|
||||
|
||||
transitionEvent.dispatch(t);
|
||||
// spotlight angles shape their shadows
|
||||
sceneChangeEvent.dispatch();
|
||||
}, []);
|
||||
|
||||
useFrame(({ invalidate }) => {
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
type ModelTransformEventData,
|
||||
} from "../../core/blitzkit/modelTransform";
|
||||
import { controlsEnabledEvent } from "../../core/controlsEnabled";
|
||||
import { sceneChangeEvent } from "../../core/three/sceneChange";
|
||||
import { useModel } from "../../hooks/useModel";
|
||||
import { Mixer } from "../../stores/mixer";
|
||||
|
||||
@@ -83,6 +84,8 @@ export function MixerModel() {
|
||||
window.removeEventListener("pointerup", handlePointerUp);
|
||||
}, []);
|
||||
|
||||
useEffect(() => sceneChangeEvent.dispatch(), [hull, turret, gun]);
|
||||
|
||||
useEffect(() => {
|
||||
function handleModelTransform(
|
||||
event: QuicklimeEvent<ModelTransformEventData>,
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { memo, useRef } from "react";
|
||||
import { Group } from "three";
|
||||
import { nameToArmorId } from "../core/blitzkit/nameToArmorId";
|
||||
import { resolveArmor } from "../core/blitzkit/resolveThickness";
|
||||
import { useArmor } from "../hooks/useArmor";
|
||||
import { useArmorPlates } from "../hooks/useArmorPlates";
|
||||
import { useConsumable } from "../hooks/useConsumable";
|
||||
import { useDuelModels } from "../hooks/useDuelModels";
|
||||
import { useDuelModules } from "../hooks/useDuelModules";
|
||||
@@ -14,14 +12,15 @@ export const PrimaryArmorScene = memo(() => {
|
||||
const turretContainer = useRef<Group>(null!);
|
||||
const gunContainer = useRef<Group>(null!);
|
||||
const protagonist = useDuelModules("protagonist");
|
||||
const armorGltf = useArmor(protagonist.tank.id).gltf;
|
||||
const armorNodes = Object.values(armorGltf.nodes);
|
||||
const models = useDuelModels("protagonist");
|
||||
const hullOrigin = toThreeVector(models.track.origin);
|
||||
const turretOrigin = toThreeVector(models.tank.turret_origin);
|
||||
const gunOrigin = toThreeVector(models.turret.gun_origin);
|
||||
const isDynamicArmorActive = useConsumable("protagonist", 73);
|
||||
|
||||
const hiddenState = isDynamicArmorActive ? "state_01" : "state_00";
|
||||
const plates = useArmorPlates(hiddenState);
|
||||
|
||||
useTankTransform(
|
||||
protagonist.track,
|
||||
protagonist.turret,
|
||||
@@ -32,100 +31,22 @@ export const PrimaryArmorScene = memo(() => {
|
||||
return (
|
||||
<>
|
||||
<group position={hullOrigin}>
|
||||
{armorNodes.map((node) => {
|
||||
const isHull = node.name.startsWith("hull_");
|
||||
const isVisible = isHull;
|
||||
const armorId = nameToArmorId(node.name);
|
||||
const { spaced, thickness } = resolveArmor(
|
||||
models.tank.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}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
<PrimaryArmorSceneComponent plates={plates.hull.primary} />
|
||||
</group>
|
||||
|
||||
<group ref={turretContainer}>
|
||||
{armorNodes.map((node) => {
|
||||
const isCurrentTurret = node.name.startsWith(
|
||||
`turret_${models.turret.model_id.toString().padStart(2, "0")}`,
|
||||
);
|
||||
const isVisible = isCurrentTurret;
|
||||
const armorId = nameToArmorId(node.name);
|
||||
const { spaced, thickness } = resolveArmor(
|
||||
models.turret.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}>
|
||||
<PrimaryArmorSceneComponent
|
||||
key={node.uuid}
|
||||
thickness={thickness}
|
||||
node={node}
|
||||
/>
|
||||
</group>
|
||||
</group>
|
||||
);
|
||||
})}
|
||||
<group position={hullOrigin}>
|
||||
<group position={turretOrigin}>
|
||||
<PrimaryArmorSceneComponent plates={plates.turret.primary} />
|
||||
</group>
|
||||
</group>
|
||||
|
||||
<group ref={gunContainer}>
|
||||
{armorNodes.map((node) => {
|
||||
const isCurrentGun = node.name.startsWith(
|
||||
`gun_${models.gun.model_id.toString().padStart(2, "0")}`,
|
||||
);
|
||||
const isVisible = isCurrentGun;
|
||||
const armorId = nameToArmorId(node.name);
|
||||
const { spaced, thickness } = resolveArmor(
|
||||
models.gun.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)}>
|
||||
<PrimaryArmorSceneComponent
|
||||
thickness={thickness}
|
||||
node={node}
|
||||
/>
|
||||
</group>
|
||||
</group>
|
||||
);
|
||||
})}
|
||||
<group position={hullOrigin}>
|
||||
<group position={turretOrigin.clone().add(gunOrigin)}>
|
||||
<PrimaryArmorSceneComponent plates={plates.gun.primary} />
|
||||
</group>
|
||||
</group>
|
||||
</group>
|
||||
</group>
|
||||
</>
|
||||
|
||||
@@ -6,7 +6,6 @@ import { useEffect, useRef } from "react";
|
||||
import {
|
||||
MeshBasicMaterial,
|
||||
NormalBlending,
|
||||
Object3D,
|
||||
ShaderMaterial,
|
||||
SubtractiveBlending,
|
||||
UniformsLib,
|
||||
@@ -14,19 +13,20 @@ import {
|
||||
Vector2,
|
||||
} from "three";
|
||||
import { degToRad } from "three/src/math/MathUtils.js";
|
||||
import { jsxTree } from "../../core/blitzkit/jsxTree";
|
||||
import type { ArmorPlate } from "../../hooks/useArmorPlates";
|
||||
import { getDuelModules } from "../../hooks/useDuelModules";
|
||||
import { hasEquipment } from "../../hooks/useEquipment";
|
||||
import { usePlateItems } from "../../hooks/usePlateItems";
|
||||
import { Tankopedia } from "../../stores/tankopedia";
|
||||
import { TankopediaPersistent } from "../../stores/tankopediaPersistent";
|
||||
import { Batch } from "../Batch";
|
||||
import { transitionEvent } from "../Lighting";
|
||||
import { spacedArmorRenderTarget } from "../PrimaryArmorRenderTarget";
|
||||
import fragmentShader from "./shaders/fragment.glsl?raw";
|
||||
import vertexShader from "./shaders/vertex.glsl?raw";
|
||||
|
||||
interface PrimaryArmorSceneComponentProps {
|
||||
thickness: number;
|
||||
node: Object3D;
|
||||
plates: ArmorPlate[];
|
||||
}
|
||||
|
||||
const excludeMaterial = new MeshBasicMaterial({
|
||||
@@ -37,8 +37,7 @@ const date = new Date();
|
||||
const isHalloween = date.getMonth() === 9 && date.getDate() === 31;
|
||||
|
||||
export function PrimaryArmorSceneComponent({
|
||||
node,
|
||||
thickness,
|
||||
plates,
|
||||
}: PrimaryArmorSceneComponentProps) {
|
||||
const material = useRef(
|
||||
new ShaderMaterial({
|
||||
@@ -55,7 +54,7 @@ export function PrimaryArmorSceneComponent({
|
||||
UniformsLib.fog,
|
||||
|
||||
{
|
||||
thickness: { value: null },
|
||||
thicknessCoefficient: { value: null },
|
||||
penetration: { value: null },
|
||||
caliber: { value: null },
|
||||
ricochet: { value: null },
|
||||
@@ -129,8 +128,8 @@ export function PrimaryArmorSceneComponent({
|
||||
? protagonist.equalizer?.armor
|
||||
: undefined) ?? 1;
|
||||
|
||||
material.current.uniforms.thickness.value =
|
||||
thickness * (hasEnhancedArmor ? 1.04 : 1) * equalizer;
|
||||
material.current.uniforms.thicknessCoefficient.value =
|
||||
(hasEnhancedArmor ? 1.04 : 1) * equalizer;
|
||||
|
||||
if (!noInvalidate) invalidate();
|
||||
}
|
||||
@@ -223,32 +222,12 @@ export function PrimaryArmorSceneComponent({
|
||||
camera.projectionMatrixInverse;
|
||||
});
|
||||
|
||||
const items = usePlateItems(plates);
|
||||
|
||||
return (
|
||||
<>
|
||||
{jsxTree(node, {
|
||||
mesh(_, props, key) {
|
||||
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}
|
||||
/>
|
||||
);
|
||||
},
|
||||
})}
|
||||
<Batch items={items} material={excludeMaterial} renderOrder={0} />
|
||||
<Batch items={items} material={material.current} renderOrder={1} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -4,7 +4,8 @@ varying vec3 vNormal;
|
||||
varying vec3 vViewPosition;
|
||||
|
||||
uniform float opacity;
|
||||
uniform float thickness;
|
||||
uniform float thicknessCoefficient;
|
||||
varying float vThickness;
|
||||
uniform float penetration;
|
||||
uniform float caliber;
|
||||
uniform float ricochet;
|
||||
@@ -43,6 +44,8 @@ vec3 getPenetrationColor(bool isThreeCalibersRule, bool couldHaveRicochet) {
|
||||
void main() {
|
||||
#include <clipping_planes_fragment>
|
||||
|
||||
float thickness = vThickness * thicknessCoefficient;
|
||||
|
||||
vec2 screenCoord = gl_FragCoord.xy / resolution;
|
||||
vec4 spacedArmorData = texture2D(spacedArmorBuffer, screenCoord);
|
||||
bool isUnderSpacedArmor = spacedArmorData.a != 0.0;
|
||||
|
||||
@@ -2,12 +2,31 @@ precision mediump float;
|
||||
|
||||
varying vec3 vNormal;
|
||||
varying vec3 vViewPosition;
|
||||
varying float vThickness;
|
||||
|
||||
#include <batching_pars_vertex>
|
||||
|
||||
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;
|
||||
vNormal = normalMatrix * normal;
|
||||
vNormal = normalMatrix * localNormal;
|
||||
|
||||
gl_Position = projectionMatrix * mvPosition;
|
||||
}
|
||||
@@ -54,8 +54,6 @@ export function SceneProps({ color = mauveDark.mauve6 }: SceneProps) {
|
||||
"#include <dithering_fragment>",
|
||||
|
||||
`
|
||||
#include <dithering_fragment>
|
||||
|
||||
vec3 scaled = vWorldPosition / size;
|
||||
float radius = length(scaled.xz);
|
||||
|
||||
@@ -67,6 +65,9 @@ export function SceneProps({ color = mauveDark.mauve6 }: SceneProps) {
|
||||
float y = x * x;
|
||||
|
||||
gl_FragColor.rgb *= y;
|
||||
|
||||
// after the falloff, so its gradient doesn't band
|
||||
#include <dithering_fragment>
|
||||
`,
|
||||
);
|
||||
},
|
||||
@@ -85,6 +86,7 @@ export function SceneProps({ color = mauveDark.mauve6 }: SceneProps) {
|
||||
<meshStandardMaterial
|
||||
color={color}
|
||||
roughness={1}
|
||||
dithering
|
||||
onBeforeCompile={handleBeforeCompile}
|
||||
/>
|
||||
</mesh>
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
import { memo, useRef } from "react";
|
||||
import { Group, Plane, Scene, Vector3 } from "three";
|
||||
import { nameToArmorId } from "../core/blitzkit/nameToArmorId";
|
||||
import { resolveArmor } from "../core/blitzkit/resolveThickness";
|
||||
import { useArmor } from "../hooks/useArmor";
|
||||
import { memo, useMemo, useRef } from "react";
|
||||
import { Group, Plane, Scene, Vector3, type Object3D } from "three";
|
||||
import { type ArmorPlate, useArmorPlates } from "../hooks/useArmorPlates";
|
||||
import { useConsumable } from "../hooks/useConsumable";
|
||||
import { useDuelModels } from "../hooks/useDuelModels";
|
||||
import { useDuelModules } from "../hooks/useDuelModules";
|
||||
import { useModel } from "../hooks/useModel";
|
||||
import { useModelNodes } from "../hooks/useModelNodes";
|
||||
import { useTankTransform } from "../hooks/useTankTransform";
|
||||
import { toThreeVector } from "../three/toThreeVector";
|
||||
import { SpacedArmorSceneComponent } from "./SpacedArmorSceneComponent";
|
||||
import {
|
||||
SpacedArmorSceneComponent,
|
||||
type ExternalModuleVariant,
|
||||
} from "./SpacedArmorSceneComponent";
|
||||
import { SpacedArmorSubExternal } from "./SpacedArmorSubExternal";
|
||||
import { SpacedArmorSubPrimary } from "./SpacedArmorSubPrimary";
|
||||
import { SpacedArmorSubSpaced } from "./SpacedArmorSubSpaced";
|
||||
|
||||
export enum ArmorType {
|
||||
Primary,
|
||||
@@ -21,14 +25,87 @@ interface SpacedArmorSceneProps {
|
||||
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 }) => {
|
||||
const turretContainer = useRef<Group>(null!);
|
||||
const gunContainer = useRef<Group>(null!);
|
||||
const protagonist = useDuelModules("protagonist");
|
||||
const { gltf: armorGltf } = useArmor(protagonist.tank.id);
|
||||
const { gltf: modelGltf } = useModel(protagonist.tank.id);
|
||||
const armorNodes = Object.values(armorGltf.nodes);
|
||||
const modelNodes = Object.values(modelGltf.nodes);
|
||||
const models = useDuelModels("protagonist");
|
||||
const hullOrigin = toThreeVector(models.track.origin);
|
||||
const turretOrigin = toThreeVector(models.tank.turret_origin);
|
||||
@@ -38,6 +115,26 @@ export const SpacedArmorScene = memo<SpacedArmorSceneProps>(({ scene }) => {
|
||||
? undefined
|
||||
: models.gun.mask + hullOrigin.y + turretOrigin.y + gunOrigin.y;
|
||||
const isDynamicArmorActive = useConsumable("protagonist", 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(models.gun.mask);
|
||||
const trackNodes = useMemo(
|
||||
() => [...modelNodes.wheels, ...modelNodes.tracks],
|
||||
[modelNodes],
|
||||
);
|
||||
const gunNodes = useMemo(
|
||||
() => (hasMask ? [...modelNodes.gun, ...modelNodes.mask] : modelNodes.gun),
|
||||
[modelNodes, hasMask],
|
||||
);
|
||||
|
||||
useTankTransform(
|
||||
protagonist.track,
|
||||
@@ -49,150 +146,37 @@ export const SpacedArmorScene = memo<SpacedArmorSceneProps>(({ scene }) => {
|
||||
return (
|
||||
<>
|
||||
<group position={hullOrigin}>
|
||||
{armorNodes.map((node) => {
|
||||
const isHull = node.name.startsWith("hull_");
|
||||
const isVisible = isHull;
|
||||
const armorId = nameToArmorId(node.name);
|
||||
const { spaced, thickness } = resolveArmor(
|
||||
models.tank.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}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
<ArmorSection {...plates.hull} scene={scene} />
|
||||
</group>
|
||||
|
||||
{modelNodes.map((node) => {
|
||||
const isWheel = node.name.startsWith("chassis_wheel_");
|
||||
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={models.track.thickness}
|
||||
variant="track"
|
||||
node={node}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
<ExternalSection
|
||||
nodes={trackNodes}
|
||||
thickness={models.track.thickness}
|
||||
variant="track"
|
||||
scene={scene}
|
||||
/>
|
||||
|
||||
<group ref={turretContainer}>
|
||||
{armorNodes.map((node) => {
|
||||
const isCurrentTurret = node.name.startsWith(
|
||||
`turret_${models.turret.model_id.toString().padStart(2, "0")}`,
|
||||
);
|
||||
const isVisible = isCurrentTurret;
|
||||
const armorId = nameToArmorId(node.name);
|
||||
const { spaced, thickness } = resolveArmor(
|
||||
models.turret.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}>
|
||||
<SpacedArmorSceneComponent
|
||||
scene={scene}
|
||||
key={`${node.uuid}-turret`}
|
||||
type={spaced ? ArmorType.Spaced : ArmorType.Primary}
|
||||
thickness={thickness}
|
||||
node={node}
|
||||
/>
|
||||
</group>
|
||||
</group>
|
||||
);
|
||||
})}
|
||||
<group position={hullOrigin}>
|
||||
<group position={turretOrigin}>
|
||||
<ArmorSection {...plates.turret} scene={scene} />
|
||||
</group>
|
||||
</group>
|
||||
|
||||
<group ref={gunContainer}>
|
||||
{armorNodes.map((node) => {
|
||||
const isCurrentGun = node.name.startsWith(
|
||||
`gun_${models.gun.model_id.toString().padStart(2, "0")}`,
|
||||
);
|
||||
const isVisible = isCurrentGun;
|
||||
const armorId = nameToArmorId(node.name);
|
||||
const { spaced, thickness } = resolveArmor(
|
||||
models.gun.armor!,
|
||||
armorId,
|
||||
);
|
||||
<group position={hullOrigin}>
|
||||
<group position={turretOrigin.clone().add(gunOrigin)}>
|
||||
<ArmorSection {...plates.gun} scene={scene} />
|
||||
</group>
|
||||
</group>
|
||||
|
||||
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)}>
|
||||
<SpacedArmorSceneComponent
|
||||
scene={scene}
|
||||
type={spaced ? ArmorType.Spaced : ArmorType.Primary}
|
||||
thickness={thickness}
|
||||
node={node}
|
||||
/>
|
||||
</group>
|
||||
</group>
|
||||
);
|
||||
})}
|
||||
|
||||
{modelNodes.map((node) => {
|
||||
const gunString = `gun_${models.gun.model_id
|
||||
.toString()
|
||||
.padStart(2, "0")}`;
|
||||
const isCurrentGun = models.gun.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={models.gun.thickness}
|
||||
variant="gun"
|
||||
node={node}
|
||||
clip={
|
||||
maskOrigin === undefined
|
||||
? undefined
|
||||
: new Plane(new Vector3(0, 0, -1), -maskOrigin)
|
||||
}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
<ExternalSection
|
||||
nodes={gunNodes}
|
||||
thickness={models.gun.thickness}
|
||||
variant="gun"
|
||||
clip={clip}
|
||||
scene={scene}
|
||||
/>
|
||||
</group>
|
||||
</group>
|
||||
</>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { isExplosive, resolvePenetrationCoefficient } from "@blitzkit/core";
|
||||
import { ShellType } from "@blitzkit/protos";
|
||||
import { useThree } from "@react-three/fiber";
|
||||
import { useCallback } from "react";
|
||||
import { useCallback, useMemo } from "react";
|
||||
import {
|
||||
type Intersection,
|
||||
MeshBasicMaterial,
|
||||
@@ -29,8 +29,6 @@ import {
|
||||
Tankopedia,
|
||||
} from "../stores/tankopedia";
|
||||
import { ArmorType } from "./SpacedArmorScene";
|
||||
import { SpacedArmorSubExternal } from "./SpacedArmorSubExternal";
|
||||
import { SpacedArmorSubSpaced } from "./SpacedArmorSubSpaced";
|
||||
|
||||
type SpacedArmorSceneComponentProps = {
|
||||
node: Object3D;
|
||||
@@ -90,12 +88,14 @@ function resolvePenetrationChance(shoot: (scale: number) => Shot | null) {
|
||||
);
|
||||
}
|
||||
|
||||
const omitMaterial = new MeshBasicMaterial({
|
||||
colorWrite: false,
|
||||
depthWrite: true,
|
||||
});
|
||||
const proxyMaterial = new MeshBasicMaterial({ visible: false });
|
||||
|
||||
/**
|
||||
* 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:
|
||||
* - 0 : core omission
|
||||
* - 1-2: spaced without depth buffer write but do test to allow additive blending
|
||||
@@ -431,59 +431,44 @@ export function SpacedArmorSceneComponent({
|
||||
[camera],
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
{props.type === ArmorType.Primary &&
|
||||
jsxTree(node, {
|
||||
mesh(_, props, key) {
|
||||
return (
|
||||
<mesh
|
||||
{...props}
|
||||
key={`${key}-spaced-component`}
|
||||
renderOrder={0}
|
||||
material={omitMaterial}
|
||||
userData={
|
||||
{
|
||||
type: ArmorType.Primary,
|
||||
thickness,
|
||||
} satisfies ArmorUserData
|
||||
}
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
|
||||
const shot = shoot(event.point, event.intersections, true)!;
|
||||
|
||||
shot.penetrationChance = resolvePenetrationChance((scale) =>
|
||||
shoot(
|
||||
event.point,
|
||||
event.intersections,
|
||||
true,
|
||||
undefined,
|
||||
scale,
|
||||
),
|
||||
);
|
||||
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.shot = shot;
|
||||
});
|
||||
}}
|
||||
/>
|
||||
);
|
||||
},
|
||||
})}
|
||||
|
||||
{props.type === ArmorType.Spaced && (
|
||||
<SpacedArmorSubSpaced node={node} thickness={thickness} />
|
||||
)}
|
||||
|
||||
{props.type === ArmorType.External && (
|
||||
<SpacedArmorSubExternal
|
||||
node={node}
|
||||
thickness={thickness}
|
||||
variant={props.variant}
|
||||
clip={clip}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
const clippedProxyMaterial = useMemo(
|
||||
() =>
|
||||
clip
|
||||
? new MeshBasicMaterial({ visible: false, clippingPlanes: [clip] })
|
||||
: 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 (
|
||||
<mesh
|
||||
{...meshProps}
|
||||
key={`${key}-proxy`}
|
||||
material={clippedProxyMaterial}
|
||||
userData={userData}
|
||||
onClick={(event) => {
|
||||
// other layers only need to be in the intersections
|
||||
if (props.type !== ArmorType.Primary) return;
|
||||
|
||||
event.stopPropagation();
|
||||
|
||||
const shot = shoot(event.point, event.intersections, true)!;
|
||||
|
||||
shot.penetrationChance = resolvePenetrationChance((scale) =>
|
||||
shoot(event.point, event.intersections, true, undefined, scale),
|
||||
);
|
||||
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.shot = shot;
|
||||
});
|
||||
}}
|
||||
/>
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,52 +1,60 @@
|
||||
import { resolvePenetrationCoefficient } from "@blitzkit/core";
|
||||
import { useEffect } from "react";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import {
|
||||
AdditiveBlending,
|
||||
MeshBasicMaterial,
|
||||
Object3D,
|
||||
Plane,
|
||||
ShaderMaterial,
|
||||
} from "three";
|
||||
import { jsxTree } from "../../core/blitzkit/jsxTree";
|
||||
import type { ArmorPlate } from "../../hooks/useArmorPlates";
|
||||
import { getDuelModules } from "../../hooks/useDuelModules";
|
||||
import { hasEquipment } from "../../hooks/useEquipment";
|
||||
import { usePlateItems } from "../../hooks/usePlateItems";
|
||||
import { Tankopedia } from "../../stores/tankopedia";
|
||||
import { ArmorType } from "../SpacedArmorScene";
|
||||
import type {
|
||||
ArmorUserData,
|
||||
ExternalModuleVariant,
|
||||
} from "../SpacedArmorSceneComponent";
|
||||
import { Batch } from "../Batch";
|
||||
import fragmentShader from "./shaders/fragment.glsl?raw";
|
||||
import vertexShader from "./shaders/vertex.glsl?raw";
|
||||
|
||||
interface SpacedArmorSubExternalProps {
|
||||
node: Object3D;
|
||||
plates: ArmorPlate[];
|
||||
thickness: number;
|
||||
variant: ExternalModuleVariant;
|
||||
clip?: Plane;
|
||||
}
|
||||
|
||||
export function SpacedArmorSubExternal({
|
||||
node,
|
||||
plates,
|
||||
thickness,
|
||||
variant,
|
||||
clip,
|
||||
}: SpacedArmorSubExternalProps) {
|
||||
const material = new ShaderMaterial({
|
||||
fragmentShader,
|
||||
vertexShader,
|
||||
const excludeMaterial = useMemo(
|
||||
() =>
|
||||
new MeshBasicMaterial({
|
||||
colorWrite: false,
|
||||
depthTest: true,
|
||||
depthWrite: true,
|
||||
...(clip ? { clippingPlanes: [clip] } : {}),
|
||||
}),
|
||||
[clip],
|
||||
);
|
||||
const material = useMemo(
|
||||
() =>
|
||||
new ShaderMaterial({
|
||||
fragmentShader,
|
||||
vertexShader,
|
||||
|
||||
depthTest: true,
|
||||
depthWrite: false,
|
||||
blending: AdditiveBlending,
|
||||
clipping: clip !== undefined,
|
||||
...(clip ? { clippingPlanes: [clip] } : {}),
|
||||
depthTest: true,
|
||||
depthWrite: false,
|
||||
blending: AdditiveBlending,
|
||||
clipping: clip !== undefined,
|
||||
...(clip ? { clippingPlanes: [clip] } : {}),
|
||||
|
||||
uniforms: {
|
||||
thickness: { value: null },
|
||||
penetration: { value: null },
|
||||
},
|
||||
});
|
||||
uniforms: {
|
||||
thickness: { value: null },
|
||||
penetration: { value: null },
|
||||
},
|
||||
}),
|
||||
[clip],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
function handleShellChange() {
|
||||
@@ -126,62 +134,15 @@ export function SpacedArmorSubExternal({
|
||||
return () => {
|
||||
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
||||
};
|
||||
});
|
||||
}, [material, thickness]);
|
||||
|
||||
|
||||
const items = usePlateItems(plates);
|
||||
|
||||
return (
|
||||
<>
|
||||
{jsxTree(node, {
|
||||
group(_, props, key) {
|
||||
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}
|
||||
/>
|
||||
);
|
||||
},
|
||||
})}
|
||||
<Batch items={items} material={excludeMaterial} renderOrder={3} />
|
||||
<Batch items={items} material={material} renderOrder={4} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
precision mediump float;
|
||||
|
||||
#include <batching_pars_vertex>
|
||||
#include <clipping_planes_pars_vertex>
|
||||
|
||||
void main() {
|
||||
#include <batching_vertex>
|
||||
#include <begin_vertex>
|
||||
#include <project_vertex>
|
||||
#include <clipping_planes_vertex>
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
import { isExplosive, resolvePenetrationCoefficient } from "@blitzkit/core";
|
||||
import { useEffect } from "react";
|
||||
import {
|
||||
AdditiveBlending,
|
||||
MeshBasicMaterial,
|
||||
ShaderMaterial,
|
||||
type Object3D,
|
||||
} from "three";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { AdditiveBlending, MeshBasicMaterial, ShaderMaterial } from "three";
|
||||
import { degToRad } from "three/src/math/MathUtils.js";
|
||||
import { jsxTree } from "../../core/blitzkit/jsxTree";
|
||||
import type { ArmorPlate } from "../../hooks/useArmorPlates";
|
||||
import { getDuelModules } from "../../hooks/useDuelModules";
|
||||
import { hasEquipment } from "../../hooks/useEquipment";
|
||||
import { usePlateItems } from "../../hooks/usePlateItems";
|
||||
import { Tankopedia } from "../../stores/tankopedia";
|
||||
import { ArmorType } from "../SpacedArmorScene";
|
||||
import type { ArmorUserData } from "../SpacedArmorSceneComponent";
|
||||
import { Batch } from "../Batch";
|
||||
import fragmentShader from "./shaders/fragment.glsl?raw";
|
||||
import vertexShader from "./shaders/vertex.glsl?raw";
|
||||
|
||||
interface SpacedArmorSubSpacedProps {
|
||||
node: Object3D;
|
||||
thickness: number;
|
||||
plates: ArmorPlate[];
|
||||
}
|
||||
|
||||
const depthWriteMaterial = new MeshBasicMaterial({
|
||||
@@ -26,26 +20,27 @@ const depthWriteMaterial = new MeshBasicMaterial({
|
||||
colorWrite: false,
|
||||
});
|
||||
|
||||
export function SpacedArmorSubSpaced({
|
||||
node,
|
||||
thickness,
|
||||
}: SpacedArmorSubSpacedProps) {
|
||||
const material = new ShaderMaterial({
|
||||
fragmentShader,
|
||||
vertexShader,
|
||||
export function SpacedArmorSubSpaced({ plates }: SpacedArmorSubSpacedProps) {
|
||||
const material = useMemo(
|
||||
() =>
|
||||
new ShaderMaterial({
|
||||
fragmentShader,
|
||||
vertexShader,
|
||||
|
||||
depthTest: true,
|
||||
depthWrite: false,
|
||||
blending: AdditiveBlending,
|
||||
depthTest: true,
|
||||
depthWrite: false,
|
||||
blending: AdditiveBlending,
|
||||
|
||||
uniforms: {
|
||||
thickness: { value: null },
|
||||
penetration: { value: null },
|
||||
caliber: { value: null },
|
||||
ricochet: { value: null },
|
||||
normalization: { value: null },
|
||||
},
|
||||
});
|
||||
uniforms: {
|
||||
thicknessCoefficient: { value: null },
|
||||
penetration: { value: null },
|
||||
caliber: { value: null },
|
||||
ricochet: { value: null },
|
||||
normalization: { value: null },
|
||||
},
|
||||
}),
|
||||
[],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
function handleShellChange() {
|
||||
@@ -75,8 +70,8 @@ export function SpacedArmorSubSpaced({
|
||||
? protagonist.equalizer?.armor
|
||||
: undefined) ?? 1;
|
||||
|
||||
material.uniforms.thickness.value =
|
||||
thickness * (hasEnhancedArmor ? 1.04 : 1) * equalizer;
|
||||
material.uniforms.thicknessCoefficient.value =
|
||||
(hasEnhancedArmor ? 1.04 : 1) * equalizer;
|
||||
}
|
||||
function handleAntagonistEquipmentChange() {
|
||||
const antagonist = getDuelModules("antagonist").tank;
|
||||
@@ -133,50 +128,15 @@ export function SpacedArmorSubSpaced({
|
||||
return () => {
|
||||
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
||||
};
|
||||
});
|
||||
}, [material]);
|
||||
|
||||
|
||||
const items = usePlateItems(plates);
|
||||
|
||||
return (
|
||||
<>
|
||||
{jsxTree(node, {
|
||||
group(_, props, key) {
|
||||
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}
|
||||
/>
|
||||
);
|
||||
},
|
||||
})}
|
||||
<Batch items={items} material={material} renderOrder={2} />
|
||||
<Batch items={items} material={depthWriteMaterial} renderOrder={5} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -3,13 +3,15 @@ precision mediump float;
|
||||
varying vec3 vNormal;
|
||||
varying vec3 vViewPosition;
|
||||
|
||||
uniform float thickness;
|
||||
uniform float thicknessCoefficient;
|
||||
varying float vThickness;
|
||||
uniform float penetration;
|
||||
uniform float caliber;
|
||||
uniform float ricochet;
|
||||
uniform float normalization;
|
||||
|
||||
void main() {
|
||||
float thickness = vThickness * thicknessCoefficient;
|
||||
float angle = acos(dot(vNormal, -vViewPosition) / length(vViewPosition));
|
||||
|
||||
bool threeCalibersRule = caliber > thickness * 3.0;
|
||||
|
||||
@@ -2,12 +2,31 @@ precision mediump float;
|
||||
|
||||
varying vec3 vNormal;
|
||||
varying vec3 vViewPosition;
|
||||
varying float vThickness;
|
||||
|
||||
#include <batching_pars_vertex>
|
||||
|
||||
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;
|
||||
vNormal = normalMatrix * normal;
|
||||
vNormal = normalMatrix * localNormal;
|
||||
|
||||
gl_Position = projectionMatrix * vViewPosition4;
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
type ModelTransformEventData,
|
||||
} from "../core/blitzkit/modelTransform";
|
||||
import { discardClippingPlane } from "../core/three/discardClippingPlane";
|
||||
import { sceneChangeEvent } from "../core/three/sceneChange";
|
||||
import { useDuelModules } from "../hooks/useDuelModules";
|
||||
import { useEquipment } from "../hooks/useEquipment";
|
||||
import { Tankopedia } from "../stores/tankopedia";
|
||||
@@ -155,6 +156,13 @@ export function StaticArmorSceneComponent({
|
||||
[hasEnhancedArmor, equalize],
|
||||
);
|
||||
|
||||
// these cast shadows
|
||||
useEffect(() => {
|
||||
sceneChangeEvent.dispatch();
|
||||
|
||||
return () => sceneChangeEvent.dispatch();
|
||||
}, [node]);
|
||||
|
||||
useEffect(() => {
|
||||
function handleHighlightArmor(selectedName?: string) {
|
||||
if (selectedName === undefined) {
|
||||
|
||||
@@ -1,20 +1,178 @@
|
||||
import { invalidate, useThree, type ThreeEvent } from "@react-three/fiber";
|
||||
import { useMemo, useRef } from "react";
|
||||
import type { Object3D, Texture } from "three";
|
||||
import { Group, Mesh, MeshStandardMaterial, Vector2 } from "three";
|
||||
import { applyPitchYawLimits } from "../core/blitz/applyPitchYawLimits";
|
||||
import { modelTransformEvent } from "../core/blitzkit/modelTransform";
|
||||
import { skinTree } from "../core/blitzkit/skinTree";
|
||||
import { controlsEnabledEvent } from "../core/controlsEnabled";
|
||||
import { useBatchGroups } from "../hooks/useBatchGroups";
|
||||
import { useDuelModels } from "../hooks/useDuelModels";
|
||||
import { getDuelModules, useDuelModules } from "../hooks/useDuelModules";
|
||||
import { useDuelModel } from "../hooks/useDuelModel";
|
||||
import { hasEquipment } from "../hooks/useEquipment";
|
||||
import { useModel } from "../hooks/useModel";
|
||||
import { useModelNodes } from "../hooks/useModelNodes";
|
||||
import { useSilhouette } from "../hooks/useSilhouette";
|
||||
import { useSkin } from "../hooks/useSkin";
|
||||
import { useSkin, type Skin } from "../hooks/useSkin";
|
||||
import { useSkinAnimation } from "../hooks/useSkinAnimation";
|
||||
import { useTankTransform } from "../hooks/useTankTransform";
|
||||
import { Tankopedia } from "../stores/tankopedia";
|
||||
import { TankopediaDisplay } from "../stores/tankopediaPersistent/constants";
|
||||
import { Batch } from "./Batch";
|
||||
|
||||
interface SectionProps {
|
||||
nodes: Object3D[];
|
||||
skins: Skin[];
|
||||
silhouette: boolean;
|
||||
// dragging scrolls the track textures
|
||||
scrollTracks?: boolean;
|
||||
// dragging turns the turret
|
||||
yaw?: boolean;
|
||||
// dragging raises and lowers the gun
|
||||
pitch?: boolean;
|
||||
}
|
||||
|
||||
function Section({
|
||||
nodes,
|
||||
skins,
|
||||
silhouette,
|
||||
scrollTracks,
|
||||
yaw,
|
||||
pitch,
|
||||
}: SectionProps) {
|
||||
const models = useDuelModels("protagonist");
|
||||
|
||||
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);
|
||||
|
||||
if (scrollTracks) {
|
||||
const offset =
|
||||
delta.current.x / window.innerWidth +
|
||||
delta.current.y / window.innerHeight;
|
||||
|
||||
for (const texture of textures.current) {
|
||||
if (!texture) continue;
|
||||
texture.offset.y += offset;
|
||||
}
|
||||
|
||||
invalidate();
|
||||
}
|
||||
|
||||
if (rotates) {
|
||||
const protagonist = getDuelModules("protagonist").tank;
|
||||
const hasImprovedVerticalStabilizer = hasEquipment(
|
||||
122,
|
||||
protagonist.equipment_preset,
|
||||
Tankopedia.state.protagonist.equipment,
|
||||
);
|
||||
const hasDownImprovedVerticalStabilizer = hasEquipment(
|
||||
124,
|
||||
protagonist.equipment_preset,
|
||||
Tankopedia.state.protagonist.equipment,
|
||||
);
|
||||
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,
|
||||
models.gun.pitch!,
|
||||
models.turret.yaw,
|
||||
hasImprovedVerticalStabilizer,
|
||||
hasDownImprovedVerticalStabilizer,
|
||||
);
|
||||
modelTransformEvent.dispatch({
|
||||
pitch: pitchAngle.current,
|
||||
yaw: yawAngle.current,
|
||||
});
|
||||
}
|
||||
}
|
||||
function handlePointerUp() {
|
||||
controlsEnabledEvent.dispatch(true);
|
||||
|
||||
window.removeEventListener("pointermove", handlePointerMove);
|
||||
window.removeEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
function handlePointerDown(event: ThreeEvent<PointerEvent>) {
|
||||
// tracks only scroll while looking at the model, not the armor
|
||||
if (
|
||||
scrollTracks &&
|
||||
Tankopedia.state.loaders.display.loaded !== TankopediaDisplay.Model
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// also fades emissive parts with the lights
|
||||
useSilhouette(silhouette, materials);
|
||||
|
||||
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>
|
||||
);
|
||||
}
|
||||
|
||||
interface TankModelProps {
|
||||
// while the lights are down, draws the tank as a flat light shape
|
||||
@@ -22,24 +180,21 @@ interface TankModelProps {
|
||||
}
|
||||
|
||||
export function TankModel({ silhouette = false }: TankModelProps) {
|
||||
const protagonistState = Tankopedia.use((state) => state.protagonist);
|
||||
const protagonistTank = Tankopedia.use((state) => state.protagonist.tank);
|
||||
const protagonist = useDuelModules("protagonist");
|
||||
|
||||
const canvas = useThree((state) => state.gl.domElement);
|
||||
const turretContainer = useRef<Group>(null!);
|
||||
const gunContainer = useRef<Group>(null!);
|
||||
const tankModelDefinition = useDuelModel("protagonist");
|
||||
const turretModelDefinition =
|
||||
tankModelDefinition.turrets[protagonistState.turret];
|
||||
const gunModelDefinition = turretModelDefinition.guns[protagonistState.gun];
|
||||
const { gltf } = useModel(protagonistState.tank);
|
||||
const nodes = Object.values(gltf.nodes);
|
||||
const skinGltf = useSkin(protagonistState.tank);
|
||||
const attachmentGltf = useSkin(protagonistState.tank, "attachment");
|
||||
|
||||
const nodes = useModelNodes();
|
||||
const skinGltf = useSkin(protagonistTank);
|
||||
const attachmentGltf = useSkin(protagonistTank, "attachment");
|
||||
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(
|
||||
protagonist.track,
|
||||
@@ -49,298 +204,32 @@ export function TankModel({ silhouette = false }: TankModelProps) {
|
||||
);
|
||||
useSkinAnimation(skins);
|
||||
|
||||
const materials = useMemo(() => {
|
||||
const collected = new Set<MeshStandardMaterial>();
|
||||
const roots = [
|
||||
gltf.scene,
|
||||
...skins.flatMap((skin) => [...skin.parts.values()].flat()),
|
||||
];
|
||||
|
||||
for (const root of roots) {
|
||||
root.traverse((object) => {
|
||||
if (!(object instanceof Mesh)) return;
|
||||
|
||||
for (const material of [object.material].flat()) {
|
||||
if (material instanceof MeshStandardMaterial) {
|
||||
collected.add(material);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return [...collected];
|
||||
}, [gltf, skins]);
|
||||
|
||||
// also fades emissive parts with the lights
|
||||
useSilhouette(silhouette, materials);
|
||||
|
||||
return (
|
||||
<>
|
||||
{nodes.map((node) => {
|
||||
const isHull = node.name === "hull";
|
||||
const isWheel = node.name.startsWith("chassis_wheel_");
|
||||
const isTrack = node.name.startsWith("chassis_track_");
|
||||
const isVisible = isHull || isWheel || isTrack;
|
||||
const position = new Vector2();
|
||||
const delta = new Vector2();
|
||||
|
||||
if (!isVisible) return null;
|
||||
|
||||
function translateTexture(offset: number) {
|
||||
const mesh = node.children[0] as Mesh;
|
||||
const material = mesh?.material as MeshStandardMaterial;
|
||||
|
||||
if (!material) return;
|
||||
|
||||
invalidate();
|
||||
|
||||
const textures = new Set([
|
||||
material.map,
|
||||
material.aoMap,
|
||||
material.normalMap,
|
||||
material.roughnessMap,
|
||||
material.metalnessMap,
|
||||
]);
|
||||
|
||||
for (const texture of textures) {
|
||||
if (texture) texture.offset.y += offset;
|
||||
}
|
||||
}
|
||||
|
||||
function onPointerDown(event: ThreeEvent<PointerEvent>) {
|
||||
if (
|
||||
isTrack &&
|
||||
Tankopedia.state.loaders.display.loaded === TankopediaDisplay.Model
|
||||
) {
|
||||
position.set(event.clientX, event.clientY);
|
||||
event.stopPropagation();
|
||||
|
||||
controlsEnabledEvent.dispatch(false);
|
||||
|
||||
window.addEventListener("pointermove", handlePointerMove);
|
||||
window.addEventListener("pointerup", handlePointerUp);
|
||||
} else {
|
||||
event.stopPropagation();
|
||||
}
|
||||
}
|
||||
function handlePointerMove(event: PointerEvent) {
|
||||
delta.set(event.clientX, event.clientY).sub(position);
|
||||
position.set(event.clientX, event.clientY);
|
||||
const deltaX = delta.x / window.innerWidth;
|
||||
const deltaY = delta.y / window.innerHeight;
|
||||
|
||||
translateTexture(deltaX + deltaY);
|
||||
}
|
||||
function handlePointerUp() {
|
||||
controlsEnabledEvent.dispatch(true);
|
||||
|
||||
window.removeEventListener("pointermove", handlePointerMove);
|
||||
window.removeEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
|
||||
return skinTree(node, skins, {
|
||||
mesh(_, props, key) {
|
||||
return (
|
||||
<mesh
|
||||
{...props}
|
||||
key={key}
|
||||
onPointerDown={onPointerDown}
|
||||
castShadow
|
||||
receiveShadow
|
||||
/>
|
||||
);
|
||||
},
|
||||
primitive(object, key) {
|
||||
return (
|
||||
<primitive
|
||||
key={key}
|
||||
object={object}
|
||||
onPointerDown={onPointerDown}
|
||||
/>
|
||||
);
|
||||
},
|
||||
});
|
||||
})}
|
||||
<Section
|
||||
nodes={nodes.tracks}
|
||||
skins={skins}
|
||||
silhouette={silhouette}
|
||||
scrollTracks
|
||||
/>
|
||||
<Section nodes={hullNodes} skins={skins} silhouette={silhouette} />
|
||||
|
||||
<group ref={turretContainer}>
|
||||
{nodes.map((node) => {
|
||||
const isTurret = node.name.startsWith("turret_");
|
||||
const isCurrentTurret =
|
||||
node.name ===
|
||||
`turret_${turretModelDefinition.model_id
|
||||
.toString()
|
||||
.padStart(2, "0")}`;
|
||||
const isVisible = isCurrentTurret;
|
||||
const position = new Vector2();
|
||||
const delta = new Vector2();
|
||||
|
||||
if (!isVisible) return null;
|
||||
|
||||
function onPointerDown(event: ThreeEvent<PointerEvent>) {
|
||||
event.stopPropagation();
|
||||
|
||||
if (!isTurret) return;
|
||||
|
||||
position.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 protagonist = getDuelModules("protagonist").tank;
|
||||
|
||||
const hasImprovedVerticalStabilizer = hasEquipment(
|
||||
122,
|
||||
protagonist.equipment_preset,
|
||||
Tankopedia.state.protagonist.equipment,
|
||||
);
|
||||
const hasDownImprovedVerticalStabilizer = hasEquipment(
|
||||
124,
|
||||
protagonist.equipment_preset,
|
||||
Tankopedia.state.protagonist.equipment,
|
||||
);
|
||||
const boundingRect = canvas.getBoundingClientRect();
|
||||
|
||||
delta.set(event.clientX, event.clientY).sub(position);
|
||||
position.set(event.clientX, event.clientY);
|
||||
|
||||
const [pitch, yaw] = applyPitchYawLimits(
|
||||
modelTransformEvent.last!.pitch,
|
||||
modelTransformEvent.last!.yaw +
|
||||
delta.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 skinTree(node, skins, {
|
||||
mesh(_, props, key) {
|
||||
return (
|
||||
<mesh
|
||||
{...props}
|
||||
key={key}
|
||||
onPointerDown={onPointerDown}
|
||||
castShadow
|
||||
receiveShadow
|
||||
/>
|
||||
);
|
||||
},
|
||||
primitive(object, key) {
|
||||
return (
|
||||
<primitive
|
||||
key={key}
|
||||
object={object}
|
||||
onPointerDown={onPointerDown}
|
||||
/>
|
||||
);
|
||||
},
|
||||
});
|
||||
})}
|
||||
<Section
|
||||
nodes={nodes.turret}
|
||||
skins={skins}
|
||||
silhouette={silhouette}
|
||||
yaw
|
||||
/>
|
||||
|
||||
<group ref={gunContainer}>
|
||||
{nodes.map((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")}`;
|
||||
const isVisible = isCurrentGun || isCurrentMantlet;
|
||||
const position = new Vector2();
|
||||
const delta = new Vector2();
|
||||
|
||||
if (!isVisible) return null;
|
||||
|
||||
let pitch = 0;
|
||||
let yaw = 0;
|
||||
|
||||
function onPointerDown(event: ThreeEvent<PointerEvent>) {
|
||||
event.stopPropagation();
|
||||
|
||||
controlsEnabledEvent.dispatch(false);
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.shot = undefined;
|
||||
draft.highlightArmor = undefined;
|
||||
});
|
||||
|
||||
position.set(event.clientX, event.clientY);
|
||||
pitch = modelTransformEvent.last!.pitch;
|
||||
yaw = modelTransformEvent.last!.yaw;
|
||||
|
||||
window.addEventListener("pointermove", handlePointerMove);
|
||||
window.addEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
function handlePointerMove(event: PointerEvent) {
|
||||
const protagonist = getDuelModules("protagonist").tank;
|
||||
|
||||
const hasImprovedVerticalStabilizer = hasEquipment(
|
||||
122,
|
||||
protagonist.equipment_preset,
|
||||
Tankopedia.state.protagonist.equipment,
|
||||
);
|
||||
const hasDownImprovedVerticalStabilizer = hasEquipment(
|
||||
124,
|
||||
protagonist.equipment_preset,
|
||||
Tankopedia.state.protagonist.equipment,
|
||||
);
|
||||
const boundingRect = canvas.getBoundingClientRect();
|
||||
delta.set(event.clientX, event.clientY).sub(position);
|
||||
position.set(event.clientX, event.clientY);
|
||||
|
||||
[pitch, yaw] = applyPitchYawLimits(
|
||||
pitch - delta.y * (Math.PI / boundingRect.height),
|
||||
yaw + delta.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 skinTree(node, skins, {
|
||||
mesh(_, props, key) {
|
||||
return (
|
||||
<mesh
|
||||
{...props}
|
||||
key={key}
|
||||
onPointerDown={onPointerDown}
|
||||
castShadow
|
||||
receiveShadow
|
||||
/>
|
||||
);
|
||||
},
|
||||
primitive(object, key) {
|
||||
return (
|
||||
<primitive
|
||||
key={key}
|
||||
object={object}
|
||||
onPointerDown={onPointerDown}
|
||||
/>
|
||||
);
|
||||
},
|
||||
});
|
||||
})}
|
||||
<Section
|
||||
nodes={gunNodes}
|
||||
skins={skins}
|
||||
silhouette={silhouette}
|
||||
yaw
|
||||
pitch
|
||||
/>
|
||||
</group>
|
||||
</group>
|
||||
</>
|
||||
|
||||
-153
@@ -1,153 +0,0 @@
|
||||
import { useRef } from "react";
|
||||
import { Group, ShaderMaterial, UniformsLib, UniformsUtils } from "three";
|
||||
import { jsxTree } from "../../../../../../../core/blitzkit/jsxTree";
|
||||
import { useArmor } from "../../../../../../../hooks/useArmor";
|
||||
import { useModel } from "../../../../../../../hooks/useModel";
|
||||
import { useTankModelDefinition } from "../../../../../../../hooks/useTankModelDefinition";
|
||||
import { useTankTransform } from "../../../../../../../hooks/useTankTransform";
|
||||
import { Duel } from "../../../../../../../stores/duel";
|
||||
import { toThreeVector } from "../../../../../../../three/toThreeVector";
|
||||
import { ModelTankWrapper } from "../../../../../../ModelTankWrapper";
|
||||
import fragmentShader from "./shaders/fragment.glsl?raw";
|
||||
import vertexShader from "./shaders/vertex.glsl?raw";
|
||||
|
||||
const skeletonMaterial = new ShaderMaterial({
|
||||
vertexShader,
|
||||
fragmentShader,
|
||||
wireframe: true,
|
||||
fog: true,
|
||||
transparent: true,
|
||||
uniforms: UniformsUtils.merge([UniformsLib.common, UniformsLib.fog]),
|
||||
depthWrite: false,
|
||||
});
|
||||
|
||||
export function TransitionSkeleton() {
|
||||
const tank = Duel.use((state) => state.protagonist.tank);
|
||||
const gun = Duel.use((state) => state.protagonist.gun);
|
||||
const track = Duel.use((state) => state.protagonist.track);
|
||||
const turret = Duel.use((state) => state.protagonist.turret);
|
||||
const turretContainer = useRef<Group>(null!);
|
||||
const gunContainer = useRef<Group>(null!);
|
||||
const { gltf: armorGltf } = useArmor(tank.id);
|
||||
const { gltf: modelGltf } = useModel(tank.id);
|
||||
const modelNodes = Object.values(modelGltf.nodes);
|
||||
const armorNodes = Object.values(armorGltf.nodes);
|
||||
const tankModelDefinition = useTankModelDefinition();
|
||||
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
||||
const gunModelDefinition = turretModelDefinition.guns[gun.id];
|
||||
const trackModelDefinition = tankModelDefinition.tracks[track.id];
|
||||
const wrapper = useRef<Group>(null);
|
||||
const hullOrigin = toThreeVector(trackModelDefinition.origin);
|
||||
const turretOrigin = toThreeVector(tankModelDefinition.turret_origin);
|
||||
const gunOrigin = toThreeVector(turretModelDefinition.gun_origin);
|
||||
|
||||
useTankTransform(track, turret, turretContainer, gunContainer);
|
||||
|
||||
return (
|
||||
<ModelTankWrapper ref={wrapper}>
|
||||
<group position={hullOrigin}>
|
||||
{armorNodes.map((node) => {
|
||||
const isHull = node.name.startsWith("hull_");
|
||||
const isVisible = isHull;
|
||||
|
||||
if (!isVisible) return null;
|
||||
|
||||
return jsxTree(node, {
|
||||
mesh(_, props, key) {
|
||||
return <mesh {...props} key={key} material={skeletonMaterial} />;
|
||||
},
|
||||
});
|
||||
})}
|
||||
</group>
|
||||
|
||||
{modelNodes.map((node) => {
|
||||
const isWheel = node.name.startsWith("chassis_wheel_");
|
||||
const isTrack = node.name.startsWith("chassis_track_");
|
||||
const isVisible = isWheel || isTrack;
|
||||
|
||||
if (!isVisible) return null;
|
||||
|
||||
return jsxTree(node, {
|
||||
mesh(_, props, key) {
|
||||
return <mesh {...props} key={key} material={skeletonMaterial} />;
|
||||
},
|
||||
});
|
||||
})}
|
||||
|
||||
<group ref={turretContainer}>
|
||||
{armorNodes.map((node) => {
|
||||
const isCurrentTurret = node.name.startsWith(
|
||||
`turret_${turretModelDefinition.model_id.toString().padStart(2, "0")}`,
|
||||
);
|
||||
const isVisible = isCurrentTurret;
|
||||
if (!isVisible) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<group key={node.uuid} position={hullOrigin}>
|
||||
<group position={turretOrigin}>
|
||||
{jsxTree(node, {
|
||||
mesh(_, props, key) {
|
||||
return (
|
||||
<mesh {...props} key={key} material={skeletonMaterial} />
|
||||
);
|
||||
},
|
||||
})}
|
||||
</group>
|
||||
</group>
|
||||
);
|
||||
})}
|
||||
|
||||
<group ref={gunContainer}>
|
||||
{armorNodes.map((node) => {
|
||||
const isCurrentGun = node.name.startsWith(
|
||||
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}`,
|
||||
);
|
||||
const isVisible = isCurrentGun;
|
||||
|
||||
if (!isVisible) return null;
|
||||
|
||||
return (
|
||||
<group key={node.uuid} position={hullOrigin}>
|
||||
<group position={turretOrigin.clone().add(gunOrigin)}>
|
||||
{jsxTree(node, {
|
||||
mesh(_, props, key) {
|
||||
return (
|
||||
<mesh
|
||||
{...props}
|
||||
key={key}
|
||||
material={skeletonMaterial}
|
||||
/>
|
||||
);
|
||||
},
|
||||
})}
|
||||
</group>
|
||||
</group>
|
||||
);
|
||||
})}
|
||||
|
||||
{modelNodes.map((node) => {
|
||||
const gunString = `gun_${gunModelDefinition.model_id
|
||||
.toString()
|
||||
.padStart(2, "0")}`;
|
||||
const isCurrentGun = gunModelDefinition.mask
|
||||
? node.name.startsWith(gunString)
|
||||
: node.name === gunString;
|
||||
const isVisible = isCurrentGun;
|
||||
|
||||
if (!isVisible) return null;
|
||||
|
||||
return jsxTree(node, {
|
||||
mesh(_, props, key) {
|
||||
return (
|
||||
<mesh {...props} key={key} material={skeletonMaterial} />
|
||||
);
|
||||
},
|
||||
});
|
||||
})}
|
||||
</group>
|
||||
</group>
|
||||
</ModelTankWrapper>
|
||||
);
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
precision mediump float;
|
||||
|
||||
#define USE_FOG
|
||||
|
||||
#include <fog_pars_fragment>
|
||||
|
||||
void main() {
|
||||
float fogFactor = smoothstep(fogNear, fogFar, vFogDepth);
|
||||
gl_FragColor = vec4(0.5, 0.5, 0.5, fogFactor);
|
||||
gl_FragColor.rgb = mix(fogColor, gl_FragColor.rgb, fogFactor);
|
||||
}
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
precision mediump float;
|
||||
|
||||
#define USE_FOG
|
||||
|
||||
#include <fog_pars_vertex>
|
||||
|
||||
varying vec3 vNormal;
|
||||
varying vec3 vViewPosition;
|
||||
|
||||
void main() {
|
||||
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
||||
|
||||
vViewPosition = mvPosition.xyz;
|
||||
vNormal = normalMatrix * normal;
|
||||
|
||||
gl_Position = projectionMatrix * mvPosition;
|
||||
|
||||
#include <fog_vertex>
|
||||
}
|
||||
@@ -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,4 @@
|
||||
import { Quicklime } from "quicklime";
|
||||
|
||||
// something that casts or receives shadows was added, removed or changed
|
||||
export const sceneChangeEvent = new Quicklime();
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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,21 @@
|
||||
import { useEffect } from "react";
|
||||
import { modelTransformEvent } from "../core/blitzkit/modelTransform";
|
||||
import { sceneChangeEvent } from "../core/three/sceneChange";
|
||||
|
||||
/**
|
||||
* Calls back on mount and whenever shadows may have changed: the tank moved or
|
||||
* something dispatched sceneChangeEvent.
|
||||
*/
|
||||
export function useOnSceneChange(callback: () => void) {
|
||||
useEffect(() => {
|
||||
callback();
|
||||
|
||||
modelTransformEvent.on(callback);
|
||||
sceneChangeEvent.on(callback);
|
||||
|
||||
return () => {
|
||||
modelTransformEvent.off(callback);
|
||||
sceneChangeEvent.off(callback);
|
||||
};
|
||||
}, [callback]);
|
||||
}
|
||||
@@ -0,0 +1,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]);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { invalidate, useThree } from "@react-three/fiber";
|
||||
import { useEffect } from "react";
|
||||
import { useOnSceneChange } from "./useOnSceneChange";
|
||||
|
||||
// shadow maps only re-render when the scene changes, not every frame
|
||||
export function useShadowsOnDemand() {
|
||||
const gl = useThree((state) => state.gl);
|
||||
|
||||
useEffect(() => {
|
||||
gl.shadowMap.autoUpdate = false;
|
||||
|
||||
return () => {
|
||||
gl.shadowMap.autoUpdate = true;
|
||||
};
|
||||
}, [gl]);
|
||||
|
||||
useOnSceneChange(() => {
|
||||
gl.shadowMap.needsUpdate = true;
|
||||
invalidate();
|
||||
});
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { invalidate, useFrame } from "@react-three/fiber";
|
||||
import { sceneChangeEvent } from "../core/three/sceneChange";
|
||||
import { Tankopedia } from "../stores/tankopedia";
|
||||
import type { Skin } from "./useSkin";
|
||||
|
||||
@@ -13,6 +14,7 @@ export function useSkinAnimation(skins: Skin[]) {
|
||||
|
||||
mixer.update(delta);
|
||||
invalidate();
|
||||
sceneChangeEvent.dispatch();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import type { QuicklimeEvent } from "quicklime";
|
||||
import { type RefObject, useEffect } from "react";
|
||||
import { Euler, Group, Vector3 } from "three";
|
||||
import { degToRad } from "three/src/math/MathUtils.js";
|
||||
import { sceneChangeEvent } from "../core/three/sceneChange";
|
||||
import {
|
||||
modelTransformEvent,
|
||||
type ModelTransformEventData,
|
||||
@@ -89,6 +90,7 @@ export function useTankTransform(
|
||||
}
|
||||
|
||||
handleModelTransformInner();
|
||||
sceneChangeEvent.dispatch();
|
||||
|
||||
modelTransformEvent.on(handleModelTransform);
|
||||
|
||||
|
||||
@@ -31,5 +31,5 @@ export const TankopediaPersistent = new Soapstone<TankopediaPersistent>(
|
||||
recentlyViewed: [],
|
||||
highGraphics: true,
|
||||
},
|
||||
"tankopedia-3",
|
||||
"tankopedia-4",
|
||||
);
|
||||
Reference in new issue
Block a user