libgarbage: implement scene scaling & fix depth

This commit is contained in:
Crispy 2023-07-24 16:28:02 +02:00
parent e5f8d6b6b3
commit a2fb224be5
4 changed files with 58 additions and 16 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, z: 0} m_LocalPosition: {x: 0, y: 0.26, z: -0.407}
m_LocalScale: {x: 3, y: 3, z: 3} 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
@ -1516,7 +1516,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!65 &1485771012 --- !u!65 &1485771012
BoxCollider: BoxCollider:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -42,12 +42,10 @@ START_RAYS_IN_SPHERE // TODO: implement
#define SURF_DIST 0.001 #define SURF_DIST 0.001
#endif #endif
// TODO: implement
#ifndef REFLECTIONS #ifndef REFLECTIONS
#define REFLECTIONS 0 #define REFLECTIONS 0
#endif #endif
// TODO: implement
#ifndef SCENE_SCALE #ifndef SCENE_SCALE
#define SCENE_SCALE 1 #define SCENE_SCALE 1
#endif #endif
@ -130,7 +128,8 @@ FragOut frag (V2F i) {
float ray_len = 0; float ray_len = 0;
float3 ray_dir = normalize(i.hit_pos - i.cam_pos); float3 ray_dir = normalize(i.hit_pos - i.cam_pos);
float3 last_bounce = i.cam_pos; float3 ray_origin = i.cam_pos;
ray_origin /= SCENE_SCALE;
SurfacePoint point_data; SurfacePoint point_data;
Ray ray; Ray ray;
@ -140,7 +139,7 @@ FragOut frag (V2F i) {
float3 first_hit; float3 first_hit;
for (int ray_num = 0; ray_num < REFLECTIONS + 1; ray_num++) { for (int ray_num = 0; ray_num < REFLECTIONS + 1; ray_num++) {
ray = cast_ray(last_bounce, ray_dir); ray = cast_ray(ray_origin, ray_dir);
if (ray_num == 0) { // before any bounces if (ray_num == 0) { // before any bounces
col = LIGHT_FN(ray); col = LIGHT_FN(ray);
first_hit = ray.hit_pos; first_hit = ray.hit_pos;
@ -154,7 +153,7 @@ FragOut frag (V2F i) {
} }
prev_gloss = ray.mat.gloss; prev_gloss = ray.mat.gloss;
ray_dir = reflect(ray_dir, ray.normal); ray_dir = reflect(ray_dir, ray.normal);
last_bounce = ray.hit_pos + ray_dir * 0.01; ray_origin = ray.hit_pos + ray_dir * 0.01;
} }
#ifdef DISCARD_ON_MISS #ifdef DISCARD_ON_MISS
@ -165,10 +164,14 @@ FragOut frag (V2F i) {
o.col = col; o.col = col;
#ifndef DISABLE_DEPTH #ifndef DISABLE_DEPTH
float3 depth_point = first_hit; float3 depth_point = first_hit * SCENE_SCALE;
if (ray.missed) if (ray.missed && ray_num == 0)
depth_point = i.hit_pos; depth_point = i.hit_pos;
#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))); float4 clip_pos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(depth_point, 1)));
#endif
o.depth = clip_pos.z / clip_pos.w; o.depth = clip_pos.z / clip_pos.w;
#ifndef UNITY_REVERSED_Z // basically only OpenGL (unity editor on linux) #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 o.depth = o.depth * 0.5 + 0.5; // remap -1 to 1 range to 0.0 to 1.0
@ -211,6 +214,7 @@ Ray cast_ray(float3 start, float3 dir, float start_len) {
ray.steps = i; ray.steps = i;
if (ray.missed) { if (ray.missed) {
ray.normal = float3(0, 0, 0); ray.normal = float3(0, 0, 0);
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 = MATERIAL_FN(ray.hit_pos).mat;

View file

@ -22,18 +22,34 @@ Shader "CrispyPin/LibGarbageExample"
int _MaxSteps; int _MaxSteps;
#define MAX_STEPS _MaxSteps #define MAX_STEPS _MaxSteps
float _MaxDist;
#define MAX_DIST _MaxDist
float _SurfDist;
#define SURF_DIST _SurfDist
#define REFLECTIONS 1 #define REFLECTIONS 1
#define DISTANCE_FN main_dist #define DISTANCE_FN main_dist
#define MATERIAL_FN main_mat #define MATERIAL_FN main_mat
#define DISABLE_DEPTH #define LIGHT_FN lighting
#define DISCARD_ON_MISS // #define DISABLE_DEPTH
#define USE_WORLD_SPACE // #define DISCARD_ON_MISS
// #define USE_WORLD_SPACE
#define SCENE_SCALE 0.05
#include "libgarbage.cginc" #include "libgarbage.cginc"
float main_dist(float3 p) { float main_dist(float3 p) {
p = repXYZ(p, 2); float d = sdPlaneY(p, 0);
return qSub(sdSphere(p, 0.5), sdSphere(p - 0.3, 0.2), 0.05); 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_mat(float3 p) {
@ -41,6 +57,15 @@ Shader "CrispyPin/LibGarbageExample"
return mSphere(p2, 0.5, mat(clamp(abs(p), 0, 1), 0.1)); return mSphere(p2, 0.5, mat(clamp(abs(p), 0, 1), 0.1));
} }
float3 lighting(Ray ray) {
float3 sun_dir = normalize(float3(1, 0.5, 0));
if (ray.missed)
return float3(0.15, 0.25, 0.3);
float3 col = 0;
col = ray.mat.col * clamp(dot(ray.normal, sun_dir),0.01,1);
return col;
}
ENDCG ENDCG
} }
} }

View file

@ -38,3 +38,16 @@ float sdSphere(float3 p, float radius) {
} }
_MAT_VARIANT1(mSphere, sdSphere, float, 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)