@pinkaphant/create-kolmo 0.1.4 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -5
- package/bin/create-kolmo.mjs +45 -19
- package/package.json +2 -1
- package/templates/longline/README.md +14 -0
- package/templates/longline/index.html +15 -0
- package/templates/longline/package.json +38 -0
- package/templates/longline/src/App.tsx +48 -0
- package/templates/longline/src/components/Boat.tsx +97 -0
- package/templates/longline/src/components/CameraRig.tsx +61 -0
- package/templates/longline/src/components/Crates.tsx +48 -0
- package/templates/longline/src/components/Hud.tsx +197 -0
- package/templates/longline/src/components/Longline.tsx +107 -0
- package/templates/longline/src/components/Ocean.tsx +27 -0
- package/templates/longline/src/components/Radar.tsx +88 -0
- package/templates/longline/src/components/Referee.tsx +39 -0
- package/templates/longline/src/components/Scene.tsx +56 -0
- package/templates/longline/src/components/Sky.tsx +23 -0
- package/templates/longline/src/components/ocean-material.ts +143 -0
- package/templates/longline/src/components/round-context.ts +26 -0
- package/templates/longline/src/game/boat-control.ts +131 -0
- package/templates/longline/src/game/constants.ts +98 -0
- package/templates/longline/src/game/input.test.ts +83 -0
- package/templates/longline/src/game/input.ts +73 -0
- package/templates/longline/src/game/logic.test.ts +110 -0
- package/templates/longline/src/game/logic.ts +109 -0
- package/templates/longline/src/game/physics.test.ts +211 -0
- package/templates/longline/src/game/radar.ts +22 -0
- package/templates/longline/src/game/registry.ts +53 -0
- package/templates/longline/src/game/sea.ts +26 -0
- package/templates/longline/src/game/session.ts +6 -0
- package/templates/longline/src/game/simulation.test.ts +393 -0
- package/templates/longline/src/game/simulation.ts +137 -0
- package/templates/longline/src/game/store.test.ts +222 -0
- package/templates/longline/src/game/store.ts +195 -0
- package/templates/longline/src/game/test-world.ts +128 -0
- package/templates/longline/src/main.tsx +8 -0
- package/templates/longline/src/sfx.ts +50 -0
- package/templates/longline/src/styles.css +374 -0
- package/templates/longline/tsconfig.json +18 -0
- package/templates/longline/vite.config.ts +28 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { useBuoyancy } from "@pinkaphant/kolmo/water/react";
|
|
2
|
+
import { useFrame } from "@react-three/fiber";
|
|
3
|
+
import { BallCollider, RigidBody, useRopeJoint, type RapierRigidBody } from "@react-three/rapier";
|
|
4
|
+
import { useLayoutEffect, useRef, type RefObject } from "react";
|
|
5
|
+
import * as THREE from "three";
|
|
6
|
+
import { BOAT, FIXED_DT, LINE } from "../game/constants.js";
|
|
7
|
+
import { NO_CONTACT } from "./Boat.js";
|
|
8
|
+
import { useRound } from "./round-context.js";
|
|
9
|
+
|
|
10
|
+
function Float({ index }: { index: number }) {
|
|
11
|
+
const { sea, clock, registry } = useRound();
|
|
12
|
+
const self = registry.floats[index]!;
|
|
13
|
+
const prev = index === 0 ? registry.boat : registry.floats[index - 1]!;
|
|
14
|
+
const object = useRef<THREE.Object3D>(null);
|
|
15
|
+
useBuoyancy(self, { surface: sea.surface, clock, timeStep: FIXED_DT });
|
|
16
|
+
// the first rope is made fast to the stern, the others to the float before
|
|
17
|
+
useRopeJoint(prev as RefObject<RapierRigidBody>, self as RefObject<RapierRigidBody>, [
|
|
18
|
+
index === 0 ? [BOAT.tow[0], BOAT.tow[1], BOAT.tow[2]] : [0, 0, 0],
|
|
19
|
+
[0, 0, 0],
|
|
20
|
+
LINE.spacing,
|
|
21
|
+
]);
|
|
22
|
+
useLayoutEffect(() => {
|
|
23
|
+
registry.floatObjects[index] = object.current;
|
|
24
|
+
return () => {
|
|
25
|
+
registry.floatObjects[index] = null;
|
|
26
|
+
};
|
|
27
|
+
}, [registry, index]);
|
|
28
|
+
const last = index === LINE.floats - 1;
|
|
29
|
+
return (
|
|
30
|
+
<RigidBody ref={self} colliders={false} position={[0, 0, BOAT.tow[2] + (index + 1) * LINE.spacing]} canSleep={false}>
|
|
31
|
+
<BallCollider args={[LINE.floatRadius]} density={LINE.floatDensity} collisionGroups={NO_CONTACT} />
|
|
32
|
+
<object3D ref={object} />
|
|
33
|
+
<mesh>
|
|
34
|
+
<sphereGeometry args={[LINE.floatRadius, 16, 12]} />
|
|
35
|
+
<meshStandardMaterial color="#ff7a1a" roughness={0.5} />
|
|
36
|
+
</mesh>
|
|
37
|
+
{last ? (
|
|
38
|
+
<group>
|
|
39
|
+
<mesh position={[0, 0.7, 0]}>
|
|
40
|
+
<cylinderGeometry args={[0.025, 0.025, 1.2, 6]} />
|
|
41
|
+
<meshStandardMaterial color="#d9d9d9" />
|
|
42
|
+
</mesh>
|
|
43
|
+
<mesh position={[0.18, 1.15, 0]}>
|
|
44
|
+
<boxGeometry args={[0.36, 0.22, 0.02]} />
|
|
45
|
+
<meshStandardMaterial color="#ffd23d" />
|
|
46
|
+
</mesh>
|
|
47
|
+
</group>
|
|
48
|
+
) : null}
|
|
49
|
+
</RigidBody>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const UP = new THREE.Vector3(0, 1, 0);
|
|
54
|
+
|
|
55
|
+
/** The rope itself: one thin cylinder per span, from the stern through every float, laid out each frame. */
|
|
56
|
+
function Rope() {
|
|
57
|
+
const { registry } = useRound();
|
|
58
|
+
const mesh = useRef<THREE.InstancedMesh>(null);
|
|
59
|
+
const points = useRef(Array.from({ length: LINE.floats + 1 }, () => new THREE.Vector3()));
|
|
60
|
+
const scratch = useRef({ mid: new THREE.Vector3(), dir: new THREE.Vector3(), quat: new THREE.Quaternion(), scale: new THREE.Vector3(), m: new THREE.Matrix4() });
|
|
61
|
+
useFrame(() => {
|
|
62
|
+
const m = mesh.current;
|
|
63
|
+
const boat = registry.boatObject;
|
|
64
|
+
if (m === null || boat === null) return;
|
|
65
|
+
const pts = points.current;
|
|
66
|
+
const { mid, dir, quat, scale, m: matrix } = scratch.current;
|
|
67
|
+
boat.updateWorldMatrix(true, false);
|
|
68
|
+
pts[0]!.set(BOAT.tow[0], BOAT.tow[1], BOAT.tow[2]).applyMatrix4(boat.matrixWorld);
|
|
69
|
+
for (let i = 0; i < LINE.floats; i++) {
|
|
70
|
+
const o = registry.floatObjects[i];
|
|
71
|
+
if (o === null || o === undefined) return;
|
|
72
|
+
o.updateWorldMatrix(true, false);
|
|
73
|
+
o.getWorldPosition(pts[i + 1]!);
|
|
74
|
+
}
|
|
75
|
+
for (let i = 0; i < LINE.floats; i++) {
|
|
76
|
+
const a = pts[i]!;
|
|
77
|
+
const b = pts[i + 1]!;
|
|
78
|
+
dir.subVectors(b, a);
|
|
79
|
+
const len = dir.length();
|
|
80
|
+
if (len < 1e-6) continue;
|
|
81
|
+
quat.setFromUnitVectors(UP, dir.divideScalar(len));
|
|
82
|
+
mid.addVectors(a, b).multiplyScalar(0.5);
|
|
83
|
+
scale.set(1, len, 1);
|
|
84
|
+
matrix.compose(mid, quat, scale);
|
|
85
|
+
m.setMatrixAt(i, matrix);
|
|
86
|
+
}
|
|
87
|
+
m.instanceMatrix.needsUpdate = true;
|
|
88
|
+
});
|
|
89
|
+
return (
|
|
90
|
+
<instancedMesh ref={mesh} args={[undefined, undefined, LINE.floats]} frustumCulled={false}>
|
|
91
|
+
<cylinderGeometry args={[0.03, 0.03, 1, 6]} />
|
|
92
|
+
<meshStandardMaterial color="#2b2b2b" roughness={0.9} />
|
|
93
|
+
</instancedMesh>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The longline: floats on rope joints behind the stern, and the rope drawn through them. */
|
|
98
|
+
export function Longline() {
|
|
99
|
+
return (
|
|
100
|
+
<>
|
|
101
|
+
{Array.from({ length: LINE.floats }, (_, i) => (
|
|
102
|
+
<Float key={i} index={i} />
|
|
103
|
+
))}
|
|
104
|
+
<Rope />
|
|
105
|
+
</>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useFrame } from "@react-three/fiber";
|
|
2
|
+
import { useEffect, useMemo } from "react";
|
|
3
|
+
import { OCEAN, createOceanGeometry, createOceanMaterial } from "./ocean-material.js";
|
|
4
|
+
import { useRound } from "./round-context.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The sea. The vertex shader runs the same Gerstner sum as the wave field the hulls float on, and reads the same
|
|
8
|
+
* clock, so a hull sits in the water it is drawn in. The grid follows the boat in whole cells, so its vertices stay on
|
|
9
|
+
* a lattice of rest positions and the waves do not shimmer as it moves.
|
|
10
|
+
*/
|
|
11
|
+
export function Ocean() {
|
|
12
|
+
const { sea, clock, registry } = useRound();
|
|
13
|
+
const material = useMemo(() => createOceanMaterial(sea.field, sea.state.foam), [sea]);
|
|
14
|
+
const geometry = useMemo(() => createOceanGeometry(), []);
|
|
15
|
+
useEffect(() => () => material.dispose(), [material]);
|
|
16
|
+
useEffect(() => () => geometry.dispose(), [geometry]);
|
|
17
|
+
useFrame(() => {
|
|
18
|
+
const boat = registry.boat.current;
|
|
19
|
+
const cell = OCEAN.size / OCEAN.segments;
|
|
20
|
+
if (boat !== null) {
|
|
21
|
+
const p = boat.translation();
|
|
22
|
+
material.uniforms.uOrigin.value.set(Math.round(p.x / cell) * cell, Math.round(p.z / cell) * cell);
|
|
23
|
+
}
|
|
24
|
+
material.uniforms.uTime.value = clock.time;
|
|
25
|
+
});
|
|
26
|
+
return <mesh geometry={geometry} material={material} frustumCulled={false} />;
|
|
27
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
import { ARENA, CRATE } from "../game/constants.js";
|
|
3
|
+
import { radar } from "../game/radar.js";
|
|
4
|
+
|
|
5
|
+
const SIZE = 156;
|
|
6
|
+
const RANGE = ARENA.radius + 12;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A heading-up radar of the arena: the boat in the middle pointing up, the crates around it, the edge as a ring. It
|
|
10
|
+
* reads `radar`, which the referee writes every physics step, in its own animation frame loop, so it costs React nothing.
|
|
11
|
+
*/
|
|
12
|
+
export function Radar() {
|
|
13
|
+
const canvas = useRef<HTMLCanvasElement>(null);
|
|
14
|
+
const warning = useRef<HTMLDivElement>(null);
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
const el = canvas.current;
|
|
18
|
+
const ctx = el?.getContext("2d") ?? null;
|
|
19
|
+
if (el === null || ctx === null) return;
|
|
20
|
+
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
21
|
+
el.width = SIZE * dpr;
|
|
22
|
+
el.height = SIZE * dpr;
|
|
23
|
+
ctx.scale(dpr, dpr);
|
|
24
|
+
const half = SIZE / 2;
|
|
25
|
+
const scale = (half - 4) / RANGE;
|
|
26
|
+
let frame = 0;
|
|
27
|
+
let outside = false;
|
|
28
|
+
|
|
29
|
+
const draw = (): void => {
|
|
30
|
+
frame = requestAnimationFrame(draw);
|
|
31
|
+
ctx.clearRect(0, 0, SIZE, SIZE);
|
|
32
|
+
ctx.save();
|
|
33
|
+
ctx.beginPath();
|
|
34
|
+
ctx.arc(half, half, half - 1, 0, Math.PI * 2);
|
|
35
|
+
ctx.fillStyle = "rgba(6, 36, 56, 0.62)";
|
|
36
|
+
ctx.fill();
|
|
37
|
+
ctx.clip();
|
|
38
|
+
if (radar.ready) {
|
|
39
|
+
const sin = Math.sin(radar.heading);
|
|
40
|
+
const cos = Math.cos(radar.heading);
|
|
41
|
+
// world offset -> screen: right is (cos, sin), forward is (sin, -cos), forward is up
|
|
42
|
+
const toScreen = (dx: number, dz: number): [number, number] => [half + (dx * cos + dz * sin) * scale, half - (dx * sin - dz * cos) * scale];
|
|
43
|
+
const [ox, oy] = toScreen(-radar.boatX, -radar.boatZ);
|
|
44
|
+
ctx.beginPath();
|
|
45
|
+
ctx.arc(ox, oy, ARENA.radius * scale, 0, Math.PI * 2);
|
|
46
|
+
ctx.strokeStyle = "rgba(255, 210, 61, 0.75)";
|
|
47
|
+
ctx.lineWidth = 1.5;
|
|
48
|
+
ctx.stroke();
|
|
49
|
+
ctx.fillStyle = "#ff3d6e";
|
|
50
|
+
for (let i = 0; i < CRATE.count; i++) {
|
|
51
|
+
const [cx, cy] = toScreen(radar.crates[i * 2]! - radar.boatX, radar.crates[i * 2 + 1]! - radar.boatZ);
|
|
52
|
+
ctx.fillRect(cx - 2.5, cy - 2.5, 5, 5);
|
|
53
|
+
}
|
|
54
|
+
const isOut = Math.hypot(radar.boatX, radar.boatZ) > ARENA.radius;
|
|
55
|
+
if (isOut !== outside) {
|
|
56
|
+
outside = isOut;
|
|
57
|
+
if (warning.current !== null) warning.current.style.opacity = isOut ? "1" : "0";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
ctx.restore();
|
|
61
|
+
// the boat: a small arrow pointing up
|
|
62
|
+
ctx.beginPath();
|
|
63
|
+
ctx.moveTo(half, half - 6);
|
|
64
|
+
ctx.lineTo(half + 4.5, half + 5);
|
|
65
|
+
ctx.lineTo(half, half + 2.5);
|
|
66
|
+
ctx.lineTo(half - 4.5, half + 5);
|
|
67
|
+
ctx.closePath();
|
|
68
|
+
ctx.fillStyle = "#ffffff";
|
|
69
|
+
ctx.fill();
|
|
70
|
+
ctx.beginPath();
|
|
71
|
+
ctx.arc(half, half, half - 1, 0, Math.PI * 2);
|
|
72
|
+
ctx.strokeStyle = "rgba(255, 255, 255, 0.55)";
|
|
73
|
+
ctx.lineWidth = 1.5;
|
|
74
|
+
ctx.stroke();
|
|
75
|
+
};
|
|
76
|
+
frame = requestAnimationFrame(draw);
|
|
77
|
+
return () => cancelAnimationFrame(frame);
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<>
|
|
82
|
+
<div className="warning" ref={warning} aria-live="polite">
|
|
83
|
+
Out of bounds: turn back
|
|
84
|
+
</div>
|
|
85
|
+
<canvas className="radar" ref={canvas} style={{ width: SIZE, height: SIZE }} aria-label="Radar of the crates around the boat" />
|
|
86
|
+
</>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useAfterPhysicsStep, useBeforePhysicsStep } from "@react-three/rapier";
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
import { planeHeading, type DriveInput } from "../game/boat-control.js";
|
|
4
|
+
import { CRATE, FIXED_DT } from "../game/constants.js";
|
|
5
|
+
import { radar } from "../game/radar.js";
|
|
6
|
+
import { useRound } from "./round-context.js";
|
|
7
|
+
|
|
8
|
+
const heading = { fx: 0, fz: -1, rx: 1, rz: 0 };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Runs the rules of the round in the physics world's own step: the helm and the arena edge before it, the hauls, the
|
|
12
|
+
* lost crates and the clock after it. It renders nothing.
|
|
13
|
+
*/
|
|
14
|
+
export function Referee() {
|
|
15
|
+
const { registry, simulation, clock, helm } = useRound();
|
|
16
|
+
const input = useRef<DriveInput>({ throttle: 0, steer: 0 });
|
|
17
|
+
|
|
18
|
+
useBeforePhysicsStep(() => {
|
|
19
|
+
const fleet = registry.fleet();
|
|
20
|
+
if (fleet === null) return;
|
|
21
|
+
helm.read(input.current);
|
|
22
|
+
const wet = registry.boatController?.stats.submergedFraction ?? 0;
|
|
23
|
+
simulation.beforeStep(fleet, input.current, wet, FIXED_DT);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
useAfterPhysicsStep(() => {
|
|
27
|
+
const fleet = registry.fleet();
|
|
28
|
+
if (fleet === null) return;
|
|
29
|
+
simulation.afterStep(fleet, clock.time, FIXED_DT);
|
|
30
|
+
const p = fleet.boat.translation();
|
|
31
|
+
planeHeading(fleet.boat.rotation(), heading);
|
|
32
|
+
radar.boatX = p.x;
|
|
33
|
+
radar.boatZ = p.z;
|
|
34
|
+
radar.heading = Math.atan2(heading.fx, -heading.fz);
|
|
35
|
+
radar.crates.set(simulation.crateXZ.subarray(0, CRATE.count * 2));
|
|
36
|
+
radar.ready = true;
|
|
37
|
+
});
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { usePhysicsClock } from "@pinkaphant/kolmo/water/react";
|
|
2
|
+
import { Physics } from "@react-three/rapier";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { FIXED_DT, GRAVITY, type SeaStateId } from "../game/constants.js";
|
|
5
|
+
import type { Helm } from "../game/input.js";
|
|
6
|
+
import { Registry } from "../game/registry.js";
|
|
7
|
+
import { createSea } from "../game/sea.js";
|
|
8
|
+
import { Simulation } from "../game/simulation.js";
|
|
9
|
+
import type { Game } from "../game/store.js";
|
|
10
|
+
import { Boat } from "./Boat.js";
|
|
11
|
+
import { CameraRig } from "./CameraRig.js";
|
|
12
|
+
import { Crates } from "./Crates.js";
|
|
13
|
+
import { Longline } from "./Longline.js";
|
|
14
|
+
import { Ocean } from "./Ocean.js";
|
|
15
|
+
import { Referee } from "./Referee.js";
|
|
16
|
+
import { Sky } from "./Sky.js";
|
|
17
|
+
import { RoundContext, type Round } from "./round-context.js";
|
|
18
|
+
|
|
19
|
+
export interface SceneProps {
|
|
20
|
+
sea: SeaStateId;
|
|
21
|
+
/** Counts up with every round; it is the seed of the crates, so a round replays the same way. */
|
|
22
|
+
runId: number;
|
|
23
|
+
game: Game;
|
|
24
|
+
helm: Helm;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function RoundScene({ sea: seaId, runId, game, helm }: SceneProps) {
|
|
28
|
+
const clock = usePhysicsClock();
|
|
29
|
+
const round = useMemo<Round>(() => {
|
|
30
|
+
const sea = createSea(seaId);
|
|
31
|
+
return { sea, clock, registry: new Registry(), simulation: new Simulation(game, sea.field, 1000 + runId), game, helm };
|
|
32
|
+
}, [seaId, runId, clock, game, helm]);
|
|
33
|
+
return (
|
|
34
|
+
<RoundContext.Provider value={round}>
|
|
35
|
+
<Sky />
|
|
36
|
+
<Ocean />
|
|
37
|
+
<Boat />
|
|
38
|
+
<Longline />
|
|
39
|
+
<Crates />
|
|
40
|
+
<Referee />
|
|
41
|
+
<CameraRig />
|
|
42
|
+
</RoundContext.Provider>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* One round's world: the sea, the boat, the line, the crates and the rules, in a <Physics> with a fixed step. Mount it
|
|
48
|
+
* with a new `key` for a new round.
|
|
49
|
+
*/
|
|
50
|
+
export function Scene(props: SceneProps) {
|
|
51
|
+
return (
|
|
52
|
+
<Physics gravity={[0, -GRAVITY, 0]} timeStep={FIXED_DT}>
|
|
53
|
+
<RoundScene {...props} />
|
|
54
|
+
</Physics>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useEffect, useMemo } from "react";
|
|
2
|
+
import * as THREE from "three";
|
|
3
|
+
import { PALETTE, SUN, createSkyMaterial } from "./ocean-material.js";
|
|
4
|
+
|
|
5
|
+
/** A gradient dome that never moves relative to the camera, and the lights of the scene. */
|
|
6
|
+
export function Sky() {
|
|
7
|
+
const material = useMemo(() => createSkyMaterial(), []);
|
|
8
|
+
const geometry = useMemo(() => new THREE.SphereGeometry(1, 24, 16), []);
|
|
9
|
+
useEffect(
|
|
10
|
+
() => () => {
|
|
11
|
+
material.dispose();
|
|
12
|
+
geometry.dispose();
|
|
13
|
+
},
|
|
14
|
+
[material, geometry],
|
|
15
|
+
);
|
|
16
|
+
return (
|
|
17
|
+
<>
|
|
18
|
+
<mesh geometry={geometry} material={material} frustumCulled={false} renderOrder={-1} />
|
|
19
|
+
<hemisphereLight args={[PALETTE.zenith, PALETTE.deep, 1.1]} />
|
|
20
|
+
<directionalLight position={[SUN.x * 60, SUN.y * 60, SUN.z * 60]} intensity={2.2} color="#fff1d6" />
|
|
21
|
+
</>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
import type { WaveField } from "@pinkaphant/kolmo/water";
|
|
3
|
+
|
|
4
|
+
/** Clear, saturated scene colours. The horizon stays crisp instead of dissolving into fog. */
|
|
5
|
+
export const PALETTE = {
|
|
6
|
+
deep: "#062f4d",
|
|
7
|
+
shallow: "#1597a6",
|
|
8
|
+
horizon: "#b9dbea",
|
|
9
|
+
zenith: "#4f93c9",
|
|
10
|
+
foam: "#f2f8fb",
|
|
11
|
+
} as const;
|
|
12
|
+
|
|
13
|
+
/** Where the sun is: a unit vector toward it. */
|
|
14
|
+
export const SUN = new THREE.Vector3(-0.45, 0.62, -0.64).normalize();
|
|
15
|
+
|
|
16
|
+
/** Broad, readable triangles: the rendering grid, not the physics grid. */
|
|
17
|
+
export const OCEAN = { size: 220, segments: 110 } as const;
|
|
18
|
+
|
|
19
|
+
const vertex = /* glsl */ `
|
|
20
|
+
uniform float uTime;
|
|
21
|
+
uniform vec2 uOrigin;
|
|
22
|
+
uniform float uLevel;
|
|
23
|
+
uniform vec4 uDirKW[WAVE_COUNT];
|
|
24
|
+
uniform vec4 uAmp[WAVE_COUNT];
|
|
25
|
+
varying vec3 vWorld;
|
|
26
|
+
|
|
27
|
+
// The exact same Gerstner displacement as WaveField.displace. Only the shading is faceted.
|
|
28
|
+
void main() {
|
|
29
|
+
vec2 rest = position.xz + uOrigin;
|
|
30
|
+
vec3 p = vec3(rest.x, uLevel, rest.y);
|
|
31
|
+
for (int i = 0; i < WAVE_COUNT; i++) {
|
|
32
|
+
vec4 d = uDirKW[i];
|
|
33
|
+
vec4 w = uAmp[i];
|
|
34
|
+
float theta = d.z * (d.x * rest.x + d.y * rest.y) - d.w * uTime + w.z;
|
|
35
|
+
p.x += w.w * d.x * cos(theta);
|
|
36
|
+
p.z += w.w * d.y * cos(theta);
|
|
37
|
+
p.y += w.x * sin(theta);
|
|
38
|
+
}
|
|
39
|
+
vWorld = p;
|
|
40
|
+
gl_Position = projectionMatrix * viewMatrix * vec4(p, 1.0);
|
|
41
|
+
}
|
|
42
|
+
`;
|
|
43
|
+
|
|
44
|
+
const fragment = /* glsl */ `
|
|
45
|
+
uniform vec3 uDeep;
|
|
46
|
+
uniform vec3 uShallow;
|
|
47
|
+
uniform vec3 uFoam;
|
|
48
|
+
uniform vec3 uSun;
|
|
49
|
+
uniform float uLevel;
|
|
50
|
+
uniform float uCrest;
|
|
51
|
+
uniform float uFoamAmount;
|
|
52
|
+
varying vec3 vWorld;
|
|
53
|
+
|
|
54
|
+
void main() {
|
|
55
|
+
// One face normal per triangle. No interpolated normals, Fresnel, specular, or atmospheric smoothing.
|
|
56
|
+
vec3 n = normalize(cross(dFdx(vWorld), dFdy(vWorld)));
|
|
57
|
+
if (n.y < 0.0) n = -n;
|
|
58
|
+
float lift = clamp((vWorld.y - uLevel) / max(uCrest, 0.05) * 0.5 + 0.5, 0.0, 1.0);
|
|
59
|
+
float diffuse = max(dot(n, uSun), 0.0);
|
|
60
|
+
float heightBand = floor(lift * 4.0) / 3.0;
|
|
61
|
+
float lightBand = floor(diffuse * 3.0) / 2.0;
|
|
62
|
+
vec3 col = mix(uDeep, uShallow, clamp(heightBand * 0.65 + lightBand * 0.25, 0.0, 1.0));
|
|
63
|
+
float foam = step(0.88, lift) * step(0.35, diffuse) * uFoamAmount;
|
|
64
|
+
col = mix(col, uFoam, foam);
|
|
65
|
+
gl_FragColor = vec4(col, 1.0);
|
|
66
|
+
#include <colorspace_fragment>
|
|
67
|
+
}
|
|
68
|
+
`;
|
|
69
|
+
|
|
70
|
+
export interface OceanUniforms {
|
|
71
|
+
[name: string]: THREE.IUniform;
|
|
72
|
+
uTime: THREE.IUniform<number>;
|
|
73
|
+
uOrigin: THREE.IUniform<THREE.Vector2>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The sea's material for one wave field. `uTime` and `uOrigin` are the two things that change every frame. */
|
|
77
|
+
export function createOceanMaterial(field: WaveField, foam: number): THREE.ShaderMaterial & { uniforms: OceanUniforms } {
|
|
78
|
+
const u = field.toUniforms();
|
|
79
|
+
const uniforms: OceanUniforms = {
|
|
80
|
+
uTime: { value: 0 },
|
|
81
|
+
uOrigin: { value: new THREE.Vector2() },
|
|
82
|
+
uLevel: { value: u.level },
|
|
83
|
+
uDirKW: { value: u.dirKW },
|
|
84
|
+
uAmp: { value: u.ampSteepPhase },
|
|
85
|
+
uCrest: { value: field.maxCrest },
|
|
86
|
+
uFoamAmount: { value: foam },
|
|
87
|
+
uDeep: { value: new THREE.Color(PALETTE.deep) },
|
|
88
|
+
uShallow: { value: new THREE.Color(PALETTE.shallow) },
|
|
89
|
+
uFoam: { value: new THREE.Color(PALETTE.foam) },
|
|
90
|
+
uSun: { value: SUN },
|
|
91
|
+
};
|
|
92
|
+
return new THREE.ShaderMaterial({
|
|
93
|
+
vertexShader: vertex,
|
|
94
|
+
fragmentShader: fragment,
|
|
95
|
+
uniforms,
|
|
96
|
+
defines: { WAVE_COUNT: u.count },
|
|
97
|
+
}) as THREE.ShaderMaterial & { uniforms: OceanUniforms };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The sea mesh's geometry: a flat grid of rest positions in the XZ plane, centred on the origin. */
|
|
101
|
+
export function createOceanGeometry(): THREE.PlaneGeometry {
|
|
102
|
+
const g = new THREE.PlaneGeometry(OCEAN.size, OCEAN.size, OCEAN.segments, OCEAN.segments);
|
|
103
|
+
g.rotateX(-Math.PI / 2);
|
|
104
|
+
return g;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const skyVertex = /* glsl */ `
|
|
108
|
+
varying vec3 vDir;
|
|
109
|
+
void main() {
|
|
110
|
+
vDir = normalize(position);
|
|
111
|
+
vec4 pos = projectionMatrix * mat4(mat3(viewMatrix)) * vec4(position, 1.0);
|
|
112
|
+
gl_Position = pos.xyww;
|
|
113
|
+
}
|
|
114
|
+
`;
|
|
115
|
+
|
|
116
|
+
const skyFragment = /* glsl */ `
|
|
117
|
+
uniform vec3 uHorizon;
|
|
118
|
+
uniform vec3 uZenith;
|
|
119
|
+
uniform vec3 uSun;
|
|
120
|
+
varying vec3 vDir;
|
|
121
|
+
void main() {
|
|
122
|
+
vec3 d = normalize(vDir);
|
|
123
|
+
vec3 col = mix(uHorizon, uZenith, pow(max(d.y, 0.0), 0.55));
|
|
124
|
+
float s = max(dot(d, uSun), 0.0);
|
|
125
|
+
col += vec3(1.0, 0.9, 0.7) * (pow(s, 600.0) * 2.0 + pow(s, 14.0) * 0.25);
|
|
126
|
+
gl_FragColor = vec4(col, 1.0);
|
|
127
|
+
#include <colorspace_fragment>
|
|
128
|
+
}
|
|
129
|
+
`;
|
|
130
|
+
|
|
131
|
+
export function createSkyMaterial(): THREE.ShaderMaterial {
|
|
132
|
+
return new THREE.ShaderMaterial({
|
|
133
|
+
vertexShader: skyVertex,
|
|
134
|
+
fragmentShader: skyFragment,
|
|
135
|
+
uniforms: {
|
|
136
|
+
uHorizon: { value: new THREE.Color(PALETTE.horizon) },
|
|
137
|
+
uZenith: { value: new THREE.Color(PALETTE.zenith) },
|
|
138
|
+
uSun: { value: SUN },
|
|
139
|
+
},
|
|
140
|
+
side: THREE.BackSide,
|
|
141
|
+
depthWrite: false,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PhysicsClock } from "@pinkaphant/kolmo/water/react";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
import type { Helm } from "../game/input.js";
|
|
4
|
+
import type { Registry } from "../game/registry.js";
|
|
5
|
+
import type { Sea } from "../game/sea.js";
|
|
6
|
+
import type { Simulation } from "../game/simulation.js";
|
|
7
|
+
import type { Game } from "../game/store.js";
|
|
8
|
+
|
|
9
|
+
/** What every part of one round's scene shares. */
|
|
10
|
+
export interface Round {
|
|
11
|
+
sea: Sea;
|
|
12
|
+
/** The water's time: the sum of the physics steps taken. The hulls and the shader read this same clock. */
|
|
13
|
+
clock: PhysicsClock;
|
|
14
|
+
registry: Registry;
|
|
15
|
+
simulation: Simulation;
|
|
16
|
+
game: Game;
|
|
17
|
+
helm: Helm;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const RoundContext = createContext<Round | null>(null);
|
|
21
|
+
|
|
22
|
+
export function useRound(): Round {
|
|
23
|
+
const round = useContext(RoundContext);
|
|
24
|
+
if (round === null) throw new Error("useRound: no round here; this component belongs inside <Scene>");
|
|
25
|
+
return round;
|
|
26
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { BOAT } from "./constants.js";
|
|
2
|
+
|
|
3
|
+
export interface Vec3 {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
z: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Quat {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
z: number;
|
|
13
|
+
w: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** What the helm needs of a body. Rapier's `RigidBody` satisfies it, so the tests drive a real Rapier body. */
|
|
17
|
+
export interface DrivableBody {
|
|
18
|
+
rotation(): Quat;
|
|
19
|
+
linvel(): Vec3;
|
|
20
|
+
angvel(): Vec3;
|
|
21
|
+
mass(): number;
|
|
22
|
+
principalInertia(): Vec3;
|
|
23
|
+
applyImpulse(impulse: Vec3, wakeUp: boolean): void;
|
|
24
|
+
applyTorqueImpulse(torque: Vec3, wakeUp: boolean): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** throttle: +1 full ahead, -1 full astern. steer: +1 hard to starboard (right), -1 hard to port. */
|
|
28
|
+
export interface DriveInput {
|
|
29
|
+
throttle: number;
|
|
30
|
+
steer: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const clamp = (v: number, lo: number, hi: number): number => (v < lo ? lo : v > hi ? hi : v);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The hull's forward direction on the water plane (unit length, y = 0) and its starboard side. Forward is -Z in the
|
|
37
|
+
* hull's frame. A hull standing on its end has no heading on the plane and gets the world's -Z.
|
|
38
|
+
*/
|
|
39
|
+
export function planeHeading(q: Quat, out: { fx: number; fz: number; rx: number; rz: number }): void {
|
|
40
|
+
// (0, 0, -1) rotated by q, x and z parts
|
|
41
|
+
let fx = -2 * (q.x * q.z + q.y * q.w);
|
|
42
|
+
let fz = -(1 - 2 * (q.x * q.x + q.y * q.y));
|
|
43
|
+
const len = Math.hypot(fx, fz);
|
|
44
|
+
if (len < 1e-6) {
|
|
45
|
+
fx = 0;
|
|
46
|
+
fz = -1;
|
|
47
|
+
} else {
|
|
48
|
+
fx /= len;
|
|
49
|
+
fz /= len;
|
|
50
|
+
}
|
|
51
|
+
out.fx = fx;
|
|
52
|
+
out.fz = fz;
|
|
53
|
+
// starboard is forward x up
|
|
54
|
+
out.rx = -fz;
|
|
55
|
+
out.rz = fx;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** How much of the helm's grip the hull has: nothing when it is clear of the water, all of it from a third submerged. */
|
|
59
|
+
export function wetGrip(submergedFraction: number): number {
|
|
60
|
+
return clamp(submergedFraction / 0.3, 0, 1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The yaw rate the helm asks for at full lock, rad/s: tighter when slow, gentler at speed. */
|
|
64
|
+
export function yawRateAt(speed: number): number {
|
|
65
|
+
const t = clamp(speed / BOAT.yawFastSpeed, 0, 1);
|
|
66
|
+
return BOAT.yawRate + (BOAT.yawRateFast - BOAT.yawRate) * t;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const heading = { fx: 0, fz: -1, rx: 1, rz: 0 };
|
|
70
|
+
|
|
71
|
+
/** Rotates v by q (or by q's inverse), into `out`. `out` may be `v`. */
|
|
72
|
+
export function rotateByQuat(q: Quat, v: Vec3, inverse: boolean, out: Vec3): Vec3 {
|
|
73
|
+
const qx = inverse ? -q.x : q.x;
|
|
74
|
+
const qy = inverse ? -q.y : q.y;
|
|
75
|
+
const qz = inverse ? -q.z : q.z;
|
|
76
|
+
const vx = v.x;
|
|
77
|
+
const vy = v.y;
|
|
78
|
+
const vz = v.z;
|
|
79
|
+
const tx = 2 * (qy * vz - qz * vy);
|
|
80
|
+
const ty = 2 * (qz * vx - qx * vz);
|
|
81
|
+
const tz = 2 * (qx * vy - qy * vx);
|
|
82
|
+
out.x = vx + q.w * tx + (qy * tz - qz * ty);
|
|
83
|
+
out.y = vy + q.w * ty + (qz * tx - qx * tz);
|
|
84
|
+
out.z = vz + q.w * tz + (qx * ty - qy * tx);
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const spin = { x: 0, y: 0, z: 0 };
|
|
89
|
+
const damp = { x: 0, y: 0, z: 0 };
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* One step of the helm, called before the world steps: thrust along the heading, a yaw torque that chases the asked-for
|
|
93
|
+
* yaw rate, and the keel's grip on sideways motion. All three are impulses of this step, scaled by `grip` (0 to 1, how
|
|
94
|
+
* much of the hull is in the water), so a hull flung clear of a wave cannot push, steer or grip.
|
|
95
|
+
*/
|
|
96
|
+
export function driveBoat(body: DrivableBody, input: DriveInput, grip: number, dt: number): void {
|
|
97
|
+
const throttle = clamp(input.throttle, -1, 1);
|
|
98
|
+
const steer = clamp(input.steer, -1, 1);
|
|
99
|
+
const g = clamp(grip, 0, 1);
|
|
100
|
+
planeHeading(body.rotation(), heading);
|
|
101
|
+
const v = body.linvel();
|
|
102
|
+
const speed = Math.hypot(v.x, v.z);
|
|
103
|
+
|
|
104
|
+
// thrust
|
|
105
|
+
const push = (throttle >= 0 ? BOAT.thrust : BOAT.reverseThrust) * throttle * g * dt;
|
|
106
|
+
// keel: the sideways part of the velocity, bled off exponentially
|
|
107
|
+
const lateral = v.x * heading.rx + v.z * heading.rz;
|
|
108
|
+
const keel = -body.mass() * lateral * (1 - Math.exp(-BOAT.keel * dt)) * g;
|
|
109
|
+
body.applyImpulse(
|
|
110
|
+
{ x: heading.fx * push + heading.rx * keel, y: 0, z: heading.fz * push + heading.rz * keel },
|
|
111
|
+
true,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
// helm: turning to starboard is a negative rotation about +Y
|
|
115
|
+
const inertia = body.principalInertia().y;
|
|
116
|
+
const wanted = -steer * yawRateAt(speed);
|
|
117
|
+
const error = wanted - body.angvel().y;
|
|
118
|
+
const alpha = clamp(BOAT.yawGain * error, -BOAT.yawGain * 2, BOAT.yawGain * 2);
|
|
119
|
+
body.applyTorqueImpulse({ x: 0, y: inertia * alpha * g * dt, z: 0 }, true);
|
|
120
|
+
|
|
121
|
+
// roll and pitch damping, in the hull's frame where each axis has its own inertia: the bilge keels of a small boat
|
|
122
|
+
const q = body.rotation();
|
|
123
|
+
const principal = body.principalInertia();
|
|
124
|
+
rotateByQuat(q, body.angvel(), true, spin);
|
|
125
|
+
const bleed = 1 - Math.exp(-BOAT.tiltDamping * dt);
|
|
126
|
+
damp.x = -principal.x * spin.x * bleed * g;
|
|
127
|
+
damp.y = 0;
|
|
128
|
+
damp.z = -principal.z * spin.z * bleed * g;
|
|
129
|
+
rotateByQuat(q, damp, false, damp);
|
|
130
|
+
body.applyTorqueImpulse(damp, true);
|
|
131
|
+
}
|