summaryrefslogtreecommitdiffstats
path: root/Shaders
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2023-04-09 17:30:30 -0700
committeryum <yum.food.vr@gmail.com>2023-04-09 18:20:15 -0700
commit40f81f1dcd4a3055244c17b08f707a04b30c82a4 (patch)
tree37e6c9ac09e0026f5728d44c0e6793d873524856 /Shaders
Add parallax shader
Add a "simple" parallax shader based on raymarching. Features: * Three parallax planes. * Each plane can be textured with PBR textures. * Base color with alpha, normal, roughness, and metallic are implemented. * Each plane has these configurable properties: * Depth. Higher values create stronger parallax. * X and Y scale. * Emission strength.
Diffstat (limited to 'Shaders')
-rw-r--r--Shaders/parallax/iq_sdf.cginc102
-rw-r--r--Shaders/parallax/math.cginc77
-rw-r--r--Shaders/parallax/motion.cginc90
-rw-r--r--Shaders/parallax/parallax.shader88
-rw-r--r--Shaders/parallax/parallax_lighting.cginc339
-rw-r--r--Shaders/parallax/pbr.cginc92
-rw-r--r--Shaders/parallax/pema99.cginc36
-rw-r--r--Shaders/parallax/poi.cginc55
8 files changed, 879 insertions, 0 deletions
diff --git a/Shaders/parallax/iq_sdf.cginc b/Shaders/parallax/iq_sdf.cginc
new file mode 100644
index 0000000..472256c
--- /dev/null
+++ b/Shaders/parallax/iq_sdf.cginc
@@ -0,0 +1,102 @@
+#include "pema99.cginc"
+
+// The MIT License
+// Copyright © 2019-2021 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, float r)
+{
+ return length(p) - 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;
+}
+
+float3 op_rep(in float3 p, in float3 c)
+{
+ return glsl_mod(p+0.5*c,c)-0.5*c;
+}
+
+// End licensed section
diff --git a/Shaders/parallax/math.cginc b/Shaders/parallax/math.cginc
new file mode 100644
index 0000000..68ff9a1
--- /dev/null
+++ b/Shaders/parallax/math.cginc
@@ -0,0 +1,77 @@
+#ifndef __MATH_INC
+#define __MATH_INC
+
+#include "pema99.cginc"
+
+// 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 frac(sin(dot(p, float2(561.0, 885.0))) * 776.2) / 2.0;
+}
+
+// Generate a random number on [0, 1].
+float rand3(float3 p)
+{
+ return frac(sin(dot(p, float3(897.0, 367.0, 197.0))) * 1073.6) / 2.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(frac(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;
+ }
+ return res;
+}
+
+#endif // __MATH_INC
+
diff --git a/Shaders/parallax/motion.cginc b/Shaders/parallax/motion.cginc
new file mode 100644
index 0000000..d6458e9
--- /dev/null
+++ b/Shaders/parallax/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/parallax/parallax.shader b/Shaders/parallax/parallax.shader
new file mode 100644
index 0000000..662a59e
--- /dev/null
+++ b/Shaders/parallax/parallax.shader
@@ -0,0 +1,88 @@
+Shader "yum_food/parallax"
+{
+ Properties
+ {
+ _Curvature_Step("Curvature step", float) = .0001
+ _Min_Hit_Dist("Min hit distance", float) = .001
+ _Max_Dist("Max ray length", float) = 100.0
+ _Ray_March_Steps("Ray march steps", float) = 32
+ _Cubemap("Cubemap", Cube) = "" {}
+
+ _Layer0_BaseColor("Layer 0 base color", 2D) = "black" {}
+ _Layer0_Normal("Layer 0 normal", 2D) = "bump" {}
+ _Layer0_Metallic("Layer 0 metallic", 2D) = "black" {}
+ _Layer0_Roughness("Layer 0 roughness", 2D) = "black" {}
+ _Layer0_Offset("Layer 0 offset", float) = -0.1
+ _Layer0_XScale("Layer 0 X Scale", float) = 1.0
+ _Layer0_YScale("Layer 0 Y Scale", float) = 1.0
+ _Layer0_Emission("Layer 0 Emission strength", float) = 0.0
+
+ _Layer1_BaseColor("Layer 1 base color", 2D) = "black" {}
+ _Layer1_Normal("Layer 1 normal", 2D) = "bump" {}
+ _Layer1_Metallic("Layer 1 metallic", 2D) = "black" {}
+ _Layer1_Roughness("Layer 1 roughness", 2D) = "black" {}
+ _Layer1_Offset("Layer 1 offset", float) = -0.2
+ _Layer1_XScale("Layer 1 X Scale", float) = 1.0
+ _Layer1_YScale("Layer 1 Y Scale", float) = 1.0
+ _Layer1_Emission("Layer 1 Emission strength", float) = 0.0
+
+ _Layer2_BaseColor("Layer 2 base color", 2D) = "black" {}
+ _Layer2_Normal("Layer 2 normal", 2D) = "bump" {}
+ _Layer2_Metallic("Layer 2 metallic", 2D) = "black" {}
+ _Layer2_Roughness("Layer 2 roughness", 2D) = "black" {}
+ _Layer2_Offset("Layer 2 offset", float) = -0.3
+ _Layer2_XScale("Layer 2 X Scale", float) = 1.0
+ _Layer2_YScale("Layer 2 Y Scale", float) = 1.0
+ _Layer2_Emission("Layer 2 Emission strength", float) = 0.0
+ }
+ SubShader
+ {
+ Pass {
+ Tags {
+ "RenderType"="Opaque"
+ "Queue"="AlphaTest+499"
+ "LightMode" = "ForwardBase"
+ }
+ Blend SrcAlpha OneMinusSrcAlpha
+ ZWrite On
+ ZTest LEqual
+ Cull Back
+
+ CGPROGRAM
+ #pragma target 5.0
+
+ #pragma multi_compile _ VERTEXLIGHT_ON
+
+ #pragma vertex vert
+ #pragma fragment frag
+
+ #define FORWARD_BASE_PASS
+
+ #include "parallax_lighting.cginc"
+ ENDCG
+ }
+ Pass {
+ Tags {
+ "RenderType" = "Opaque"
+ "LightMode" = "ForwardAdd"
+ "Queue"="AlphaTest+499"
+ }
+ Blend One One
+ ZWrite On
+ ZTest LEqual
+ Cull Back
+
+ CGPROGRAM
+ #pragma target 5.0
+
+ #pragma multi_compile_fwdadd
+
+ #pragma vertex vert
+ #pragma fragment frag
+
+ #include "parallax_lighting.cginc"
+ ENDCG
+ }
+ }
+}
+
diff --git a/Shaders/parallax/parallax_lighting.cginc b/Shaders/parallax/parallax_lighting.cginc
new file mode 100644
index 0000000..2d7386f
--- /dev/null
+++ b/Shaders/parallax/parallax_lighting.cginc
@@ -0,0 +1,339 @@
+#ifndef PARALLAX_LIGHTING
+#define PARALLAX_LIGHTING
+
+#include "AutoLight.cginc"
+#include "iq_sdf.cginc"
+#include "math.cginc"
+#include "motion.cginc"
+#include "pbr.cginc"
+#include "poi.cginc"
+
+struct appdata
+{
+ float4 position : POSITION;
+ float2 uv : TEXCOORD0;
+ float3 normal : NORMAL;
+};
+
+float _Curvature_Step;
+float _Min_Hit_Dist;
+float _Max_Dist;
+float _Ray_March_Steps;
+
+sampler2D _Layer0_BaseColor;
+sampler2D _Layer1_BaseColor;
+sampler2D _Layer2_BaseColor;
+
+sampler2D _Layer0_Normal;
+sampler2D _Layer1_Normal;
+sampler2D _Layer2_Normal;
+
+sampler2D _Layer0_Metallic;
+sampler2D _Layer1_Metallic;
+sampler2D _Layer2_Metallic;
+
+sampler2D _Layer0_Roughness;
+sampler2D _Layer1_Roughness;
+sampler2D _Layer2_Roughness;
+
+float _Layer0_Offset;
+float _Layer0_XScale;
+float _Layer0_YScale;
+float _Layer0_Emission;
+
+float _Layer1_Offset;
+float _Layer1_XScale;
+float _Layer1_YScale;
+float _Layer1_Emission;
+
+float _Layer2_Offset;
+float _Layer2_XScale;
+float _Layer2_YScale;
+float _Layer2_Emission;
+
+void getVertexLightColor(inout v2f i)
+{
+ #if defined(VERTEXLIGHT_ON)
+ float3 light_pos = float3(unity_4LightPosX0.x, unity_4LightPosY0.x,
+ unity_4LightPosZ0.x);
+ float3 light_float = light_pos - i.worldPos;
+ float3 light_dir = normalize(light_float);
+ float ndotl = DotClamped(i.normal, light_dir);
+ // Light fills an expanding sphere with surface area 4 * pi * r^2.
+ // By conservation of energy, this means that at distance r, light intensity
+ // is proportional to 1/(r^2).
+ float attenuation = 1 / (1 + dot(light_float, light_float) * unity_4LightAtten0.x);
+ i.vertexLightColor = unity_LightColor[0].rgb * ndotl * attenuation;
+
+ i.vertexLightColor = Shade4PointLights(
+ unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
+ unity_LightColor[0].rgb,
+ unity_LightColor[1].rgb,
+ unity_LightColor[2].rgb,
+ unity_LightColor[3].rgb,
+ unity_4LightAtten0, i.worldPos, i.normal
+ );
+ #endif
+}
+
+v2f vert(appdata v)
+{
+ v2f o;
+ o.position = UnityObjectToClipPos(v.position);
+ o.worldPos = mul(unity_ObjectToWorld, v.position);
+ o.normal = UnityObjectToWorldNormal(v.normal);
+ o.uv.xy = float2(0, 0);
+ o.uv.zw = 1.0 - v.uv;
+ getVertexLightColor(o);
+ return o;
+}
+
+float getWorldSpaceDepth(in const float3 worldPos)
+{
+ float4 clip_pos = mul(UNITY_MATRIX_VP, float4(worldPos, 1.0));
+ return clip_pos.z / clip_pos.w;
+}
+
+float4 getLayerBaseColor(in float2 uv, in int layer)
+{
+ [forcecase]
+ switch (layer) {
+ case 0:
+ return tex2Dgrad(_Layer0_BaseColor, uv, ddx(uv.x), ddy(uv.y));
+ case 1:
+ return tex2Dgrad(_Layer1_BaseColor, uv, ddx(uv.x), ddy(uv.y));
+ case 2:
+ return tex2Dgrad(_Layer2_BaseColor, uv, ddx(uv.x), ddy(uv.y));
+ default:
+ return 0.0;
+ }
+}
+
+float3 getLayerNormal(in float2 uv, in int layer)
+{
+ [forcecase]
+ switch (layer) {
+ case 0:
+ return tex2Dgrad(_Layer0_Normal, uv, ddx(uv.x), ddy(uv.y));
+ case 1:
+ return tex2Dgrad(_Layer1_Normal, uv, ddx(uv.x), ddy(uv.y));
+ case 2:
+ return tex2Dgrad(_Layer2_Normal, uv, ddx(uv.x), ddy(uv.y));
+ default:
+ return 0.0;
+ }
+}
+
+float getLayerMetallic(in float2 uv, in int layer)
+{
+ [forcecase]
+ switch (layer) {
+ case 0:
+ return tex2Dgrad(_Layer0_Metallic, uv, ddx(uv.x), ddy(uv.y));
+ case 1:
+ return tex2Dgrad(_Layer1_Metallic, uv, ddx(uv.x), ddy(uv.y));
+ case 2:
+ return tex2Dgrad(_Layer2_Metallic, uv, ddx(uv.x), ddy(uv.y));
+ default:
+ return 0.0;
+ }
+}
+
+float getLayerRoughness(in float2 uv, in int layer)
+{
+ [forcecase]
+ switch (layer) {
+ case 0:
+ return tex2Dgrad(_Layer0_Roughness, uv, ddx(uv.x), ddy(uv.y));
+ case 1:
+ return tex2Dgrad(_Layer1_Roughness, uv, ddx(uv.x), ddy(uv.y));
+ case 2:
+ return tex2Dgrad(_Layer2_Roughness, uv, ddx(uv.x), ddy(uv.y));
+ default:
+ return 0.0;
+ }
+}
+
+float getLayerOffset(in int layer)
+{
+ [forcecase]
+ switch (layer) {
+ case 0:
+ return _Layer0_Offset;
+ case 1:
+ return _Layer1_Offset;
+ case 2:
+ return _Layer2_Offset;
+ default:
+ return 0.0;
+ }
+}
+
+float getLayerXScale(in int layer)
+{
+ [forcecase]
+ switch (layer) {
+ case 0:
+ return _Layer0_XScale;
+ case 1:
+ return _Layer1_XScale;
+ case 2:
+ return _Layer2_XScale;
+ default:
+ return 0.0;
+ }
+}
+
+float getLayerYScale(in int layer)
+{
+ [forcecase]
+ switch (layer) {
+ case 0:
+ return _Layer0_YScale;
+ case 1:
+ return _Layer1_YScale;
+ case 2:
+ return _Layer2_YScale;
+ default:
+ return 0.0;
+ }
+}
+
+float getLayerEmission(in int layer)
+{
+ [forcecase]
+ switch (layer) {
+ case 0:
+ return _Layer0_Emission;
+ case 1:
+ return _Layer1_Emission;
+ case 2:
+ return _Layer2_Emission;
+ default:
+ return 0.0;
+ }
+}
+
+float2 getLayerScale(in int layer)
+{
+ return float2(getLayerXScale(layer), getLayerYScale(layer));
+}
+
+float4 effect(inout v2f i, out float depth)
+{
+ float4 clip_pos = mul(UNITY_MATRIX_VP, float4(i.worldPos, 1.0));
+ depth = clip_pos.z / clip_pos.w;
+
+ float3 camera_position = _WorldSpaceCameraPos.xyz;
+ float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos);
+
+ const float3 ro = i.worldPos;
+ const float3 rd = view_dir * -1.0;
+
+ static const float MIN_D = _Min_Hit_Dist;
+ static const float MAX_D = _Max_Dist;
+
+ float4 result = float4(1.0, 1.0, 1.0, 0.0);
+
+ float foreground_depth = -100.0;
+ bool ray_hit = false;
+
+ float3 object_offset = transpose(unity_ObjectToWorld)[3].xyz;
+
+ float4x4 object_rotate = float4x4(
+ unity_ObjectToWorld[0].xyz, 0,
+ unity_ObjectToWorld[1].xyz, 0,
+ unity_ObjectToWorld[2].xyz, 0,
+ 0, 0, 0, 1
+ );
+ float3 object_scale_vec = float3(
+ length(float3(unity_ObjectToWorld[0].x, unity_ObjectToWorld[1].x, unity_ObjectToWorld[2].x)),
+ length(float3(unity_ObjectToWorld[0].y, unity_ObjectToWorld[1].y, unity_ObjectToWorld[2].y)),
+ length(float3(unity_ObjectToWorld[0].z, unity_ObjectToWorld[1].z, unity_ObjectToWorld[2].z))
+ );
+ float object_scale = min(object_scale_vec.x, min(object_scale_vec.y, object_scale_vec.z));
+
+ for (float layer = 3 - 1; layer >= 0; layer--) {
+ bool shade_out = false;
+ float3 layer_offset = float3(0.0, 0.0, 1.0) * getLayerOffset(layer) * object_scale;
+ float2 layer_scale = getLayerScale(layer);
+
+ float cum_dist = 0.0;
+ float3 bbox = float3(1.0, 1.0, 0.0) * 0.2;
+ float3 pp;
+ for (int ii = 0; ii < _Ray_March_Steps; ii++) {
+ pp = ro + rd * cum_dist - object_offset;
+ pp = mul(transpose(object_rotate), float4(pp, 1.0)).xyz;
+ pp /= object_scale_vec;
+
+ const float3 off = layer_offset;
+ pp += off;
+
+ pp.xy /= layer_scale;
+
+ float d0 = distance_from_box(pp / object_scale, bbox) * object_scale;
+ if (d0 < MIN_D) {
+ foreground_depth = getWorldSpaceDepth(pp);
+ shade_out = true;
+ ray_hit = true;
+ break;
+ }
+ cum_dist += d0;
+ if (cum_dist >= MAX_D) {
+ break;
+ }
+ }
+ if (shade_out) {
+ float2 uv = pp.xy;
+ uv /= (bbox.xy * 2.0) * object_scale;
+ uv.x += 0.5;
+ uv.y += 0.5;
+ // Render textures left-to-right when looking in the mirror.
+ if (isInMirror()) {
+ uv.x = 1.0 - uv.x;
+ }
+ uv = clamp(uv, 0.0, 1.0);
+
+ float4 color = getLayerBaseColor(uv, (int) layer);
+ if (color.a < 0.9) {
+ color.a = 0.0;
+ }
+ const float3 normal = getLayerNormal(uv, (int) layer);
+ const float metallic = getLayerMetallic(uv, (int) layer);
+ const float smoothness = 1.0 - getLayerRoughness(uv, (int) layer);
+
+ bool custom_cubemap = (layer == 1);
+ float4 lit_color = getLitColor(i, color, pp, normal, metallic, smoothness, custom_cubemap);
+
+ lit_color.rgb += color.rgb * getLayerEmission(layer);
+
+ result.rgb = lerp(result.rgb, lit_color.rgb, lit_color.a);
+
+ float a_remainder = abs(result.a - color.a);
+ float a_increment = a_remainder * color.a;
+ result.a += a_increment;
+ }
+ }
+
+
+ // No ray hit: return default material
+ float base_depth = depth;
+ float metallic = 1.0;
+ float smoothness = 0.5;
+ bool custom_cubemap = true;
+ float4 bg_color = float4(1.0, 1.0, 1.0, 1.0);
+ bg_color = getLitColor(i, bg_color, i.worldPos, i.normal, metallic, smoothness, custom_cubemap);
+
+ result.rgb = lerp(bg_color.rgb, result.rgb, result.a);
+ result.a = 1.0;
+
+ return result;
+}
+
+fixed4 frag(v2f i, out float depth : SV_DepthLessEqual) : SV_Target
+{
+ return effect(i, depth);
+}
+
+#endif // PARALLAX_LIGHTING
+
diff --git a/Shaders/parallax/pbr.cginc b/Shaders/parallax/pbr.cginc
new file mode 100644
index 0000000..e5548c7
--- /dev/null
+++ b/Shaders/parallax/pbr.cginc
@@ -0,0 +1,92 @@
+#ifndef __PBR_INC
+#define __PBR_INC
+
+#include "UnityPBSLighting.cginc"
+
+// xdd
+UNITY_DECLARE_TEXCUBE(_Cubemap);
+
+struct v2f
+{
+ float4 position : SV_POSITION;
+ float4 uv : TEXCOORD0;
+ float3 normal : TEXCOORD1;
+ float3 worldPos : TEXCOORD2;
+
+ #if defined(VERTEXLIGHT_ON)
+ float3 vertexLightColor : TEXCOORD3;
+ #endif
+};
+
+UnityLight GetLight(v2f i, float3 worldPos, float3 normal)
+{
+ UNITY_LIGHT_ATTENUATION(attenuation, 0, 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 - worldPos);
+ #else
+ light.dir = _WorldSpaceLightPos0.xyz;
+ #endif
+ light.ndotl = DotClamped(normal, light.dir);
+
+ return light;
+}
+
+UnityIndirect GetIndirect(v2f i, float3 view_dir, float3 normal,
+ float smoothness, bool custom_cubemap) {
+ 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(normal, 1)));
+ float3 reflect_dir = reflect(-view_dir, normal);
+ // There's a nonlinear relationship between mipmap level and roughness.
+ float roughness = 1 - smoothness;
+ roughness *= 1.7 - .7 * roughness;
+ if (custom_cubemap) {
+ float3 env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
+ _Cubemap,
+ reflect_dir,
+ roughness * UNITY_SPECCUBE_LOD_STEPS);
+ indirect.specular = env_sample;
+ } else {
+ float3 env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
+ unity_SpecCube0,
+ reflect_dir,
+ roughness * UNITY_SPECCUBE_LOD_STEPS);
+ indirect.specular = env_sample;
+ }
+ #endif
+
+ return indirect;
+}
+
+fixed4 getLitColor(v2f i, fixed4 albedo, float3 worldPos, float3 normal,
+ float metallic, float smoothness,
+ bool custom_cubemap)
+{
+ float3 specular_tint;
+ float one_minus_reflectivity;
+ albedo.rgb = DiffuseAndSpecularFromMetallic(
+ albedo, metallic, specular_tint, one_minus_reflectivity);
+
+ float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos);
+
+ fixed3 pbr = UNITY_BRDF_PBS(albedo, specular_tint,
+ one_minus_reflectivity, smoothness,
+ view_dir, normal,
+ GetLight(i, worldPos, normal),
+ GetIndirect(i, view_dir, normal, smoothness, custom_cubemap)).rgb;
+
+ return fixed4(saturate(pbr), albedo.a);
+}
+
+#endif // __PBR_INC
diff --git a/Shaders/parallax/pema99.cginc b/Shaders/parallax/pema99.cginc
new file mode 100644
index 0000000..1c4b746
--- /dev/null
+++ b/Shaders/parallax/pema99.cginc
@@ -0,0 +1,36 @@
+/*
+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 __PEMA_99_INC
+#define __PEMA_99_INC
+
+#define glsl_mod(x,y) (((x)-(y)*floor((x)/(y))))
+
+bool isInMirror()
+{
+ return unity_CameraProjection[2][0] != 0.f || unity_CameraProjection[2][1]
+ != 0.f;
+}
+
+#endif // __PEMA_99_INC
diff --git a/Shaders/parallax/poi.cginc b/Shaders/parallax/poi.cginc
new file mode 100644
index 0000000..aaf6e5c
--- /dev/null
+++ b/Shaders/parallax/poi.cginc
@@ -0,0 +1,55 @@
+/*
+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;
+}