libgarbage: change default to be only using one sdf function, with materials, since it seems to get optimised out anyway

This commit is contained in:
Crispy 2023-07-24 18:15:12 +02:00
parent eadf1193f6
commit 86a3bad992
3 changed files with 59 additions and 40 deletions

View file

@ -598,8 +598,8 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 683266143} m_GameObject: {fileID: 683266143}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0.26, z: -0.407} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 10, y: 10, z: 10} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 7 m_RootOrder: 7
@ -1678,7 +1678,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1485771011} m_GameObject: {fileID: 1485771011}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.429, y: 0.221, z: -1.243} m_LocalPosition: {x: 0.429, y: 0.221, z: -1.743}
m_LocalScale: {x: 19.2, y: 1, z: 1} m_LocalScale: {x: 19.2, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
@ -1761,7 +1761,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1500710831} m_GameObject: {fileID: 1500710831}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} 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_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}

View file

@ -3,23 +3,39 @@
#include "UnityCG.cginc" #include "UnityCG.cginc"
/* /*
# defines with no defaults ## defines with no defaults
# default is object space
USE_WORLD_SPACE USE_WORLD_SPACE
# renders missed rays as transparent
DISCARD_ON_MISS DISCARD_ON_MISS
USE_MATERIALS_IN_RAYMARCH // TODO: implement, this is easier to work with but more expensive
# 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 DISABLE_DEPTH
START_RAYS_IN_BOX // TODO: implement START_RAYS_IN_BOX // TODO: implement
START_RAYS_IN_SPHERE // 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 // scene sdf with only distance
#ifndef DISTANCE_FN #ifndef DISTANCE_FN
#define DISTANCE_FN default_distance_sdf #define DISTANCE_FN default_distance_sdf
#endif #endif
#else
// scene sdf that includes material data #define DISTANCE_FN material_fn_as_dist
#ifndef MATERIAL_FN
#define MATERIAL_FN default_material_sdf
#endif #endif
// calculates a color from a Ray // calculates a color from a Ray
@ -105,7 +121,7 @@ struct Ray {
float DISTANCE_FN(float3 p); float DISTANCE_FN(float3 p);
SurfacePoint MATERIAL_FN(float3 p); SurfacePoint SCENE_FN(float3 p);
float3 LIGHT_FN(Ray ray); float3 LIGHT_FN(Ray ray);
Ray cast_ray(float3 p, float3 d, float startDist = 0); Ray cast_ray(float3 p, float3 d, float startDist = 0);
@ -219,7 +235,7 @@ Ray cast_ray(float3 start, float3 dir, float start_len) {
ray.mat = mat(); ray.mat = mat();
} else { } else {
ray.normal = get_normal(ray.hit_pos, ray.dist); ray.normal = get_normal(ray.hit_pos, ray.dist);
ray.mat = MATERIAL_FN(ray.hit_pos).mat; ray.mat = SCENE_FN(ray.hit_pos).mat;
} }
return ray; return ray;
} }
@ -228,6 +244,10 @@ float default_distance_sdf(float3 p) {
return sdSphere(p, 0.5); return sdSphere(p, 0.5);
} }
float material_fn_as_dist(float3 p) {
return SCENE_FN(p).dist;
}
SurfacePoint default_material_sdf(float3 p) { SurfacePoint default_material_sdf(float3 p) {
return mSphere(p, 0.5); return mSphere(p, 0.5);
} }

View file

@ -5,7 +5,7 @@ Shader "CrispyPin/LibGarbageExample"
[Header(Raymarcher Properties)] [Header(Raymarcher Properties)]
_MaxSteps ("Max steps", Int) = 128 _MaxSteps ("Max steps", Int) = 128
_MaxDist ("Max distance", Float) = 128 _MaxDist ("Max distance", Float) = 128
_SurfDist ("Surface distance threshold", Range(0.00001, 0.05)) = 0.001 _SurfDist ("Surface distance threshold", Range(0.0001, 0.05)) = 0.001
} }
SubShader SubShader
@ -21,38 +21,28 @@ Shader "CrispyPin/LibGarbageExample"
#pragma fragment frag #pragma fragment frag
int _MaxSteps; int _MaxSteps;
#define MAX_STEPS _MaxSteps
float _MaxDist; float _MaxDist;
#define MAX_DIST _MaxDist
float _SurfDist; float _SurfDist;
#define MAX_STEPS _MaxSteps
#define MAX_DIST _MaxDist
#define SURF_DIST _SurfDist #define SURF_DIST _SurfDist
#define REFLECTIONS 3 #define REFLECTIONS 3
#define DISTANCE_FN main_dist
#define MATERIAL_FN main_mat
#define LIGHT_FN lighting #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 DISABLE_DEPTH
// #define DISCARD_ON_MISS // #define DISCARD_ON_MISS
#define USE_WORLD_SPACE // #define USE_WORLD_SPACE
#define SCENE_SCALE 0.05 #define SCENE_SCALE 0.05
#include "libgarbage.cginc" #include "libgarbage.cginc"
float main_dist(float3 p) { SurfacePoint main(float3 p) {
float d = sdPlaneY(p, 0);
d = qIntersect(d, sdSphere(p, 9), 0.5);
d = qUnion(d, sdSphere(p - float3(0, 2, 0), 2));
d = qUnion(d, sdTorus(rotX(p, _Time * 40 + UNITY_PI / 2), 5, 0.5), 0.5);
d = qUnion(d, sdTorus(rotZ(p, _Time * 40 + UNITY_PI / 2), 5, 0.5), 0.5);
d = qUnion(d, sdTorus(rotX(p, _Time * 40), 5, 0.5), 0.5);
d = qUnion(d, sdTorus(rotZ(p, _Time * 40), 5, 0.5), 0.5);
// small spheres
float3 p2 = abs(rotY(p, -20 * _Time)) - float3(1.5, sin(_Time.y * 5) + 1, 1.5);
d = qUnion(d, sdSphere(p2, 0.7), 0.2);
return d;
}
SurfacePoint main_mat(float3 p) {
Material grass = mat(float3(0.001, 0.1, 0.001), 0.3); Material grass = mat(float3(0.001, 0.1, 0.001), 0.3);
Material dirt = mat(float3(0.1, 0.04, 0.01), 0); Material dirt = mat(float3(0.1, 0.04, 0.01), 0);
Material metal = mat(0.1, 1); Material metal = mat(0.1, 1);
@ -71,10 +61,19 @@ Shader "CrispyPin/LibGarbageExample"
return d; return d;
} }
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) { float3 lighting(Ray ray) {
if (ray.missed) if (ray.missed)
return lRenderSky(ray.dir, normalize(float3(4,2,1))); return lRenderSky(ray.dir, normalize(float3(4,2,1)));
// return float3(0.15, 0.25, 0.3);
float3 sun_dir = normalize(float3(4, 2, 1)); float3 sun_dir = normalize(float3(4, 2, 1));
float3 col = 0; float3 col = 0;