Files
Cruise/src/components/ShipWake.jsx

90 lines
2.5 KiB
JavaScript

import React, { useRef, useMemo } from "react";
import { useFrame } from "@react-three/fiber";
import * as THREE from "three";
/**
* Soft Stern Wash & Wake Particles
* Completely eliminates any flat rectangular planes.
*/
export default function ShipWake() {
const pointsRef = useRef();
const particleTexture = useMemo(() => {
const canvas = document.createElement("canvas");
canvas.width = 64;
canvas.height = 64;
const ctx = canvas.getContext("2d");
const grad = ctx.createRadialGradient(32, 32, 0, 32, 32, 32);
grad.addColorStop(0, "rgba(255, 255, 255, 0.7)");
grad.addColorStop(0.35, "rgba(224, 242, 254, 0.4)");
grad.addColorStop(1, "rgba(255, 255, 255, 0)");
ctx.fillStyle = grad;
ctx.fillRect(0, 0, 64, 64);
const tex = new THREE.CanvasTexture(canvas);
tex.needsUpdate = true;
return tex;
}, []);
const count = 400;
const [positions, velocities] = useMemo(() => {
const pos = new Float32Array(count * 3);
const vel = new Float32Array(count * 3);
for (let i = 0; i < count; i++) {
// Trailing behind the stern from Z = -8 to -35
pos[i * 3] = (Math.random() - 0.5) * (3.0 + Math.random() * 5.0); // X spreads
pos[i * 3 + 1] = -0.25; // Y right on water
pos[i * 3 + 2] = -8 - Math.random() * 28; // Z aft
vel[i * 3] = (Math.random() - 0.5) * 0.2;
vel[i * 3 + 2] = -(0.9 + Math.random() * 1.4); // Rushes aft at stately cruising speed
}
return [pos, vel];
}, []);
useFrame((state, delta) => {
if (!pointsRef.current) return;
const posAttr = pointsRef.current.geometry.attributes.position;
const pos = posAttr.array;
for (let i = 0; i < count; i++) {
const idx = i * 3;
pos[idx + 2] += velocities[idx + 2] * delta;
// Wrap around when trailing too far
if (pos[idx + 2] < -42) {
pos[idx] = (Math.random() - 0.5) * 3.5;
pos[idx + 2] = -8 - Math.random() * 4;
}
}
posAttr.needsUpdate = true;
});
return (
<points ref={pointsRef}>
<bufferGeometry>
<bufferAttribute
attach="attributes-position"
count={count}
array={positions}
itemSize={3}
/>
</bufferGeometry>
<pointsMaterial
size={1.4}
map={particleTexture}
transparent
opacity={0.42}
blending={THREE.AdditiveBlending}
depthWrite={false}
color="#e0f2fe"
/>
</points>
);
}