From 273a3ab97da0aa543e970ce5c45908ee89c2f632 Mon Sep 17 00:00:00 2001 From: yum Date: Fri, 14 Apr 2023 18:07:52 -0700 Subject: Add avatar cloning shader See README.md. --- Demos/av_clone_demo.gif | Bin 0 -> 4179758 bytes README.md | 31 ++- Shaders/avatar_clone/avatar_clone.shader | 96 +++++++ Shaders/avatar_clone/avatar_clone_lighting.cginc | 309 +++++++++++++++++++++++ Shaders/avatar_clone/iq_sdf.cginc | 102 ++++++++ Shaders/avatar_clone/math.cginc | 87 +++++++ Shaders/avatar_clone/motion.cginc | 90 +++++++ Shaders/avatar_clone/pbr.cginc | 94 +++++++ Shaders/avatar_clone/pema99.cginc | 36 +++ Shaders/avatar_clone/poi.cginc | 60 +++++ Shaders/avatar_clone/shadertoy.cginc | 36 +++ 11 files changed, 939 insertions(+), 2 deletions(-) create mode 100644 Demos/av_clone_demo.gif create mode 100644 Shaders/avatar_clone/avatar_clone.shader create mode 100644 Shaders/avatar_clone/avatar_clone_lighting.cginc create mode 100644 Shaders/avatar_clone/iq_sdf.cginc create mode 100644 Shaders/avatar_clone/math.cginc create mode 100644 Shaders/avatar_clone/motion.cginc create mode 100644 Shaders/avatar_clone/pbr.cginc create mode 100644 Shaders/avatar_clone/pema99.cginc create mode 100644 Shaders/avatar_clone/poi.cginc create mode 100644 Shaders/avatar_clone/shadertoy.cginc diff --git a/Demos/av_clone_demo.gif b/Demos/av_clone_demo.gif new file mode 100644 index 0000000..dcdd153 Binary files /dev/null and b/Demos/av_clone_demo.gif differ diff --git a/README.md b/README.md index 882a6c0..064113c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Then assign the shader you want (like yum\_food/parallax) to a material. Please ask setup questions [on the discord](https://discord.gg/YWmCvbCRyn). -## Parallax +## Parallax (`yum_food/parallax`) ![Parallax demo](Demos/parallax_demo.gif) @@ -20,7 +20,7 @@ In these demos, I have the shader on a quad. Full demo video [here](https://youtu.be/WvPdqxmrZzI). -## Displacement +## Displacement (`yum_food/displacement`) ![Displacement demo](Demos/displacement_demo.gif) @@ -38,3 +38,30 @@ In these demos, I have the shader on a 100x100 plane. Full demo video [here](https://youtu.be/Giui4aCjtI0). +## Avatar cloning (`yum_food/avatar_clone`) + +![Avatar cloning demo](Demos/av_clone_demo.gif) + +A cloning system using a geometry shader. + +* Same features as the displacement shader. +* Spawn up to 4 clones at controllable x/y/z offsets (in meters). +* Clones spawn on the left or right side, in alternating order. + +To use it in game: +* Assign the shader to all material slots you want to clone. +* Assign textures. +* Create 4 animations: + * Set number of clones to 0. + * Set number of clones to 4. + * Set clone X offset to 0. + * Set clone X offset to 1. +* Create 2 float params: + * `Clone_Count`: controls number of clones. + * `Clone_dx`: controls each clone's X axis offset. +* Create 2 layers in your FX layer: + * One uses `Clone_Count` to blend between 0 and 4 clones. + * One uses `Clone_dx` to blend between 0 and 1 x offset. +* Create two radial puppet controls, one for `Clone_Count`, and one for + `Clone_dx`. + diff --git a/Shaders/avatar_clone/avatar_clone.shader b/Shaders/avatar_clone/avatar_clone.shader new file mode 100644 index 0000000..709a301 --- /dev/null +++ b/Shaders/avatar_clone/avatar_clone.shader @@ -0,0 +1,96 @@ +Shader "yum_food/avatar_clone" +{ + Properties + { + _BaseColor("Base color", 2D) = "white" {} + _Emission("Emission", 2D) = "black" {} + _Emission_Strength("Emission strength", float) = 0.0 + _Normal("Normal", 2D) = "bump" {} + [ToggleUI] _Disable_Normal_Texture("Disable normal texture", float) = 1 + [ToggleUI] _Disable_Normal_Recalc("Disable normal recalculation", float) = 1 + _Metallic("Metallic", 2D) = "black" {} + _Roughness("Roughness", 2D) = "black" {} + _Cubemap("Cubemap", Cube) = "" {} + + _Height("Height", 2D) = "black" {} + [IntRange] _Height_LOD("Height LOD", Range(0, 8)) = 8 + _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 + + [IntRange] _Num_Clones("Number of clones", Range(0, 4)) = 0 + _Clone_dx("Clone 0 dx", float) = 1.0 + _Clone_dy("Clone 0 dy", float) = 0.0 + _Clone_dz("Clone 0 dz", 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 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 HEIGHT_AA_LEVEL 2 + + #include "avatar_clone_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 geometry geom + #pragma fragment frag + + #define HEIGHT_AA_LEVEL 2 + + #include "avatar_clone_lighting.cginc" + ENDCG + } + } +} + diff --git a/Shaders/avatar_clone/avatar_clone_lighting.cginc b/Shaders/avatar_clone/avatar_clone_lighting.cginc new file mode 100644 index 0000000..7471157 --- /dev/null +++ b/Shaders/avatar_clone/avatar_clone_lighting.cginc @@ -0,0 +1,309 @@ +#ifndef AVATAR_CLONE_LIGHTING +#define AVATAR_CLONE_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 _Emission; +float _Emission_Strength; +sampler2D _Normal; +sampler2D _Metallic; +sampler2D _Roughness; + +bool _Disable_Normal_Texture; +bool _Disable_Normal_Recalc; + +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; + +float _Num_Clones; +float _Clone_dx; +float _Clone_dy; +float _Clone_dz; + +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 = v.uv; + + getVertexLightColor(o); + + return o; +} + +float getDisplacement(float2 uv) +{ + // Manipulate vertex in world space. + float height = 0; + + float2 traveling_uv = 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)); + // Trivial optimization. No need to keep going if no displacement is desired. + if (z0 == 0) { + return 0; + } + #if HEIGHT_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 HEIGHT_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 HEIGHT_AA_LEVEL == 0 + height += z0; + #elif HEIGHT_AA_LEVEL == 1 + height += (z0 + z1 + z2 + z3 + z4) / 5.0; + #elif HEIGHT_AA_LEVEL == 2 + height += (z0 + z1 + z2 + z3 + z4 + z5 + z6 + z7 + z8) / 9.0; + #endif + + height = pow(height, _Height_Exponent); + height *= _Height_Scale; + + { + float mask = tex2Dlod(_Height_Mask, float4(uv, _Height_LOD, 0)); + mask = pow(mask, _Height_Mask_Exponent); + height *= mask; + } + + // 0 at middle, 1 or -1 at edges + if (_Center_Out_Speed > 0.0) { + float2 middle_out_uv = 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; + height *= middle_out_height; + } + + return height; +} + +void displace(inout v2f vert) +{ + float height = getDisplacement(vert.uv); + + vert.worldPos += height * vert.normal; + vert.objPos = mul(unity_WorldToObject, float4(vert.worldPos, 1.0)); + vert.clipPos = UnityObjectToClipPos(vert.objPos); +} + +float3 getNormal( + v2f v0, + v2f v1, + v2f v2, + float duv_scale) { + float2 duv_v0v1 = v1.uv - v0.uv; + float2 duv_v0v2 = v2.uv - v0.uv; + + float h0 = getDisplacement(v0.uv); + float h1 = getDisplacement(v0.uv + duv_v0v1 * duv_scale); + float h2 = getDisplacement(v0.uv + duv_v0v2 * duv_scale); + + float dh_v0v1 = h1 - h0; + float dh_v0v2 = h2 - h0; + + float3 dpos_v0v1 = v1.worldPos - v0.worldPos; + float3 dpos_v0v2 = v2.worldPos - v0.worldPos; + + float3 p0 = v0.worldPos + h0 * v0.normal; + float3 p1 = v0.worldPos + dpos_v0v1 * duv_scale + lerp(h0, h1, duv_scale) * v1.normal; + float3 p2 = v0.worldPos + dpos_v0v2 * duv_scale + lerp(h0, h2, duv_scale) * v2.normal; + + float3 tangent = normalize(p1 - p0); + float3 bitangent = normalize(p2 - p0); + float3 new_normal = normalize(cross(tangent, bitangent)); + return new_normal; +} + +// maxvertexcount == the number of vertices we create +[maxvertexcount(15)] +void geom(triangle v2f tri_in[3], + uint pid: SV_PrimitiveID, + inout TriangleStream tri_out) +{ + float dx = 0.5; + + v2f t0 = tri_in[0]; + v2f t1 = tri_in[1]; + v2f t2 = tri_in[2]; + + displace(t0); + displace(t1); + displace(t2); + + // Math from here: + // http://tonfilm.blogspot.com/2007/01/calculate-normals-in-shader.html + for (uint i = 0; i < 3; i++) { + v2f v0 = tri_in[(i + 0) % 3]; + v2f v1 = tri_in[(i + 1) % 3]; + v2f v2 = tri_in[(i + 2) % 3]; + + if (!_Disable_Normal_Recalc) { + float3 aa_normal = 0; + aa_normal += getNormal(v0, v1, v2, 0.3); + aa_normal += getNormal(v0, v1, v2, 0.4); + aa_normal += getNormal(v0, v1, v2, 0.5); + aa_normal += getNormal(v0, v1, v2, 0.6); + aa_normal += getNormal(v0, v1, v2, 0.7); + aa_normal = normalize(aa_normal); + + if (i == 0) { + t0.normal = aa_normal; + } else if (i == 1) { + t1.normal = aa_normal; + } else if (i == 2) { + t2.normal = aa_normal; + } + } + } + + tri_out.Append(t0); + tri_out.Append(t1); + tri_out.Append(t2); + tri_out.RestartStrip(); + + const int num_clones = max((int) floor(_Num_Clones), 0); + for (int i = 2; i < num_clones + 2; i++) { + v2f t0p = t0; + v2f t1p = t1; + v2f t2p = t2; + + t0p.worldPos.x += _Clone_dx * (i / 2) * (i % 2 == 0 ? 1.0 : -1.0); + t0p.worldPos.y += _Clone_dy * (i / 2); + t0p.worldPos.z += _Clone_dz * (i / 2); + + t1p.worldPos.x += _Clone_dx * (i / 2) * (i % 2 == 0 ? 1.0 : -1.0); + t1p.worldPos.y += _Clone_dy * (i / 2); + t1p.worldPos.z += _Clone_dz * (i / 2); + + t2p.worldPos.x += _Clone_dx * (i / 2) * (i % 2 == 0 ? 1.0 : -1.0); + t2p.worldPos.y += _Clone_dy * (i / 2); + t2p.worldPos.z += _Clone_dz * (i / 2); + + t0p.objPos = mul(unity_WorldToObject, float4(t0p.worldPos, 1.0)); + t0p.clipPos = UnityObjectToClipPos(t0p.objPos); + + t1p.objPos = mul(unity_WorldToObject, float4(t1p.worldPos, 1.0)); + t1p.clipPos = UnityObjectToClipPos(t1p.objPos); + + t2p.objPos = mul(unity_WorldToObject, float4(t2p.worldPos, 1.0)); + t2p.clipPos = UnityObjectToClipPos(t2p.objPos); + + tri_out.Append(t0p); + tri_out.Append(t1p); + tri_out.Append(t2p); + 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 emission = tex2D(_Emission, i.uv); + float3 normal = _Disable_Normal_Texture ? i.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); + } + + float4 lit_color = getLitColor(i, albedo, i.worldPos, normal, metallic, + 1.0 - roughness, /*custom_cubemap=*/true); + + lit_color.rgb += emission * _Emission_Strength; + + return lit_color; +} + +fixed4 frag(v2f i, out float depth : SV_DepthLessEqual) : SV_Target +{ + return effect(i, depth); +} + +#endif // AVATAR_CLONE_LIGHTING + diff --git a/Shaders/avatar_clone/iq_sdf.cginc b/Shaders/avatar_clone/iq_sdf.cginc new file mode 100644 index 0000000..472256c --- /dev/null +++ b/Shaders/avatar_clone/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.xp.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/avatar_clone/math.cginc b/Shaders/avatar_clone/math.cginc new file mode 100644 index 0000000..c8fdf13 --- /dev/null +++ b/Shaders/avatar_clone/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/avatar_clone/motion.cginc b/Shaders/avatar_clone/motion.cginc new file mode 100644 index 0000000..d6458e9 --- /dev/null +++ b/Shaders/avatar_clone/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/avatar_clone/pbr.cginc b/Shaders/avatar_clone/pbr.cginc new file mode 100644 index 0000000..690f547 --- /dev/null +++ b/Shaders/avatar_clone/pbr.cginc @@ -0,0 +1,94 @@ +#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/avatar_clone/pema99.cginc b/Shaders/avatar_clone/pema99.cginc new file mode 100644 index 0000000..1c4b746 --- /dev/null +++ b/Shaders/avatar_clone/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/avatar_clone/poi.cginc b/Shaders/avatar_clone/poi.cginc new file mode 100644 index 0000000..5e31a42 --- /dev/null +++ b/Shaders/avatar_clone/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/avatar_clone/shadertoy.cginc b/Shaders/avatar_clone/shadertoy.cginc new file mode 100644 index 0000000..b19193d --- /dev/null +++ b/Shaders/avatar_clone/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 -- cgit v1.2.3