cvr-props/Assets/raymarched/lib/libgarbage_shapes.cginc

53 lines
1.5 KiB
HLSL

#define _MAT_VARIANT1(NEW_NAME, BASE_FN, TYPE_1, ARG_1) \
SurfacePoint NEW_NAME(float3 p, TYPE_1 ARG_1, Material mat = DEFAULT_MAT) {\
SurfacePoint o;\
o.dist = BASE_FN(p, ARG_1);\
o.mat = mat;\
return o;\
}
#define _MAT_VARIANT2(NEW_NAME, BASE_FN, TYPE_1, ARG_1, TYPE_2, ARG_2) \
SurfacePoint NEW_NAME(float3 p, TYPE_1 ARG_1, TYPE_2 ARG_2, Material mat = DEFAULT_MAT) {\
SurfacePoint o;\
o.dist = BASE_FN(p, ARG_1, ARG_2);\
o.mat = mat;\
return o;\
}
#define _MAT_VARIANT3(NEW_NAME, BASE_FN, TYPE_1, ARG_1, TYPE_2, ARG_2, TYPE_3, ARG_3) \
SurfacePoint NEW_NAME(float3 p, TYPE_1 ARG_1, TYPE_2 ARG_2, TYPE_3 ARG_3, Material mat = DEFAULT_MAT) {\
SurfacePoint o;\
o.dist = BASE_FN(p, ARG_1, ARG_2, ARG_3);\
o.mat = mat;\
return o;\
}
//
// Most of these are taken from https://iquilezles.org/articles/distfunctions/
// Thank you Inigo Quilez <3
float sdBox(float3 p, float3 size) {
float3 q = abs(p) - size / 2.0;
return length(max(q, 0)) + min(max(q.x, max(q.y, q.z)), 0);
}
_MAT_VARIANT1(mBox, sdBox, float3, size)
float sdSphere(float3 p, float radius) {
return length(p) - radius;
}
_MAT_VARIANT1(mSphere, sdSphere, float, radius)
float sdTorus(float3 p, float radius, float thickness) {
float2 q = float2(length(p.xz) - radius, p.y);
return length(q) - thickness;
}
_MAT_VARIANT2(mTorus, sdTorus, float, radius, float, thickness)
float sdPlaneY(float3 p, float height) {
return p.y - height;
}
_MAT_VARIANT1(mPlaneY, sdPlaneY, float, height)