summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2025-06-03 03:14:20 -0700
committeryum <yum.food.vr@gmail.com>2025-06-03 03:14:20 -0700
commit73a232ed7a570aa2e821e26ed7c0fd3db210d246 (patch)
treee94b34043e9f1b3e36897193b59e8773bc49de95
parente3a6302979fc0813f6a0cfe2463a30a79929607d (diff)
Add depth prepass and more c30 stuff
-rw-r--r--2ner.cginc19
-rw-r--r--2ner.shader49
-rw-r--r--custom30.cginc104
-rw-r--r--features.cginc8
4 files changed, 173 insertions, 7 deletions
diff --git a/2ner.cginc b/2ner.cginc
index ba8b6c8..c0f2ac1 100644
--- a/2ner.cginc
+++ b/2ner.cginc
@@ -167,7 +167,7 @@ v2f vert(appdata v) {
}
float4 frag(v2f i, uint facing : SV_IsFrontFace
-#if defined(_HARNACK_TRACING) || defined(_SHATTER_WAVE) || defined(_VERTEX_DOMAIN_WARPING) || defined(_CUSTOM30)
+#if defined(_HARNACK_TRACING) || defined(_SHATTER_WAVE) || defined(_VERTEX_DOMAIN_WARPING) || (defined(_CUSTOM30) && !defined(_DEPTH_PREPASS))
, out float depth : SV_DepthLessEqual
#endif
) : SV_Target {
@@ -217,19 +217,34 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace
i.uv01.xy = eye_effect_00.uv;
#endif
+#if defined(_CUSTOM30) && defined(FORWARD_BASE_PASS)
#if defined(_CUSTOM30_BASICCUBE)
Custom30Output basic_cube_output = BasicCube(i);
i.pos = UnityObjectToClipPos(basic_cube_output.objPos);
i.normal = basic_cube_output.normal;
+#if !defined(_DEPTH_PREPASS)
depth = basic_cube_output.depth;
#endif
+#endif
#if defined(_CUSTOM30_BASICWEDGE)
Custom30Output basic_wedge_output = BasicWedge(i);
i.pos = UnityObjectToClipPos(basic_wedge_output.objPos);
i.normal = basic_wedge_output.normal;
+#if !defined(_DEPTH_PREPASS)
depth = basic_wedge_output.depth;
#endif
+#endif
+#endif // FORWARD_BASE_PASS
+
+#if defined(_CUSTOM30_BASICPLATFORM)
+ Custom30Output basic_platform_output = BasicPlatform(i);
+ i.pos = UnityObjectToClipPos(basic_platform_output.objPos);
+ i.normal = basic_platform_output.normal;
+#if !defined(_DEPTH_PREPASS)
+ depth = basic_platform_output.depth;
+#endif
+#endif
YumPbr pbr = GetYumPbr(i);
@@ -334,7 +349,7 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace
UNITY_APPLY_FOG(_unity_fogCoord, lit.rgb);
return lit;
-#elif defined(SHADOW_CASTER_PASS) || defined(MASKED_STENCIL1_PASS) || defined(MASKED_STENCIL2_PASS) || defined(MASKED_STENCIL3_PASS) || defined(MASKED_STENCIL4_PASS)
+#elif defined(SHADOW_CASTER_PASS) || defined(MASKED_STENCIL1_PASS) || defined(MASKED_STENCIL2_PASS) || defined(MASKED_STENCIL3_PASS) || defined(MASKED_STENCIL4_PASS) || defined(DEPTH_PREPASS)
return 0;
#endif
}
diff --git a/2ner.shader b/2ner.shader
index 8e75585..b756d1b 100644
--- a/2ner.shader
+++ b/2ner.shader
@@ -111,6 +111,7 @@ Shader "yum_food/2ner"
//ifex _Custom30_BasicCube_Enabled==0
[HideInInspector] m_start_Custom30_BasicCube("Basic cube", Float) = 0
[ThryToggle(_CUSTOM30_BASICCUBE)]_Custom30_BasicCube_Enabled("Enable", Float) = 0
+ [ThryToggle(_CUSTOM30_BASICCUBE_CHAMFER)]_Custom30_BasicCube_Chamfer("Chamfer", Float) = 0
[HideInInspector] m_end_Custom30_BasicCube("Basic cube", Float) = 0
//endex
@@ -120,6 +121,14 @@ Shader "yum_food/2ner"
[HideInInspector] m_end_Custom30_BasicWedge("Basic wedge", Float) = 0
//endex
+ //ifex _Custom30_BasicPlatform_Enabled==0
+ [HideInInspector] m_start_Custom30_BasicPlatform("Basic platform", Float) = 0
+ [ThryToggle(_CUSTOM30_BASICPLATFORM)]_Custom30_BasicPlatform_Enabled("Enable", Float) = 0
+ [ThryToggle(_CUSTOM30_BASICPLATFORM_CHAMFER)]_Custom30_BasicPlatform_Chamfer("Chamfer", Float) = 0
+ [ThryToggle(_CUSTOM30_BASICPLATFORM_Y_ALIGNED)]_Custom30_BasicPlatform_Y_Aligned("Y aligned", Float) = 0
+ [HideInInspector] m_end_Custom30_BasicPlatform("Basic platform", Float) = 0
+ //endex
+
[HideInInspector] m_end_Custom30("Custom 30", Float) = 0
//endex
@@ -1806,11 +1815,19 @@ Shader "yum_food/2ner"
//endex
[HideInInspector] m_renderingOptions("Rendering Options", Float) = 0
+
[Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull", Float) = 2
[Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 4
[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Source Blend", Float) = 1
[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Destination Blend", Float) = 0
[Enum(Off, 0, On, 1)] _ZWrite("ZWrite", Int) = 1
+
+ //ifex _Depth_Prepass_Enabled==0
+ [HideInInspector] m_start_Depth_Prepass("Depth Prepass", Float) = 0
+ [ThryToggle(_DEPTH_PREPASS)] _Depth_Prepass_Enabled("Enable", Float) = 0
+ [HideInInspector] m_end_Depth_Prepass("Depth Prepass", Float) = 0
+ //endex
+
[HideInInspector] m_start_blending ("Blending--{button_help:{text:Tutorial,action:{type:URL,data:https://www.poiyomi.com/rendering/blending},hover:Documentation}}", Float) = 0
[DoNotAnimate][Enum(Thry.BlendOp)]_BlendOp ("RGB Blend Op", Int) = 0
[DoNotAnimate][Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("RGB Source Blend", Int) = 1
@@ -1867,6 +1884,33 @@ Shader "yum_food/2ner"
SubShader {
Tags { "RenderType" = "Opaque" "Queue" = "Geometry" "VRCFallback" = "Standard" }
+ //ifex _Depth_Prepass_Enabled==0
+ Pass {
+ Name "DEPTHPREPASS"
+ Tags { }
+
+ ColorMask 0
+ ZWrite On
+ ZTest LEqual
+ Cull [_Cull]
+
+ CGPROGRAM
+ #pragma target 5.0
+ #pragma multi_compile_instancing
+ #pragma vertex vert
+ #pragma fragment frag
+
+ //ifex _Tessellation_Enabled==0
+ #pragma hull hull
+ #pragma domain domain
+ //endex
+
+ #define DEPTH_PREPASS
+
+ #include "2ner.cginc"
+ ENDCG
+ }
+ //endex
//ifex _Masked_Stencil1_Enabled==0
Pass {
Name "MASKEDSTENCIL1"
@@ -2062,7 +2106,12 @@ Shader "yum_food/2ner"
Blend [_SrcBlend] [_DstBlend], [_SrcBlendAlpha] [_DstBlendAlpha]
Cull [_Cull]
ZWrite [_ZWrite]
+ //ifex _Depth_Prepass_Enabled==0
+ ZTest LEqual
+ //endex
+ //ifex _Depth_Prepass_Enabled!=0
ZTest [_ZTest]
+ //endex
//ifex _Stencil_Enabled==0
Stencil
diff --git a/custom30.cginc b/custom30.cginc
index 38fe2c2..bbc8fce 100644
--- a/custom30.cginc
+++ b/custom30.cginc
@@ -8,6 +8,8 @@
#if defined(_CUSTOM30)
#define CUSTOM30_MAX_STEPS 30
+#define SQRT_2 1.414213562
+#define RCP_SQRT_2 0.707106781
struct Custom30Output {
float3 objPos;
@@ -26,8 +28,8 @@ float cut_with_box(float3 p, float d, float3 box_size) {
float2 pp_yz = p.yz;
// Rotate by 45 degrees
- float c = 0.70710678;
- float s = 0.70710678;
+ float c = RCP_SQRT_2;
+ float s = RCP_SQRT_2;
pp_xy = float2(c * p.x - s * p.y, s * p.x + c * p.y);
d = max(d, distance_from_box(float3(pp_xy, p.z), box_size));
@@ -46,7 +48,9 @@ float BasicCube_map(float3 p) {
float core_d = distance_from_box(p, 0.95);
float d = min(box_d, core_d);
+#if defined(_CUSTOM30_BASICCUBE_CHAMFER)
d = cut_with_box(p, d, 1.3);
+#endif
return d;
}
@@ -85,14 +89,19 @@ Custom30Output BasicCube(v2f i) {
}
Custom30Output o;
+#if !defined(_DEPTH_PREPASS)
clip(epsilon - d);
+#endif
float3 objPos = ro + rd * d_acc;
o.objPos = objPos;
// Transform from SDF space back to object space
float3 objSpacePos = objPos + (i.objPos + frag_to_origin);
float4 clipPos = UnityObjectToClipPos(objSpacePos);
o.depth = clipPos.z / clipPos.w;
- o.normal = BasicCube_normal(objPos);
+
+ float3 sdfNormal = BasicCube_normal(objPos) * float3(-1, 1, 1);
+ o.normal = UnityObjectToWorldNormal(sdfNormal);
+
return o;
}
#endif
@@ -103,7 +112,6 @@ float BasicWedge_map(float3 p) {
float cut_plane_d = distance_from_plane(p - float3(0, 0, 0), -normalize(float3(1, 0, 1)), 0);
float d = op_sub(box_d, cut_plane_d);
- //d = cut_with_box(p, d, 1.3);
return d;
}
@@ -141,14 +149,100 @@ Custom30Output BasicWedge(v2f i) {
}
Custom30Output o;
+ //clip(epsilon - d);
+ float3 objPos = ro + rd * d_acc;
+ o.objPos = objPos;
+ // Transform from SDF space back to object space
+ float3 objSpacePos = objPos + (i.objPos + frag_to_origin);
+ float4 clipPos = UnityObjectToClipPos(objSpacePos);
+ o.depth = clipPos.z / clipPos.w;
+
+ float3 sdfNormal = BasicWedge_normal(objPos) * float3(-1, 1, 1);
+ o.normal = UnityObjectToWorldNormal(sdfNormal);
+
+ return o;
+}
+#endif
+
+#if defined(_CUSTOM30_BASICPLATFORM)
+float BasicPlatform_map(float3 p) {
+ #if defined(_CUSTOM30_BASICPLATFORM_Y_ALIGNED)
+ p.xy = p.yx;
+ #endif
+
+ float3 platform_size = float3(1.0, 0.4, 0.2);
+ float box_d = distance_from_box_frame(p, platform_size, .08);
+ float core_d = distance_from_box(p, platform_size - 0.05);
+ float d = min(box_d, core_d);
+
+#if defined(_CUSTOM30_BASICPLATFORM_CHAMFER)
+ {
+ float3 pp = p;
+ pp.xy = float2(RCP_SQRT_2 * p.x - RCP_SQRT_2 * p.y, RCP_SQRT_2 * p.x + RCP_SQRT_2 * p.y);
+ d = max(d, distance_from_box(pp, 0.9));
+ }
+ {
+ float3 pp = p;
+ pp.xz = float2(RCP_SQRT_2 * p.x - RCP_SQRT_2 * p.z, RCP_SQRT_2 * p.x + RCP_SQRT_2 * p.z);
+ d = max(d, distance_from_box(pp, 0.78));
+ }
+ {
+ float3 pp = p;
+ pp.yz = float2(RCP_SQRT_2 * p.y - RCP_SQRT_2 * p.z, RCP_SQRT_2 * p.y + RCP_SQRT_2 * p.z);
+ float c = 0.36;
+ d = max(d, distance_from_box(pp, float3(1.0, c, c)));
+ }
+#endif
+
+ return d;
+}
+
+float3 BasicPlatform_normal(float3 p) {
+ float epsilon = 1E-3;
+ float center_d = BasicPlatform_map(p);
+ float3 n = float3(
+ BasicPlatform_map(p + float3(epsilon, 0, 0)) - center_d,
+ BasicPlatform_map(p + float3(0, epsilon, 0)) - center_d,
+ BasicPlatform_map(p + float3(0, 0, epsilon)) - center_d);
+ return normalize(n);
+}
+
+Custom30Output BasicPlatform(v2f i) {
+ float3 objSpaceCameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0));
+
+ float3 frag_to_origin = GetFragToOrigin(i);
+
+ float3 ro = -frag_to_origin;
+ float3 rd = normalize(i.objPos - objSpaceCameraPos);
+
+ float d;
+ float d_acc = 0;
+ float epsilon = 5E-3;
+ // Max distance for platform bounding sphere
+ float max_d = length(float3(1.0, 0.4, 0.2)) * 2.0f;
+ [loop]
+ for (uint ii = 0; ii < CUSTOM30_MAX_STEPS; ++ii) {
+ float3 p = ro + rd * d_acc;
+ d = BasicPlatform_map(p);
+ d_acc += d;
+ if (d < epsilon) break;
+ if (d_acc > max_d) break;
+ }
+
+ Custom30Output o;
+#if !defined(_DEPTH_PREPASS)
clip(epsilon - d);
+#endif
float3 objPos = ro + rd * d_acc;
o.objPos = objPos;
// Transform from SDF space back to object space
float3 objSpacePos = objPos + (i.objPos + frag_to_origin);
float4 clipPos = UnityObjectToClipPos(objSpacePos);
o.depth = clipPos.z / clipPos.w;
- o.normal = BasicWedge_normal(objPos);
+
+ float3 sdfNormal = BasicPlatform_normal(objPos) * float3(-1, 1, 1);
+ o.normal = UnityObjectToWorldNormal(sdfNormal);
+
return o;
}
#endif
diff --git a/features.cginc b/features.cginc
index 31ad8fd..cf546f8 100644
--- a/features.cginc
+++ b/features.cginc
@@ -294,7 +294,15 @@
//ifex _Custom30_Enabled==0
#pragma shader_feature_local _CUSTOM30
#pragma shader_feature_local _CUSTOM30_BASICCUBE
+#pragma shader_feature_local _CUSTOM30_BASICCUBE_CHAMFER
#pragma shader_feature_local _CUSTOM30_BASICWEDGE
+#pragma shader_feature_local _CUSTOM30_BASICPLATFORM
+#pragma shader_feature_local _CUSTOM30_BASICPLATFORM_CHAMFER
+#pragma shader_feature_local _CUSTOM30_BASICPLATFORM_Y_ALIGNED
+//endex
+
+//ifex _Depth_Prepass_Enabled==0
+#pragma shader_feature_local _DEPTH_PREPASS
//endex
#endif // __FEATURES_INC