lygia
/lighting
/raymarch
/cast
)Cast a ray
Dependencies:
Use:
<float> castRay( in <vec3> pos, in <vec3> nor )
#ifndef RAYMARCH_SAMPLES
#define RAYMARCH_SAMPLES 256
#endif
#ifndef RAYMARCH_MIN_DIST
#define RAYMARCH_MIN_DIST 0.1
#endif
#ifndef RAYMARCH_MAX_DIST
#define RAYMARCH_MAX_DIST 20.0
#endif
#ifndef RAYMARCH_MIN_HIT_DIST
#define RAYMARCH_MIN_HIT_DIST 0.00001 * t
#endif
#ifndef FNC_RAYMARCH_CAST
#define FNC_RAYMARCH_CAST
Material raymarchCast( in vec3 ro, in vec3 rd ) {
float tmin = RAYMARCH_MIN_DIST;
float tmax = RAYMARCH_MAX_DIST;
float t = tmin;
Material m = materialNew();
m.valid = false;
for (int i = 0; i < RAYMARCH_SAMPLES; i++) {
Material res = RAYMARCH_MAP_FNC(ro + rd * t);
if (res.sdf < RAYMARCH_MIN_HIT_DIST || t > tmax)
break;
m = res;
t += res.sdf;
}
#if defined(RAYMARCH_BACKGROUND) || defined(RAYMARCH_FLOOR)
if ( t > tmax )
m.valid = false;
#endif
m.sdf = t;
return m;
}
#endif
Dependencies:
Use:
<float> castRay( in <float3> pos, in <float3> nor )
#ifndef RAYMARCH_SAMPLES
#define RAYMARCH_SAMPLES 256
#endif
#ifndef RAYMARCH_MIN_DIST
#define RAYMARCH_MIN_DIST 0.1
#endif
#ifndef RAYMARCH_MAX_DIST
#define RAYMARCH_MAX_DIST 20.0
#endif
#ifndef RAYMARCH_MIN_HIT_DIST
#define RAYMARCH_MIN_HIT_DIST 0.00001 * t
#endif
#ifndef FNC_RAYMARCH_CAST
#define FNC_RAYMARCH_CAST
Material raymarchCast( in float3 ro, in float3 rd ) {
float tmin = RAYMARCH_MIN_DIST;
float tmax = RAYMARCH_MAX_DIST;
float t = tmin;
Material m = materialNew();
m.valid = false;
for (int i = 0; i < RAYMARCH_SAMPLES; i++) {
Material res = RAYMARCH_MAP_FNC(ro + rd * t);
if (res.sdf < RAYMARCH_MIN_HIT_DIST || t > tmax)
break;
m = res;
t += res.sdf;
}
#if defined(RAYMARCH_BACKGROUND) || defined(RAYMARCH_FLOOR)
if ( t > tmax )
m.valid = false;
#endif
m.sdf = t;
return m;
}
#endif
// #include "map.wgls"
fn raymarchCast(ro: vec3f, rd: vec3f ) -> f32 {
var dist = -1.0;
var t = -1.0;
for (var i = 0; i < 120 && t < 100.0; i++) {
let h = map( ro + rd * t );
if (abs(h) < (0.001 * t)) {
dist = t;
break;
}
t += h;
}
return dist;
}
Dependencies:
Use:
<float> castRay( in <float3> pos, in <float3> nor )
#ifndef RAYMARCH_SAMPLES
#define RAYMARCH_SAMPLES 64
#endif
#ifndef RAYMARCH_MAP_FNC
#define RAYMARCH_MAP_FNC(POS) raymarchMap(POS)
#endif
#ifndef RAYMARCH_MAP_TYPE
#define RAYMARCH_MAP_TYPE float4
#endif
#ifndef RAYMARCH_MAP_DISTANCE
#define RAYMARCH_MAP_DISTANCE w
#endif
#ifndef FNC_RAYMARCH_CAST
#define FNC_RAYMARCH_CAST
inline __host__ __device__ RAYMARCH_MAP_TYPE raymarchCast(const float3& ro, const float3& rd) {
float tmin = 1.0;
float tmax = 20.0;
// #if defined(RAYMARCH_FLOOR)
// float tp1 = (0.0-ro.y)/rd.y; if( tp1>0.0 ) tmax = min( tmax, tp1 );
// float tp2 = (1.6-ro.y)/rd.y; if( tp2>0.0 ) { if( ro.y>1.6 ) tmin = max( tmin, tp2 );
// else tmax = min( tmax, tp2 ); }
// #endif
float t = tmin;
RAYMARCH_MAP_TYPE m = make_float4(-1.0f);
for ( int i = 0; i < RAYMARCH_SAMPLES; i++ ) {
float precis = 0.00001*t;
RAYMARCH_MAP_TYPE res = RAYMARCH_MAP_FNC( ro + rd * t );
if ( res.w < precis || t > tmax )
break;
t += res.w;
m = res;
}
#if defined(RAYMARCH_BACKGROUND) || defined(RAYMARCH_FLOOR)
if ( t > tmax )
m = make_float4(-1.0);
#endif
m.RAYMARCH_MAP_DISTANCE = t;
return m;
}
#endif
LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.
Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).
It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.
Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository