// Super Kart Mode 7 renderer: a Phaser Shader GameObject covering the // below-horizon band, sampling the rasterized top-down track texture with the // classic per-pixel inverse projection, plus the matching world→screen sprite // projector. The ground shader and the projector share ONE tuning object so // they can never disagree about where the ground is. // // WebGL only — SuperKartGame shows a notice card on the Canvas renderer. // Debug aids (wired to keys in the scene): setFlipY() flips the quad's y // orientation and a checkerboard texture can be substituted for the track. import * as Phaser from 'phaser'; import { GAME_WIDTH, GAME_HEIGHT } from '../../config.js'; const TAU = Math.PI * 2; // One source of truth for the projection. horizonFrac is the fraction of the // screen above the ground plane; focal/camHeight set how "low" the camera // feels; followDist puts the player kart near the lower third of the screen. export const MODE7 = { horizonFrac: 0.40, focal: 330, camHeight: 40, followDist: 42, nearZ: 6, farZ: 1700, fogStart: 950, fogEnd: 1650, camLerp: 0.12, // per-frame heading smoothing spriteBase: 34, // world size (units) a 1.0-scale 64px sprite frame represents }; // Racer spritesheet rotation-frame count — the single source of truth for // the 8-view-angle kart art contract (data/superkart-artwork.json, the // procedural fallback in SuperKartRaster.js, and the frame math below all // derive from this instead of a repeated magic number). export const KART_ROT_FRAMES = 8; const FRAG = ` precision mediump float; uniform sampler2D iChannel0; uniform vec2 uQuad; uniform vec2 uCam; uniform float uAngle; uniform float uCamH; uniform float uFocal; uniform float uWorld; uniform vec3 uFog; uniform vec2 uFogRange; uniform float uFlipY; varying vec2 fragCoord; void main () { vec2 pc = fragCoord; float py = mix(pc.y, uQuad.y - pc.y, uFlipY); py = max(py, 0.75); float z = uCamH * uFocal / py; float xoff = (pc.x - uQuad.x * 0.5) * z / uFocal; vec2 fwd = vec2(cos(uAngle), sin(uAngle)); vec2 rgt = vec2(-fwd.y, fwd.x); vec2 world = uCam + fwd * z + rgt * xoff; vec2 uv = world / uWorld; vec3 col; if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { col = uFog; } else { col = texture2D(iChannel0, uv).rgb; } float fog = smoothstep(uFogRange.x, uFogRange.y, z); gl_FragColor = vec4(mix(col, uFog, fog), 1.0); } `; export class SuperKartMode7 { // trackTextureKey must already exist (scene.textures.addCanvas result). // fogColor: {r,g,b} 0..1 floats matching the theme's fog hex. constructor(scene, { trackTextureKey, worldSize, fogColor, depthBase = 0 }) { this.scene = scene; this.worldSize = worldSize; this.horizonY = Math.round(GAME_HEIGHT * MODE7.horizonFrac); const groundH = GAME_HEIGHT - this.horizonY; this.cam = { x: worldSize / 2, y: worldSize / 2, angle: 0, height: MODE7.camHeight, focal: MODE7.focal, }; const base = new Phaser.Display.BaseShader('SuperKartMode7', FRAG, undefined, { uQuad: { type: '2f', value: { x: GAME_WIDTH, y: groundH } }, uCam: { type: '2f', value: { x: this.cam.x, y: this.cam.y } }, uAngle: { type: '1f', value: 0 }, uCamH: { type: '1f', value: MODE7.camHeight }, uFocal: { type: '1f', value: MODE7.focal }, uWorld: { type: '1f', value: worldSize }, uFog: { type: '3f', value: fogColor }, uFogRange: { type: '2f', value: { x: MODE7.fogStart, y: MODE7.fogEnd } }, uFlipY: { type: '1f', value: 1 }, }); this.ground = scene.add.shader(base, GAME_WIDTH / 2, this.horizonY + groundH / 2, GAME_WIDTH, groundH, [trackTextureKey]); this.ground.setDepth(depthBase); } // Follow camera: smoothly chase the kart's heading, sit followDist behind. // Always resets height/focal to the standard values, so normal gameplay // stays identical regardless of what a prior cinematic camera pose left // this.cam in (see setCamera()). follow(kart, snap = false) { const lerp = snap ? 1 : MODE7.camLerp; let diff = (kart.heading - this.cam.angle) % TAU; if (diff > Math.PI) diff -= TAU; if (diff < -Math.PI) diff += TAU; this.cam.angle += diff * lerp; const fx = Math.cos(this.cam.angle); const fy = Math.sin(this.cam.angle); this.cam.x = kart.x - fx * MODE7.followDist; this.cam.y = kart.y - fy * MODE7.followDist; this.cam.height = MODE7.camHeight; this.cam.focal = MODE7.focal; this.syncUniforms(); } // Arbitrary camera pose — used by the post-race victory-cam director for // cinematic shots (also the free-fly debug camera hook). setCamera(x, y, angle, height = MODE7.camHeight, focal = MODE7.focal) { this.cam.x = x; this.cam.y = y; this.cam.angle = angle; this.cam.height = height; this.cam.focal = focal; this.syncUniforms(); } syncUniforms() { const u = this.ground.uniforms; u.uCam.value.x = this.cam.x; u.uCam.value.y = this.cam.y; u.uAngle.value = this.cam.angle; u.uCamH.value = this.cam.height; u.uFocal.value = this.cam.focal; } setFlipY(flip) { this.ground.uniforms.uFlipY.value = flip ? 1 : 0; } // World point → screen. Returns null when culled (behind camera / past fog). // scale is for a 64px frame representing MODE7.spriteBase world units; // multiply for larger objects. project(wx, wy) { const fx = Math.cos(this.cam.angle); const fy = Math.sin(this.cam.angle); const rx = -fy; const ry = fx; const dx = wx - this.cam.x; const dy = wy - this.cam.y; const zc = dx * fx + dy * fy; if (zc < MODE7.nearZ || zc > MODE7.farZ) return null; const xc = dx * rx + dy * ry; const px = GAME_WIDTH / 2 + (xc * this.cam.focal) / zc; if (px < -160 || px > GAME_WIDTH + 160) return null; return { x: px, y: this.horizonY + (this.cam.height * this.cam.focal) / zc, scale: (MODE7.spriteBase / 64) * (this.cam.focal / zc), z: zc, fog: Phaser.Math.Clamp((zc - MODE7.fogStart) / (MODE7.fogEnd - MODE7.fogStart), 0, 1), }; } // View-angle frame for a kart heading (8-frame sheets, frame 0 = rear view). angleFrame(heading, angles = KART_ROT_FRAMES) { let rel = (heading - this.cam.angle) % TAU; if (rel < 0) rel += TAU; return Math.round((rel / TAU) * angles) % angles; } destroy() { this.ground?.destroy(); this.ground = null; } } // Horizon backdrop: a TileSprite strip above the ground band whose horizontal // scroll tracks the camera angle (full turn = repeatsPerTurn texture widths). export class Mode7Backdrop { constructor(scene, textureKey, horizonY, depth = -1) { this.texWidth = scene.textures.get(textureKey).getSourceImage().width || 1024; this.sprite = scene.add.tileSprite(GAME_WIDTH / 2, horizonY / 2, GAME_WIDTH, horizonY, textureKey) .setDepth(depth); const texH = scene.textures.get(textureKey).getSourceImage().height || 300; this.sprite.tileScaleX = this.sprite.tileScaleY = horizonY / texH; this.repeatsPerTurn = 3; } update(camAngle) { this.sprite.tilePositionX = ((camAngle / TAU) * this.texWidth * this.repeatsPerTurn) / this.sprite.tileScaleX; } destroy() { this.sprite?.destroy(); this.sprite = null; } }