video / src /mv /engine /gas.ts
Ethscriptions's picture
v8:远光照亮整块路面并闪白;喷射气体改为自然粒子气体;新增动感歌词(LRC / 打轴工具,随导出)
71ce0f9 verified
Raw History Blame Contribute Delete
5.22 kB
// Natural-looking thruster gas: a particle model with drag, ground impact,
// lateral roll-out, buoyant rise and expansion — drawn with a cached soft
// cloud sprite (normal blending, no glow) so hundreds of puffs stay cheap.
import { hash } from './util'
export interface GasState {
/** world z of the car */
z: number
/** height of the car's underside above the road (m) */
y: number
/** forward speed of the car (m/s) */
vz: number
power: number
/** π/2 = blowing down, π = blowing backwards */
angle: number
}
export interface GasP {
x: number
y: number
z: number
r: number
a: number
warm: boolean
}
const sprites: Record<string, HTMLCanvasElement> = {}
function sprite(warm: boolean): HTMLCanvasElement {
const key = warm ? 'w' : 'c'
if (sprites[key]) return sprites[key]
const c = document.createElement('canvas')
c.width = c.height = 128
const g = c.getContext('2d')!
// soft body
const body = g.createRadialGradient(64, 58, 4, 64, 64, 62)
const base = warm ? [196, 186, 180] : [186, 192, 204]
body.addColorStop(0, `rgba(${base[0] + 20},${base[1] + 20},${base[2] + 20},1)`)
body.addColorStop(0.45, `rgba(${base[0]},${base[1]},${base[2]},0.75)`)
body.addColorStop(0.8, `rgba(${base[0] - 30},${base[1] - 30},${base[2] - 25},0.25)`)
body.addColorStop(1, `rgba(${base[0] - 40},${base[1] - 40},${base[2] - 35},0)`)
g.fillStyle = body
g.fillRect(0, 0, 128, 128)
// self-shadowed underside
const sh = g.createLinearGradient(0, 40, 0, 128)
sh.addColorStop(0, 'rgba(40,46,60,0)')
sh.addColorStop(1, 'rgba(40,46,60,0.35)')
g.globalCompositeOperation = 'source-atop'
g.fillStyle = sh
g.fillRect(0, 0, 128, 128)
// a few lumps so it reads as vapour, not a disc
g.globalCompositeOperation = 'destination-out'
for (let i = 0; i < 7; i++) {
const a = (i / 7) * Math.PI * 2
g.fillStyle = 'rgba(0,0,0,0.35)'
g.beginPath()
g.arc(64 + Math.cos(a) * 54, 64 + Math.sin(a) * 54, 14 + 6 * hash(i), 0, Math.PI * 2)
g.fill()
}
sprites[key] = c
return c
}
/**
* Particles alive at tNow, emitted from `nozzles` (car-relative x, z) while
* the thrusters are on. `stateAt(te)` gives the car state at emission time.
*/
export function gasParticles(tNow: number, from: number, stateAt: (te: number) => GasState, nozzles: [number, number][], rate = 46): GasP[] {
const out: GasP[] = []
const dt = 1 / rate
const maxLife = 3.6
const start = Math.max(from, tNow - maxLife)
for (let te = Math.ceil(start / dt) * dt; te <= tNow; te += dt) {
const s = stateAt(te)
if (s.power < 0.15) continue
const a = tNow - te
nozzles.forEach(([nx, nz], n) => {
const h1 = hash(te * 91.7 + n * 13.1)
const h2 = hash(te * 37.3 + n * 7.7)
const h3 = hash(te * 53.9 + n * 3.3)
const life = 2.4 + 1.2 * h1
if (a > life) return
const drag = 2.2
const sd = (1 - Math.exp(-drag * a)) / drag
const back = s.angle > 2.2
let x = nx
let y: number
let z: number
if (!back) {
const vj = 15 * s.power * (0.8 + 0.4 * h2)
const y0 = s.y + 0.2
const drop = vj * sd
z = s.z + nz + s.vz * sd
if (drop < y0 - 0.25) {
y = y0 - drop
x += (h3 - 0.5) * 0.6 * a
} else {
// hit the road: roll outwards along the surface, then lift
const sHit = (y0 - 0.25) / vj
const spread = Math.max(0, sd - sHit) * 10
const ang = h3 * Math.PI * 2
x += Math.cos(ang) * spread * 1.2
z += Math.sin(ang) * spread * 0.8
y = 0.3 + 0.55 * Math.max(0, a - sHit * 1.2) + h2 * 0.4
}
} else {
const vj = 22 * s.power * (0.8 + 0.4 * h2)
z = s.z + nz + (s.vz - vj) * sd
y = s.y + 0.4 + 0.35 * a + (h2 - 0.5) * 0.5 * a
x += (h3 - 0.5) * 1.6 * a
}
// turbulence
x += Math.sin(a * 2.7 + h1 * 6) * 0.25 * a
y += Math.sin(a * 3.1 + h2 * 6) * 0.12 * a
const r = 0.22 + 1.5 * Math.pow(a, 0.75) * (0.7 + 0.6 * h2)
const fadeIn = Math.min(1, a / 0.06)
const alpha = 0.62 * s.power * Math.pow(1 - a / life, 1.4) * fadeIn
out.push({ x, y, z, r, a: alpha, warm: h1 > 0.72 })
})
}
return out
}
type Proj = (x: number, y: number, z: number) => { x: number; y: number; k: number; z: number } | null
/** draw particles with a projection; `maxR` caps sprite size on screen */
export function drawGas(ctx: CanvasRenderingContext2D, ps: GasP[], project: Proj, maxR: number, nearFade = 2) {
const list: { x: number; y: number; r: number; a: number; d: number; warm: boolean }[] = []
for (const p of ps) {
const q = project(p.x, p.y, p.z)
if (!q) continue
const r = Math.min(maxR, p.r * q.k)
if (r < 0.6) continue
const near = Math.min(1, Math.max(0, (q.z - nearFade) / 5))
const a = p.a * near
if (a < 0.01) continue
list.push({ x: q.x, y: q.y, r, a, d: q.z, warm: p.warm })
}
list.sort((p, q) => q.d - p.d)
const cool = sprite(false)
const warm = sprite(true)
for (const g of list) {
ctx.globalAlpha = Math.min(1, g.a)
ctx.drawImage(g.warm ? warm : cool, g.x - g.r, g.y - g.r, g.r * 2, g.r * 2)
}
ctx.globalAlpha = 1
}