libgarbage: add some common lighting operations

This commit is contained in:
Crispy 2023-07-24 17:14:06 +02:00
parent a2fb224be5
commit eadf1193f6
7 changed files with 198 additions and 36 deletions

View file

@ -599,7 +599,7 @@ Transform:
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: 1, y: 1, z: 1}
m_LocalScale: {x: 10, y: 10, z: 10}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 7
@ -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
@ -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.243}
m_LocalScale: {x: 19.2, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4

View file

@ -63,7 +63,7 @@ Material:
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _MaxDist: 128
- _MaxDist: 512
- _MaxSteps: 512
- _Metallic: 0
- _Mode: 0

View file

@ -103,14 +103,16 @@ struct Ray {
float min_dist; // smallest distance encountered along the path (only useful for misses; shadow calculations)
};
#include "libgarbage_shapes.cginc"
#include "libgarbage_operations.cginc"
float DISTANCE_FN(float3 p);
SurfacePoint MATERIAL_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;
o.vertex = UnityObjectToClipPos(v.vertex);

View file

@ -27,13 +27,13 @@ Shader "CrispyPin/LibGarbageExample"
float _SurfDist;
#define SURF_DIST _SurfDist
#define REFLECTIONS 1
#define REFLECTIONS 3
#define DISTANCE_FN main_dist
#define MATERIAL_FN main_mat
#define LIGHT_FN lighting
// #define DISABLE_DEPTH
// #define DISCARD_ON_MISS
// #define USE_WORLD_SPACE
#define USE_WORLD_SPACE
#define SCENE_SCALE 0.05
#include "libgarbage.cginc"
@ -53,16 +53,34 @@ Shader "CrispyPin/LibGarbageExample"
}
SurfacePoint main_mat(float3 p) {
float3 p2 = repXYZ(p, 2);
return mSphere(p2, 0.5, mat(clamp(abs(p), 0, 1), 0.1));
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);
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;
}
float3 lighting(Ray ray) {
float3 sun_dir = normalize(float3(1, 0.5, 0));
if (ray.missed)
return float3(0.15, 0.25, 0.3);
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;
col = ray.mat.col * clamp(dot(ray.normal, sun_dir),0.01,1);
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;
}

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

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

View file

@ -139,26 +139,3 @@ inline float3 repXZ(float3 p, float x, float z) {
o.z *= sign(p.z);
return o;
}
// --------------------------------
// 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)];
}