diff options
| -rw-r--r-- | Demos/displacement_demo.gif | bin | 0 -> 4717920 bytes | |||
| -rw-r--r-- | README.md | 18 | ||||
| -rw-r--r-- | Shaders/displacement/displacement.shader | 87 | ||||
| -rw-r--r-- | Shaders/displacement/displacement_lighting.cginc | 208 | ||||
| -rw-r--r-- | Shaders/displacement/iq_sdf.cginc | 102 | ||||
| -rw-r--r-- | Shaders/displacement/math.cginc | 87 | ||||
| -rw-r--r-- | Shaders/displacement/motion.cginc | 90 | ||||
| -rw-r--r-- | Shaders/displacement/pbr.cginc | 93 | ||||
| -rw-r--r-- | Shaders/displacement/pema99.cginc | 36 | ||||
| -rw-r--r-- | Shaders/displacement/poi.cginc | 60 | ||||
| -rw-r--r-- | Shaders/displacement/shadertoy.cginc | 36 | ||||
| -rw-r--r-- | Textures/geometry_noise.png | bin | 0 -> 1796080 bytes | |||
| -rw-r--r-- | Textures/geometry_noise_mask.png | bin | 0 -> 453564 bytes |
13 files changed, 817 insertions, 0 deletions
diff --git a/Demos/displacement_demo.gif b/Demos/displacement_demo.gif Binary files differnew file mode 100644 index 0000000..90ba0d0 --- /dev/null +++ b/Demos/displacement_demo.gif @@ -20,3 +20,21 @@ In these demos, I have the shader on a quad. Full demo video [here](https://youtu.be/WvPdqxmrZzI). +## Displacement + + + +A simple displacement shader. + +* Displacement height is specified with a texture. +* Height texture can be translated at configurable X/Y speeds, giving a + flowy effect. +* Height can be masked to concentrate height mapping into specific areas. +* A built-in center-out effect is available. +* Basic physically-based shading is implemented (albedo with alpha, normal, + roughness, metallic, cubemap). + +In these demos, I have the shader on a 100x100 plane. + +Full demo video [here](https://youtu.be/Giui4aCjtI0). + diff --git a/Shaders/displacement/displacement.shader b/Shaders/displacement/displacement.shader new file mode 100644 index 0000000..3c55ef3 --- /dev/null +++ b/Shaders/displacement/displacement.shader @@ -0,0 +1,87 @@ +Shader "yum_food/displacement" +{ + Properties + { + _BaseColor("Base color", 2D) = "white" {} + _Normal("Normal", 2D) = "bump" {} + _Metallic("Metallic", 2D) = "black" {} + _Roughness("Roughness", 2D) = "black" {} + _Cubemap("Cubemap", Cube) = "" {} + + _Height("Height", 2D) = "black" {} + _Height_LOD("Height LOD", float) = 8.0 + _Height_Scale("Height scale", float) = 1.0 + _Height_Exponent("Height exponent", float) = 1.0 + _Height_Speed_X("Height speed (X axis)", float) = 0.0 + _Height_Speed_Y("Height speed (Y axis)", float) = 0.0 + _Height_AA_Sample_Scale("Height anti-alias UV scale", float) = 0.001 + + _Height_Mask("Height mask", 2D) = "white" {} + _Height_Mask_Exponent("Height mask exponent", float) = 1.0 + + _Center_Out_Speed("Center out speed", float) = 0.0 + _Center_Out_Sharpness("Center out sharpness", float) = 4.0 + _Center_Out_Min_Radius("Center out min radius", float) = -1.0 + _Center_Out_Max_Radius("Center out max radius", float) = 2.7 + } + SubShader + { + Pass { + Tags { + "RenderType"="Opaque" + "Queue"="AlphaTest+499" + "LightMode" = "ForwardBase" + } + Blend SrcAlpha OneMinusSrcAlpha + ZWrite On + ZTest LEqual + Cull Off + + CGPROGRAM + #pragma target 5.0 + + #pragma multi_compile _ VERTEXLIGHT_ON + + #pragma vertex vert + #pragma geometry geom + #pragma fragment frag + + #define FORWARD_BASE_PASS + + // Three anti-aliasing levels. + // 0: no anti-aliasing + // 1: sample 4 neighbors (diagonals) + // 2: sample 8 neighbors (diagonals + cartesian) + #define OFFSET_AA_LEVEL 2 + + #include "displacement_lighting.cginc" + ENDCG + } + Pass { + Tags { + "RenderType" = "Opaque" + "LightMode" = "ForwardAdd" + "Queue"="AlphaTest+499" + } + Blend One One + ZWrite On + ZTest LEqual + Cull Off + + CGPROGRAM + #pragma target 5.0 + + #pragma multi_compile_fwdadd + + #pragma vertex vert + #pragma geometry geom + #pragma fragment frag + + #define OFFSET_AA_LEVEL 2 + + #include "displacement_lighting.cginc" + ENDCG + } + } +} + diff --git a/Shaders/displacement/displacement_lighting.cginc b/Shaders/displacement/displacement_lighting.cginc new file mode 100644 index 0000000..efd9255 --- /dev/null +++ b/Shaders/displacement/displacement_lighting.cginc @@ -0,0 +1,208 @@ +#ifndef DISPLACEMENT_LIGHTING +#define DISPLACEMENT_LIGHTING + +#include "AutoLight.cginc" +#include "iq_sdf.cginc" +#include "math.cginc" +#include "motion.cginc" +#include "pbr.cginc" +#include "poi.cginc" +#include "shadertoy.cginc" + +sampler2D _BaseColor; +sampler2D _Normal; +sampler2D _Metallic; +sampler2D _Roughness; + +sampler2D _Height; +float _Height_LOD; +float _Height_Exponent; +float _Height_Scale; +float _Height_Speed_X; +float _Height_Speed_Y; +float _Height_AA_Sample_Scale; + +sampler2D _Height_Mask; +float _Height_Mask_Exponent; + +float _Center_Out_Speed; +float _Center_Out_Sharpness; +float _Center_Out_Min_Radius; +float _Center_Out_Max_Radius; + +struct appdata +{ + float4 position : POSITION; + float2 uv : TEXCOORD0; + float3 normal : NORMAL; +}; + +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.objPos = v.position; + o.clipPos = UnityObjectToClipPos(v.position); + o.worldPos = mul(unity_ObjectToWorld, v.position); + + o.normal = UnityObjectToWorldNormal(v.normal); + o.uv = 1.0 - v.uv; + + getVertexLightColor(o); + + return o; +} + +void displace(inout v2f vert) +{ + 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 + ); + + // Manipulate vertex in world space. + float3 pp = 0; + + float2 traveling_uv = vert.uv; + traveling_uv.x += _Time[0] * _Height_Speed_X; + traveling_uv.y += _Time[0] * _Height_Speed_Y; + traveling_uv = glsl_mod(traveling_uv, 1.0); + + const float duv = _Height_AA_Sample_Scale; + + float z0 = tex2Dlod(_Height, float4(traveling_uv.x, traveling_uv.y, _Height_LOD, 0)); + #if OFFSET_AA_LEVEL >= 1 + float z1 = tex2Dlod(_Height, float4(traveling_uv.x + duv, traveling_uv.y + duv, _Height_LOD, 0)); + float z2 = tex2Dlod(_Height, float4(traveling_uv.x + duv, traveling_uv.y - duv, _Height_LOD, 0)); + float z3 = tex2Dlod(_Height, float4(traveling_uv.x - duv, traveling_uv.y - duv, _Height_LOD, 0)); + float z4 = tex2Dlod(_Height, float4(traveling_uv.x - duv, traveling_uv.y + duv, _Height_LOD, 0)); + #endif + #if OFFSET_AA_LEVEL >= 2 + float z5 = tex2Dlod(_Height, float4(traveling_uv.x + duv, traveling_uv.y, _Height_LOD, 0)); + float z6 = tex2Dlod(_Height, float4(traveling_uv.x - duv, traveling_uv.y, _Height_LOD, 0)); + float z7 = tex2Dlod(_Height, float4(traveling_uv.x, traveling_uv.y + duv, _Height_LOD, 0)); + float z8 = tex2Dlod(_Height, float4(traveling_uv.x, traveling_uv.y - duv, _Height_LOD, 0)); + #endif + + #if OFFSET_AA_LEVEL == 0 + pp.z += z0; + #elif OFFSET_AA_LEVEL == 1 + pp.z += (z0 + z1 + z2 + z3 + z4) / 5.0; + #elif OFFSET_AA_LEVEL == 2 + pp.z += (z0 + z1 + z2 + z3 + z4 + z5 + z6 + z7 + z8) / 9.0; + #endif + + pp.z = pow(pp.z, _Height_Exponent); + pp.z *= _Height_Scale; + + { + float mask = tex2Dlod(_Height_Mask, float4(vert.uv, _Height_LOD, 0)); + mask = pow(mask, _Height_Mask_Exponent); + pp.z *= mask; + } + + // 0 at middle, 1 or -1 at edges + if (_Center_Out_Speed > 0.0) { + float2 middle_out_uv = vert.uv * 2.0 - 1.0; + + float center_dist2 = length2(middle_out_uv); + float ring_radius = fmod(_Time[1] * _Center_Out_Speed, + _Center_Out_Max_Radius - _Center_Out_Min_Radius) + + _Center_Out_Min_Radius; + + // How far am I from the desired ring? + float ring_dist = dabs(center_dist2 - ring_radius, _Center_Out_Sharpness); + float ring_scale = exp(-1.0 * ring_dist); + + float middle_out_height = ring_scale; + pp.z *= middle_out_height; + } + + pp = mul(object_rotate, float4(pp, 1.0)).xyz; + + vert.worldPos += pp; + vert.objPos = mul(unity_WorldToObject, float4(vert.worldPos, 1.0)); + vert.clipPos = UnityObjectToClipPos(vert.objPos); +} + +// 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) +{ + float dx = 0.5; + + v2f cur = tri_in[0]; + displace(cur); + tri_out.Append(cur); + + cur = tri_in[1]; + displace(cur); + tri_out.Append(cur); + + cur = tri_in[2]; + displace(cur); + tri_out.Append(cur); + + tri_out.RestartStrip(); +} + +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 effect(inout v2f i, out float depth) +{ + depth = -1000.0; + + float4 albedo = tex2D(_BaseColor, i.uv); + float3 normal = tex2D(_Normal, i.uv); + float metallic = tex2D(_Metallic, i.uv); + float roughness = tex2D(_Roughness, i.uv); + if (albedo.a > 0) { + depth = getWorldSpaceDepth(i.worldPos); + } + return getLitColor(i, albedo, i.worldPos, normal, metallic, 1.0 - roughness, + /*custom_cubemap=*/true); + + return 1; +} + +fixed4 frag(v2f i, out float depth : SV_DepthLessEqual) : SV_Target +{ + return effect(i, depth); +} + +#endif // DISPLACEMENT_LIGHTING + diff --git a/Shaders/displacement/iq_sdf.cginc b/Shaders/displacement/iq_sdf.cginc new file mode 100644 index 0000000..472256c --- /dev/null +++ b/Shaders/displacement/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/displacement/math.cginc b/Shaders/displacement/math.cginc new file mode 100644 index 0000000..c8fdf13 --- /dev/null +++ b/Shaders/displacement/math.cginc @@ -0,0 +1,87 @@ +#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); +} + +float dabs(float a, float k) +{ + return log2(exp2(k * a) + exp2(-1.0 * k * a)); +} + +// 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; +} + +float length2(float2 p) +{ + return p.x * p.x + p.y * p.y; +} + +// 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/displacement/motion.cginc b/Shaders/displacement/motion.cginc new file mode 100644 index 0000000..d6458e9 --- /dev/null +++ b/Shaders/displacement/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/displacement/pbr.cginc b/Shaders/displacement/pbr.cginc new file mode 100644 index 0000000..e552bcc --- /dev/null +++ b/Shaders/displacement/pbr.cginc @@ -0,0 +1,93 @@ +#ifndef __PBR_INC +#define __PBR_INC + +#include "UnityPBSLighting.cginc" + +// xdd +UNITY_DECLARE_TEXCUBE(_Cubemap); + +struct v2f +{ + float4 clipPos : SV_POSITION; + float2 uv : TEXCOORD0; + float3 normal : TEXCOORD1; + float3 worldPos : TEXCOORD2; + float3 objPos : TEXCOORD3; + + #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; +} + +float4 getLitColor(v2f i, float4 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); + + float3 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 float4(saturate(pbr), albedo.a); +} + +#endif // __PBR_INC diff --git a/Shaders/displacement/pema99.cginc b/Shaders/displacement/pema99.cginc new file mode 100644 index 0000000..1c4b746 --- /dev/null +++ b/Shaders/displacement/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/displacement/poi.cginc b/Shaders/displacement/poi.cginc new file mode 100644 index 0000000..5e31a42 --- /dev/null +++ b/Shaders/displacement/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/displacement/shadertoy.cginc b/Shaders/displacement/shadertoy.cginc new file mode 100644 index 0000000..b19193d --- /dev/null +++ b/Shaders/displacement/shadertoy.cginc @@ -0,0 +1,36 @@ +#ifndef __SHADERTOY_INC +#define __SHADERTOY_INC + +#include "AutoLight.cginc" +#include "pema99.cginc" +#include "poi.cginc" + +// https://www.shadertoy.com/view/3ttSzr +void effect_crumpled_wave( out float4 fragColor, in float2 uv ){ + for(float i = 1.0; i < 8.0; i++) { + uv.y += i * 0.1 / i * + sin(uv.x * i * i + _Time[3] * 0.5) * + sin(uv.y * i * i + _Time[3] * 0.5); + } + + float3 col; + col.r = uv.y - 0.1; + col.g = uv.y + 0.3; + col.b = uv.y + 0.95; + + col = RGBtoHSV(col); + + float x_diff = 0.50 - col.x; + col.x = 0.80 + x_diff * 0.5; + col.x += uv.x / 16.0 + .05; + + col.y *= 0.8; + col.z *= 0.7; // Darken + + col.x = glsl_mod(col.x, 1.0); + col = HSVtoRGB(col); + + fragColor = float4(col,1.0); +} + +#endif // __SHADERTOY_INC diff --git a/Textures/geometry_noise.png b/Textures/geometry_noise.png Binary files differnew file mode 100644 index 0000000..d09f378 --- /dev/null +++ b/Textures/geometry_noise.png diff --git a/Textures/geometry_noise_mask.png b/Textures/geometry_noise_mask.png Binary files differnew file mode 100644 index 0000000..cd82701 --- /dev/null +++ b/Textures/geometry_noise_mask.png |
