diff --git a/Assets/raymarched/Raymarching.unity b/Assets/raymarched/Raymarching.unity index d7510e8..35aef40 100644 --- a/Assets/raymarched/Raymarching.unity +++ b/Assets/raymarched/Raymarching.unity @@ -598,8 +598,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 683266143} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0.26, z: -0.407} - m_LocalScale: {x: 10, y: 10, z: 10} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 7 @@ -1678,7 +1678,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1485771011} 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_Children: [] m_Father: {fileID: 0} @@ -1761,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} diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index 169ca38..705a4f5 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -3,23 +3,39 @@ #include "UnityCG.cginc" /* -# defines with no defaults -USE_WORLD_SPACE -DISCARD_ON_MISS -USE_MATERIALS_IN_RAYMARCH // TODO: implement, this is easier to work with but more expensive -DISABLE_DEPTH +## 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 + START_RAYS_IN_BOX // TODO: implement START_RAYS_IN_SPHERE // TODO: implement */ -// scene sdf with only distance -#ifndef DISTANCE_FN -#define DISTANCE_FN default_distance_sdf -#endif // scene sdf that includes material data -#ifndef MATERIAL_FN -#define MATERIAL_FN default_material_sdf +#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 @@ -105,7 +121,7 @@ struct Ray { float DISTANCE_FN(float3 p); -SurfacePoint MATERIAL_FN(float3 p); +SurfacePoint SCENE_FN(float3 p); float3 LIGHT_FN(Ray ray); 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(); } else { 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; } @@ -228,6 +244,10 @@ 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); } diff --git a/Assets/raymarched/lib/libgarbage_example.shader b/Assets/raymarched/lib/libgarbage_example.shader index a2bcde2..354fd4f 100644 --- a/Assets/raymarched/lib/libgarbage_example.shader +++ b/Assets/raymarched/lib/libgarbage_example.shader @@ -5,7 +5,7 @@ Shader "CrispyPin/LibGarbageExample" [Header(Raymarcher Properties)] _MaxSteps ("Max steps", Int) = 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 @@ -21,38 +21,28 @@ Shader "CrispyPin/LibGarbageExample" #pragma fragment frag int _MaxSteps; - #define MAX_STEPS _MaxSteps float _MaxDist; - #define MAX_DIST _MaxDist float _SurfDist; + #define MAX_STEPS _MaxSteps + #define MAX_DIST _MaxDist #define SURF_DIST _SurfDist #define REFLECTIONS 3 - #define DISTANCE_FN main_dist - #define MATERIAL_FN main_mat + #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 USE_WORLD_SPACE #define SCENE_SCALE 0.05 - #include "libgarbage.cginc" - float main_dist(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) { + 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); @@ -71,10 +61,19 @@ Shader "CrispyPin/LibGarbageExample" 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) { if (ray.missed) 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 col = 0;