From 8bf828da6dca1b3c763253b0a0cfda4294d69b61 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 23 Feb 2021 02:26:11 +0100 Subject: [PATCH] Added two-tiled Truchet sample shader --- .../ComputeSharp.Sample.SwapChain/Program.cs | 2 +- .../Shaders/TwoTiledTruchet.cs | 153 ++++++++++++++++++ 2 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 samples/ComputeSharp.Sample.SwapChain/Shaders/TwoTiledTruchet.cs diff --git a/samples/ComputeSharp.Sample.SwapChain/Program.cs b/samples/ComputeSharp.Sample.SwapChain/Program.cs index f9030a45..1bb260cd 100644 --- a/samples/ComputeSharp.Sample.SwapChain/Program.cs +++ b/samples/ComputeSharp.Sample.SwapChain/Program.cs @@ -7,7 +7,7 @@ namespace ComputeSharp.Sample.SwapChain { static void Main(string[] args) { - Win32ApplicationRunner.Run(new SwapChainApplication(static (texture, time) => new FractalTiling(texture, (float)time.TotalSeconds))); + Win32ApplicationRunner.Run(new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds))); } } } diff --git a/samples/ComputeSharp.Sample.SwapChain/Shaders/TwoTiledTruchet.cs b/samples/ComputeSharp.Sample.SwapChain/Shaders/TwoTiledTruchet.cs new file mode 100644 index 00000000..a9f7781a --- /dev/null +++ b/samples/ComputeSharp.Sample.SwapChain/Shaders/TwoTiledTruchet.cs @@ -0,0 +1,153 @@ +namespace ComputeSharp.Sample.SwapChain.Shaders +{ + /// + /// A shader creating an animated two-tiles Truchet arrangement. + /// Ported from . + /// Created by Shane. + /// + [AutoConstructor] + internal readonly partial struct TwoTiledTruchet : IComputeShader + { + /// + /// The target texture. + /// + public readonly IReadWriteTexture2D texture; + + /// + /// The current time since the start of the application. + /// + public readonly float time; + + // Fast hash for a pair of floats + private static float Hash21(Float2 p) + { + return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new Float2(27.619f, 57.583f))) * 43758.5453f); + } + + // HLSL's port of the GLSL mod intrinsic + private static float Mod(float x, float y) + { + return x - y * Hlsl.Floor(x / y); + } + + /// + /// Calculates the Truchet distance field. + /// + /// The input value to compute the distance field for. + /// The resulting Truchet angle. + /// The distance field for the input value. + private Float2 DistanceField(Float2 p, out Float2 ang) + { + Float2 ip2 = Hlsl.Floor(p / 2.0f); + Float2 ip = Hlsl.Floor(p); + float rnd = Hash21(ip); + float rnd3 = Hash21(ip + 0.57f); + Float2 d = 1e5f; + + p -= ip + 0.5f; + + if (Mod(ip2.X + ip2.Y, 2.0f) < 0.5f) + { + d = Hlsl.Abs(p); + ang = p.YX; + + if (Mod(ip.X + 1.0f, 2.0f) < 0.5f) + { + ang.X *= -1.0f; + + } + + if (Mod(ip.Y + 1.0f, 2.0f) > 0.5f) + { + ang.Y *= -1.0f; + } + + if (rnd3 < .5) + { + d = d.YX; + ang = ang.YX; + } + + ang += 0.5f; + ang *= 3.0f; + + } + else + { + if (rnd < .5) + { + p = p.YX * new Float2(1, -1); + } + + d.X = Hlsl.Length(p - 0.5f) - 0.5f; + d.Y = Hlsl.Length(p + 0.5f) - 0.5f; + + d = Hlsl.Abs(d); + + ang = 0; + ang.X = -Hlsl.Atan((p.Y - 0.5f) / (p.X - 0.5f)); + ang.Y = -Hlsl.Atan((p.Y + 0.5f) / (p.X + 0.5f)); + + if (Mod(ip.X + ip.Y, 2.0f) < 0.5f) + { + ang *= -1.0f; + } + + if (rnd < 0.5f) + { + ang *= -1.0f; + } + + if (rnd3 < 0.5f) + { + d = d.YX; + ang = ang.YX; + } + + ang *= 4.0f / 6.2831853f; + ang *= 2.0f; + } + + ang = Hlsl.Frac(ang + time / 4.0f); + + return d; + } + + /// + public void Execute() + { + int width = texture.Width; + int height = texture.Height; + + Float2 uv = ((Float2)ThreadIds.XY - (Float2)(new Int2(width, height)) * 0.5f) / height; + + float gSc = 7.0f; + Float2 p = uv * gSc - new Float2(-1, -0.5f) * time / 2.0f; + float sf = 2.0f / height * gSc; + float lSc = 6.0f; + float lw = 1.0f / lSc / gSc; + Float2 d = DistanceField(p, out Float2 ang) - 2.5f / lSc; + Float3 col = new Float3(1.0f, 0.9f, 0.8f); + + for (int i = 0; i < 2; i++) + { + float di = d[i] - lw / 4.0f; + float tracks = Hlsl.Clamp(Hlsl.Sin(ang[i] * 6.2831f + time * 6.0f) * 4.0f, 0.0f, 1.0f); + float gap = 1.0f + lw; + + col = Hlsl.Lerp(col, 0, (1.0f - Hlsl.SmoothStep(0.0f, sf * 6.0f, di)) * 0.35f); + col = Hlsl.Lerp(col, 0, 1.0f - Hlsl.SmoothStep(0.0f, sf, di)); + col = Hlsl.Lerp(col, new(1.0f, 0.9f, 0.8f), 1.0f - Hlsl.SmoothStep(0.0f, sf, di + lw * 2.0f)); + col = Hlsl.Lerp(col, 0, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + gap / lSc)); + col = Hlsl.Lerp(col, 1, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + gap / lSc + lw)); + col = Hlsl.Lerp(col, 0, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + 2.0f * gap / lSc)); + col = Hlsl.Lerp(col, 1 * tracks, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + 2.0f * gap / lSc + lw)); + } + + Float3 rgb = Hlsl.Sqrt(Hlsl.Max(col, 0.0f)); + Float4 color = new(rgb.X, rgb.Y, rgb.Z, 1.0f); + + texture[ThreadIds.XY] = color; + } + } +}