Compare commits

..

7 commits

11 changed files with 715 additions and 74 deletions

View file

@ -529,7 +529,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!65 &683266144
BoxCollider:
m_ObjectHideFlags: 0
@ -598,7 +598,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 683266143}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.754, y: -0.24626623, z: 0.587}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
@ -773,7 +773,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!4 &779234489
Transform:
m_ObjectHideFlags: 0
@ -1288,6 +1288,99 @@ MeshFilter:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 950930976}
m_Mesh: {fileID: 4393975565123819156, guid: 955b9c52fc0be3217a2074e1e2169b9d, type: 3}
--- !u!1 &1274159448
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1274159452}
- component: {fileID: 1274159451}
- component: {fileID: 1274159450}
- component: {fileID: 1274159449}
m_Layer: 0
m_Name: Cube (2)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!65 &1274159449
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1274159448}
m_Material: {fileID: 0}
m_IsTrigger: 1
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!23 &1274159450
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1274159448}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: c5afce7a29c155681939e1ad4f96dc18, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &1274159451
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1274159448}
m_Mesh: {fileID: -5495902117074765545, guid: 85702488cb92b9182a44437d143f9fc4, type: 3}
--- !u!4 &1274159452
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1274159448}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 1, y: 0.26, z: -0.407}
m_LocalScale: {x: 0.05, y: 0.05, z: 0.05}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 8
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1450495540
GameObject:
m_ObjectHideFlags: 0
@ -1516,7 +1609,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!65 &1485771012
BoxCollider:
m_ObjectHideFlags: 0
@ -1585,8 +1678,8 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1485771011}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.452, y: 0.221, z: -0.984}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalPosition: {x: 0.429, y: 0.221, z: -1.743}
m_LocalScale: {x: 19.2, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
@ -1668,7 +1761,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1500710831}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalPosition: {x: 0, y: 0.009, z: -0.786}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}

View file

@ -63,8 +63,8 @@ Material:
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _MaxDist: 256
- _MaxSteps: 256
- _MaxDist: 512
- _MaxSteps: 512
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1

View file

@ -1,61 +1,290 @@
struct appdata
{
#ifndef LIBGARBAGE_INCLUDED
#define LIBGARBAGE_INCLUDED
#include "UnityCG.cginc"
/*
## defines with no defaults
# default is object space
USE_WORLD_SPACE
# renders missed rays as transparent
DISCARD_ON_MISS
# use if the shape of the scene is significnatly different from the shape of the material space
# will use SCENE_FN only for the final step, and DISTANCE_FN for raymarching
# DISTANCE_FN must return a float
SEPARATE_MATERIAL_AND_DIST_FUNCTIONS
# don't write depth (will have the depth defined by the original mesh)
DISABLE_DEPTH
# meant for frontface culled meshes, makes depth no further than the mesh edges
LIMIT_DEPTH_TO_MESH
START_RAYS_IN_BOX // TODO: implement
START_RAYS_IN_SPHERE // TODO: implement
*/
// scene sdf that includes material data
#ifndef SCENE_FN
#define SCENE_FN default_material_sdf
#endif
#ifdef SEPARATE_MATERIAL_AND_DIST_FUNCTIONS
// scene sdf with only distance
#ifndef DISTANCE_FN
#define DISTANCE_FN default_distance_sdf
#endif
#else
#define DISTANCE_FN material_fn_as_dist
#endif
// calculates a color from a Ray
#ifndef LIGHT_FN
#define LIGHT_FN default_lighting
#endif
// set to less than one if there are artifacts in distorted sdfs
#ifndef STEP_MULTIPLIER
#define STEP_MULTIPLIER 1
#endif
// max ray steps, from last bounce (reset on each reflection)
#ifndef MAX_STEPS
#define MAX_STEPS 128
#endif
// max ray length, from last bounce (length is reset on each reflection)
#ifndef MAX_DIST
#define MAX_DIST 128
#endif
// the largest surface distance that is counted as a hit
#ifndef SURF_DIST
#define SURF_DIST 0.001
#endif
#ifndef REFLECTIONS
#define REFLECTIONS 0
#endif
#ifndef SCENE_SCALE
#define SCENE_SCALE 1
#endif
// TODO: implement
#ifndef AMBIENT_OCCLUSION_STEPS
#define AMBIENT_OCCLUSION_STEPS 4
#endif
struct AppData {
float4 vertex : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
struct V2F {
float4 vertex : SV_POSITION;
float3 vCamPos : TEXCOORD1;
float3 vHitPos : TEXCOORD2;
float3 cam_pos : TEXCOORD0;
float3 hit_pos : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
};
struct fragOut
{
fixed4 col : SV_Target;
// float depth : SV_Depth;
struct FragOut {
fixed3 col : SV_Target;
#ifndef DISABLE_DEPTH
float depth : SV_Depth;
#endif
};
v2f vert (appdata v)
{
v2f o;
struct Material {
float3 col;
float gloss;
};
#define DEFAULT_MAT {float3(1, 1, 1), 0}
Material mat(float3 col = float3(1, 1,1 ), float gloss = 0) {
Material m;
m.col = col;
m.gloss = gloss;
return m;
}
struct SurfacePoint {
float dist;
Material mat;
};
//used for lighting a point
struct Ray {
float dist;
int steps;
Material mat;
float3 start;
float3 dir;
float3 hit_pos;
float3 normal;
bool missed;
float min_dist; // smallest distance encountered along the path (only useful for misses; shadow calculations)
};
float DISTANCE_FN(float3 p);
SurfacePoint SCENE_FN(float3 p);
float3 LIGHT_FN(Ray ray);
Ray cast_ray(float3 p, float3 d, float startDist = 0);
#include "libgarbage_shapes.cginc"
#include "libgarbage_operations.cginc"
#include "libgarbage_lighting.cginc"
V2F vert (AppData v) {
V2F o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_OUTPUT(V2F, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
#ifdef USE_WORLD_SPACE
o.vCamPos = _WorldSpaceCameraPos;
o.vHitPos = mul(unity_ObjectToWorld, v.vertex);
o.cam_pos = _WorldSpaceCameraPos;
o.hit_pos = mul(unity_ObjectToWorld, v.vertex);
#else
o.vCamPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1));
o.vHitPos = v.vertex;
o.cam_pos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1));
o.hit_pos = v.vertex;
#endif
return o;
}
fragOut frag (v2f i)
{
fragOut o;
o.col = 1;
o.col.r = 0;
FragOut frag (V2F i) {
float ray_len = 0;
float3 ray_dir = normalize(i.hit_pos - i.cam_pos);
float3 ray_origin = i.cam_pos;
ray_origin /= SCENE_SCALE;
SurfacePoint point_data;
Ray ray;
float3 col;
float color_used = 0;// what amount of the final color has been calculated
float prev_gloss = 0;
float3 first_hit;
int ray_num = 0;
for (; ray_num < REFLECTIONS + 1;) {
ray = cast_ray(ray_origin, ray_dir);
if (ray_num == 0) { // before any bounces
col = LIGHT_FN(ray);
first_hit = ray.hit_pos;
if (ray.steps == 0) {
// discard;
// TODO: sky fn
}
} else {
float col_amount = color_used + ((1 - prev_gloss) * (1 - color_used));
col = lerp(LIGHT_FN(ray), col, col_amount);
color_used = col_amount;
}
ray_num++;
if (ray.missed || ray.mat.gloss < 0.01) {
break;
}
prev_gloss = ray.mat.gloss;
ray_dir = reflect(ray_dir, ray.normal);
ray_origin = ray.hit_pos + ray_dir * 0.01;
}
#ifdef DISCARD_ON_MISS
if (ray.missed && ray_num == 1) discard;
#endif
FragOut o;
o.col = col;
#ifndef DISABLE_DEPTH
float3 depth_point = first_hit * SCENE_SCALE;
if (ray.missed && ray_num == 1)
depth_point = i.hit_pos;
#ifdef LIMIT_DEPTH_TO_MESH
if (length(ray.start - ray.hit_pos) > length(i.cam_pos - i.hit_pos))
depth_point = i.hit_pos;
#endif
#ifdef USE_WORLD_SPACE
float4 clip_pos = mul(UNITY_MATRIX_VP, float4(depth_point, 1));
#else
float4 clip_pos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(depth_point, 1)));
#endif
o.depth = clip_pos.z / clip_pos.w;
#ifndef UNITY_REVERSED_Z // basically only OpenGL (unity editor on linux)
o.depth = o.depth * 0.5 + 0.5; // remap -1 to 1 range to 0.0 to 1.0
#endif
#endif
return o;
}
// #define SOME_MAGIC(main_fn) float3 main1()\
// main_fn \
inline float3 get_normal(float3 pos, float surface_distance, float epsilon = 0.001) {
// if epsilon is smaller than 0.001, there are often artifacts
const float2 e = float2(epsilon, 0);
float3 n = surface_distance - float3(
DISTANCE_FN(pos - e.xyy),
DISTANCE_FN(pos - e.yxy),
DISTANCE_FN(pos - e.yyx));
return normalize(n);
}
#define SOME_MAGIC(PASS, function_definition) float3 main##PASS() function_definition
Ray cast_ray(float3 start, float3 dir, float start_len) {
float ray_len = start_len;
#define DO_MAGIC MAIN_FN(1)
Ray ray;
ray.dir = dir;
ray.start = start;
ray.min_dist = 100100100; // budget "infinity"
ray.missed = true;
// #define SECOND_PASS 1
// #define AA MAIN_FN
// #define BB MAIN_FN
for (int i = 0; i < MAX_STEPS; i++) {
ray.hit_pos = start + ray_len * dir;
ray.dist = DISTANCE_FN(ray.hit_pos);
if (ray.dist < SURF_DIST) {
ray.missed = false;
break;
}
ray.min_dist = min(ray.min_dist, ray.dist);
ray_len += ray.dist * STEP_MULTIPLIER;
if (ray_len > MAX_DIST) break;
}
ray.steps = i;
if (ray.missed) {
ray.normal = float3(0, 0, 0);
ray.mat = mat();
} else {
ray.normal = get_normal(ray.hit_pos, ray.dist);
ray.mat = SCENE_FN(ray.hit_pos).mat;
}
return ray;
}
float default_distance_sdf(float3 p) {
return sdSphere(p, 0.5);
}
float material_fn_as_dist(float3 p) {
return SCENE_FN(p).dist;
}
SurfacePoint default_material_sdf(float3 p) {
return mSphere(p, 0.5);
}
float3 default_lighting(Ray ray) {
float3 sun_dir = normalize(float3(1, 0.5, 0));
if (ray.missed)
return 0.1;
float3 col = 0;
col = ray.mat.col * dot(ray.normal, sun_dir);
return col;
}
// float3 main2() \
// main_fn
// \
// float3 main2() \
// main_fn\
// #define SECOND_PASS \
#endif // LIBGARBAGE_INCLUDED

View file

@ -1 +0,0 @@
#define DO_MAGIC(p) MAIN_FN(p)

View file

@ -2,17 +2,16 @@ Shader "CrispyPin/LibGarbageExample"
{
Properties
{
[Header(Raymarcher Properties)]
_MaxSteps ("Max steps", Int) = 256
_MaxDist ("Max distance", Float) = 256
_SurfDist ("Surface distance threshold", Range(0.00001, 0.05)) = 0.001
_MaxSteps ("Max steps", Int) = 128
_MaxDist ("Max distance", Float) = 128
_SurfDist ("Surface distance threshold", Range(0.0001, 0.05)) = 0.001
}
SubShader
{
Tags { "RenderType"="Opaque" }
Cull Off
Cull Front
LOD 100
Pass
@ -21,22 +20,68 @@ Shader "CrispyPin/LibGarbageExample"
#pragma vertex vert
#pragma fragment frag
int _MaxSteps;
float _MaxDist;
float _SurfDist;
#define MAX_STEPS _MaxSteps
#define MAX_DIST _MaxDist
#define SURF_DIST _SurfDist
#define REFLECTIONS 3
#define LIGHT_FN lighting
#define SCENE_FN main
// #define SEPARATE_MATERIAL_AND_DIST_FUNCTIONS
// #define SCENE_FN separate_mat
// #define DISTANCE_FN separate_dist
// #define DISABLE_DEPTH
// #define DISCARD_ON_MISS
// #define USE_WORLD_SPACE
#define SCENE_SCALE 0.05
#include "libgarbage.cginc"
// #define MAIN_FN(PASS) float3 main##PASS ()\
SurfacePoint main(float3 p) {
Material grass = mat(float3(0.001, 0.1, 0.001), 0.3);
Material dirt = mat(float3(0.1, 0.04, 0.01), 0);
Material metal = mat(0.1, 1);
Material blue = mat(float3(0.05, 0.1, 0.2), 0);
#define MAIN_FN(PASS) SOME_MAGIC(PASS,\
{\
return float3(1,0,0);\
}\
)
// #include "libgarbage_end.cginc"
// #define DO_MAGIC(p) MAIN_FN(p)
DO_MAGIC
SurfacePoint d = mPlaneY(p, 0, grass);
d = qIntersect(d, mSphere(p, 9, dirt), 0.5);
d = qUnion(d, mSphere(p - float3(0, 2, 0), 2, metal));
d = qUnion(d, mTorus(rotX(p, _Time * 40 + UNITY_PI / 2), 5, 0.5, blue), 0.5);
d = qUnion(d, mTorus(rotZ(p, _Time * 40 + UNITY_PI / 2), 5, 0.5, blue), 0.5);
d = qUnion(d, mTorus(rotX(p, _Time * 40), 5, 0.5, blue), 0.5);
d = qUnion(d, mTorus(rotZ(p, _Time * 40), 5, 0.5, blue), 0.5);
// small spheres
float3 p2 = abs(rotY(p, -20 * _Time)) - float3(1.5, sin(_Time.y * 5) + 1, 1.5);
d = qUnion(d, mSphere(p2, 0.7, metal), 0.2);
return d;
}
// {return 0;}
// MAIN_FN(1) {return 0;}
SurfacePoint separate_mat(float3 p) {
Material blue = mat(float3(0.05, 0.1, 0.2), 0);
SurfacePoint d = mSphere(p, 1, blue);
return d;
}
float separate_dist(float3 p) {
return main(p).dist;
}
float3 lighting(Ray ray) {
if (ray.missed)
return lRenderSky(ray.dir, normalize(float3(4,2,1)));
float3 sun_dir = normalize(float3(4, 2, 1));
float3 col = 0;
col = ray.mat.col * lSun(ray.normal, sun_dir);
col *= lShadow(ray.hit_pos + ray.normal * SURF_DIST, sun_dir, 50);
col += ray.mat.col * lSky(ray.normal);
return col;
}
ENDCG
}

View file

@ -0,0 +1,63 @@
// --------------------------------
// common lighting operations
// --------------------------------
float3 lRenderSun(float3 ray_dir, float3 sun_dir) {
float alignment = min(acos(dot(ray_dir, sun_dir)), 1);
float sun_amount = smax(min(0.03 / alignment, 5) - 0.06, 0, 0.15);
return sun_amount* float3(0.8, 0.4, 0.1);
}
// a basic procedural sky
float3 lRenderSky(float3 ray_dir, float3 sun_dir) {
float3 rendered_sun = lRenderSun(ray_dir, sun_dir);
// float3 rendered_sun = max(0, pow(dot(ray_dir, sun_dir) + 0.4, 10)-28) * float3(0.8, 0.4, 0);
return float3(0.7, 0.75, 0.8) - abs(ray_dir.y) * 0.5 + rendered_sun;
// return rendered_sun;
}
//calculate sky light
float3 lSky(float3 normal, float3 sky_col = float3(0.5, 0.8, 0.9)) {
return sky_col * (0.5 + 0.5 * normal.y);
}
float lSun(float3 normal, float3 sun_dir, float3 sun_col = float3(7, 5.5, 3)) {
return sun_col * max(dot(normal, sun_dir), 0);
}
// soft shadows
float lShadow(float3 p, float3 sun_dir, float sharpness = 8) {
float shadow = 1;
for (float ray_len = 0.001; ray_len < MAX_DIST / 2.0;)
{
float dist = DISTANCE_FN(p + sun_dir * ray_len);
if (dist < SURF_DIST) return 0;
shadow = min(shadow, sharpness * dist / ray_len);
ray_len += dist;
}
return shadow;
}
// --------------------------------
// misc
// --------------------------------
float3 HSV(float h, float s, float v) {
h *= 6;
float c = s * v;
float x = c * (1 - abs(fmod(h, 2) - 1));
float m = v - c;
c += m;
x += m;
float3 colors[6] = {
float3(c, x, m),
float3(x, c, m),
float3(m, c, x),
float3(m, x, c),
float3(x, m, c),
float3(c, m, x)};
return colors[int(h)];
}

View file

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 26203816190e7e521a12959d895d68a3
guid: ad884ac058b95a125963d424efaab135
ShaderImporter:
externalObjects: {}
defaultTextures: []

View file

@ -0,0 +1,141 @@
// soft min of a and b with smoothing factor k
float smin(float a, float b, float k) {
float h = max(k - abs(a-b), 0) / k;
return min(a, b) - h * h * h * k * (1 / 6.0);
}
// soft max of a and b with smoothing factor k
float smax(float a, float b, float k) {
float h = max(k - abs(a - b), 0) / k;
return max(a, b) + h * h * h * k * (1 / 6.0);
}
Material mixMat(Material a, Material b, float fac) {
Material m;
m.col = lerp(a.col, b.col, fac);
m.gloss = lerp(a.gloss, b.gloss, fac);
return m;
}
Material mixMat(SurfacePoint a, SurfacePoint b) {
float fac = clamp(a.dist/(a.dist + b.dist), 0, 1);
return mixMat(a.mat, b.mat, fac);
}
// --------------------------------
// base sdf operations
// --------------------------------
float qUnion(float a, float b) {
return min(a, b);
}
float qUnion(float a, float b, float smooth) {
return smin(a, b, smooth);
}
SurfacePoint qUnion(SurfacePoint a, SurfacePoint b) {
SurfacePoint o;
o.dist = min(a.dist, b.dist);
o.mat = mixMat(a, b);
return o;
}
SurfacePoint qUnion(SurfacePoint a, SurfacePoint b, float smooth) {
SurfacePoint o;
o.dist = smin(a.dist, b.dist, smooth);
o.mat = mixMat(a, b);
return o;
}
float qSub(float a, float b) {
return max(a, -b);
}
float qSub(float a, float b, float smooth) {
return smax(a, -b, smooth);
}
SurfacePoint qSub(SurfacePoint a, SurfacePoint b) {
SurfacePoint o;
o.dist = max(a.dist, -b.dist);
o.mat = a.mat;
return o;
}
SurfacePoint qSub(SurfacePoint a, SurfacePoint b, float smooth) {
SurfacePoint o;
o.dist = smax(a.dist, -b.dist, smooth);
o.mat = a.mat;
return o;
}
float qIntersect(float a, float b) {
return max(a, b);
}
float qIntersect(float a, float b, float smooth) {
return smax(a, b, smooth);
}
SurfacePoint qIntersect(SurfacePoint a, SurfacePoint b) {
SurfacePoint o;
o.dist = max(a.dist, b.dist);
o.mat = mixMat(a, b);
return o;
}
SurfacePoint qIntersect(SurfacePoint a, SurfacePoint b, float smooth) {
SurfacePoint o;
o.dist = smax(a.dist, b.dist, smooth);
o.mat = mixMat(a, b);
return o;
}
float qRound(float a, float radius) {
return a - radius;
}
SurfacePoint qRound(SurfacePoint a, float radius) {
a.dist -= radius;
return a;
}
// --------------------------------
// spatial warping
// --------------------------------
float3 rotX(float3 p, float a) {
return mul(float3x3(1, 0, 0, 0, cos(a), -sin(a), 0, sin(a), cos(a)), p);
}
float3 rotY(float3 p, float a) {
return mul(float3x3(cos(a), 0, sin(a), 0, 1, 0, -sin(a), 0, cos(a)), p);
}
float3 rotZ(float3 p, float a) {
return mul(float3x3(cos(a), -sin(a), 0, sin(a), cos(a), 0, 0, 0, 1), p);
}
// repeats space every r units, centered on the origin
float3 repXYZ(float3 p, float3 r) {
float3 o = p;
o = fmod(abs(p + r/2.0), r) - r/2.0;
o *= sign(o);
return o;
}
// repeats space every r units, centered on the origin, no sign
float3 repXYZUnsigned(float3 p, float3 r) {
return fmod(abs(p + r / 2.0), r) - r / 2.0;
}
// repeats space
inline float3 repXZ(float3 p, float x, float z) {
float3 o = p;
o.x = fmod(abs(p.x) + x / 2.0, x) - x / 2.0;
o.x *= sign(p.x);
o.z = fmod(abs(p.z) + z / 2.0, z) - z / 2.0;
o.z *= sign(p.z);
return o;
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 6f298ca37830a108492fa68448a84676
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,53 @@
#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)

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 4d08637fa3f7e141190ffd1bdbca2c19
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant: