lygia
/lighting
/raymarch
/normal
)Calculate normals http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
Dependencies:
Use:
<float> raymarchNormal( in <vec3> pos )
#ifndef RAYMARCH_NORMAL_OFFSET
#define RAYMARCH_NORMAL_OFFSET 0.0001
#endif
#ifndef FNC_RAYMARCH_NORMAL
#define FNC_RAYMARCH_NORMAL
vec3 raymarchNormal(vec3 pos, vec2 pixel) {
vec2 offset = vec2(1.0, -1.0) * pixel;
return normalize( offset.xyy * RAYMARCH_MAP_FNC( pos + offset.xyy ).sdf +
offset.yyx * RAYMARCH_MAP_FNC( pos + offset.yyx ).sdf +
offset.yxy * RAYMARCH_MAP_FNC( pos + offset.yxy ).sdf +
offset.xxx * RAYMARCH_MAP_FNC( pos + offset.xxx ).sdf );
}
vec3 raymarchNormal(vec3 pos, float e) {
vec2 offset = vec2(1.0, -1.0) * e;
return normalize( offset.xyy * RAYMARCH_MAP_FNC( pos + offset.xyy ).sdf +
offset.yyx * RAYMARCH_MAP_FNC( pos + offset.yyx ).sdf +
offset.yxy * RAYMARCH_MAP_FNC( pos + offset.yxy ).sdf +
offset.xxx * RAYMARCH_MAP_FNC( pos + offset.xxx ).sdf );
}
vec3 raymarchNormal( in vec3 pos ) {
return raymarchNormal(pos, RAYMARCH_NORMAL_OFFSET);
}
#endif
Dependencies:
Use:
<float> raymarchNormal( in <float3> pos )
#ifndef RAYMARCH_NORMAL_OFFSET
#define RAYMARCH_NORMAL_OFFSET 0.0001
#endif
#ifndef FNC_RAYMARCH_NORMAL
#define FNC_RAYMARCH_NORMAL
float3 raymarchNormal(float3 pos, float2 pixel) {
float2 offset = float2(1.0, -1.0) * pixel;
return normalize( offset.xyy * RAYMARCH_MAP_FNC(pos + offset.xyy).sdf +
offset.yyx * RAYMARCH_MAP_FNC(pos + offset.yyx).sdf +
offset.yxy * RAYMARCH_MAP_FNC(pos + offset.yxy).sdf +
offset.xxx * RAYMARCH_MAP_FNC(pos + offset.xxx).sdf);
}
float3 raymarchNormal(float3 pos, float e) {
const float2 offset = float2(1.0, -1.0) * e;
return normalize(offset.xyy * RAYMARCH_MAP_FNC(pos + offset.xyy).sdf +
offset.yyx * RAYMARCH_MAP_FNC(pos + offset.yyx).sdf +
offset.yxy * RAYMARCH_MAP_FNC(pos + offset.yxy).sdf +
offset.xxx * RAYMARCH_MAP_FNC(pos + offset.xxx).sdf);
}
float3 raymarchNormal( in float3 pos ) {
return raymarchNormal(pos, RAYMARCH_NORMAL_OFFSET);
}
#endif
// #include "map.wgls"
fn raymarchNormal( pos: vec3f ) -> vec3f {
let eps = 0.002;
let v1 = vec3f( 1.0,-1.0,-1.0);
let v2 = vec3f(-1.0,-1.0, 1.0);
let v3 = vec3f(-1.0, 1.0,-1.0);
let v4 = vec3f( 1.0, 1.0, 1.0);
return normalize( v1 * map( pos + v1 * eps ) +
v2 * map( pos + v2 * eps ) +
v3 * map( pos + v3 * eps ) +
v4 * map( pos + v4 * eps ) );
}
Dependencies:
Use:
<float> raymarchNormal( in <float3> pos )
#ifndef RAYMARCH_MAP_FNC
#define RAYMARCH_MAP_FNC(POS) raymarchMap(POS)
#endif
#ifndef RAYMARCH_MAP_DISTANCE
#define RAYMARCH_MAP_DISTANCE w
#endif
#ifndef FNC_RAYMARCH_NORMAL
#define FNC_RAYMARCH_NORMAL
inline __host__ __device__ float3 raymarchNormal(const float3& pos, float2 pixel) {
float2 offset = make_float2(1.0f, -1.0f) * pixel;
float3 offset_xyy = make_float3(offset.x, offset.y, offset.y);
float3 offset_yyx = make_float3(offset.y, offset.y, offset.x);
float3 offset_yxy = make_float3(offset.y, offset.x, offset.y);
float3 offset_xxx = make_float3(offset.x, offset.x, offset.x);
return normalize( offset_xyy * RAYMARCH_MAP_FNC( pos + offset_xyy ).RAYMARCH_MAP_DISTANCE +
offset_yyx * RAYMARCH_MAP_FNC( pos + offset_yyx ).RAYMARCH_MAP_DISTANCE +
offset_yxy * RAYMARCH_MAP_FNC( pos + offset_yxy ).RAYMARCH_MAP_DISTANCE +
offset_xxx * RAYMARCH_MAP_FNC( pos + offset_xxx ).RAYMARCH_MAP_DISTANCE );
}
inline __host__ __device__ float3 raymarchNormal(const float3& pos, float e) {
const float2 offset = make_float2(1.0f, -1.0f) * e;
float3 offset_xyy = make_float3(offset.x, offset.y, offset.y);
float3 offset_yyx = make_float3(offset.y, offset.y, offset.x);
float3 offset_yxy = make_float3(offset.y, offset.x, offset.y);
float3 offset_xxx = make_float3(offset.x, offset.x, offset.x);
return normalize( offset_xyy * RAYMARCH_MAP_FNC( pos + offset_xyy ).RAYMARCH_MAP_DISTANCE +
offset_yyx * RAYMARCH_MAP_FNC( pos + offset_yyx ).RAYMARCH_MAP_DISTANCE +
offset_yxy * RAYMARCH_MAP_FNC( pos + offset_yxy ).RAYMARCH_MAP_DISTANCE +
offset_xxx * RAYMARCH_MAP_FNC( pos + offset_xxx ).RAYMARCH_MAP_DISTANCE );
}
inline __host__ __device__ float3 raymarchNormal(const float3& pos) {
return raymarchNormal(pos, 0.5773f * 0.0005f);
}
#endif
precision mediump float;
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
// #include "lygia/lighting/atmosphere.glsl"
// #define RAYMARCH_BACKGROUND atmosphere(normal, normalize(u_light))
// #define RAYMARCH_AMBIENT atmosphere(normal, normalize(u_light))
float checkBoard(vec2 uv, vec2 _scale) {
uv = floor(fract(uv * _scale) * 2.0);
return min(1.0, uv.x + uv.y) - (uv.x * uv.y);
}
Material raymarchMap( in vec3 pos ) {
float check = 0.5 + checkBoard(pos.xz, vec2(1.0, 1.0)) * 0.5;
Material res = materialNew(vec3(check), 0.0, 0.5, planeSDF(pos));
res = opUnion( res, materialNew( vec3(1.0, 1.0, 1.0), 1.0, 0.0, sphereSDF( pos-vec3( 0.0, 0.60, 0.0), 0.5 ) ) );
res = opUnion( res, materialNew( vec3(0.0, 1.0, 1.0), boxSDF( pos-vec3( 2.0, 0.5, 0.0), vec3(0.4, 0.4, 0.4) ) ) );
res = opUnion( res, materialNew( vec3(0.3, 0.3, 1.0), torusSDF( pos-vec3( 0.0, 0.5, 2.0), vec2(0.4,0.1) ) ) );
res = opUnion( res, materialNew( vec3(0.3, 0.1, 0.3), capsuleSDF( pos,vec3(-2.3, 0.4,-0.2), vec3(-1.6,0.75,0.2), 0.2 ) ) );
res = opUnion( res, materialNew( vec3(0.5, 0.3, 0.4), triPrismSDF( pos-vec3(-2.0, 0.50,-2.0), vec2(0.5,0.1) ) ) );
res = opUnion( res, materialNew( vec3(0.2, 0.2, 0.8), cylinderSDF( pos-vec3( 2.0, 0.50,-2.0), vec2(0.2,0.4) ) ) );
res = opUnion( res, materialNew( vec3(0.7, 0.5, 0.2), coneSDF( pos-vec3( 0.0, 0.75,-2.0), vec3(0.8,0.6,0.6) ) ) );
res = opUnion( res, materialNew( vec3(0.4, 0.2, 0.9), hexPrismSDF( pos-vec3(-2.0, 0.60, 2.0), vec2(0.5,0.1) ) ) );
res = opUnion( res, materialNew( vec3(0.1, 0.3, 0.6), pyramidSDF( pos-vec3( 2.0, 0.10, 2.0), 1.0 ) ) );;
return res;
}
void main() {
vec3 color = vec3(0.0);
vec2 pixel = 1.0/u_resolution;
vec2 st = gl_FragCoord.xy * pixel;
vec2 uv = ratio(st, u_resolution);
vec2 mo = u_mouse * pixel;
float time = 32.0 + u_time * 1.5;
vec3 cam = vec3( 4.5*cos(0.1*time - 7.0*mo.x), 2.2, 4.5*sin(0.1*time - 7.0*mo.x) );
color = raymarch(cam, vec3(0.0), uv).rgb;
color = linear2gamma(color);
gl_FragColor = vec4( color, 1.0 );
}
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