summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2025-10-12 16:28:53 -0700
committeryum <yum.food.vr@gmail.com>2025-10-12 16:28:53 -0700
commita4bf31470f7e2855f13d922e3e7ad1c7767d9afd (patch)
treeeeefe31633142978c609c2dd2b4d6890349526a6
parent6ac3da1b0bd363d70c2f6e4b7b921f2f929dedac (diff)
add geometry shader
-rw-r--r--3ner.cginc46
-rw-r--r--3ner.shader22
-rw-r--r--custom31.slang28
-rw-r--r--features.cginc4
-rw-r--r--geometry.cginc16
-rw-r--r--globals.cginc6
-rw-r--r--math.cginc1
-rw-r--r--vertex.cginc7
8 files changed, 99 insertions, 31 deletions
diff --git a/3ner.cginc b/3ner.cginc
index 0536fb5..bac74ad 100644
--- a/3ner.cginc
+++ b/3ner.cginc
@@ -9,6 +9,7 @@
#include "AutoLight.cginc"
#include "brdf.cginc"
+#include "geometry.cginc"
#include "pbr.cginc"
#include "lighting.cginc"
#include "globals.cginc"
@@ -50,6 +51,7 @@ v2f vert(appdata v) {
return o;
}
+//ifex _Tessellation_Enabled==0
struct tess_factors {
float edge[3] : SV_TessFactor;
float inside : SV_InsideTessFactor;
@@ -170,6 +172,50 @@ v2f domain(
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
return o;
}
+//endex
+
+//ifex _Geometry_Shader_Enabled==0
+// maxvertexcount == the number of vertices we create
+[maxvertexcount(3)]
+void geom(triangle v2f tri_in[3],
+ uint pid: SV_PrimitiveID,
+ inout TriangleStream<v2f> tri_out)
+{
+ v2f v0 = tri_in[0];
+ v2f v1 = tri_in[1];
+ v2f v2 = tri_in[2];
+
+#if defined(_CENTER_OFFSET)
+ float3 n0 = v0.normal;
+ float3 n1 = v1.normal;
+ float3 n2 = v2.normal;
+#if defined(_CUSTOM31_FRAGMENT_NORMALS) || defined(_CUSTOM31_TESSELLATION)
+ float3 tmp;
+ deform_normal(v0.objPos_orig, n0, tmp);
+ deform_normal(v1.objPos_orig, n1, tmp);
+ deform_normal(v2.objPos_orig, n2, tmp);
+ // the average direction doesn't have to be precise, so don't bother with
+ // normalize().
+ float3 n = (n0 + n1 + n2) * 0.333f;
+#else
+ float3 n = (v0.normal + v1.normal + v2.normal) * 0.333f;
+#endif
+ float height = center_offset(v0.uv0);
+ v0.objPos += n * height;
+ v1.objPos += n * height;
+ v2.objPos += n * height;
+ propagateObjPos(v0);
+ propagateObjPos(v1);
+ propagateObjPos(v2);
+#endif
+
+ // Output transformed geometry.
+ tri_out.Append(v0);
+ tri_out.Append(v1);
+ tri_out.Append(v2);
+ tri_out.RestartStrip();
+}
+//endex
float4 frag(v2f i, uint facing : SV_IsFrontFace
#if defined(_CUSTOM31_TUBES)
diff --git a/3ner.shader b/3ner.shader
index 7e6564c..aad3949 100644
--- a/3ner.shader
+++ b/3ner.shader
@@ -40,6 +40,21 @@ Shader "yum_food/3ner"
[HideInInspector] m_end_Main("Main", Float) = 0
[HideInInspector] m_start_Gimmicks("Gimmicks", Float) = 0
+ //ifex _Geometry_Shader_Enabled==0
+ [HideInInspector] m_start_Geometry_Shader("Geometry Shader", Float) = 0
+ [ThryToggle(_GEOMETRY_SHADER)] _Geometry_Shader_Enabled("Enable", Float) = 0
+
+ //ifex _Center_Offset_Enabled==0
+ [HideInInspector] m_start_Center_Offset("Center offset", Float) = 0
+ [ThryToggle(_CENTER_OFFSET)] _Center_Offset_Enabled("Enable", Float) = 0
+ _Center_Offset_Heightmap("Heightmap", 2D) = "black" {}
+ _Center_Offset_Factor("Factor", Range(-1, 1)) = 1
+ [HideInInspector] m_end_Center_Offset("Center offset", Float) = 0
+ //endex
+
+ [HideInInspector] m_end_Geometry_Shader("Geometry Shader", Float) = 0
+ //endex
+
//ifex _Custom31_Enabled==0
[HideInInspector] m_start_Custom31("Custom31", Float) = 0
[ThryToggle(_CUSTOM31)] _Custom31_Enabled("Enable", Float) = 0
@@ -53,6 +68,7 @@ Shader "yum_food/3ner"
//ifex _Custom31_XZ_Tube_Enabled==0
[HideInInspector] m_start_Custom31_XZ_Tube("XZ Tube", Float) = 0
[ThryToggle(_CUSTOM31_XZ_TUBE)] _Custom31_XZ_Tube_Enabled("Enable", Float) = 0
+ // 0.999... limit prevents NaNs which show up at 1.0
_Custom31_XZ_Tube_t("t", Range(-1,1)) = 0
[HideInInspector] m_end_Custom31_XZ_Tube("XZ Tube", Float) = 0
//endex
@@ -188,6 +204,9 @@ Shader "yum_food/3ner"
#pragma multi_compile_instancing
#pragma multi_compile_fog
#pragma vertex vert
+ //ifex _Geometry_Enabled==0
+ #pragma geometry geom
+ //endex
//ifex _Tessellation_Enabled==0
#pragma hull hull
#pragma domain domain
@@ -215,6 +234,9 @@ Shader "yum_food/3ner"
#pragma multi_compile_instancing
#pragma multi_compile_fog
#pragma vertex vert
+ //ifex _Geometry_Enabled==0
+ #pragma geometry geom
+ //endex
//ifex _Tessellation_Enabled==0
#pragma hull hull
#pragma domain domain
diff --git a/custom31.slang b/custom31.slang
index 61afe4a..441daeb 100644
--- a/custom31.slang
+++ b/custom31.slang
@@ -62,34 +62,6 @@ float3x3 inverse(float3x3 m, float det) {
return inv;
}
-[Differentiable]
-public float3 c31_deform(float3 xyz, no_diff float A, no_diff float k, no_diff float t) {
- float x = xyz.x;
- float y = xyz.y;
- float z = xyz.z;
-
- float x0 = x + sin(y * k) * 0.1;
- float y0 = y + sin(x * k * 2) * 0.5 + cos(z);
- float z0 = (z + sin(x * y * k * PI + t) * A) * (1.0 + sin(y * k) * sin(x * k));
-
- x0 += z0 * 0.1 * sin(z0 * PI + 1.5);
- y0 += z0 * 0.1 * sin(z0 * PI + 1.5);
-
- x0 -= 0.0;
- y0 -= 1.2;
-
- return float3(
- x0, y0, z0
- );
-}
-
-
-// Deform a normal vector using the inverse transpose of the jacobian.
-public void c31_deform_normal(float3 xyz, inout float3 normal,
- inout float3 tangent, float A, float k, float t) {
- R3R3_NORMALS(normal, tangent, c31_deform, A, k, t);
-}
-
// Takes map a 2x2 quad on the xy plane to a tube. The circular cross section
// is on the xz plane.
[Differentiable]
diff --git a/features.cginc b/features.cginc
index c6786d4..ce8d07d 100644
--- a/features.cginc
+++ b/features.cginc
@@ -43,4 +43,8 @@
#pragma shader_feature_local _UV_SCROLL
//endex
+//ifex _Center_Offset_Enabled==0
+#pragma shader_feature_local _CENTER_OFFSET
+//endex
+
#endif // __FEATURES_INC
diff --git a/geometry.cginc b/geometry.cginc
new file mode 100644
index 0000000..9ddb011
--- /dev/null
+++ b/geometry.cginc
@@ -0,0 +1,16 @@
+#ifndef __GEOMETRY_INC
+#define __GEOMETRY_INC
+
+#include "interpolators.cginc"
+#include "globals.cginc"
+
+#if defined(_CENTER_OFFSET)
+float center_offset(float2 uv) {
+ float height = _Center_Offset_Heightmap.SampleLevel(point_repeat_s,
+ uv * _Center_Offset_Heightmap_ST.xy + _Center_Offset_Heightmap_ST.zw, 0).r;
+ return height * _Center_Offset_Factor;
+}
+#endif // _CENTER_OFFSET
+
+
+#endif // __GEOMETRY_INC
diff --git a/globals.cginc b/globals.cginc
index 1bd9498..3ba2e60 100644
--- a/globals.cginc
+++ b/globals.cginc
@@ -72,4 +72,10 @@ float _Custom31_YZ_Tube_t;
float2 _UV_Scroll_Speed;
#endif // _UV_SCROLL
+#if defined(_CENTER_OFFSET)
+texture2D _Center_Offset_Heightmap;
+float4 _Center_Offset_Heightmap_ST;
+float _Center_Offset_Factor;
+#endif // _CENTER_OFFSET
+
#endif // __GLOBALS_INC
diff --git a/math.cginc b/math.cginc
index af71fc6..4cfed67 100644
--- a/math.cginc
+++ b/math.cginc
@@ -4,6 +4,7 @@
#define PI 3.14159265358979f
#define RCP_PI (1.0f / PI)
#define TAU (2.0f * PI)
+#define SQRT_3 1.73205081f
float sin_noise_3d(float3 uvw) {
return sin(uvw[0]) * sin(uvw[1]) * sin(uvw[2]);
diff --git a/vertex.cginc b/vertex.cginc
index 2f5b5f5..58c5209 100644
--- a/vertex.cginc
+++ b/vertex.cginc
@@ -4,22 +4,23 @@
#include "custom31.hlsl"
void deform(inout float3 objPos) {
+ float t_eps = 1e-9;
#if defined(_CUSTOM31_XZ_TUBE)
{
- float t = _Custom31_XZ_Tube_t;
+ float t = clamp(_Custom31_XZ_Tube_t, -1+t_eps, 1-t_eps);;
objPos = plane_to_tube(objPos.xyz, t);
}
#endif // _CUSTOM31_XZ_TUBE
#if defined(_CUSTOM31_YZ_TUBE)
{
- float t = _Custom31_YZ_Tube_t;
+ float t = clamp(_Custom31_YZ_Tube_t, -1+t_eps, 1-t_eps);
objPos = plane_to_tube(objPos.yxz, t);
objPos = objPos.yxz;
}
#endif // _CUSTOM31_YZ_TUBE
#if defined(_CUSTOM31_XY_TUBE)
{
- float t = _Custom31_XY_Tube_t;
+ float t = clamp(_Custom31_XY_Tube_t, -1+t_eps, 1-t_eps);
objPos = plane_to_tube(objPos.xzy, t);
objPos = objPos.xzy;
}