SPB Git

Repository Public

Rings.metal 6.6 KB 156 lines

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project          : ORIX Cosmos10 * File             : Shaders/Rings.metal11 * Description      : Planetary ring annulus in the body's equatorial plane with12 *                    a radial colour/opacity profile and the planet's shadow13 *                    (spheroid, soft penumbra from the light's angular size).14 * Module           : Shaders15 * Repository       : https://git.spboucher.ai/orix-cosmos16 * Project Website  : https://www.orixlab.dev17 * Created          : 2026-11-2418 * Last Modified    : 2026-11-2419 *20 * =============================================================================21 * AUTHOR & PROFESSIONAL AFFILIATION22 * =============================================================================23 *24 * Author           : Simon-Pierre Boucher25 * Position         : Professor of Finance26 * Institution      : Université du Québec en Outaouais (UQO)27 * Department       : Département des sciences administratives28 * Organization     : SP Boucher29 *30 * Website          : https://spboucher.ai31 * Git              : https://git.spboucher.ai32 *33 * =============================================================================34 * OFFICIAL CONTACT DIRECTORY35 * =============================================================================36 *37 * General          : info@spboucher.ai38 * Professional     : contact@spboucher.ai39 * Direct           : simon@spboucher.ai40 * Research         : research@spboucher.ai41 * Development      : dev@spboucher.ai42 * Support          : support@spboucher.ai43 * Security/Admin   : admin@spboucher.ai44 * Notifications    : notifications@spboucher.ai45 *46 * Preferred Contact: support@spboucher.ai47 *48 * =============================================================================49 * COPYRIGHT & INTELLECTUAL PROPERTY50 * =============================================================================51 *52 * Copyright (c) 202653 * Simon-Pierre Boucher54 *55 * All rights reserved where applicable.56 *57 * This file is governed by the applicable project license,58 * copyright notices, institutional agreements and relevant law.59 *60 * Unless expressly authorized by the applicable license or61 * rights holder, copying, redistribution, sublicensing,62 * publication and commercial exploitation are not permitted.63 *64 * Existing third-party rights and notices remain unaffected.65 *66 * =============================================================================67 * CONFIDENTIALITY & SECURITY68 * =============================================================================69 *70 * Classification   : Public71 * Distribution     : Public repository; use is governed by the project license72 * Security Contact : admin@spboucher.ai73 *74 * Access to this source does not independently grant permission75 * to redistribute or disclose confidential materials.76 *77 * Do not embed credentials, private keys, access tokens,78 * certificates or confidential personal information.79 *80 * =============================================================================81 * LICENSE & MAINTENANCE82 * =============================================================================83 *84 * License          : None specified (no license file in this repository)85 * Maintainer       : Simon-Pierre Boucher86 * Technical Contact: dev@spboucher.ai87 *88 * =============================================================================89 * END OF SP BOUCHER SOURCE HEADER90 * =============================================================================91 */9293#include "CosmosShared.h"9495struct RingVaryings {96    float4 position [[position]];97    float2 local;   // position in the ring plane, in units of the outer radius98};99100vertex RingVaryings ring_vertex(uint vid [[vertex_id]],101                                constant FrameUniforms &frame [[buffer(1)]],102                                constant RingUniforms &ring [[buffer(2)]]) {103    float2 corner = float2(float(vid & 1u), float((vid >> 1) & 1u)) * 2.0f - 1.0f;104    RingVaryings out;105    float3 viewPos = (ring.modelView * float4(corner * ring.ring.y, 0.0f, 1.0f)).xyz;106    out.position = cosmos_crop(frame, cosmos_clip(frame, viewPos, 1.0f));107    out.local = corner;108    return out;109}110111fragment float4 ring_fragment(RingVaryings in [[stage_in]],112                              constant FrameUniforms &frame [[buffer(1)]],113                              constant RingUniforms &ring [[buffer(2)]],114                              texture2d<float> profileTex [[texture(0)]],115                              sampler profileSampler [[sampler(1)]]) {116    float inner = ring.ring.x;117    float outer = ring.ring.y;118    float rn = length(in.local);119    float r = rn * outer;120    float s = (r - inner) / (outer - inner);121    float aa = max(fwidth(s), 1e-5f);122    float edge = smoothstep(0.0f, aa, s) * (1.0f - smoothstep(1.0f - aa, 1.0f, s));123    if (edge <= 0.0f) { discard_fragment(); }124125    float4 profile = ring.color.a > 0.5f ? profileTex.sample(profileSampler, float2(clamp(s, 0.0f, 1.0f), 0.5f)) : float4(1.0f);126    float3 tint = profile.rgb * ring.color.rgb;127    float density = clamp(profile.a * ring.ring.z, 0.0f, 1.0f);128    float opacity = density * edge;129130    float3 q = float3(in.local * outer, 0.0f);131    float3 lv = ring.light.positionRadius.xyz - q;132    float ldist = length(lv);133    float3 l = lv / ldist;134135    // Planet shadow: work in the space where the spheroid is the unit sphere.136    float3 qs = q / ring.radii.xyz;137    float3 lsRaw = l / ring.radii.xyz;138    float lsLen = length(lsRaw);139    float3 ls = lsRaw / lsLen;140    float along = -dot(qs, ls);141    float shadow = 1.0f;142    if (along > 0.0f) {143        float miss = length(qs + ls * along);144        // Penumbra half-width: light angular radius times the distance behind the planet.145        float w = max((along / lsLen) * (ring.light.positionRadius.w / ldist) / ring.radii.x, 2.0f * fwidth(miss));146        shadow = smoothstep(1.0f - w, 1.0f + w, miss);147    }148149    // Lit face when camera and light are on the same side of the ring plane; the unlit face only150    // shows light diffusely transmitted through the particles (illustrative model, see docs).151    bool sameSide = (ring.cameraPos.z * ring.light.positionRadius.z) >= 0.0f;152    float transmitted = ring.ring.w * (1.0f - density);153    float3 radiance = tint * ring.light.irradiance.rgb * shadow * (sameSide ? 1.0f : transmitted);154    return float4(radiance * opacity, opacity);155}156
Files main
Menu

Theme