mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-30 05:14:06 +01:00
Compare commits
4
Commits
c6f8c1e6a5
...
d424679257
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d424679257 | ||
|
|
4248e19f68 | ||
|
|
59ef527967 | ||
|
|
39aeba0766 |
No files matched your search
+4
-1
@@ -14,7 +14,10 @@
|
||||
"RMAO",
|
||||
"Verts",
|
||||
"drei",
|
||||
"frameloop"
|
||||
"frameloop",
|
||||
"pointermove",
|
||||
"pointerup",
|
||||
"pointerdown"
|
||||
],
|
||||
"userWords": [
|
||||
"WOTB",
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
import { I_HAT, J_HAT } from "@blitzkit/core";
|
||||
import { invalidate, useFrame, useThree } from "@react-three/fiber";
|
||||
import { useCallback, useEffect, useRef, type RefObject } from "react";
|
||||
import { Quaternion, Vector2, Vector3 } from "three";
|
||||
import {
|
||||
cameraSwingHorizontalAmplitude,
|
||||
cameraSwingHorizontalFrequency,
|
||||
cameraSwingVerticalAmplitude,
|
||||
cameraSwingVerticalFrequency,
|
||||
initialCameraPhi,
|
||||
initialCameraR,
|
||||
initialCameraTheta,
|
||||
} from "../config/camera";
|
||||
import { Tankopedia } from "../stores/tankopedia";
|
||||
|
||||
const clientPosition = new Vector2();
|
||||
const workingVector3 = new Vector3();
|
||||
const workingQuaternion = new Quaternion();
|
||||
|
||||
const cameraCenter = new Vector3(0, 1, 0);
|
||||
|
||||
const PHI_EPSILON = 2 ** -8;
|
||||
|
||||
export function TankopediaControls() {
|
||||
const canvas = useThree((state) => state.gl.domElement);
|
||||
const camera = useThree((state) => state.camera);
|
||||
|
||||
const r = useRef(initialCameraR);
|
||||
const phi = useRef(initialCameraPhi);
|
||||
const theta = useRef(initialCameraTheta);
|
||||
|
||||
const disturbed = Tankopedia.use((state) => state.disturbed);
|
||||
|
||||
const updateCameraPosition = useCallback(() => {
|
||||
workingVector3
|
||||
.set(
|
||||
Math.sin(phi.current) * Math.sin(theta.current),
|
||||
Math.cos(phi.current),
|
||||
Math.sin(phi.current) * Math.cos(theta.current),
|
||||
)
|
||||
.multiplyScalar(r.current);
|
||||
camera.position.copy(cameraCenter).add(workingVector3);
|
||||
|
||||
workingQuaternion.setFromAxisAngle(I_HAT, phi.current - Math.PI / 2);
|
||||
camera.quaternion
|
||||
.setFromAxisAngle(J_HAT, theta.current)
|
||||
.multiply(workingQuaternion);
|
||||
|
||||
invalidate();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
updateCameraPosition();
|
||||
|
||||
function handlePointerDown(event: PointerEvent) {
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.disturbed = true;
|
||||
});
|
||||
|
||||
clientPosition.set(event.clientX, event.clientY);
|
||||
|
||||
window.addEventListener("pointermove", handlePointerMove);
|
||||
window.addEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
|
||||
function handlePointerMove(event: PointerEvent) {
|
||||
event.preventDefault();
|
||||
|
||||
workingVector3.copy(camera.position).sub(cameraCenter);
|
||||
|
||||
r.current = workingVector3.length();
|
||||
const dx = event.movementX / window.innerWidth;
|
||||
const dy = event.movementY / window.innerHeight;
|
||||
|
||||
const dTheta = -dx * Math.PI;
|
||||
const dPhi = -dy * Math.PI;
|
||||
|
||||
theta.current = Math.atan2(workingVector3.x, workingVector3.z) + dTheta;
|
||||
const phi0 = Math.acos(workingVector3.y / r.current);
|
||||
phi.current = phi0 + dPhi;
|
||||
const maxPhi =
|
||||
Math.PI / 2 + Math.asin(cameraCenter.y / r.current) - PHI_EPSILON;
|
||||
|
||||
if (phi.current < 0 || phi.current > maxPhi) phi.current = phi0;
|
||||
|
||||
updateCameraPosition();
|
||||
}
|
||||
|
||||
function handlePointerUp() {
|
||||
window.removeEventListener("pointermove", handlePointerMove);
|
||||
window.removeEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
|
||||
canvas.addEventListener("pointerdown", handlePointerDown);
|
||||
|
||||
return () => {
|
||||
canvas.removeEventListener("pointerdown", handlePointerDown);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
!disturbed && (
|
||||
<Swing
|
||||
r={r}
|
||||
phi={phi}
|
||||
theta={theta}
|
||||
updateCameraPosition={updateCameraPosition}
|
||||
/>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
interface SwingProps {
|
||||
r: RefObject<number>;
|
||||
phi: RefObject<number>;
|
||||
theta: RefObject<number>;
|
||||
updateCameraPosition: () => void;
|
||||
}
|
||||
|
||||
export function Swing({ r, phi, theta, updateCameraPosition }: SwingProps) {
|
||||
useFrame(({ clock }) => {
|
||||
const t = 2 * Math.PI * clock.getElapsedTime();
|
||||
|
||||
phi.current =
|
||||
initialCameraPhi +
|
||||
cameraSwingVerticalAmplitude * Math.cos(cameraSwingVerticalFrequency * t);
|
||||
theta.current =
|
||||
initialCameraTheta +
|
||||
cameraSwingHorizontalAmplitude *
|
||||
Math.sin(cameraSwingHorizontalFrequency * t);
|
||||
|
||||
updateCameraPosition();
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import { type StageParameters } from "@protos/blitz_static_tank_upgrade_single_stage";
|
||||
import { OrbitControls } from "@react-three/drei";
|
||||
import { Canvas } from "@react-three/fiber";
|
||||
import { fov } from "../../config/camera";
|
||||
import { useTankChassisModel } from "../../hooks/useTankChassisModel";
|
||||
import { useTankGunModel } from "../../hooks/useTankGunModel";
|
||||
import { useTankHullModel } from "../../hooks/useTankHullModel";
|
||||
import { useTankTurretModel } from "../../hooks/useTankTurretModel";
|
||||
import { TankopediaControls } from "../TankopediaControls";
|
||||
import { TankopediaLighting } from "../TankopediaLighting";
|
||||
import { SceneProps } from "../TankopediaSceneProps";
|
||||
import styles from "./index.module.css";
|
||||
@@ -18,7 +19,7 @@ export function TankopediaSandbox({ parameters }: TankopediaSandboxProps) {
|
||||
<div className={styles.wrapper}>
|
||||
<div className={styles.sandbox}>
|
||||
<Canvas
|
||||
camera={{ fov: 40 }}
|
||||
camera={{ fov }}
|
||||
shadows
|
||||
className={styles.canvas}
|
||||
frameloop="demand"
|
||||
@@ -53,8 +54,7 @@ function Content({ parameters }: TankopediaSandboxProps) {
|
||||
|
||||
<TankopediaLighting />
|
||||
<SceneProps />
|
||||
|
||||
<OrbitControls />
|
||||
<TankopediaControls />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { degToRad } from "three/src/math/MathUtils.js";
|
||||
|
||||
export const fov = 20;
|
||||
|
||||
export const initialCameraR = 15;
|
||||
export const initialCameraPhi = degToRad(90 - 10);
|
||||
export const initialCameraTheta = degToRad(90 + 15);
|
||||
|
||||
export const cameraSwingVerticalAmplitude = degToRad(5);
|
||||
export const cameraSwingVerticalFrequency = Math.E ** -6;
|
||||
export const cameraSwingHorizontalAmplitude = degToRad(30);
|
||||
export const cameraSwingHorizontalFrequency = Math.E ** -5;
|
||||
@@ -5,6 +5,8 @@ import { type TankState } from "../tankopedia/tankState";
|
||||
import { characteristicsOrder } from "../tankopedia/characteristicsOrder";
|
||||
|
||||
interface Tankopedia {
|
||||
disturbed: boolean;
|
||||
|
||||
compare: TankopediaCompare;
|
||||
groups: Record<string, boolean>;
|
||||
|
||||
@@ -24,6 +26,8 @@ for (const { name } of characteristicsOrder) {
|
||||
}
|
||||
|
||||
export const Tankopedia = new Varuna<Tankopedia, Tank>((tank) => ({
|
||||
disturbed: false,
|
||||
|
||||
compare: TankopediaCompare.TierAndClass,
|
||||
groups,
|
||||
|
||||
|
||||
@@ -43,6 +43,10 @@ export function createTankState({ tank, id }: Tank) {
|
||||
speed: 0,
|
||||
terrainHardness: TerrainHardness.Hard,
|
||||
|
||||
isHullRotating: false,
|
||||
isTurretRotating: false,
|
||||
isGunRotating: false,
|
||||
|
||||
status: status as Record<VehicleStatusKey, boolean>,
|
||||
} satisfies TankState;
|
||||
}
|
||||
Reference in new issue
Block a user