summaryrefslogtreecommitdiffstats
path: root/Shaders
diff options
context:
space:
mode:
Diffstat (limited to 'Shaders')
-rw-r--r--Shaders/Motion.cginc90
-rw-r--r--Shaders/STT_generated_template.cginc19
-rw-r--r--Shaders/STT_text.cginc187
-rw-r--r--Shaders/TaSTT.shader1
-rw-r--r--Shaders/TaSTT_lighting.cginc (renamed from Shaders/TaSTT_lighting_template.cginc)372
-rw-r--r--Shaders/TaSTT_template.shader4
-rw-r--r--Shaders/eyes_data.cginc24
-rw-r--r--Shaders/hg_sdf.cginc187
-rw-r--r--Shaders/iq_sdf.cginc136
-rw-r--r--Shaders/math.cginc88
-rw-r--r--Shaders/pbr.cginc126
-rw-r--r--Shaders/pema99.cginc31
-rw-r--r--Shaders/poi.cginc60
-rw-r--r--Shaders/ray_march.cginc142
14 files changed, 1137 insertions, 330 deletions
diff --git a/Shaders/Motion.cginc b/Shaders/Motion.cginc
new file mode 100644
index 0000000..d6458e9
--- /dev/null
+++ b/Shaders/Motion.cginc
@@ -0,0 +1,90 @@
+#ifndef MOTION_
+#define MOTION_
+
+// xyz represent quaternion vector, w represents theta.
+typedef float4 Quaternion;
+
+// Math from here:
+// https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
+float3 qrot(in float3 v, in Quaternion q)
+{
+ float a = q.w;
+ float b = q.x;
+ float c = q.y;
+ float d = q.z;
+
+ float a2 = a*a;
+ float b2 = b*b;
+ float c2 = c*c;
+ float d2 = d*d;
+
+ float3x3 rot = float3x3(
+ (a2 + b2 - c2) - d2, 2*b*c - 2*a*d, 2*b*d + 2*a*c,
+ 2*b*c + 2*a*d, (a2 - b2) + (c2 - d2), 2*c*d - 2*a*b,
+ 2*b*d - 2*a*c, 2*c*d + 2*a*b, ((a2 - b2) - c2) + d2
+ );
+
+ return mul(rot, v);
+}
+
+Quaternion qinv(in Quaternion q)
+{
+ return Quaternion(q.xyz, -q.w);
+}
+
+// Multiply two quaternions.
+// Math from here: https://www.haroldserrano.com/blog/quaternions-in-computer-graphics
+Quaternion qmul(in Quaternion a, in Quaternion b)
+{
+ return Quaternion(a.w * b.xyz + b.w * a.xyz + cross(a.xyz, b.xyz), a.w * b.w - dot(a.xyz, b.xyz));
+}
+
+float4 affine3(in float3 m)
+{
+ return float4(m, 1.0);
+}
+
+float4x4 affine3x3(in float3x3 m)
+{
+ return float4x4(
+ m[0][0], m[0][1], m[0][2], 0,
+ m[1][0], m[1][1], m[1][2], 0,
+ m[2][0], m[2][1], m[2][2], 0,
+ 0, 0, 0, 1
+ );
+}
+
+float4x4 eye()
+{
+ return float4x4(
+ 1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1
+ );
+}
+
+// Return affine translation matrix.
+float4x4 translate(in float dx, in float dy, in float dz)
+{
+ return float4x4(
+ 1, 0, 0, dx,
+ 0, 1, 0, dy,
+ 0, 0, 1, dz,
+ 0, 0, 0, 1
+ );
+}
+
+// Return affine scaling matrix.
+float4x4 scale(in float sx, in float sy, in float sz)
+{
+ return float4x4(
+ sx, 0, 0, 0,
+ 0, sy, 0, 0,
+ 0, 0, sz, 0,
+ 0, 0, 0, 1
+ );
+}
+
+#endif // MOTION_
+
diff --git a/Shaders/STT_generated_template.cginc b/Shaders/STT_generated_template.cginc
new file mode 100644
index 0000000..9091e8e
--- /dev/null
+++ b/Shaders/STT_generated_template.cginc
@@ -0,0 +1,19 @@
+#ifndef __STT_GENERATED_INC__
+#define __STT_GENERATED_INC__
+
+// %TEMPLATE__CG_ROW_COL_CONSTANTS%
+
+// %TEMPLATE__CG_ROW_COL_PARAMS%
+
+// Get the value of the parameter for the cell we're in.
+uint GetLetterParameter(float2 uv)
+{
+ float CHAR_COL = floor(uv.x * BOARD_NCOLS);
+ float CHAR_ROW = floor(uv.y * BOARD_NROWS);
+ int res = 0;
+
+ // %TEMPLATE__CG_LETTER_ACCESSOR%
+ return res;
+}
+
+#endif // __STT_GENERATED_INC__
diff --git a/Shaders/STT_text.cginc b/Shaders/STT_text.cginc
new file mode 100644
index 0000000..1e7a96e
--- /dev/null
+++ b/Shaders/STT_text.cginc
@@ -0,0 +1,187 @@
+#ifndef __STT_TEXT_INC__
+#define __STT_TEXT_INC__
+
+#include "stt_generated.cginc"
+
+float Enable_Dithering;
+
+SamplerState linear_clamp_sampler;
+
+Texture2D _Font_0x0000_0x1FFF;
+float4 _Font_0x0000_0x1FFF_TexelSize;
+Texture2D _Font_0x2000_0x3FFF;
+float4 _Font_0x2000_0x3FFF_TexelSize;
+Texture2D _Font_0x4000_0x5FFF;
+float4 _Font_0x4000_0x5FFF_TexelSize;
+Texture2D _Font_0x6000_0x7FFF;
+float4 _Font_0x6000_0x7FFF_TexelSize;
+Texture2D _Font_0x8000_0x9FFF;
+float4 _Font_0x8000_0x9FFF_TexelSize;
+Texture2D _Font_0xA000_0xBFFF;
+float4 _Font_0xA000_0xBFFF_TexelSize;
+Texture2D _Font_0xC000_0xDFFF;
+float4 _Font_0xC000_0xDFFF_TexelSize;
+Texture2D _Img_0xE000_0xE03F;
+float4 _Img_0xE000_0xE03F_TexelSize;
+
+float2 AddMarginToUV(float2 uv, float2 margin)
+{
+ float2 lo = float2(-margin.x / 2, -margin.y / 2);
+ float2 hi = float2(1.0 + margin.x / 2, 1.0 + margin.y / 2);
+
+ return clamp(lerp(lo, hi, uv), 0.0, 1.0);
+}
+
+// Generate a random number on [0, 1].
+float prng(float2 p)
+{
+ return frac(sin(dot(p, float2(561.0, 885.0))) * 776.2) / 2.0;
+}
+
+bool f3ltf3(fixed3 a, fixed3 b)
+{
+ return a[0] < b[0] &&
+ a[1] < b[1] &&
+ a[2] < b[2];
+}
+
+// Write the nth letter in the current cell and return the value of the
+// pixel.
+// `texture_rows` and `texture_cols` indicate how many rows and columns are
+// in the texture being sampled.
+float2 GetLetterUV(float2 uv, int nth_letter,
+ float texture_cols, float texture_rows,
+ float board_cols, float board_rows,
+ float margin)
+{
+ // UV spans from [0,1] to [0,1].
+ // 'U' is horizontal; cols.
+ // 'V' is vertical; rows.
+ //
+ // I want to divide the mesh into an m x n grid.
+ // I want to know what grid cell I'm in. This is simply u * m, v * n.
+
+ // OK, I know what cell I'm in. Now I need to know how far across it I
+ // am. Produce a float in the range [0, 1).
+ float CHAR_FRAC_COL = uv.x * board_cols - floor(uv.x * board_cols);
+ float CHAR_FRAC_ROW = uv.y * board_rows - floor(uv.y * board_rows);
+
+ // Avoid rendering pixels right on the edge of the slot. If we were to
+ // do this, then that value would get stretched due to clamping
+ // (AddMarginToUV), resulting in long lines on the edge of the display.
+ float lo = margin / 2;
+ float hi = 1.0 - margin / 2;
+ if (margin != 0 &&
+ (CHAR_FRAC_ROW < lo ||
+ CHAR_FRAC_COL < lo ||
+ CHAR_FRAC_ROW > hi ||
+ CHAR_FRAC_COL > hi)) {
+ return float2(-1, -1);
+ }
+
+ float LETTER_COL = fmod(nth_letter, floor(texture_cols));
+ float LETTER_ROW = floor(texture_rows) - floor(nth_letter / floor(texture_cols));
+
+ float LETTER_UV_ROW = (LETTER_ROW + CHAR_FRAC_ROW - 1.00) / texture_rows;
+ float LETTER_UV_COL = (LETTER_COL + CHAR_FRAC_COL) / texture_cols;
+
+ float2 result;
+ result.x = LETTER_UV_COL;
+ result.y = LETTER_UV_ROW;
+
+ return result;
+}
+
+float4 GetLetter(float2 uv) {
+ fixed4 text = fixed4(0, 0, 0, 0);
+ bool discard_text = false;
+
+ uint letter = GetLetterParameter(uv);
+
+ float texture_cols;
+ float texture_rows;
+ float2 letter_uv;
+ bool is_emote = false;
+ if (letter < 0xE000) {
+ letter_uv = GetLetterUV(uv, letter % 0x2000, TEXTURE_NCOLS, TEXTURE_NROWS, BOARD_NCOLS, BOARD_NROWS, /*margin=*/0.02);
+ } else {
+ is_emote = true;
+ texture_cols = 16.0;
+ texture_rows = 8.0;
+ // This will need to be updated if we create multiple emote textures.
+ letter_uv = GetLetterUV(uv, letter % 0x2000, texture_cols, texture_rows, BOARD_NCOLS, BOARD_NROWS, /*margin=*/0);
+ }
+
+ if (letter_uv.x == -1 && letter_uv.y == -1) {
+ discard_text = true;
+ }
+
+ // We use ddx/ddy to get the correct mipmaps of the font textures. This
+ // confers 2 main benefits:
+ // 1. We don't use as much VRAM for distant players.
+ // 2. Glyphs anti-alias much more nicely.
+ const float iddx = ddx(letter_uv.x);
+ const float iddy = ddy(letter_uv.y);
+
+ if (Enable_Dithering && !is_emote) {
+ // Add noise to UV.
+ // Here, iddx and iddy tell us how big the current UV cell is with respect to
+ // screen space (i.e. how many pixels wide it is).
+ float noise = frac(prng(letter_uv) + _Time[0]);
+ letter_uv.x += (noise - 0.5) * iddx / 4.0;
+ letter_uv.y += (noise - 0.5) * iddy / 4.0;
+ }
+
+ int which_texture = (int) floor(letter / (uint) (64 * 128));
+ [forcecase] switch (which_texture)
+ {
+ case 0:
+ // Divide iddx, iddy by 2.0 to remain on a higher-detail mip level for
+ // longer.
+ text += _Font_0x0000_0x1FFF.SampleGrad(linear_clamp_sampler,
+ letter_uv, iddx / 2.0, iddy / 2.0);
+ break;
+ case 1:
+ text += _Font_0x2000_0x3FFF.SampleGrad(linear_clamp_sampler,
+ letter_uv, iddx / 2.0, iddy / 2.0);
+ break;
+ case 2:
+ text += _Font_0x4000_0x5FFF.SampleGrad(linear_clamp_sampler,
+ letter_uv, iddx / 2.0, iddy / 2.0);
+ break;
+ case 3:
+ text += _Font_0x6000_0x7FFF.SampleGrad(linear_clamp_sampler,
+ letter_uv, iddx / 2.0, iddy / 2.0);
+ break;
+ case 4:
+ text += _Font_0x8000_0x9FFF.SampleGrad(linear_clamp_sampler,
+ letter_uv, iddx / 2.0, iddy / 2.0);
+ break;
+ case 5:
+ text += _Font_0xA000_0xBFFF.SampleGrad(linear_clamp_sampler,
+ letter_uv, iddx / 2.0, iddy / 2.0);
+ break;
+ case 6:
+ text += _Font_0xC000_0xDFFF.SampleGrad(linear_clamp_sampler,
+ letter_uv, iddx / 2.0, iddy / 2.0);
+ break;
+ case 7:
+ text += _Img_0xE000_0xE03F.SampleGrad(linear_clamp_sampler,
+ letter_uv, iddx / 2.0, iddy / 2.0);
+ break;
+ default:
+ // Return some distinctive pattern that will look like a bug.
+ return fixed4(1, 0, _SinTime[0], 1);
+ }
+
+ // The edges of each letter cell can be slightly grey due to mip maps.
+ // Detect this and shade it as the background.
+ fixed3 grey = 0.5;
+ if (f3ltf3(text.rgb, grey) || discard_text || is_emote) {
+ return 0;
+ }
+ return fixed4(text.rgb, 1);
+}
+
+#endif // __STT_TEXT_INC__
+
diff --git a/Shaders/TaSTT.shader b/Shaders/TaSTT.shader
deleted file mode 100644
index 8b13789..0000000
--- a/Shaders/TaSTT.shader
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/Shaders/TaSTT_lighting_template.cginc b/Shaders/TaSTT_lighting.cginc
index b8b9de2..10bfd0c 100644
--- a/Shaders/TaSTT_lighting_template.cginc
+++ b/Shaders/TaSTT_lighting.cginc
@@ -3,27 +3,12 @@
#include "AutoLight.cginc"
#include "UnityPBSLighting.cginc"
+#include "ray_march.cginc"
+#include "pbr.cginc"
+#include "poi.cginc"
+#include "stt_generated.cginc"
+#include "stt_text.cginc"
-struct appdata
-{
- float4 position : POSITION;
- float2 uv : TEXCOORD0;
- float3 normal : NORMAL;
-};
-
-struct v2f
-{
- float4 position : SV_POSITION;
- float4 uv : TEXCOORD0;
- float3 normal : TEXCOORD1;
- float3 worldPos : TEXCOORD2;
-
- #if defined(VERTEXLIGHT_ON)
- float3 vertexLightColor : TEXCOORD3;
- #endif
-};
-
-SamplerState linear_clamp_sampler;
SamplerState linear_repeat_sampler;
float BG_Enable;
@@ -31,35 +16,12 @@ Texture2D BG_BaseColor;
Texture2D BG_NormalMap;
Texture2D BG_Metallic;
Texture2D BG_Smoothness;
-Texture2D BG_Emission_Mask;
float BG_Smoothness_Invert;
float BG_NormalStrength;
-float3 BG_Emission_Color;
float4 BG_BaseColor_ST;
float4 BG_NormalMap_ST;
float4 BG_Metallic_ST;
float4 BG_Smoothness_ST;
-float4 BG_Emission_Mask_ST;
-
-float Enable_Dithering;
-float AA_Amount;
-
-Texture2D _Font_0x0000_0x1FFF;
-float4 _Font_0x0000_0x1FFF_TexelSize;
-Texture2D _Font_0x2000_0x3FFF;
-float4 _Font_0x2000_0x3FFF_TexelSize;
-Texture2D _Font_0x4000_0x5FFF;
-float4 _Font_0x4000_0x5FFF_TexelSize;
-Texture2D _Font_0x6000_0x7FFF;
-float4 _Font_0x6000_0x7FFF_TexelSize;
-Texture2D _Font_0x8000_0x9FFF;
-float4 _Font_0x8000_0x9FFF_TexelSize;
-Texture2D _Font_0xA000_0xBFFF;
-float4 _Font_0xA000_0xBFFF_TexelSize;
-Texture2D _Font_0xC000_0xDFFF;
-float4 _Font_0xC000_0xDFFF_TexelSize;
-Texture2D _Img_0xE000_0xE03F;
-float4 _Img_0xE000_0xE03F_TexelSize;
fixed4 Text_Color;
fixed4 Background_Color;
@@ -74,25 +36,7 @@ float Render_Visual_Indicator;
float Margin_Scale;
float Margin_Rounding_Scale;
float Enable_Margin_Effect_Squares;
-
-float Enable_Custom_Cubemap;
-UNITY_DECLARE_TEXCUBE(Custom_Cubemap);
-
-// %TEMPLATE__CG_ROW_COL_CONSTANTS%
-
-float3 HUEtoRGB(in float H)
-{
- float R = abs(H * 6 - 3) - 1;
- float G = 2 - abs(H * 6 - 2);
- float B = 2 - abs(H * 6 - 4);
- return saturate(float3(R, G, B));
-}
-
-float3 HSVtoRGB(in float3 HSV)
-{
- float3 RGB = HUEtoRGB(HSV.x);
- return ((RGB - 1) * HSV.y + 1) * HSV.z;
-}
+float Enable_Ray_March;
float _TaSTT_Indicator_0;
float _TaSTT_Indicator_1;
@@ -109,8 +53,6 @@ fixed4 float3tofixed4(in float3 f3, in float alpha)
alpha);
}
-// %TEMPLATE__CG_ROW_COL_PARAMS%
-
void getVertexLightColor(inout v2f i)
{
#if defined(VERTEXLIGHT_ON)
@@ -142,18 +84,12 @@ v2f vert(appdata v)
o.position = UnityObjectToClipPos(v.position);
o.worldPos = mul(unity_ObjectToWorld, v.position);
o.normal = UnityObjectToWorldNormal(v.normal);
+
o.uv.xy = TRANSFORM_TEX(v.uv, BG_BaseColor);
o.uv.zw = 1.0 - v.uv;
getVertexLightColor(o);
- return o;
-}
-float2 AddMarginToUV(float2 uv, float2 margin)
-{
- float2 lo = float2(-margin.x / 2, -margin.y / 2);
- float2 hi = float2(1.0 + margin.x / 2, 1.0 + margin.y / 2);
-
- return clamp(lerp(lo, hi, uv), 0.0, 1.0);
+ return o;
}
// dist = sqrt(dx^2 + dy^2) = sqrt(<dx,dy> * <dx,dy>)
@@ -165,22 +101,18 @@ bool InRadius2(float2 uv, float2 pos, float radius2)
bool InMargin(float2 uv, float2 margin)
{
- if (uv.x < margin.x ||
- uv.x > 1 - margin.x ||
- uv.y < margin.y ||
- uv.y > 1 - margin.y) {
- return true;
- }
-
- return false;
+ bool b0 = uv.x < margin.x;
+ bool b1 = uv.x > 1 - margin.x;
+ bool b2 = uv.y < margin.y;
+ bool b3 = uv.y > 1 - margin.y;
+
+ // De Morgan's law:
+ // a OR b = !(!a AND !b)
+ return !(!b0 * !b1 * !b2 * !b3);
}
bool InSpeechIndicator(float2 uv, float2 margin)
{
- if (!Render_Visual_Indicator) {
- return false;
- }
-
// Margin is uv_margin/2 wide/tall.
// We want a circle whose radius is ~80% of that.
float radius_factor = 0.95;
@@ -192,11 +124,7 @@ bool InSpeechIndicator(float2 uv, float2 margin)
// bottom.
indicator_center.y = 1.0 - indicator_center.y;
- if (InRadius2(uv, indicator_center, radius * radius)) {
- return true;
- }
-
- return false;
+ return Render_Visual_Indicator && InRadius2(uv, indicator_center, radius * radius);
}
bool InMarginRounding(float2 uv, float2 margin, float rounding, bool interior)
@@ -233,64 +161,6 @@ bool InMarginRounding(float2 uv, float2 margin, float rounding, bool interior)
return false;
}
-// Write the nth letter in the current cell and return the value of the
-// pixel.
-// `texture_rows` and `texture_cols` indicate how many rows and columns are
-// in the texture being sampled.
-float2 GetLetter(float2 uv, int nth_letter,
- float texture_cols, float texture_rows,
- float board_cols, float board_rows,
- float margin)
-{
- // UV spans from [0,1] to [0,1].
- // 'U' is horizontal; cols.
- // 'V' is vertical; rows.
- //
- // I want to divide the mesh into an m x n grid.
- // I want to know what grid cell I'm in. This is simply u * m, v * n.
-
- // OK, I know what cell I'm in. Now I need to know how far across it I
- // am. Produce a float in the range [0, 1).
- float CHAR_FRAC_COL = uv.x * board_cols - floor(uv.x * board_cols);
- float CHAR_FRAC_ROW = uv.y * board_rows - floor(uv.y * board_rows);
-
- // Avoid rendering pixels right on the edge of the slot. If we were to
- // do this, then that value would get stretched due to clamping
- // (AddMarginToUV), resulting in long lines on the edge of the display.
- float lo = margin / 2;
- float hi = 1.0 - margin / 2;
- if (margin != 0 &&
- (CHAR_FRAC_ROW < lo ||
- CHAR_FRAC_COL < lo ||
- CHAR_FRAC_ROW > hi ||
- CHAR_FRAC_COL > hi)) {
- return float2(-1, -1);
- }
-
- float LETTER_COL = fmod(nth_letter, floor(texture_cols));
- float LETTER_ROW = floor(texture_rows) - floor(nth_letter / floor(texture_cols));
-
- float LETTER_UV_ROW = (LETTER_ROW + CHAR_FRAC_ROW - 1.00) / texture_rows;
- float LETTER_UV_COL = (LETTER_COL + CHAR_FRAC_COL) / texture_cols;
-
- float2 result;
- result.x = LETTER_UV_COL;
- result.y = LETTER_UV_ROW;
-
- return result;
-}
-
-// Get the value of the parameter for the cell we're in.
-uint GetLetterParameter(float2 uv)
-{
- float CHAR_COL = floor(uv.x * BOARD_NCOLS);
- float CHAR_ROW = floor(uv.y * BOARD_NROWS);
- int res = 0;
-
- // %TEMPLATE__CG_LETTER_ACCESSOR%
- return res;
-}
-
fixed sq_dist(fixed2 p0, fixed2 p1)
{
fixed2 delta = p1 - p0;
@@ -304,7 +174,6 @@ fixed4 effect_squares (v2f i)
uv.y *= 2; // Text box has 2:1 aspect ratio
const fixed time = _Time.y;
- #define PI 3.1415926535
fixed theta = PI/4 + sin(time / 4) * 0.1;
fixed2x2 rot =
fixed2x2(cos(theta), -1 * sin(theta),
@@ -381,75 +250,6 @@ fixed4 margin_effect(v2f i)
}
}
-UnityLight GetLight(v2f i)
-{
- UNITY_LIGHT_ATTENUATION(attenuation, 0, i.worldPos);
- float3 light_color = _LightColor0.rgb * attenuation;
-
- UnityLight light;
- light.color = light_color;
- #if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT)
- light.dir = normalize(_WorldSpaceLightPos0.xyz - i.worldPos);
- #else
- light.dir = _WorldSpaceLightPos0.xyz;
- #endif
- light.ndotl = DotClamped(i.normal, light.dir);
-
- return light;
-}
-
-UnityIndirect GetIndirect(v2f i, float3 view_dir, float smoothness) {
- UnityIndirect indirect;
- indirect.diffuse = 0;
- indirect.specular = 0;
-
- #if defined(VERTEXLIGHT_ON)
- indirect.diffuse = i.vertexLightColor;
- #endif
-
- #if defined(FORWARD_BASE_PASS)
- indirect.diffuse += max(0, ShadeSH9(float4(i.normal, 1)));
- float3 reflect_dir = reflect(-view_dir, i.normal);
- // There's a nonlinear relationship between mipmap level and roughness.
- float roughness = 1 - smoothness;
- roughness *= 1.7 - .7 * roughness;
- float3 env_sample;
- if (Enable_Custom_Cubemap) {
- env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
- Custom_Cubemap,
- reflect_dir,
- roughness * UNITY_SPECCUBE_LOD_STEPS);
- } else {
- env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
- unity_SpecCube0,
- reflect_dir,
- roughness * UNITY_SPECCUBE_LOD_STEPS);
- }
- indirect.specular = env_sample;
- #endif
-
- return indirect;
-}
-
-void initNormal(inout v2f i)
-{
- if (BG_Enable) {
- i.normal = UnpackScaleNormal(
- BG_NormalMap.SampleGrad(linear_repeat_sampler, i.uv.xy,
- ddx(i.uv.x), ddy(i.uv.y)),
- BG_NormalStrength);
- // Swap Y and Z
- i.normal = i.normal.xzy;
- }
- i.normal = normalize(i.normal);
-}
-
-float getWorldSpaceDepth(in v2f i)
-{
- float4 clip_pos = mul(UNITY_MATRIX_VP, float4(i.worldPos, 1.0));
- return clip_pos.z / clip_pos.w;
-}
-
fixed4 light(v2f i,
Texture2D albedo_map,
Texture2D normal_map,
@@ -463,7 +263,7 @@ fixed4 light(v2f i,
{
initNormal(i);
- depth = getWorldSpaceDepth(i);
+ depth = getWorldSpaceDepth(i.worldPos);
float2 iddx = ddx(i.uv.x);
float2 iddy = ddy(i.uv.y);
@@ -510,7 +310,7 @@ fixed4 light(v2f i,
fixed4 light(v2f i, fixed4 unlit, out float depth)
{
- depth = getWorldSpaceDepth(i);
+ depth = getWorldSpaceDepth(i.worldPos);
// Get color in spherical harmonics
fixed3 albedo = unlit.rgb;
@@ -535,24 +335,15 @@ fixed4 light(v2f i, fixed4 unlit, out float depth)
return fixed4(pbr, unlit.a);
}
-bool f3ltf3(fixed3 a, fixed3 b)
-{
- return a[0] < b[0] &&
- a[1] < b[1] &&
- a[2] < b[2];
-}
-
-// Generate a random number on [0, 1].
-float prng(float2 p)
-{
- return frac(sin(dot(p, float2(561.0, 885.0))) * 776.2) / 2.0;
-}
-
fixed4 frag(v2f i, out float depth : SV_DepthLessEqual) : SV_Target
{
float2 uv = i.uv.zw;
depth = -1000.0;
+ if (Enable_Ray_March) {
+ return stt_ray_march(i, depth);
+ }
+
// Fix text orientation
uv.y = 0.5 - uv.y;
uv.x = 1.0 - uv.x;
@@ -597,110 +388,36 @@ fixed4 frag(v2f i, out float depth : SV_DepthLessEqual) : SV_Target
uv_margin *= 4;
float2 uv_with_margin = AddMarginToUV(uv, uv_margin);
- fixed4 text = fixed4(0, 0, 0, 0);
- bool discard_text = false;
-
- uint letter = GetLetterParameter(uv_with_margin);
+ fixed4 text = GetLetter(uv_with_margin);
- float texture_cols;
- float texture_rows;
- float2 letter_uv;
- bool is_emote = false;
- if (letter < 0xE000) {
- letter_uv = GetLetter(uv_with_margin, letter % 0x2000, TEXTURE_NCOLS, TEXTURE_NROWS, BOARD_NCOLS, BOARD_NROWS, /*margin=*/0.02);
- } else {
- is_emote = true;
- texture_cols = 16.0;
- texture_rows = 8.0;
- // This will need to be updated if we create multiple emote textures.
- letter_uv = GetLetter(uv_with_margin, letter % 0x2000, texture_cols, texture_rows, BOARD_NCOLS, BOARD_NROWS, /*margin=*/0);
- }
-
- if (letter_uv.x == -1 && letter_uv.y == -1) {
- discard_text = true;
- }
-
- // We use ddx/ddy to get the correct mipmaps of the font textures. This
- // confers 2 main benefits:
- // 1. We don't use as much VRAM for distant players.
- // 2. Glyphs anti-alias much more nicely.
- const float iddx = ddx(letter_uv.x);
- const float iddy = ddy(letter_uv.y);
-
- if (Enable_Dithering && !is_emote) {
- // Add noise to UV.
- // Here, iddx and iddy tell us how big the current UV cell is with respect to
- // screen space (i.e. how many pixels wide it is).
- float noise = frac(prng(letter_uv) + _Time[0]);
- letter_uv.x += (noise - 0.5) * iddx / 4.0;
- letter_uv.y += (noise - 0.5) * iddy / 4.0;
- }
-
- int which_texture = (int) floor(letter / (uint) (64 * 128));
- [forcecase] switch (which_texture)
- {
- case 0:
- // Divide iddx, iddy by 2.0 to remain on a higher-detail mip level for
- // longer.
- text += _Font_0x0000_0x1FFF.SampleGrad(linear_clamp_sampler,
- letter_uv, iddx / 2.0, iddy / 2.0);
- break;
- case 1:
- text += _Font_0x2000_0x3FFF.SampleGrad(linear_clamp_sampler,
- letter_uv, iddx / 2.0, iddy / 2.0);
- break;
- case 2:
- text += _Font_0x4000_0x5FFF.SampleGrad(linear_clamp_sampler,
- letter_uv, iddx / 2.0, iddy / 2.0);
- break;
- case 3:
- text += _Font_0x6000_0x7FFF.SampleGrad(linear_clamp_sampler,
- letter_uv, iddx / 2.0, iddy / 2.0);
- break;
- case 4:
- text += _Font_0x8000_0x9FFF.SampleGrad(linear_clamp_sampler,
- letter_uv, iddx / 2.0, iddy / 2.0);
- break;
- case 5:
- text += _Font_0xA000_0xBFFF.SampleGrad(linear_clamp_sampler,
- letter_uv, iddx / 2.0, iddy / 2.0);
- break;
- case 6:
- text += _Font_0xC000_0xDFFF.SampleGrad(linear_clamp_sampler,
- letter_uv, iddx / 2.0, iddy / 2.0);
- break;
- case 7:
- text += _Img_0xE000_0xE03F.SampleGrad(linear_clamp_sampler,
- letter_uv, iddx / 2.0, iddy / 2.0);
- break;
- default:
- // Return some distinctive pattern that will look like a bug.
- depth = getWorldSpaceDepth(i);
- return fixed4(1, 0, _SinTime[0], 1);
- }
-
- // The edges of each letter cell can be slightly grey due to mip maps.
- // Detect this and shade it as the background.
- fixed3 grey = 0.5;
- if (f3ltf3(text.rgb, grey) || discard_text || is_emote) {
+ if (text.a == 0) {
fixed4 bg;
if (BG_Enable) {
+
+#if 0
+fixed4 light(inout v2f i,
+ fixed4 albedo,
+ float metallic,
+ float smoothness)
+#endif
+
+ const float iddx = ddx(i.uv.x);
+ const float iddy = ddy(i.uv.y);
+ fixed4 albedo = BG_BaseColor.SampleGrad(linear_repeat_sampler, i.uv.xy,
+ iddx, iddy);
+ float metallic = BG_Metallic.SampleGrad(linear_repeat_sampler, i.uv.xy,
+ iddx, iddy);
+ float smoothness = BG_Smoothness.SampleGrad(linear_repeat_sampler, i.uv.xy,
+ iddx, iddy);
+
bg = light(i,
- BG_BaseColor,
- BG_NormalMap,
- BG_NormalStrength,
- BG_Metallic,
- BG_Smoothness,
- BG_Smoothness_Invert,
- BG_Emission_Mask,
- BG_Emission_Color,
- depth);
+ albedo, metallic, smoothness);
} else {
bg = light(i, Background_Color, depth);
}
// Hack: If alpha (text.w) is less than 0.5, don't render it. This
// eliminates outlines around simple emotes with transparent backgrounds.
- if (is_emote && text.w > 0.5) {
+ if (text.w > 0.5) {
// Use emote alpha to mix emote color with background color (compositing).
text.rgb = lerp(bg.rgb, text.rgb, text.w);
bg = light(i, fixed4(text.rgb, 1.0), depth);
@@ -712,4 +429,3 @@ fixed4 frag(v2f i, out float depth : SV_DepthLessEqual) : SV_Target
}
#endif // TASTT_LIGHTING
-
diff --git a/Shaders/TaSTT_template.shader b/Shaders/TaSTT_template.shader
index 44c3b41..3c6feba 100644
--- a/Shaders/TaSTT_template.shader
+++ b/Shaders/TaSTT_template.shader
@@ -1,4 +1,4 @@
-Shader "TaSTT"
+Shader "yum_food/TaSTT_exp"
{
Properties
{
@@ -16,6 +16,8 @@
Margin_Rounding_Scale("Margin rounding scale", float) = 0.03
[MaterialToggle] Enable_Margin_Effect_Squares(
"Enable margin effect: Squares", float) = 0
+ [MaterialToggle] Enable_Ray_March(
+ "Enable ray marching", float) = 0
[MaterialToggle] Enable_Dithering("Enable dithering", float) = 1
diff --git a/Shaders/eyes_data.cginc b/Shaders/eyes_data.cginc
new file mode 100644
index 0000000..d789116
--- /dev/null
+++ b/Shaders/eyes_data.cginc
@@ -0,0 +1,24 @@
+#ifndef __EYES_DATA_INC__
+#define __EYES_DATA_INC__
+
+struct appdata
+{
+ float4 position : POSITION;
+ float2 uv : TEXCOORD0;
+ float3 normal : NORMAL;
+};
+
+struct v2f
+{
+ float4 position : SV_POSITION;
+ float4 uv : TEXCOORD0;
+ float3 normal : TEXCOORD1;
+ float3 worldPos : TEXCOORD2;
+
+ #if defined(VERTEXLIGHT_ON)
+ float3 vertexLightColor : TEXCOORD3;
+ #endif
+};
+
+#endif // __EYES_DATA_INC__
+
diff --git a/Shaders/hg_sdf.cginc b/Shaders/hg_sdf.cginc
new file mode 100644
index 0000000..b1de0b0
--- /dev/null
+++ b/Shaders/hg_sdf.cginc
@@ -0,0 +1,187 @@
+#ifndef __HG_SDF__
+#define __HG_SDF__
+
+#include "pema99.cginc"
+
+////////////////////////////////////////////////////////////////
+//
+// HG_SDF
+//
+// GLSL LIBRARY FOR BUILDING SIGNED DISTANCE BOUNDS
+//
+// version 2021-07-28
+//
+// Check https://mercury.sexy/hg_sdf for updates
+// and usage examples. Send feedback to spheretracing@mercury.sexy.
+//
+// Brought to you by MERCURY https://mercury.sexy/
+//
+//
+//
+// Released dual-licensed under
+// Creative Commons Attribution-NonCommercial (CC BY-NC)
+// or
+// MIT License
+// at your choice.
+//
+// SPDX-License-Identifier: MIT OR CC-BY-NC-4.0
+//
+// /////
+//
+// CC-BY-NC-4.0
+// https://creatifloatommons.org/licenses/by-nc/4.0/legalcode
+// https://creatifloatommons.org/licenses/by-nc/4.0/
+//
+// /////
+//
+// MIT License
+//
+// Copyright (c) 2011-2021 Mercury Demogroup
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+//
+// /////
+//
+////////////////////////////////////////////////////////////////
+//
+// How to use this:
+//
+// 1. Build some system to #include glsl files in each other.
+// Include this one at the very start. Or just paste everywhere.
+// 2. Build a sphere tracer. See those papers:
+// * "Sphere Tracing" https://link.springer.com/article/10.1007%2Fs003710050084
+// * "Enhanced Sphere Tracing" http://diglib.eg.org/handle/10.2312/stag.20141233.001-008
+// * "Improved Ray Casting of Procedural Distance Bounds" https://www.bibsonomy.org/bibtex/258e85442234c3ace18ba4d89de94e57d
+// The Raymnarching Toolbox Thread on pouet can be helpful as well
+// http://www.pouet.net/topic.php?which=7931&page=1
+// and contains links to many more resources.
+// 3. Use the tools in this library to build your distance bound f().
+// 4. ???
+// 5. Win a compo.
+//
+// (6. Buy us a beer or a good vodka or something, if you like.)
+//
+////////////////////////////////////////////////////////////////
+//
+// Table of Contents:
+//
+// * Helper functions and macros
+// * Collection of some primitive objects
+// * Domain Manipulation operators
+// * Object combination operators
+//
+////////////////////////////////////////////////////////////////
+//
+// Why use this?
+//
+// The point of this lib is that everything is structured according
+// to patterns that we ended up using when building geometry.
+// It makes it more easy to write code that is reusable and that somebody
+// else can actually understand. Especially code on Shadertoy (which seems
+// to be what everybody else is looking at for "inspiration") tends to be
+// really ugly. So we were forced to do something about the situation and
+// release this lib ;)
+//
+// Everything in here can probably be done in some better way.
+// Please experiment. We'd love some feedback, especially if you
+// use it in a scene production.
+//
+// The main patterns for building geometry this way are:
+// * Stay Lipschitz continuous. That means: don't have any distance
+// gradient larger than 1. Try to be as close to 1 as possible -
+// Distances are euclidean distances, don't fudge around.
+// Underestimating distances will happen. That's why calling
+// it a "distance bound" is more correct. Don't ever multiply
+// distances by some value to "fix" a Lipschitz continuity
+// violation. The invariant is: each fSomething() function returns
+// a correct distance bound.
+// * Use very few primitives and combine them as building blocks
+// using combine opertors that preserve the invariant.
+// * Multiply objects by repeating the domain (space).
+// If you are using a loop inside your distance function, you are
+// probably doing it wrong (or you are building boring fractals).
+// * At right-angle intersections between objects, build a new local
+// coordinate system from the two distances to combine them in
+// interesting ways.
+// * As usual, there are always times when it is best to not follow
+// specific patterns.
+//
+////////////////////////////////////////////////////////////////
+//
+// FAQ
+//
+// Q: Why is there no sphere tracing code in this lib?
+// A: Because our system is way too complex and always changing.
+// This is the constant part. Also we'd like everyone to
+// explore for themselves.
+//
+// Q: This does not work when I paste it into Shadertoy!!!!
+// A: Yes. It is GLSL, not GLSL ES. We like real OpenGL
+// because it has way more features and is more likely
+// to work compared to browser-based WebGL. We recommend
+// you consider using OpenGL for your productions. Most
+// of this can be ported easily though.
+//
+// Q: How do I material?
+// A: We recommend something like this:
+// Write a material ID, the distance and the local coordinate
+// p into some global variables whenever an object's distance is
+// smaller than the stored distance. Then, at the end, evaluate
+// the material to get color, roughness, etc., and do the shading.
+//
+// Q: I found an error. Or I made some function that would fit in
+// in this lib. Or I have some suggestion.
+// A: Awesome! Drop us a mail at spheretracing@mercury.sexy.
+//
+// Q: Why is this not on github?
+// A: Because we were too lazy. If we get bugged about it enough,
+// we'll do it.
+//
+// Q: Your license sucks for me.
+// A: Oh. What should we change it to?
+//
+// Q: I have trouble understanding what is going on with my distances.
+// A: Some visualization of the distance field helps. Try drawing a
+// plane that you can sweep through your scene with some color
+// representation of the distance field at each point and/or iso
+// lines at regular intervals. Visualizing the length of the
+// gradient (or better: how much it deviates from being equal to 1)
+// is immensely helpful for understanding which parts of the
+// distance field are broken.
+//
+////////////////////////////////////////////////////////////////
+
+#define PI 3.14159265
+
+// Repeat around the origin by a fixed angle.
+// For easier use, num of repetitions is use to specify the angle.
+float pModPolar(inout float2 p, float repetitions) {
+ float angle = 2*PI/repetitions;
+ float a = atan2(p.y, p.x) + angle/2.;
+ float r = length(p);
+ float c = floor(a/angle);
+ a = glsl_mod(a,angle) - angle/2.;
+ p = float2(cos(a), sin(a))*r;
+ // For an odd number of repetitions, fix cell index of the cell in -x direction
+ // (cell index would be e.g. -5 and 5 in the two halves of the cell):
+ if (abs(c) >= (repetitions/2)) c = abs(c);
+ return c;
+}
+
+#endif // __HG_SDF__
diff --git a/Shaders/iq_sdf.cginc b/Shaders/iq_sdf.cginc
new file mode 100644
index 0000000..d82e24c
--- /dev/null
+++ b/Shaders/iq_sdf.cginc
@@ -0,0 +1,136 @@
+#ifndef __IQ_SDF_INC__
+#define __IQ_SDF_INC__
+
+#include "pema99.cginc"
+
+// The MIT License
+// Copyright © 2020 Inigo Quilez
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+float distance_from_octahedron(in float3 p)
+{
+ float s = 1.0;
+ float3 pp = abs(p);
+ float m = pp.x+pp.y+pp.z-s;
+ float3 q;
+ if( 3.0*pp.x < m ) q = pp.xyz;
+ else if( 3.0*pp.y < m ) q = pp.yzx;
+ else if( 3.0*pp.z < m ) q = pp.zxy;
+ else return m*0.57735027;
+
+ float k = clamp(0.5*(q.z-q.y+s),0.0,s);
+ return length(float3(q.x,q.y-s+k,q.z-k));
+}
+
+float distance_from_sphere(float3 p)
+{
+ return length(p);
+}
+
+float distance_from_sphere(float3 p, float3 c, float r)
+{
+ return length(p - c) - r;
+}
+
+float distance_from_cut_sphere( in float3 p, in float r, in float h )
+{
+ float w = sqrt(r*r-h*h); // constant for a given shape
+
+ float2 q = float2( length(p.xz), p.y );
+
+ float s = max( (h-r)*q.x*q.x+w*w*(h+r-2.0*q.y), h*q.x-w*q.y );
+
+ return (s<0.0) ? length(q)-r :
+ (q.x<w) ? h - q.y :
+ length(q-float2(w,h));
+}
+
+float distance_from_cut_hollow_sphere( float3 p, float r, float h, float t )
+{
+ float2 q = float2( length(p.xz), p.y );
+
+ float w = sqrt(r*r-h*h);
+
+ return ((h*q.x<w*q.y) ? length(q-float2(w,h)) :
+ abs(length(q)-r) ) - t;
+}
+
+float distance_from_box(float3 p, float3 b)
+{
+ float3 q = abs(p) - b;
+ return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
+}
+
+float distance_from_box_frame(float3 p, float3 b, float e)
+{
+ p = abs(p)-b;
+ float3 q = abs(p+e)-e;
+
+ return min(min(
+ length(max(float3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0),
+ length(max(float3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)),
+ length(max(float3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0));
+}
+
+float distance_from_pyramid(float3 p, float h, bool invert)
+{
+ float m2 = h*h + 0.25;
+
+ // symmetry
+ p.xz = abs(p.xz); // do p=abs(p) instead for double pyramid
+ p.xz = (p.z>p.x) ? p.zx : p.xz;
+ p.xz -= 0.5;
+
+ // project into face plane (2D)
+ float3 q = float3( p.z, h*p.y-0.5*p.x, h*p.x+0.5*p.y);
+
+ float s = max(-q.x,0.0);
+ float t = clamp( (q.y-0.5*q.x)/(m2+0.25), 0.0, 1.0 );
+
+ float a = m2*(q.x+s)*(q.x+s) + q.y*q.y;
+ float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t);
+
+ float d2 = max(-q.y,q.x*m2+q.y*0.5) < 0.0 ? 0.0 : min(a,b);
+
+ // recover 3D and scale, and add sign
+ return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y));;
+}
+
+float distance_from_plane(float3 p, float3 n, float h)
+{
+ // n must be normalized
+ return dot(p,n) + h;
+}
+
+float distance_from_torus( float3 p, float2 t )
+{
+ float2 q = float2(length(p.xz)-t.x,p.y);
+ return length(q)-t.y;
+}
+
+float distance_from_capped_torus( float3 p, float2 sc, float ra, float rb)
+{
+ p.x = abs(p.x);
+ float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy);
+ return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb;
+}
+
+float3 op_rep(in float3 p, in float3 c)
+{
+ return glsl_mod(p+0.5*c,c)-0.5*c;
+}
+
+float smoothstep_cubic(float x)
+{
+ return x * x * (3.0 - 2.0 * x);
+}
+
+float smoothstep_quintic(float x)
+{
+ return x*x*x*(x*(x*6.0-15.0)+10.0);
+}
+
+// End licensed section
+
+#endif // __IQ_SDF_INC__
+
diff --git a/Shaders/math.cginc b/Shaders/math.cginc
new file mode 100644
index 0000000..061d4c0
--- /dev/null
+++ b/Shaders/math.cginc
@@ -0,0 +1,88 @@
+#ifndef __MATH_INC
+#define __MATH_INC
+
+#include "pema99.cginc"
+
+#define PI 3.14159265
+
+// Differentiable approximation of the standard `max` function.
+float dmax(float a, float b, float k)
+{
+ return log2(exp2(k * a) + exp2(k * b)) / k;
+}
+
+// Differentiable approximation of the standard `min` function.
+float dmin(float a, float b, float k)
+{
+ return -1.0 * dmax(-1.0 * a, -1.0 * b, k);
+}
+
+// Generate a random number on [0, 1].
+float rand2(float3 p)
+{
+ return glsl_mod(sin(dot(p, float2(561.0, 885.0))) * 776.2, 1.0);
+}
+
+// Generate a random number on [0, 1].
+float rand3(float3 p)
+{
+ return glsl_mod(sin(dot(p, float3(897.0, 367.0, 197.0))) * 1073.6, 1.0);
+}
+
+// 3 dimensional value noise. `p` is assumed to be a point inside a unit cube.
+// Theory: https://en.wikipedia.org/wiki/Value_noise
+float vnoise3d(float3 p)
+{
+ float3 pu = floor(p);
+ float3 pv = glsl_mod(p, 1.0);
+
+ // Assign random numbers to the corner of a cube.
+ float n000 = rand3(pu + float3(0,0,0));
+ float n001 = rand3(pu + float3(0,0,1));
+ float n010 = rand3(pu + float3(0,1,0));
+ float n011 = rand3(pu + float3(0,1,1));
+ float n100 = rand3(pu + float3(1,0,0));
+ float n101 = rand3(pu + float3(1,0,1));
+ float n110 = rand3(pu + float3(1,1,0));
+ float n111 = rand3(pu + float3(1,1,1));
+
+ float n00 = lerp(n000, n001, pv.z);
+ float n01 = lerp(n010, n011, pv.z);
+ float n10 = lerp(n100, n101, pv.z);
+ float n11 = lerp(n110, n111, pv.z);
+
+ float n0 = lerp(n00, n01, pv.y);
+ float n1 = lerp(n10, n11, pv.y);
+
+ float n = lerp(n0, n1, pv.x);
+
+ return n;
+}
+
+float fbm(float3 p, const int n_octaves, float w)
+{
+ float g = exp2(-w);
+ float a = 1.0;
+ float p_scale = 1.0;
+
+ float res = 0.0;
+ for (int i = 0; i < n_octaves; i++) {
+ res += a * vnoise3d(p * p_scale);
+
+ p_scale /= w;
+ a *= g;
+ }
+
+ // On average, vnoise3d returns 0.5.
+ // Sum of any geometric series is, for growth parameter r and constant a,
+ // a / (1 - r).
+ // We want to map onto [0, 1], so divide by this expected sum.
+ // Use a = 1, to account for the worst-case possibility that every call to
+ // vnoise3d() returns 1.
+ res /= (1 / (1 - g));
+
+ return res;
+}
+
+#endif // __MATH_INC
+
diff --git a/Shaders/pbr.cginc b/Shaders/pbr.cginc
new file mode 100644
index 0000000..0731ca5
--- /dev/null
+++ b/Shaders/pbr.cginc
@@ -0,0 +1,126 @@
+#ifndef __PBR_INC__
+#define __PBR_INC__
+
+#include "AutoLight.cginc"
+#include "eyes_data.cginc"
+#include "UnityPBSLighting.cginc"
+
+static float BG_Effect_Bias = 0.0;
+static float BG_Effect_Weight = 1.0;
+
+float BG_Effect_Emission_Strength;
+float Enable_Custom_Cubemap;
+UNITY_DECLARE_TEXCUBE(Custom_Cubemap);
+
+sampler2D BG_Emission_Mask;
+sampler2D BG_Emission_Color;
+
+float BG_Emission_Strength;
+float4 BG_Emission_Mask_ST;
+
+
+UnityIndirect GetIndirect(v2f i, float3 view_dir, float smoothness) {
+ UnityIndirect indirect;
+ indirect.diffuse = 0;
+ indirect.specular = 0;
+
+ #if defined(VERTEXLIGHT_ON)
+ indirect.diffuse = i.vertexLightColor;
+ #endif
+
+ #if defined(FORWARD_BASE_PASS)
+ indirect.diffuse += max(0, ShadeSH9(float4(i.normal, 1)));
+ float3 reflect_dir = reflect(-view_dir, i.normal);
+ // There's a nonlinear relationship between mipmap level and roughness.
+ float roughness = 1 - smoothness;
+ roughness *= 1.7 - .7 * roughness;
+ float3 env_sample;
+ if (Enable_Custom_Cubemap) {
+ env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
+ Custom_Cubemap,
+ reflect_dir,
+ roughness * UNITY_SPECCUBE_LOD_STEPS);
+ } else {
+ env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
+ unity_SpecCube0,
+ reflect_dir,
+ roughness * UNITY_SPECCUBE_LOD_STEPS);
+ }
+ indirect.specular = env_sample;
+ #endif
+
+ return indirect;
+}
+
+UnityLight GetLight(v2f i)
+{
+ UNITY_LIGHT_ATTENUATION(attenuation, 0, i.worldPos);
+ float3 light_color = _LightColor0.rgb * attenuation;
+
+ UnityLight light;
+ light.color = light_color;
+ #if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT)
+ light.dir = normalize(_WorldSpaceLightPos0.xyz - i.worldPos);
+ #else
+ light.dir = _WorldSpaceLightPos0.xyz;
+ #endif
+ light.ndotl = DotClamped(i.normal, light.dir);
+
+ return light;
+}
+
+void initNormal(inout v2f i)
+{
+ i.normal = normalize(i.normal);
+}
+
+fixed4 light(inout v2f i,
+ fixed4 albedo,
+ float metallic,
+ float smoothness)
+{
+ initNormal(i);
+
+ float2 iddx = ddx(i.uv.x);
+ float2 iddy = ddy(i.uv.y);
+
+ bool is_ray_hit = (albedo.r > 0 || albedo.g > 0 || albedo.b > 0);
+ if (is_ray_hit) {
+ albedo.rgb *= BG_Effect_Weight;
+ albedo.rgb += BG_Effect_Bias;
+ } else {
+ albedo.rgb = float3(0, 0, 0);
+ }
+
+ float3 specular_tint;
+ float one_minus_reflectivity;
+ albedo.rgb = DiffuseAndSpecularFromMetallic(
+ albedo, metallic, specular_tint, one_minus_reflectivity);
+
+ float emission_mask_sample = tex2Dgrad(BG_Emission_Mask, i.uv.xy, iddx, iddy);
+ fixed3 emission = emission_mask_sample *
+ tex2Dgrad(BG_Emission_Color, i.uv.xy, iddx, iddy) * BG_Emission_Strength;
+
+ float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos);
+ fixed3 pbr = UNITY_BRDF_PBS(albedo,
+ specular_tint,
+ one_minus_reflectivity,
+ smoothness,
+ i.normal,
+ view_dir,
+ GetLight(i),
+ GetIndirect(i, view_dir, smoothness)).rgb;
+ pbr.rgb += emission;
+ pbr.rgb += albedo.rgb * BG_Effect_Emission_Strength;
+
+ return fixed4(saturate(pbr), albedo.a);
+}
+
+float getWorldSpaceDepth(in float3 world_pos)
+{
+ float4 clip_pos = mul(UNITY_MATRIX_VP, float4(world_pos, 1.0));
+ return clip_pos.z / clip_pos.w;
+}
+
+#endif // __PBR_INC__
+
diff --git a/Shaders/pema99.cginc b/Shaders/pema99.cginc
new file mode 100644
index 0000000..c895a48
--- /dev/null
+++ b/Shaders/pema99.cginc
@@ -0,0 +1,31 @@
+/*
+MIT License
+
+Copyright (c) 2022 Pema Malling
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef __PEMA99_INC__
+#define __PEMA99_INC__
+
+#define glsl_mod(x,y) (((x)-(y)*floor((x)/(y))))
+
+#endif // __PEMA99_INC__
+
diff --git a/Shaders/poi.cginc b/Shaders/poi.cginc
new file mode 100644
index 0000000..87382bb
--- /dev/null
+++ b/Shaders/poi.cginc
@@ -0,0 +1,60 @@
+#ifndef __POI_INC__
+#define __POI_INC__
+
+/*
+MIT License
+
+Copyright (c) 2018 King Arthur
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+static const float Epsilon = 1e-10;
+float3 RGBtoHCV(in float3 RGB)
+{
+ // Based on work by Sam Hocevar and Emil Persson
+ float4 P = (RGB.g < RGB.b) ? float4(RGB.bg, -1.0, 2.0 / 3.0) : float4(RGB.gb, 0.0, -1.0 / 3.0);
+ float4 Q = (RGB.r < P.x) ? float4(P.xyw, RGB.r) : float4(RGB.r, P.yzx);
+ float C = Q.x - min(Q.w, Q.y);
+ float H = abs((Q.w - Q.y) / (6 * C + Epsilon) + Q.z);
+ return float3(H, C, Q.x);
+}
+
+float3 RGBtoHSV(in float3 RGB)
+{
+ float3 HCV = RGBtoHCV(RGB);
+ float S = HCV.y / (HCV.z + Epsilon);
+ return float3(HCV.x, S, HCV.z);
+}
+
+float3 HUEtoRGB(in float H)
+{
+ float R = abs(H * 6 - 3) - 1;
+ float G = 2 - abs(H * 6 - 2);
+ float B = 2 - abs(H * 6 - 4);
+ return saturate(float3(R, G, B));
+}
+
+float3 HSVtoRGB(in float3 HSV)
+{
+ float3 RGB = HUEtoRGB(HSV.x);
+ return ((RGB - 1) * HSV.y + 1) * HSV.z;
+}
+
+#endif // __POI_INC__
diff --git a/Shaders/ray_march.cginc b/Shaders/ray_march.cginc
new file mode 100644
index 0000000..3dce6dd
--- /dev/null
+++ b/Shaders/ray_march.cginc
@@ -0,0 +1,142 @@
+#ifndef __RAY_MARCH_INC__
+#define __RAY_MARCH_INC__
+
+#include "eyes_data.cginc"
+#include "iq_sdf.cginc"
+#include "math.cginc"
+#include "Motion.cginc"
+#include "pbr.cginc"
+#include "pema99.cginc"
+#include "poi.cginc"
+#include "stt_text.cginc"
+
+float stt_map(float3 p, out float3 hsv, out float smoothness, out float alpha, out float2 text_uv)
+{
+ hsv[0] = 0;
+ hsv[1] = 1;
+ hsv[2] = 1;
+ smoothness = 0.3;
+ alpha = 0;
+
+ float dist = 1000 * 1000 * 1000;
+ {
+ float3 pp = p;
+
+ pp.x -= 0.02;
+ pp.z -= 0.01;
+
+ float d = distance_from_box_frame(pp, float3(6, .5, 3) * .003, .0002);
+
+ alpha = (d < dist) * 1 + (d >= dist) * alpha;
+ dist = min(dist, d);
+ }
+ {
+ float3 pp = p;
+
+ pp.x -= 0.02;
+ pp.z -= 0.01;
+
+ float3 box_scale = float3(.01, .0001, .005) * 1.6;
+ float d = distance_from_box(pp, box_scale);
+
+ text_uv = (clamp(pp.xz, -1 * box_scale.xz, box_scale.xz) / box_scale.xz);
+ text_uv = (text_uv + 1) / 2;
+
+ alpha = (d < dist) * 1 + (d >= dist) * alpha;
+ hsv[1] = (d < dist) * 0 + (d >= dist) * hsv[1];
+ hsv[2] = (d < dist) * 0 + (d >= dist) * hsv[2];
+ dist = min(dist, d);
+ }
+
+ return dist;
+}
+
+float3 stt_calculate_normal(in float3 p)
+{
+ // Setting a very low value makes the surface normals look noisy. In this
+ // case. that's desired, since it produces a glittery effect.
+ const float3 small_step = float3(0.0001, 0.0, 0.0);
+
+ // Calculate the 3D gradient. By definition, the gradient is orthogonal
+ // (normal) to the surface.
+ float3 hsv;
+ float smoothness;
+ float alpha;
+ float2 text_uv;
+ float gradient_x = stt_map(p + small_step.xyy, hsv, smoothness, alpha, text_uv) - stt_map(p - small_step.xyy, hsv, smoothness, alpha, text_uv);
+ float gradient_y = stt_map(p + small_step.yxy, hsv, smoothness, alpha, text_uv) - stt_map(p - small_step.yxy, hsv, smoothness, alpha, text_uv);
+ float gradient_z = stt_map(p + small_step.yyx, hsv, smoothness, alpha, text_uv) - stt_map(p - small_step.yyx, hsv, smoothness, alpha, text_uv);
+
+ float3 normal = float3(gradient_x, gradient_y, gradient_z);
+
+ return normalize(normal);
+}
+
+float4 stt_ray_march(float3 ro, float3 rd, inout v2f v2f_i, inout float depth)
+{
+ float total_distance_traveled = 0.0;
+ const float MINIMUM_HIT_DISTANCE = 1.0 / (1000 * 20);
+ const float MAXIMUM_TRACE_DISTANCE = 1000.0;
+ float3 current_position = 0;
+ float distance_to_closest = 1;
+
+ #define STT_RAY_MARCH_STEPS 32
+ float4 color = 0;
+ float metallic = 0.5;
+ float smoothness;
+ float alpha;
+ float3 hsv;
+ float2 text_uv;
+
+ for (int i = 0; i < STT_RAY_MARCH_STEPS &&
+ distance_to_closest > MINIMUM_HIT_DISTANCE &&
+ total_distance_traveled < MAXIMUM_TRACE_DISTANCE; i++)
+ {
+ current_position = ro + total_distance_traveled * rd;
+ distance_to_closest = stt_map(current_position, hsv, smoothness, alpha, text_uv);
+ total_distance_traveled += distance_to_closest;
+ }
+
+ float3 normal = stt_calculate_normal(current_position);
+ v2f_i.normal = normalize(mul(unity_ObjectToWorld, normal));
+
+ float epsilon = .005;
+ if (text_uv.x > epsilon && text_uv.x < 1 - epsilon &&
+ text_uv.y > epsilon && text_uv.y < 1 - epsilon) {
+ text_uv.y = 1.0 - text_uv.y;
+ // Make backside render left-to-right.
+ text_uv.x = lerp(text_uv.x, 1.0 - text_uv.x, (normal.y + 1) / 2);
+ hsv[0] = 0;
+ text_uv = AddMarginToUV(1.0 - text_uv, .01);
+ hsv[1] = 0;
+ hsv[2] = GetLetter(text_uv);
+ }
+
+ depth = getWorldSpaceDepth(mul(unity_ObjectToWorld, float4(current_position, 1.0)).xyz);
+ color.xyz = HSVtoRGB(hsv);
+ color.w = alpha;
+
+ depth = lerp(-1000, depth, distance_to_closest < MINIMUM_HIT_DISTANCE);
+ return lerp(0, light(v2f_i, color, metallic, smoothness), distance_to_closest < MINIMUM_HIT_DISTANCE);
+}
+
+float4 stt_ray_march(inout v2f v2f_i, inout float depth)
+{
+ float4 ray_march_color;
+ {
+ float3 camera_position = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0)).xyz;
+ float3 ro = camera_position;
+ float3 rd = normalize(mul(unity_WorldToObject, float4(v2f_i.worldPos, 1.0)).xyz - ro);
+ float3 old_normal = v2f_i.normal;
+ ray_march_color = stt_ray_march(ro, rd, v2f_i, depth);
+ //v2f_i.normal = old_normal;
+ }
+
+ float4 background_color = float4(0, 0, 0, 0);
+
+ float3 final_color = lerp(background_color.rgb, ray_march_color.rgb, ray_march_color.a);
+ return float4(final_color, 1.0);
+}
+
+#endif // __RAY_MARCH_INC__
+