Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project : ORIX Cosmos10 * File : Shaders/Atmosphere.metal11 * Description : Single-scattering Rayleigh/Mie atmosphere ray-marched per12 * pixel, seen from space or from inside, composited with13 * dual-source blending (in-scattering + per-channel14 * transmittance).15 * Module : Shaders16 * Repository : https://git.spboucher.ai/orix-cosmos17 * Project Website : https://www.orixlab.dev18 * Created : 2026-11-2419 * Last Modified : 2026-11-2420 *21 * =============================================================================22 * AUTHOR & PROFESSIONAL AFFILIATION23 * =============================================================================24 *25 * Author : Simon-Pierre Boucher26 * Position : Professor of Finance27 * Institution : Université du Québec en Outaouais (UQO)28 * Department : Département des sciences administratives29 * Organization : SP Boucher30 *31 * Website : https://spboucher.ai32 * Git : https://git.spboucher.ai33 *34 * =============================================================================35 * OFFICIAL CONTACT DIRECTORY36 * =============================================================================37 *38 * General : info@spboucher.ai39 * Professional : contact@spboucher.ai40 * Direct : simon@spboucher.ai41 * Research : research@spboucher.ai42 * Development : dev@spboucher.ai43 * Support : support@spboucher.ai44 * Security/Admin : admin@spboucher.ai45 * Notifications : notifications@spboucher.ai46 *47 * Preferred Contact: support@spboucher.ai48 *49 * =============================================================================50 * COPYRIGHT & INTELLECTUAL PROPERTY51 * =============================================================================52 *53 * Copyright (c) 202654 * Simon-Pierre Boucher55 *56 * All rights reserved where applicable.57 *58 * This file is governed by the applicable project license,59 * copyright notices, institutional agreements and relevant law.60 *61 * Unless expressly authorized by the applicable license or62 * rights holder, copying, redistribution, sublicensing,63 * publication and commercial exploitation are not permitted.64 *65 * Existing third-party rights and notices remain unaffected.66 *67 * =============================================================================68 * CONFIDENTIALITY & SECURITY69 * =============================================================================70 *71 * Classification : Public72 * Distribution : Public repository; use is governed by the project license73 * Security Contact : admin@spboucher.ai74 *75 * Access to this source does not independently grant permission76 * to redistribute or disclose confidential materials.77 *78 * Do not embed credentials, private keys, access tokens,79 * certificates or confidential personal information.80 *81 * =============================================================================82 * LICENSE & MAINTENANCE83 * =============================================================================84 *85 * License : None specified (no license file in this repository)86 * Maintainer : Simon-Pierre Boucher87 * Technical Contact: dev@spboucher.ai88 *89 * =============================================================================90 * END OF SP BOUCHER SOURCE HEADER91 * =============================================================================92 */9394#include "CosmosShared.h"9596struct AtmoVaryings {97 float4 position [[position]];98};99100struct AtmoOut {101 float4 inscatter [[color(0), index(0)]];102 float4 transmittance [[color(0), index(1)]];103};104105vertex AtmoVaryings atmosphere_vertex(uint vid [[vertex_id]],106 constant FrameUniforms &frame [[buffer(1)]],107 constant AtmosphereUniforms &atmo [[buffer(2)]]) {108 float2 corner = float2(float(vid & 1u), float((vid >> 1) & 1u));109 float2 ndc = mix(atmo.rect.xy, atmo.rect.zw, corner);110 AtmoVaryings out;111 out.position = cosmos_crop(frame, float4(ndc, 0.5f, 1.0f));112 return out;113}114115static inline bool cosmos_atmosphere_shade(const FrameUniforms frame, const AtmosphereUniforms atmo,116 float2 fragCoord, float sceneDepth, thread AtmoOut &out) {117 const AtmosphereParams a = atmo.atmosphere;118 float2 ndc = cosmos_frag_to_ndc(frame, fragCoord);119 float3 rayRaw = float3(ndc.x * frame.projParams.x, ndc.y * frame.projParams.y, -1.0f);120 float rayLen = length(rayRaw);121 float3 d = rayRaw / rayLen;122123 // Closest-approach parametrisation: every quantity below is planet-centric and small, so the124 // result does not lose precision when the planet is far from the camera.125 float3 c = atmo.centerView.xyz;126 float tca = dot(c, d);127 float3 o = -cross(d, cross(c, d)); // closest point of the ray, relative to the centre128 float rTop = a.shape.y;129 float rGround = a.shape.x;130 if (dot(o, o) > rTop * rTop * 1.3f) { return false; }131132 // Stretch along the pole so that the spheroid becomes a sphere.133 float3 pole = atmo.poleView.xyz;134 float stretch = atmo.poleView.w;135 float3 os = o + pole * (dot(o, pole) * stretch);136 float3 dsRaw = d + pole * (dot(d, pole) * stretch);137 float dl = length(dsRaw);138 float3 dir = dsRaw / dl;139 float3 lraw = atmo.lightDirView.xyz;140 float3 l = normalize(lraw + pole * (dot(lraw, pole) * stretch));141142 float t0, t1;143 if (!cosmos_ray_sphere(os, dir, rTop, t0, t1)) { return false; }144 float tCamera = -tca * dl;145 float start = max(t0, tCamera);146 float end = t1;147 float g0, g1;148 bool ground = false;149 if (cosmos_ray_sphere(os, dir, rGround, g0, g1) && g0 > start) {150 end = min(end, g0);151 ground = true;152 }153 if (sceneDepth > 0.0f) {154 float tScene = (frame.projParams.z / sceneDepth) * rayLen;155 float tsScene = (tScene - tca) * dl;156 // The planet's own surface is handled analytically (ground); anything clearly in front of157 // it is a foreign object that ends the path early.158 if (!ground || tsScene < end - 0.01f * rGround) { end = min(end, tsScene); }159 }160 if (end <= start) { return false; }161162 int viewSteps = int(atmo.steps.x);163 int lightSteps = int(atmo.steps.y);164 // Inside the atmosphere the samples are spaced quadratically: dense near the camera, where a165 // low-altitude ray crosses most of the air, sparse towards the top. From space they are uniform.166 bool inside = tCamera > t0;167 float span = end - start;168 float mu = dot(dir, l);169 float phaseR = cosmos_phase_rayleigh(mu);170 float phaseM = cosmos_phase_mie(mu, a.mieExt.w);171 float3 tau = float3(0.0f);172 float3 sumR = float3(0.0f);173 float3 sumM = float3(0.0f);174 for (int i = 0; i < viewSteps; ++i) {175 float u0 = float(i) / float(viewSteps), u1 = float(i + 1) / float(viewSteps);176 if (inside) { u0 *= u0; u1 *= u1; }177 float ds = (u1 - u0) * span;178 float3 q = os + dir * (start + 0.5f * (u0 + u1) * span);179 float h = length(q) - rGround;180 float3 density = cosmos_atmo_density(a, h);181 float3 stepTau = cosmos_atmo_extinction(a, density) * ds;182 float3 transmittanceToCamera = exp(-(tau + 0.5f * stepTau));183 tau += stepTau;184 // Planet shadow: how far above the ground the ray towards the light passes. A soft edge a quarter of185 // a scale height wide replaces the binary test, whose position would alias with the sample spacing.186 float along = dot(q, l);187 float lit = 1.0f;188 if (along < 0.0f) {189 float clearance = length(q - l * along) - rGround;190 float w = 0.25f * a.rayleigh.w;191 lit = smoothstep(-w, w, clearance);192 }193 if (lit > 0.0f) {194 float3 tLight = exp(-cosmos_atmo_optical_depth(a, q, l, lightSteps)) * lit;195 sumR += transmittanceToCamera * tLight * (density.x * ds);196 sumM += transmittanceToCamera * tLight * (density.y * ds);197 }198 }199 // Scene radiance is expressed so that a white Lambertian surface under irradiance E has radiance E200 // (physical radiance times pi); the in-scattered radiance E * beta * phase * ds gets the same factor.201 float3 inscatter = (sumR * a.rayleigh.rgb * phaseR + sumM * a.mie.rgb * phaseM) * atmo.irradiance.rgb * COSMOS_PI;202 out.inscatter = float4(inscatter, 0.0f);203 out.transmittance = float4(exp(-tau), 1.0f);204 return true;205}206207fragment AtmoOut atmosphere_fragment_ms(AtmoVaryings in [[stage_in]],208 constant FrameUniforms &frame [[buffer(1)]],209 constant AtmosphereUniforms &atmo [[buffer(2)]],210 depth2d_ms<float> depthTex [[texture(0)]]) {211 AtmoOut out;212 // The farthest of the pixel's depth samples (reversed-Z: the smallest). On a silhouette this is the sky or213 // the background object, so the analytic ground intersection decides for the planet's own limb.214 uint2 px = uint2(in.position.xy);215 float depth = 1.0f;216 for (uint i = 0u; i < depthTex.get_num_samples(); ++i) { depth = min(depth, depthTex.read(px, i)); }217 if (!cosmos_atmosphere_shade(frame, atmo, in.position.xy, depth, out)) { discard_fragment(); }218 return out;219}220221fragment AtmoOut atmosphere_fragment(AtmoVaryings in [[stage_in]],222 constant FrameUniforms &frame [[buffer(1)]],223 constant AtmosphereUniforms &atmo [[buffer(2)]],224 depth2d<float> depthTex [[texture(0)]]) {225 AtmoOut out;226 float depth = depthTex.read(uint2(in.position.xy), 0);227 if (!cosmos_atmosphere_shade(frame, atmo, in.position.xy, depth, out)) { discard_fragment(); }228 return out;229}230