Added Octagrams sample shader

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-04 15:14:48 +01:00
1 親 32b194550e
コミット 3523395be0
5ファイル変更
+145 -1

検索条件に一致するファイルはありません

+2 -1
ファイルの表示
@@ -14,7 +14,8 @@ namespace ComputeSharp.SwapChain
new SwapChainApplication<ColorfulInfinity>(static (texture, time) => new(texture, (float)time.TotalSeconds)),
new SwapChainApplication<FractalTiling>(static (texture, time) => new(texture, (float)time.TotalSeconds)),
new SwapChainApplication<TwoTiledTruchet>(static (texture, time) => new(texture, (float)time.TotalSeconds)),
new SwapChainApplication<MengerJourney>(static (texture, time) => new(texture, (float)time.TotalSeconds))
new SwapChainApplication<MengerJourney>(static (texture, time) => new(texture, (float)time.TotalSeconds)),
new SwapChainApplication<Octagrams>(static (texture, time) => new(texture, (float)time.TotalSeconds))
};
static void Main()
+129
ファイルの表示
@@ -0,0 +1,129 @@
namespace ComputeSharp.SwapChain.Shaders
{
/// <summary>
/// A shader creating an octagram animation, inspired by arabesque.
/// Ported from <see href="https://www.shadertoy.com/view/tlVGDt"/>.
/// <para>Created by whisky_shusuky.</para>
/// </summary>
[AutoConstructor]
internal readonly partial struct Octagrams : IComputeShader
{
/// <summary>
/// The target texture.
/// </summary>
public readonly IReadWriteTexture2D<Float4> texture;
/// <summary>
/// The current time since the start of the application.
/// </summary>
public readonly float time;
private static Float2x2 Rotate(float a)
{
float c = Hlsl.Cos(a), s = Hlsl.Sin(a);
return new(c, s, -s, c);
}
private static float Box(Float3 pos, float scale)
{
static float SDBox(Float3 p, Float3 b)
{
Float3 q = Hlsl.Abs(p) - b;
return Hlsl.Length(Hlsl.Max(q, 0.0f)) +
Hlsl.Min(Hlsl.Max(q.X, Hlsl.Max(q.Y, q.Z)), 0.0f);
}
pos *= scale;
float b = SDBox(pos, new(0.4f, 0.4f, 0.1f)) / 1.5f;
pos.XY *= 5.0f;
pos.Y -= 3.5f;
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.75f));
return -b;
}
private static float BoxSet(Float3 pos, float gTime)
{
Float3 pos_origin = pos;
pos = pos_origin;
pos.Y += Hlsl.Sin(gTime * 0.4f) * 2.5f;
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
float box1 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
pos = pos_origin;
pos.Y -= Hlsl.Sin(gTime * 0.4f) * 2.5f;
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
float box2 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
pos = pos_origin;
pos.X += Hlsl.Sin(gTime * 0.4f) * 2.5f;
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
float box3 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
pos = pos_origin;
pos.X -= Hlsl.Sin(gTime * 0.4f) * 2.5f;
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
float box4 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
pos = pos_origin;
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
float box5 = Box(pos, 0.5f) * 6.0f;
pos = pos_origin;
float box6 = Box(pos, 0.5f) * 6.0f;
float result = Hlsl.Max(Hlsl.Max(Hlsl.Max(Hlsl.Max(Hlsl.Max(box1, box2), box3), box4), box5), box6);
return result;
}
/// <inheritdoc/>
public void Execute()
{
Int2 resolution = new(texture.Width, texture.Height);
Float2 p = ((Float2)ThreadIds.XY * 2.0f - resolution) / Hlsl.Min(resolution.X, resolution.Y);
Float3 ro = new(0.0f, -0.2f, time * 4.0f);
Float3 ray = Hlsl.Normalize(new Float3(p, 1.5f));
ray.XY = Hlsl.Mul(ray.XY, Rotate(Hlsl.Sin(time * 0.03f) * 5.0f));
ray.YZ = Hlsl.Mul(ray.YZ, Rotate(Hlsl.Sin(time * 0.05f) * 0.2f));
float t = 0.1f;
float ac = 0.0f;
for (int i = 0; i < 99; i++)
{
static Float3 Mod(Float3 x, float y)
{
return x - y * Hlsl.Floor(x / y);
}
Float3 pos = ro + ray * t;
pos = Mod(pos - 2.0f, 4.0f) - 2.0f;
float gTime = time - i * 0.01f;
float d = BoxSet(pos, gTime);
d = Hlsl.Max(Hlsl.Abs(d), 0.01f);
ac += Hlsl.Exp(-d * 23.0f);
t += d * 0.55f;
}
Float3 col = ac * 0.02f + new Float3(0.0f, 0.2f * Hlsl.Abs(Hlsl.Sin(time)), 0.5f + Hlsl.Sin(time) * 0.2f);
Float4 color = new(col, 1.0f - t * (0.02f + 0.02f * Hlsl.Sin(time)));
texture[ThreadIds.XY] = color;
}
}
}
バイナリファイルは表示されません。

変更後

幅:  |  高さ:  |  サイズ: 197 KiB

+7
ファイルの表示
@@ -31,6 +31,7 @@
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\FractalTiling.cs" Link="Shaders\FractalTiling.cs" />
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\MengerJourney.cs" Link="Shaders\MengerJourney.cs" />
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\TwoTiledTruchet.cs" Link="Shaders\TwoTiledTruchet.cs" />
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\Octagrams.cs" Link="Shaders\Octagrams.cs" />
</ItemGroup>
<ItemGroup>
@@ -47,9 +48,15 @@
<None Update="Assets\MengerJourney.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\Octagrams.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\TwoTiledTruchet.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\Octagrams.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Texture2DTests.Pixels.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Texture2DTests.Pixels.g.cs</LastGenOutput>
+7
ファイルの表示
@@ -43,6 +43,13 @@ namespace ComputeSharp.Tests
RunAndCompareShader(device, static texture => new TwoTiledTruchet(texture, 0), 0.00033f);
}
[CombinatorialTestMethod]
[AllDevices]
public void Octagrams(Device device)
{
RunAndCompareShader(device, static texture => new Octagrams(texture, 0), 0.00000586f);
}
/// <summary>
/// Executes a given test for a specified shader.
/// </summary>