summaryrefslogtreecommitdiffstats
path: root/ray_marching.cginc
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2025-11-04 21:50:42 -0800
committeryum <yum.food.vr@gmail.com>2025-11-04 21:50:42 -0800
commit672a7acca1f0401de7ac0ac94d3379081fa363e6 (patch)
tree1fed9ab4791de35d9fdc69bdff437fea2223349e /ray_marching.cginc
parentac78ce59232f698dfd721b0048336cd346612613 (diff)
begin work on marching in vert/domain shaders (NOT WORKING)
Diffstat (limited to 'ray_marching.cginc')
-rw-r--r--ray_marching.cginc33
1 files changed, 23 insertions, 10 deletions
diff --git a/ray_marching.cginc b/ray_marching.cginc
index 7b95972..aeb35a1 100644
--- a/ray_marching.cginc
+++ b/ray_marching.cginc
@@ -1,6 +1,8 @@
#ifndef __RAY_MARCHING_INC
#define __RAY_MARCHING_INC
+#if defined(_RAY_MARCHING)
+
#include "math.cginc"
#include "ray_marching_maps.hlsl"
#include "texture_utils.cginc"
@@ -25,6 +27,13 @@ void GetRoRd(v2f i, out float3 ro, out float3 rd) {
ro = getObjPos(i);
float3 rd_world = i.worldPos.xyz - _WorldSpaceCameraPos;
rd = normalize(mul(unity_WorldToObject, rd_world));
+#if defined(_RAY_MARCHING_SCALING)
+ float3 scale = float3(
+ _Ray_Marching_Scaling_Factor_X,
+ _Ray_Marching_Scaling_Factor_Y,
+ _Ray_Marching_Scaling_Factor_Z);
+ ro *= scale;
+#endif // _RAY_MARCHING_SCALING
}
float map(float3 p) {
@@ -146,7 +155,6 @@ float domain_repeat(inout float3 p) {
return d;
}
-#if defined(_RAY_MARCHING)
bool get_one_hit(float3 ro, float3 rd, out float3 hitPos, out float hitD) {
const float kMinDist = _Ray_Marching_Min_Dist;
const float kMaxDist = _Ray_Marching_Max_Dist;
@@ -202,7 +210,7 @@ bool get_hit(float3 ro, float3 rd, out float3 hitPos, out float hitD) {
count_minus_1.x * z_every_x + count_minus_1.y * z_every_y
);
const float3 offset_midpoint = max_offset * 0.5f;
-#endif
+#endif // _RAY_MARCHING_CART_INSTANCING_OFFSETS
hitD = 1e9;
for (uint xi = 0; xi < count.x; ++xi)
@@ -217,7 +225,7 @@ bool get_hit(float3 ro, float3 rd, out float3 hitPos, out float hitD) {
y_every_x * xi + y_every_z * zi,
z_every_x * xi + z_every_y * yi);
cur_pos += cur_offset - offset_midpoint;
-#endif
+#endif // _RAY_MARCHING_CART_INSTANCING_OFFSETS
if (!get_one_hit(cur_pos, rd, hitPos_tmp, hitD_tmp)) {
continue;
}
@@ -227,13 +235,13 @@ bool get_hit(float3 ro, float3 rd, out float3 hitPos, out float hitD) {
}
}
return hitD != 1e9;
-#else
+#else // !_RAY_MARCHING_CART_INSTANCING
return get_one_hit(ro, rd, hitPos, hitD);
-#endif
+#endif // _RAY_MARCHING_CART_INSTANCING
}
#endif // _RAY_MARCHING
-void ray_march(inout v2f i) {
+void ray_march(inout v2f i, bool is_fragment) {
#if defined(_RAY_MARCHING)
float3 ro, rd;
GetRoRd(i, ro, rd);
@@ -250,7 +258,10 @@ void ray_march(inout v2f i) {
float3 hit_pos;
float hit_d;
- if (!get_hit(ro, rd, hit_pos, hit_d)) {
+ // Fragment shader can discard pixels, and here it's desired.
+ // Vertex shader very well may not have hit anything, but that doesn't mean
+ // that the fragment shader can't finish the job.
+ if (!get_hit(ro, rd, hit_pos, hit_d) && is_fragment) {
discard;
}
@@ -272,15 +283,17 @@ void ray_march(inout v2f i) {
#endif
#if defined(_VERTEX_DEFORMATION)
- {
+ if (is_fragment) {
float3 tmp_pos = ro;
deform_normal(tmp_pos, lclNorm, lclTan);
}
#endif
i.objPos = lclPos;
- i.normal = lclNorm;
- i.tangent.xyz = lclTan;
+ if (is_fragment) {
+ i.normal = lclNorm;
+ i.tangent.xyz = lclTan;
+ }
#endif // _RAY_MARCHING
}