Ray Marching
🎨 Ray Marching
Section titled “🎨 Ray Marching”Strata provides GPU-accelerated ray marching for rendering complex procedural geometry defined by Signed Distance Functions (SDFs).
Quick Start
Section titled “Quick Start”import { Raymarching } from '@strata-game-library/core';
<Raymarching />Component
Section titled “Component”<Raymarching>
Section titled “<Raymarching>”Full-screen ray marching renderer:
import { Raymarching } from '@strata-game-library/core';
<Raymarching // SDF scene sdf={customSDF}
// Quality maxSteps={100} maxDistance={100} surfaceDistance={0.001}
// Lighting lightPosition={[10, 20, 10]} ambientLight={0.1}
// Materials materialFunction={customMaterial}
// Effects shadows softShadows ambientOcclusion
// Post-processing fog fogColor="#000000" fogDensity={0.02}/>SDF Primitives
Section titled “SDF Primitives”Basic Shapes
Section titled “Basic Shapes”import { sdSphere, sdBox, sdPlane, sdCapsule, sdTorus, sdCone, sdCylinder} from '@strata-game-library/core';
// Sphereconst sphere = sdSphere(point, center, radius);
// Boxconst box = sdBox(point, center, size);
// Planeconst plane = sdPlane(point, normal, distance);
// Capsuleconst capsule = sdCapsule(point, start, end, radius);
// Torusconst torus = sdTorus(point, center, majorRadius, minorRadius);
// Coneconst cone = sdCone(point, tip, height, angle);
// Cylinderconst cylinder = sdCylinder(point, start, end, radius);SDF Operations
Section titled “SDF Operations”import { opUnion, opSubtraction, opIntersection, opSmoothUnion, opSmoothSubtraction, opSmoothIntersection} from '@strata-game-library/core';
// Boolean operationsconst union = opUnion(d1, d2);const subtraction = opSubtraction(d1, d2);const intersection = opIntersection(d1, d2);
// Smooth blendingconst smoothUnion = opSmoothUnion(d1, d2, smoothness);const smoothSubtraction = opSmoothSubtraction(d1, d2, smoothness);const smoothIntersection = opSmoothIntersection(d1, d2, smoothness);Domain Operations
Section titled “Domain Operations”import { opRepeat, opRepeatLimited, opTwist, opBend, opDisplace} from '@strata-game-library/core';
// Infinite repetitionconst repeated = opRepeat(point, spacing);
// Limited repetitionconst limited = opRepeatLimited(point, spacing, count);
// Twistconst twisted = opTwist(point, amount);
// Bendconst bent = opBend(point, amount);
// Displacementconst displaced = opDisplace(point, noiseFunction);Custom SDF Scenes
Section titled “Custom SDF Scenes”Simple Scene
Section titled “Simple Scene”function myScene(point: [number, number, number]): number { // Ground plane const ground = sdPlane(point, [0, 1, 0], 0);
// Sphere above ground const sphere = sdSphere(point, [0, 1, 0], 1);
// Combine return opUnion(ground, sphere);}
<Raymarching sdf={myScene} />Complex Scene with Materials
Section titled “Complex Scene with Materials”interface SDFResult { distance: number; materialId: number;}
function complexScene(point: [number, number, number]): SDFResult { // Ground (material 0) const ground = { distance: sdPlane(point, [0, 1, 0], 0), materialId: 0 };
// Metal sphere (material 1) const sphere = { distance: sdSphere(point, [0, 1, 0], 1), materialId: 1 };
// Glass cube (material 2) const cube = { distance: sdBox(point, [2, 0.5, 0], [0.5, 0.5, 0.5]), materialId: 2 };
// Combine with material tracking let result = ground; if (sphere.distance < result.distance) result = sphere; if (cube.distance < result.distance) result = cube;
return result;}
function materialFunction(materialId: number, point: [number, number, number], normal: [number, number, number]) { switch (materialId) { case 0: return { color: [0.2, 0.3, 0.1], roughness: 1 }; // Ground case 1: return { color: [0.8, 0.8, 0.9], roughness: 0.1 }; // Metal case 2: return { color: [0.9, 0.9, 1.0], roughness: 0, transmission: 0.9 }; // Glass default: return { color: [1, 0, 1], roughness: 0.5 }; }}
<Raymarching sdf={complexScene} materialFunction={materialFunction}/>Animated Scene
Section titled “Animated Scene”import { useFrame } from '@react-three/fiber';
function AnimatedRaymarching() { const [time, setTime] = useState(0);
useFrame((_, delta) => { setTime(t => t + delta); });
const animatedScene = useCallback((point: [number, number, number]) => { // Animated sphere position const spherePos: [number, number, number] = [ Math.sin(time) * 2, 1 + Math.sin(time * 2) * 0.5, Math.cos(time) * 2 ];
const ground = sdPlane(point, [0, 1, 0], 0); const sphere = sdSphere(point, spherePos, 0.5);
return opUnion(ground, sphere); }, [time]);
return <Raymarching sdf={animatedScene} />;}Lighting & Shadows
Section titled “Lighting & Shadows”Basic Lighting
Section titled “Basic Lighting”<Raymarching lightPosition={[10, 20, 10]} lightColor="#ffffff" lightIntensity={1} ambientLight={0.1} ambientColor="#334455"/>Soft Shadows
Section titled “Soft Shadows”<Raymarching shadows softShadows shadowSoftness={32} shadowBias={0.01}/>Ambient Occlusion
Section titled “Ambient Occlusion”<Raymarching ambientOcclusion aoSteps={5} aoIntensity={0.5} aoRadius={0.5}/>Global Illumination (Approximate)
Section titled “Global Illumination (Approximate)”<Raymarching globalIllumination giSamples={4} giIntensity={0.3}/>Effects
Section titled “Effects”<Raymarching fog fogColor="#112233" fogDensity={0.02} fogType="exponential" // or "linear"/><Raymarching glow glowColor="#00ffff" glowIntensity={0.5} glowRadius={0.1}/>Reflections
Section titled “Reflections”<Raymarching reflections reflectionBounces={3} reflectionFalloff={0.5}/>Shader Access
Section titled “Shader Access”Use ray marching shaders directly:
import { createRaymarchingMaterial } from '@strata-game-library/core';
const material = createRaymarchingMaterial({ sdfFunction: ` float map(vec3 p) { float ground = p.y; float sphere = length(p - vec3(0, 1, 0)) - 1.0; return min(ground, sphere); } `, maxSteps: 100, maxDistance: 100});Performance
Section titled “Performance”Quality Settings
Section titled “Quality Settings”// Mobile<Raymarching maxSteps={50} surfaceDistance={0.01} shadows={false} ambientOcclusion={false}/>
// Desktop<Raymarching maxSteps={100} surfaceDistance={0.001} shadows softShadows ambientOcclusion/>
// High-end<Raymarching maxSteps={200} surfaceDistance={0.0001} shadows softShadows ambientOcclusion reflections reflectionBounces={4}/>Optimization Tips
Section titled “Optimization Tips”- Reduce maxSteps for complex scenes
- Increase surfaceDistance for faster convergence
- Use bounding volumes to skip empty space
- Limit shadow/AO steps on mobile
Related
Section titled “Related”- Terrain - SDF-based terrain
- Volumetrics - Volumetric effects
- Shaders - Custom shader access