217 lines
7.3 KiB
TypeScript
217 lines
7.3 KiB
TypeScript
// Volumetric GLSL shader exhaust plumes, mach shock diamonds (this doesn't look so much better than p1), and altitude/throttle plume scaling.
|
|
import * as THREE from "three";
|
|
import { getSoftParticleTexture } from "../sceneManager.js";
|
|
import { isReducedMotionEffects } from "../../theme.js";
|
|
import { getPlumeStyle, type NozzleEmitter } from "./materials.js";
|
|
|
|
const PLUME_VERTEX_SHADER = `
|
|
uniform float uVacuum;
|
|
varying vec2 vUv;
|
|
varying vec3 vNormalW;
|
|
varying vec3 vViewDir;
|
|
void main() {
|
|
vUv = uv;
|
|
float t = clamp(1.0 - uv.y, 0.0, 1.0);
|
|
float flare = mix(1.0, 1.0 + uVacuum * 1.45 * pow(max(0.0001, t), 0.85), step(0.001, t));
|
|
vec3 pos = vec3(position.x * flare, position.y, position.z * flare);
|
|
vec4 worldPos = modelMatrix * vec4(pos, 1.0);
|
|
vNormalW = normalize((modelMatrix * vec4(normal, 0.0)).xyz);
|
|
vViewDir = normalize(cameraPosition - worldPos.xyz);
|
|
gl_Position = projectionMatrix * viewMatrix * worldPos;
|
|
}
|
|
`;
|
|
|
|
const PLUME_FRAGMENT_SHADER = `
|
|
uniform float uTime;
|
|
uniform float uVacuum;
|
|
uniform float uThrottle;
|
|
uniform float uReduced;
|
|
uniform vec3 uCoreColor;
|
|
uniform vec3 uInnerColor;
|
|
uniform vec3 uOuterColor;
|
|
uniform vec3 uDiamondColor;
|
|
varying vec2 vUv;
|
|
varying vec3 vNormalW;
|
|
varying vec3 vViewDir;
|
|
void main() {
|
|
float t = clamp(1.0 - vUv.y, 0.0, 1.0);
|
|
float ndv = abs(dot(normalize(vNormalW), normalize(vViewDir)));
|
|
float fresnel = pow(max(0.0001, clamp(ndv, 0.0, 1.0)), 1.25);
|
|
|
|
float entryFade = smoothstep(0.0, 0.05, t);
|
|
float tailLen = mix(0.95, 0.80, uVacuum);
|
|
float tailFade = pow(max(0.0001, 1.0 - smoothstep(0.14, tailLen, t)), 1.35);
|
|
|
|
float speed = (1.0 - uReduced) * uTime;
|
|
float turb = 0.5 + 0.5 * sin(t * 15.0 - speed * 22.0 + sin(vUv.x * 18.8495 + speed * 5.0) * 1.35);
|
|
float ripple = 0.84 + 0.16 * sin(t * 34.0 - speed * 31.0 + cos(vUv.x * 25.1327));
|
|
|
|
float machVis = 1.0 - uVacuum * 0.92;
|
|
float diamondWave = pow(max(0.0, sin(t * 24.0 + 0.4)), 4.0) * (1.0 - smoothstep(0.04, 0.72, t));
|
|
float throatHot = 1.0 - smoothstep(0.0, 0.24, t);
|
|
|
|
vec3 plumeCol = mix(uOuterColor, uInnerColor, smoothstep(0.0, 0.65, ndv) * (1.0 - t * 0.5));
|
|
plumeCol = mix(plumeCol, uCoreColor, throatHot * fresnel);
|
|
plumeCol += uDiamondColor * (diamondWave * machVis * 1.25);
|
|
|
|
float intensity = (0.85 + uThrottle * 0.7) * (0.72 + 0.28 * turb * ripple);
|
|
vec3 finalRgb = plumeCol * intensity;
|
|
float alpha = (fresnel * entryFade * tailFade * (0.64 + 0.32 * turb)) + (diamondWave * machVis * fresnel * 0.36);
|
|
alpha *= clamp(0.45 + uThrottle * 0.55, 0.35, 1.05);
|
|
if (alpha < 0.008) discard;
|
|
gl_FragColor = vec4(finalRgb, clamp(alpha, 0.0, 0.96));
|
|
}
|
|
`;
|
|
|
|
export function setPlumeAtmosphere(
|
|
plume: THREE.Group,
|
|
altKm: number,
|
|
throttle: number,
|
|
): void {
|
|
const vacuum = Math.max(0, Math.min(1, altKm / 65));
|
|
const thr = Math.max(0.15, Math.min(1.4, throttle));
|
|
plume.traverse((node: any) => {
|
|
const mat = node.material as THREE.ShaderMaterial | undefined;
|
|
if (mat?.userData?.isPlumeShader && mat.uniforms) {
|
|
mat.uniforms.uVacuum.value = vacuum;
|
|
mat.uniforms.uThrottle.value = thr;
|
|
if (mat.uniforms.uReduced) {
|
|
mat.uniforms.uReduced.value = isReducedMotionEffects() ? 1.0 : 0.0;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
export function buildStagePlume(
|
|
emitters: NozzleEmitter[],
|
|
stageDiameter: number,
|
|
): {
|
|
plume: THREE.Group;
|
|
anchor: THREE.Group;
|
|
} {
|
|
const plume = new THREE.Group();
|
|
plume.visible = false;
|
|
const anchor = new THREE.Group();
|
|
if (emitters.length === 0) {
|
|
emitters.push({
|
|
x: 0,
|
|
y: 0,
|
|
z: 0,
|
|
radius: Math.max(0.6, stageDiameter * 0.25),
|
|
propellantType: "kerolox",
|
|
});
|
|
}
|
|
let cx = 0;
|
|
let cy = 0;
|
|
let cz = 0;
|
|
for (const e of emitters) {
|
|
cx += e.x;
|
|
cy += e.y;
|
|
cz += e.z;
|
|
}
|
|
cx /= emitters.length;
|
|
cy /= emitters.length;
|
|
cz /= emitters.length;
|
|
anchor.position.set(cx, cy, cz);
|
|
plume.position.set(cx, cy, cz);
|
|
const maxVisualNozzles =
|
|
emitters.length <= 6 ? emitters : emitters.slice(0, 6);
|
|
const scaleBoost = emitters.length > 6 ? 1.35 : 1.0;
|
|
const glowTex = getSoftParticleTexture();
|
|
for (const e of maxVisualNozzles) {
|
|
const style = getPlumeStyle(e.propellantType);
|
|
const r = Math.max(0.35, e.radius * 0.95 * scaleBoost);
|
|
const plumeLen = Math.max(11, r * 18 * style.lenFactor);
|
|
const nozzlePlume = new THREE.Group();
|
|
nozzlePlume.position.set(e.x - cx, e.y - cy, e.z - cz);
|
|
const shaderMat = new THREE.ShaderMaterial({
|
|
uniforms: {
|
|
uTime: { value: 0 },
|
|
uVacuum: { value: 0 },
|
|
uThrottle: { value: 1 },
|
|
uReduced: { value: isReducedMotionEffects() ? 1.0 : 0.0 },
|
|
uCoreColor: { value: new THREE.Color(style.core).multiplyScalar(1.35) },
|
|
uInnerColor: {
|
|
value: new THREE.Color(style.inner).multiplyScalar(1.15),
|
|
},
|
|
uOuterColor: { value: new THREE.Color(style.outer) },
|
|
uDiamondColor: {
|
|
value: new THREE.Color(style.diamond).multiplyScalar(1.4),
|
|
},
|
|
},
|
|
vertexShader: PLUME_VERTEX_SHADER,
|
|
fragmentShader: PLUME_FRAGMENT_SHADER,
|
|
transparent: true,
|
|
blending: THREE.AdditiveBlending,
|
|
depthWrite: false,
|
|
side: THREE.DoubleSide,
|
|
});
|
|
shaderMat.userData.isPlumeShader = true;
|
|
const envelope = new THREE.Mesh(
|
|
new THREE.CylinderGeometry(r * 0.85, r * 2.1, plumeLen, 24, 20, true),
|
|
shaderMat,
|
|
);
|
|
envelope.position.y = -plumeLen * 0.5;
|
|
envelope.onBeforeRender = () => {
|
|
if (!isReducedMotionEffects()) {
|
|
shaderMat.uniforms.uTime.value = performance.now() * 0.001;
|
|
}
|
|
};
|
|
nozzlePlume.add(envelope);
|
|
const innerCoreMat = new THREE.ShaderMaterial({
|
|
uniforms: {
|
|
uTime: shaderMat.uniforms.uTime,
|
|
uVacuum: shaderMat.uniforms.uVacuum,
|
|
uThrottle: shaderMat.uniforms.uThrottle,
|
|
uReduced: shaderMat.uniforms.uReduced,
|
|
uCoreColor: { value: new THREE.Color(style.core).multiplyScalar(1.6) },
|
|
uInnerColor: {
|
|
value: new THREE.Color(style.diamond).multiplyScalar(1.3),
|
|
},
|
|
uOuterColor: { value: new THREE.Color(style.inner) },
|
|
uDiamondColor: {
|
|
value: new THREE.Color(style.core).multiplyScalar(1.6),
|
|
},
|
|
},
|
|
vertexShader: PLUME_VERTEX_SHADER,
|
|
fragmentShader: PLUME_FRAGMENT_SHADER,
|
|
transparent: true,
|
|
blending: THREE.AdditiveBlending,
|
|
depthWrite: false,
|
|
side: THREE.DoubleSide,
|
|
});
|
|
innerCoreMat.userData.isPlumeShader = true;
|
|
const innerMesh = new THREE.Mesh(
|
|
new THREE.CylinderGeometry(
|
|
r * 0.42,
|
|
r * 0.95,
|
|
plumeLen * 0.62,
|
|
18,
|
|
12,
|
|
true,
|
|
),
|
|
innerCoreMat,
|
|
);
|
|
innerMesh.position.y = -(plumeLen * 0.62) * 0.5;
|
|
nozzlePlume.add(innerMesh);
|
|
const throatSprite = new THREE.Sprite(
|
|
new THREE.SpriteMaterial({
|
|
map: glowTex,
|
|
color: style.core,
|
|
transparent: true,
|
|
opacity: 0.88,
|
|
blending: THREE.AdditiveBlending,
|
|
depthWrite: false,
|
|
}),
|
|
);
|
|
throatSprite.position.set(0, -r * 1.08, 0);
|
|
throatSprite.scale.set(r * 2.8, r * 2.2, 1);
|
|
nozzlePlume.add(throatSprite);
|
|
plume.add(nozzlePlume);
|
|
}
|
|
const primaryStyle = getPlumeStyle(emitters[0]?.propellantType);
|
|
const plumeLight = new THREE.PointLight(primaryStyle.light, 6.5, 160, 1.4);
|
|
plumeLight.position.set(0, -3.5, 0);
|
|
plume.add(plumeLight);
|
|
return { plume, anchor };
|
|
}
|