Files
Gas-Tank-AR-App/GasTankAR/Views/TankAR/TankShaders.metal
2026-09-02 20:54:36 +02:00

57 lines
1.9 KiB
Metal

//
// TankShaders.metal
// GasTankAR
//
// Created by Dennis Nemec on 26.08.26.
//
#include <metal_stdlib>
#include <RealityKit/RealityKit.h>
using namespace metal;
// Sampler für die durchgereichten Texturen des Originalmaterials.
constexpr sampler textureSampler(address::clamp_to_edge, filter::bicubic);
/// Surface-Shader für den unterirdischen Tank: reicht Basisfarbe/Roughness/Metallic
/// des Originalmaterials durch und verblasst die Opacity anhand der Welt-Höhe.
///
/// custom_parameter():
/// x = Boden-Höhe (Welt-Y)
/// y = Fade-Band (Meter, über die die Transparenz einsetzt)
/// z = minimale Opacity des vergrabenen Teils
[[visible]]
void tankDepthFadeSurface(realitykit::surface_parameters params)
{
auto tex = params.textures();
auto constants = params.material_constants();
float2 uv = params.geometry().uv0();
// Modelle aus USDZ nutzen eine gespiegelte Y-UV.
uv.y = 1.0 - uv.y;
// Basisfarbe = Tint * Textur.
half3 tint = (half3)constants.base_color_tint();
half3 color = (half3)tex.base_color().sample(textureSampler, uv).rgb;
color *= tint;
params.surface().set_base_color(color);
// Roughness / Metallic durchreichen (wie PhysicallyBasedMaterial).
half roughness = tex.roughness().sample(textureSampler, uv).r * constants.roughness_scale();
params.surface().set_roughness(roughness);
half metallic = tex.metallic().sample(textureSampler, uv).r * constants.metallic_scale();
params.surface().set_metallic(metallic);
// Höhenbasierter Alpha-Fade.
float3 worldPosition = params.geometry().world_position();
float4 custom = params.uniforms().custom_parameter();
float groundY = custom.x;
float band = max(custom.y, 1e-3);
float belowAlpha = custom.z;
float t = clamp((groundY - worldPosition.y) / band, 0.0, 1.0);
float alpha = mix(1.0, belowAlpha, t);
params.surface().set_opacity(half(alpha));
}