SPB Git

Repository Public

Lines.metal 7.5 KB 180 lines

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project          : ORIX Cosmos10 * File             : Shaders/Lines.metal11 * Description      : Anti-aliased polylines of constant screen width (orbits,12 *                    trajectories, great-circle grids): instanced segment quads13 *                    expanded in screen space, near-plane clipping, dashes and14 *                    per-vertex fade.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 LineIn {97    float4 a [[attribute(0)]];      // xyz: first point (camera-relative, scene axes) or unit direction, w: alpha98    float4 b [[attribute(1)]];      // xyz: second point, w: alpha99    float2 dash [[attribute(2)]];   // accumulated screen length at both ends (pt)100};101102struct LineVaryings {103    float4 position [[position]];104    float across [[center_no_perspective]];   // signed distance from the centre line (target px)105    float alpha [[center_no_perspective]];106    float dash [[center_no_perspective]];107    float halfWidth [[flat]];108};109110vertex LineVaryings line_vertex(LineIn in [[stage_in]],111                                uint vid [[vertex_id]],112                                constant FrameUniforms &frame [[buffer(1)]],113                                constant LineUniforms &line [[buffer(2)]]) {114    LineVaryings out;115    out.position = float4(2.0f, 2.0f, 2.0f, 1.0f);116    out.across = 0.0f; out.alpha = 0.0f; out.dash = 0.0f; out.halfWidth = 0.0f;117118    bool directions = line.offsetRel.w > 0.5f;119    float3 ra = in.a.xyz + (directions ? float3(0.0f) : line.offsetRel.xyz);120    float3 rb = in.b.xyz + (directions ? float3(0.0f) : line.offsetRel.xyz);121    float3x3 rot = float3x3(frame.viewRot.columns[0].xyz, frame.viewRot.columns[1].xyz, frame.viewRot.columns[2].xyz);122    float3 va = rot * ra;123    float3 vb = rot * rb;124    float alphaA = in.a.w, alphaB = in.b.w;125    float dashA = in.dash.x * line.params.w, dashB = in.dash.y * line.params.w;126127    // Clip the segment against the near plane in view space.128    float zn = directions ? -1e-4f : -frame.projParams.z;129    if (va.z > zn && vb.z > zn) { return out; }130    if (va.z > zn) {131        float t = (zn - va.z) / (vb.z - va.z);132        va = mix(va, vb, t); alphaA = mix(alphaA, alphaB, t); dashA = mix(dashA, dashB, t);133    } else if (vb.z > zn) {134        float t = (zn - vb.z) / (va.z - vb.z);135        vb = mix(vb, va, t); alphaB = mix(alphaB, alphaA, t); dashB = mix(dashB, dashA, t);136    }137138    float w = directions ? 0.0f : 1.0f;139    float4 ca = cosmos_clip(frame, va, w);140    float4 cb = cosmos_clip(frame, vb, w);141    float2 halfViewport = frame.viewport.xy * 0.5f;142    float2 pa = ca.xy / ca.w * halfViewport;143    float2 pb = cb.xy / cb.w * halfViewport;144    float2 delta = pb - pa;145    float len = length(delta);146    float2 dir = len > 1e-5f ? delta / len : float2(1.0f, 0.0f);147    float2 normal = float2(-dir.y, dir.x);148149    bool second = (vid & 1u) != 0u;150    float side = (vid & 2u) != 0u ? 1.0f : -1.0f;151    float halfWidth = 0.5f * line.params.x * frame.scaleParams.x;152    float expand = halfWidth + 1.0f;153    float4 clip = second ? cb : ca;154    float2 px = (second ? pb : pa) + normal * (side * expand);155    clip.xy = px / halfViewport * clip.w;156    out.position = cosmos_crop(frame, clip);157    out.across = side * expand;158    out.alpha = second ? alphaB : alphaA;159    out.dash = second ? dashB : dashA;160    out.halfWidth = halfWidth;161    return out;162}163164fragment float4 line_fragment(LineVaryings in [[stage_in]],165                              constant FrameUniforms &frame [[buffer(1)]],166                              constant LineUniforms &line [[buffer(2)]]) {167    float coverage = clamp(in.halfWidth + 0.5f - abs(in.across), 0.0f, 1.0f);168    // Lines thinner than a pixel keep their width and lose opacity instead.169    coverage *= min(1.0f, in.halfWidth * 2.0f + 0.25f);170    float on = line.params.y, off = line.params.z;171    if (off > 0.0f) {172        float period = on + off;173        float phase = fmod(in.dash, period);174        float aa = max(fwidth(in.dash), 1e-4f);175        coverage *= smoothstep(0.0f, aa, phase) * (1.0f - smoothstep(on - aa, on, phase));176    }177    float alpha = line.color.a * in.alpha * coverage;178    return float4(line.color.rgb * alpha, alpha);179}180
Files main
Menu

Theme