光の筋のシェーダーに遮る物の影を加える

このコミットが含まれているのは:
routersys committed 2026-10-25 20:48:12 +09:00
1 親 1adff6ba63
コミット c195191fe7
6ファイル変更
+341 -38

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

+68 -2
ファイルの表示
@@ -4,12 +4,20 @@ Texture2D Level2 : register(t2);
Texture2D Level3 : register(t3);
Texture2D Level4 : register(t4);
Texture2D Level5 : register(t5);
Texture2D Occluder0 : register(t6);
Texture2D Occluder1 : register(t7);
Texture2D Occluder2 : register(t8);
Texture2D Occluder3 : register(t9);
SamplerState Sampler0 : register(s0);
SamplerState Sampler1 : register(s1);
SamplerState Sampler2 : register(s2);
SamplerState Sampler3 : register(s3);
SamplerState Sampler4 : register(s4);
SamplerState Sampler5 : register(s5);
SamplerState Sampler6 : register(s6);
SamplerState Sampler7 : register(s7);
SamplerState Sampler8 : register(s8);
SamplerState Sampler9 : register(s9);
cbuffer Constants : register(b0)
{
@@ -26,6 +34,10 @@ cbuffer Constants : register(b0)
float4 wind : packoffset(c18);
float4 mode : packoffset(c19);
float4 fade : packoffset(c20);
float4 occluderPlanes[4] : packoffset(c21);
float4 occluderImageX[4] : packoffset(c25);
float4 occluderImageY[4] : packoffset(c29);
float4 occluderStrength : packoffset(c33);
};
static const int MaximumLevel = 5;
@@ -38,6 +50,7 @@ static const float DustSecondWeight = 0.5;
static const float LinearFalloffLevel = 0.9999;
static float4 SceneUv[6];
static float4 OccluderUv[4];
static float2 ScenePoint;
uint Mix(uint value)
@@ -106,6 +119,50 @@ float4 ReadLevel(int level, float2 imagePosition)
return Level5.SampleLevel(Sampler5, uv, 0);
}
float OccluderAlpha(int index, float2 imagePosition)
{
float4 uvs = OccluderUv[index];
float2 uv = uvs.xy + (imagePosition - ScenePoint) * uvs.zw;
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0)
return 0.0;
if (index == 0)
return Occluder0.SampleLevel(Sampler6, uv, 0).a;
if (index == 1)
return Occluder1.SampleLevel(Sampler7, uv, 0).a;
if (index == 2)
return Occluder2.SampleLevel(Sampler8, uv, 0).a;
return Occluder3.SampleLevel(Sampler9, uv, 0).a;
}
float GetTransmission(float3 volumePoint, float3 sourcePoint)
{
float transmission = 1.0;
[unroll]
for (int index = 0; index < 4; ++index)
{
float strength = occluderStrength[index];
if (strength > 0.0)
{
float4 plane = occluderPlanes[index];
float near = dot(plane.xyz, volumePoint) + plane.w;
float span = dot(plane.xyz, sourcePoint) + plane.w - near;
if (abs(span) > RateEpsilon)
{
float ratio = -near / span;
if (ratio > 0.0 && ratio < 1.0)
{
float4 crossing = float4(volumePoint + (sourcePoint - volumePoint) * ratio, 1.0);
float2 imagePosition = float2(dot(occluderImageX[index], crossing), dot(occluderImageY[index], crossing));
transmission *= 1.0 - strength * OccluderAlpha(index, imagePosition);
}
}
}
}
return transmission;
}
float4 SampleGobo(float2 imagePosition, float lod)
{
float clamped = clamp(lod, 0.0, (float)MaximumLevel);
@@ -170,7 +227,11 @@ float4 main(
float4 uv2 : TEXCOORD2,
float4 uv3 : TEXCOORD3,
float4 uv4 : TEXCOORD4,
float4 uv5 : TEXCOORD5
float4 uv5 : TEXCOORD5,
float4 uv6 : TEXCOORD6,
float4 uv7 : TEXCOORD7,
float4 uv8 : TEXCOORD8,
float4 uv9 : TEXCOORD9
) : SV_TARGET
{
SceneUv[0] = uv0;
@@ -179,6 +240,10 @@ float4 main(
SceneUv[3] = uv3;
SceneUv[4] = uv4;
SceneUv[5] = uv5;
OccluderUv[0] = uv6;
OccluderUv[1] = uv7;
OccluderUv[2] = uv8;
OccluderUv[3] = uv9;
ScenePoint = scenePosition.xy;
float footprint = mode.z;
@@ -256,7 +321,8 @@ float4 main(
if (dust.x > 0.0)
density = max(1.0 + dust.x * (2.0 * Dust((volumePoint + wind.xyz) * dust.y, seed) - 1.0), 0.0);
sum += emission * (GetFalloff(travel) * phase * density);
float transmission = occluderStrength.x + occluderStrength.y + occluderStrength.z + occluderStrength.w > 0.0 ? GetTransmission(volumePoint, sourcePoint) : 1.0;
sum += emission * (GetFalloff(travel) * phase * density * transmission);
}
return float4(sum * stepWeight * tint.rgb, 0.0);
+159 -13
ファイルの表示
@@ -17,22 +17,22 @@ public sealed class ShaftCustomEffectTests(ITestOutputHelper output)
const float Absolute = 0.003f;
const double ToleratedFraction = 0.995;
static readonly RawRectF Bounds = new(0f, 0f, GoboSize, GoboSize);
static VolumetricLightParameters Typical => new(-90d, 55d, 10d, 300d, 100d, Colors.White, 60d, 1.5d, 30d, 0d, 40d, 40d, -90d, 0, VolumetricLightQuality.Ultra);
static Vector3 Emission(int x, int y)
static Func<int, int, Vector3> EmissionOf(int size) => (x, y) =>
{
const int Margin = 12;
if (x < Margin || y < Margin || x >= GoboSize - Margin || y >= GoboSize - Margin)
if (x < Margin || y < Margin || x >= size - Margin || y >= size - Margin)
return Vector3.Zero;
var bars = (x / 6) % 2 == 0 ? 0.9f : 0.2f;
var dx = x - GoboSize / 2;
var dy = y - GoboSize / 2;
var dx = x - size / 2;
var dy = y - size / 2;
var disc = dx * dx + dy * dy < 24 * 24 ? 1f : 0.25f;
return new Vector3(bars, y / (float)GoboSize, disc);
}
return new Vector3(bars, y / (float)size, disc);
};
static Vector3 CenteredAt(int size) => new(-size / 2f, -size / 2f, 0f);
static readonly Vector3 CenteredDraw = new(-64f, -64f, 0f);
@@ -48,21 +48,70 @@ public sealed class ShaftCustomEffectTests(ITestOutputHelper output)
* Matrix4x4.CreateRotationZ(MathF.PI * rotationDegrees.Z / 180f, pivot);
}
static (FloatRendering Rendering, ShaftShaderModel Model, ShaftScene Scene) Render(IGraphicsDevicesAndContext devices, VolumetricLightParameters parameters, DrawDescription draw)
sealed class OccluderRig : IDisposable
{
var pyramid = new GoboPyramid(GoboSize, GoboSize, Emission);
Assert.True(ShaftScene.TryCreate(parameters, draw, Bounds, Canvas, Canvas, 0, EffectDescriptions.Fps, out var scene));
readonly OccluderLease lease;
readonly OccluderSlot[] slots;
readonly ID2D1Bitmap1[] bitmaps;
readonly OccluderLink link = new();
public OccluderRig(IGraphicsDevicesAndContext devices, IReadOnlyList<(DrawDescription Draw, GoboPyramid Image, float Strength)> occluders)
{
lease = OccluderRegistry.Acquire(devices, 1);
link.Group = lease.Group;
LinkId = OccluderLinks.Register(link);
slots = new OccluderSlot[occluders.Count];
bitmaps = new ID2D1Bitmap1[occluders.Count];
for (var index = 0; index < occluders.Count; index++)
{
var (draw, image, strength) = occluders[index];
Assert.True(ViewTransform.TryCreate(draw, out var view));
slots[index] = lease.Group.Claim()!;
bitmaps[index] = image.CreateBitmaps(devices)[0];
slots[index].Publish(bitmaps[index], new Occluder(view, GoboMapping.Create(draw.Zoom, draw.Invert, draw.CenterPoint), strength));
}
}
public int LinkId { get; }
public ID2D1Image GetOutput(int slot) => lease.Group.GetOutput(slot);
public void Dispose()
{
foreach (var slot in slots)
slot.Dispose();
OccluderLinks.Unregister(LinkId);
lease.Dispose();
foreach (var bitmap in bitmaps)
bitmap.Dispose();
}
}
static (FloatRendering Rendering, ShaftShaderModel Model, ShaftScene Scene) Render(IGraphicsDevicesAndContext devices, VolumetricLightParameters parameters, DrawDescription draw, IReadOnlyList<(DrawDescription Draw, GoboPyramid Image, float Strength)>? occluders = null, int size = GoboSize)
{
var pyramid = new GoboPyramid(size, size, EmissionOf(size));
Assert.True(ShaftScene.TryCreate(parameters, draw, new RawRectF(0f, 0f, size, size), Canvas, Canvas, 0, EffectDescriptions.Fps, out var scene));
using var rig = new OccluderRig(devices, occluders ?? []);
using var effect = new ShaftCustomEffect(devices);
var bitmaps = pyramid.CreateBitmaps(devices);
try
{
for (var level = 0; level < bitmaps.Length; level++)
effect.SetInput(level, bitmaps[level], true);
for (var slot = 0; slot < VolumetricLightSettings.OccluderCapacity; slot++)
effect.SetInput(VolumetricLightSettings.GoboLevelCount + slot, rig.GetOutput(slot), true);
effect.Link = rig.LinkId;
effect.Apply(scene.Constants, default, true);
using var image = effect.Output;
var rendering = FloatRendering.Capture(devices, image);
effect.SetInput(0, null, true);
return (rendering, new ShaftShaderModel(scene.Constants, pyramid), scene);
for (var index = 0; index < VolumetricLightSettings.GoboLevelCount + VolumetricLightSettings.OccluderCapacity; index++)
effect.SetInput(index, null, true);
var planes = (occluders ?? []).Select(occluder =>
{
Assert.True(ViewTransform.TryCreate(occluder.Draw, out var view));
return (OccluderPlane.Create(scene.Constants.CameraFromLocal, new Occluder(view, GoboMapping.Create(occluder.Draw.Zoom, occluder.Draw.Invert, occluder.Draw.CenterPoint), occluder.Strength)), occluder.Image);
}).ToArray();
return (rendering, new ShaftShaderModel(scene.Constants, pyramid, planes), scene);
}
finally
{
@@ -125,6 +174,99 @@ public sealed class ShaftCustomEffectTests(ITestOutputHelper output)
AssertFollowsTheModel(rendering, model, $"{azimuth}/{elevation}/{spread}");
}
const int ScreenSize = 64;
static GoboPyramid Stripes() => new(ScreenSize, ScreenSize, (x, y) => new Vector4(0f, 0f, 0f, (x / 8) % 2 == 0 ? 1f : 0f));
static GoboPyramid Disc() => new(ScreenSize, ScreenSize, (x, y) =>
{
var dx = x - ScreenSize / 2;
var dy = y - ScreenSize / 2;
return new Vector4(0f, 0f, 0f, dx * dx + dy * dy < 24 * 24 ? 0.7f : 0f);
});
static (DrawDescription Draw, GoboPyramid Image, float Strength) Occlude(GoboPyramid image, Vector3 draw, Vector3? rotation = null, float strength = 1f, float zoom = 1f)
=> (Pose(draw, rotation, zoom: new Vector2(zoom)), image, strength);
public static TheoryData<string> Shadows => new()
{
"stripes",
"soft-disc",
"two",
"four",
"tilted",
"behind-the-window",
};
static IReadOnlyList<(DrawDescription Draw, GoboPyramid Image, float Strength)> NamedShadow(string name) => name switch
{
"stripes" => [Occlude(Stripes(), new Vector3(-32f, 20f, 60f))],
"soft-disc" => [Occlude(Disc(), new Vector3(-32f, 10f, 70f), strength: 0.6f)],
"two" => [Occlude(Stripes(), new Vector3(-32f, 10f, 40f)), Occlude(Disc(), new Vector3(-20f, 40f, 110f), strength: 0.8f)],
"four" => [Occlude(Disc(), new Vector3(-60f, 20f, 40f)), Occlude(Disc(), new Vector3(10f, 20f, 60f)), Occlude(Stripes(), new Vector3(-30f, 50f, 90f), strength: 0.5f), Occlude(Disc(), new Vector3(-20f, 60f, 140f), strength: 0.9f)],
"tilted" => [Occlude(Stripes(), new Vector3(-32f, 20f, 70f), new Vector3(35f, -25f, 12f))],
_ => [Occlude(Stripes(), new Vector3(-64f, -100f, -60f), zoom: 2f)],
};
[Theory]
[MemberData(nameof(Shadows))]
public void TheShaderFollowsTheModelWithOccluders(string name)
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
var occluders = NamedShadow(name);
var (shadowed, model, _) = Render(context, Typical, Pose(CenteredDraw), occluders);
var (clear, _, _) = Render(context, Typical, Pose(CenteredDraw));
AssertFollowsTheModel(shadowed, model, name);
var darker = shadowed.Coordinates().Count(point => shadowed[point.X, point.Y].X < clear[point.X, point.Y].X * 0.9f - Absolute);
output.WriteLine($"{name}: darker={darker}");
if (name == "behind-the-window")
Assert.Equal(0, darker);
else
Assert.True(darker > 200, $"{name} {darker}");
}
[Fact]
public void TheShaderFollowsTheModelWhereTheStepCountIsHeldAtItsUpperLimit()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
var (rendering, model, _) = Render(context, Typical with { Quality = VolumetricLightQuality.Standard, LampSize = 0d, Elevation = 25d, Length = 2500d }, Pose(CenteredAt(512)), size: 512);
AssertFollowsTheModel(rendering, model, "upper limit");
}
[Fact]
public void TheShaderFollowsTheModelWhereTheStepCountIsHeldAtItsLowerLimit()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
var (rendering, model, _) = Render(context, Typical with { Quality = VolumetricLightQuality.Standard, Length = 12d, Elevation = 80d }, Pose(CenteredDraw));
AssertFollowsTheModel(rendering, model, "lower limit");
}
[Fact]
public void AnOccluderOfNoStrengthOrBeyondTheSourceLeavesTheLightAsItWas()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
var (clear, _, _) = Render(context, Typical, Pose(CenteredDraw));
var (weak, _, _) = Render(context, Typical, Pose(CenteredDraw), [Occlude(Stripes(), new Vector3(-32f, 20f, 60f), strength: 0f)]);
var (behind, _, _) = Render(context, Typical, Pose(CenteredDraw), [Occlude(Stripes(), new Vector3(-64f, -100f, -60f), zoom: 2f)]);
Assert.All(clear.Coordinates(), point =>
{
Assert.Equal(clear[point.X, point.Y], weak[point.X, point.Y]);
Assert.Equal(clear[point.X, point.Y], behind[point.X, point.Y]);
});
}
public static TheoryData<string> Poses => new()
{
"rotated",
@@ -133,6 +275,8 @@ public sealed class ShaftCustomEffectTests(ITestOutputHelper output)
"inverted",
"off-center",
"close",
"orthographic",
"orthographic-turned",
};
static DrawDescription NamedPose(string name) => name switch
@@ -142,6 +286,8 @@ public sealed class ShaftCustomEffectTests(ITestOutputHelper output)
"zoomed" => Pose(CenteredDraw, zoom: new Vector2(1.6f, 0.7f)),
"inverted" => Pose(CenteredDraw, invert: true),
"off-center" => Pose(CenteredDraw, new Vector3(0f, 20f, 0f), center: new Vector2(24f, -16f)),
"orthographic" => Pose(new Vector3(-62.4f, -64.3f, 0f)) with { PerspectiveDistance = float.PositiveInfinity },
"orthographic-turned" => Pose(CenteredDraw, new Vector3(0f, 0f, 30f)) with { PerspectiveDistance = float.PositiveInfinity },
_ => Pose(new Vector3(-64f, -64f, 400f), new Vector3(0f, 0f, 15f)),
};
+43 -4
ファイルの表示
@@ -18,6 +18,11 @@ internal sealed class GoboPyramid
readonly int[] heights = new int[VolumetricLightSettings.GoboLevelCount];
public GoboPyramid(int width, int height, Func<int, int, Vector3> emission)
: this(width, height, (x, y) => new Vector4(emission(x, y), MaximumAlpha))
{
}
public GoboPyramid(int width, int height, Func<int, int, Vector4> texel)
{
widths[0] = width;
heights[0] = height;
@@ -26,12 +31,12 @@ internal sealed class GoboPyramid
{
for (var x = 0; x < width; x++)
{
var value = emission(x, y);
var value = texel(x, y);
var offset = (y * width + x) * Channels;
levels[0][offset] = value.X;
levels[0][offset + 1] = value.Y;
levels[0][offset + 2] = value.Z;
levels[0][offset + 3] = MaximumAlpha;
levels[0][offset + 3] = value.W;
}
}
@@ -103,7 +108,7 @@ internal sealed class GoboPyramid
}
}
internal sealed class ShaftShaderModel(ShaftConstants constants, GoboPyramid pyramid)
internal sealed class ShaftShaderModel(ShaftConstants constants, GoboPyramid pyramid, IReadOnlyList<(OccluderPlane Plane, GoboPyramid Image)>? occluders = null)
{
const float RateEpsilon = 1e-9f;
const float UnitScale = 1f / 16777216f;
@@ -115,6 +120,7 @@ internal sealed class ShaftShaderModel(ShaftConstants constants, GoboPyramid pyr
readonly ShaftConstants c = constants;
readonly GoboPyramid pyramid = pyramid;
readonly IReadOnlyList<(OccluderPlane Plane, GoboPyramid Image)> occluders = occluders ?? [];
static uint Mix(uint value)
{
@@ -227,6 +233,39 @@ internal sealed class ShaftShaderModel(ShaftConstants constants, GoboPyramid pyr
return blend <= 0f ? low : Vector4.Lerp(low, ReadLevel(upper, image), blend);
}
float OccluderAlpha(GoboPyramid image, Vector2 position)
{
var u = position.X / image.Width;
var v = position.Y / image.Height;
return u < 0f || u > 1f || v < 0f || v > 1f ? 0f : image.Bilinear(0, u, v).W;
}
static float Dot(Vector4 row, Vector3 point) => row.X * point.X + row.Y * point.Y + row.Z * point.Z + row.W;
float GetTransmission(Vector3 volumePoint, Vector3 sourcePoint)
{
var transmission = 1f;
foreach (var (plane, image) in occluders)
{
if (!(plane.Strength > 0f))
continue;
var near = Dot(plane.Plane, volumePoint);
var span = Dot(plane.Plane, sourcePoint) - near;
if (!(MathF.Abs(span) > RateEpsilon))
continue;
var ratio = -near / span;
if (!(ratio > 0f && ratio < 1f))
continue;
var crossing = volumePoint + (sourcePoint - volumePoint) * ratio;
transmission *= 1f - plane.Strength * OccluderAlpha(image, new Vector2(Dot(plane.ImageX, crossing), Dot(plane.ImageY, crossing)));
}
return transmission;
}
public Vector3 Shade(int pixelX, int pixelY)
{
var scene = new Vector2(pixelX + 0.5f, pixelY + 0.5f);
@@ -300,7 +339,7 @@ internal sealed class ShaftShaderModel(ShaftConstants constants, GoboPyramid pyr
if (c.Dust.X > 0f)
density = MathF.Max(1f + c.Dust.X * (2f * Dust((volumePoint + wind) * c.Dust.Y, seed) - 1f), 0f);
sum += new Vector3(gobo.X, gobo.Y, gobo.Z) * (GetFalloff(travel) * phase * density);
sum += new Vector3(gobo.X, gobo.Y, gobo.Z) * (GetFalloff(travel) * phase * density * GetTransmission(volumePoint, sourcePoint));
}
return sum * stepWeight * new Vector3(c.Tint.X, c.Tint.Y, c.Tint.Z);
+55 -7
ファイルの表示
@@ -36,6 +36,9 @@ internal sealed class ShaftCustomEffect : D2D1CustomShaderEffectBase
Mode,
Fade,
OutputBounds,
CameraFromLocal,
Link,
Revision,
}
public Matrix4x4 LocalFromCamera { set => SetValue((int)PropertyIndex.LocalFromCamera, value); }
@@ -57,6 +60,9 @@ internal sealed class ShaftCustomEffect : D2D1CustomShaderEffectBase
public Vector4 Mode { set => SetValue((int)PropertyIndex.Mode, value); }
public Vector4 Fade { set => SetValue((int)PropertyIndex.Fade, value); }
public Vector4 OutputBounds { set => SetValue((int)PropertyIndex.OutputBounds, value); }
public Matrix4x4 CameraFromLocal { set => SetValue((int)PropertyIndex.CameraFromLocal, value); }
public int Link { set => SetValue((int)PropertyIndex.Link, value); }
public int Revision { set => SetValue((int)PropertyIndex.Revision, value); }
public void Apply(in ShaftConstants next, in ShaftConstants last, bool force)
{
@@ -98,15 +104,20 @@ internal sealed class ShaftCustomEffect : D2D1CustomShaderEffectBase
Fade = next.Fade;
if (force || last.OutputBounds != next.OutputBounds)
OutputBounds = next.OutputBounds;
if (force || last.CameraFromLocal != next.CameraFromLocal)
CameraFromLocal = next.CameraFromLocal;
}
[CustomEffect(VolumetricLightSettings.GoboLevelCount)]
[CustomEffect(VolumetricLightSettings.GoboLevelCount + VolumetricLightSettings.OccluderCapacity)]
internal sealed class EffectImpl : D2D1CustomShaderEffectImplBase<EffectImpl>
{
private ConstantBuffer _cb;
private Vector4 _outputBounds;
private RawRect _outputRect;
private readonly RawRect[] _levelRects = new RawRect[VolumetricLightSettings.GoboLevelCount];
private readonly RawRect[] _inputRects = new RawRect[VolumetricLightSettings.GoboLevelCount + VolumetricLightSettings.OccluderCapacity];
private Matrix4x4 _cameraFromLocal = Matrix4x4.Identity;
private int _link;
private int _revision;
[CustomEffectProperty(PropertyType.Matrix4x4, (int)PropertyIndex.LocalFromCamera)]
public Matrix4x4 LocalFromCamera { get => _cb.LocalFromCamera; set { _cb.LocalFromCamera = ViewTransform.IsFinite(value) ? value : default; UpdateConstants(); } }
@@ -177,6 +188,15 @@ internal sealed class ShaftCustomEffect : D2D1CustomShaderEffectBase
}
}
[CustomEffectProperty(PropertyType.Matrix4x4, (int)PropertyIndex.CameraFromLocal)]
public Matrix4x4 CameraFromLocal { get => _cameraFromLocal; set { _cameraFromLocal = ViewTransform.IsFinite(value) ? value : Matrix4x4.Identity; UpdateConstants(); } }
[CustomEffectProperty(PropertyType.Int32, (int)PropertyIndex.Link)]
public int Link { get => _link; set { _link = value; UpdateConstants(); } }
[CustomEffectProperty(PropertyType.Int32, (int)PropertyIndex.Revision)]
public int Revision { get => _revision; set { _revision = value; UpdateConstants(); } }
public EffectImpl() : base(ShaderResourceUri.Get("Shaft"))
{
}
@@ -191,6 +211,7 @@ internal sealed class ShaftCustomEffect : D2D1CustomShaderEffectBase
try
{
ResolveOccluders();
drawInformation.SetPixelShaderConstantBuffer(_cb);
}
catch (Exception exception)
@@ -200,24 +221,47 @@ internal sealed class ShaftCustomEffect : D2D1CustomShaderEffectBase
}
}
private void ResolveOccluders()
{
_cb.OccluderStrength = default;
if (!OccluderLinks.TryGet(_link, out var link) || link.Group is not { } group)
return;
Span<Occluder> occluders = stackalloc Occluder[VolumetricLightSettings.OccluderCapacity];
Span<float> strengths = stackalloc float[VolumetricLightSettings.OccluderCapacity];
group.Snapshot(occluders);
for (var index = 0; index < occluders.Length; index++)
{
var plane = OccluderPlane.Create(_cameraFromLocal, occluders[index]);
_cb.OccluderPlanes[index] = plane.Plane;
_cb.OccluderImageX[index] = plane.ImageX;
_cb.OccluderImageY[index] = plane.ImageY;
strengths[index] = plane.Strength;
}
_cb.OccluderStrength = new Vector4(strengths[0], strengths[1], strengths[2], strengths[3]);
}
public override void MapInputRectsToOutputRect(
RawRect[] inputRects,
RawRect[] inputOpaqueSubRects,
out RawRect outputRect,
out RawRect outputOpaqueSubRect)
{
for (var index = 0; index < _levelRects.Length; index++)
_levelRects[index] = inputRects[index];
for (var index = 0; index < _inputRects.Length; index++)
_inputRects[index] = inputRects[index];
outputRect = _outputRect;
outputOpaqueSubRect = default;
}
public override void MapOutputRectToInputRects(RawRect outputRect, RawRect[] inputRects)
{
for (var index = 0; index < _levelRects.Length; index++)
for (var index = 0; index < _inputRects.Length; index++)
{
var rect = _levelRects[index];
inputRects[index] = new RawRect(rect.Left - 1, rect.Top - 1, rect.Right + 1, rect.Bottom + 1);
var rect = _inputRects[index];
inputRects[index] = rect.Right > rect.Left && rect.Bottom > rect.Top
? new RawRect(rect.Left - 1, rect.Top - 1, rect.Right + 1, rect.Bottom + 1)
: rect;
}
}
@@ -240,6 +284,10 @@ internal sealed class ShaftCustomEffect : D2D1CustomShaderEffectBase
public Vector4 Wind;
public Vector4 Mode;
public Vector4 Fade;
public OccluderPlaneSet OccluderPlanes;
public OccluderPlaneSet OccluderImageX;
public OccluderPlaneSet OccluderImageY;
public Vector4 OccluderStrength;
}
}
}
+4 -2
ファイルの表示
@@ -41,7 +41,8 @@ internal readonly record struct ShaftConstants(
Vector4 Wind,
Vector4 Mode,
Vector4 Fade,
Vector4 OutputBounds);
Vector4 OutputBounds,
Matrix4x4 CameraFromLocal);
internal readonly record struct ShaftScene(ShaftConstants Constants, float Scale, ScreenRect Canvas, ScreenRect Shaft, ViewTransform View, LightVolume Volume)
{
@@ -96,7 +97,8 @@ internal readonly record struct ShaftScene(ShaftConstants Constants, float Scale
new Vector4((float)(Math.Cos(windAngle) * windOffset), (float)(-Math.Sin(windAngle) * windOffset), 0f, 0f),
new Vector4(VolumetricLightSettings.MinimumSteps, DustTapsPerCell, 1f / limits.ResolutionScale, 0f),
VolumetricLightSettings.GetFade(parameters.Falloff, length),
shaft.ToVector());
shaft.ToVector(),
view.CameraFromLocal);
scene = new ShaftScene(constants, limits.ResolutionScale, canvas, shaft, view, volume);
return true;
}
+12 -10
ファイルの表示
@@ -224,6 +224,17 @@ internal sealed class VolumetricLightEffectProcessor(IGraphicsDevicesAndContext
disposer.Collect(shaft);
var context = devices.DeviceContext;
var flood = new Flood(context) { Color = Vector4.Zero };
var emptyCrop = new Crop(context) { Rectangle = Vector4.Zero };
disposer.Collect(flood);
disposer.Collect(emptyCrop);
using (var floodOutput = flood.Output)
emptyCrop.SetInput(0, floodOutput, true);
_emptyOutput = emptyCrop.Output;
disposer.Collect(_emptyOutput);
for (var slot = 0; slot < VolumetricLightSettings.OccluderCapacity; slot++)
shaft.SetInput(VolumetricLightSettings.GoboLevelCount + slot, _emptyOutput, true);
var previous = emission.Output;
disposer.Collect(previous);
shaft.SetInput(0, previous, true);
@@ -269,15 +280,6 @@ internal sealed class VolumetricLightEffectProcessor(IGraphicsDevicesAndContext
_renderOutput = _render.Output;
disposer.Collect(_renderOutput);
var flood = new Flood(context) { Color = Vector4.Zero };
var emptyCrop = new Crop(context) { Rectangle = Vector4.Zero };
disposer.Collect(flood);
disposer.Collect(emptyCrop);
using (var floodOutput = flood.Output)
emptyCrop.SetInput(0, floodOutput, true);
_emptyOutput = emptyCrop.Output;
disposer.Collect(_emptyOutput);
_composite = new Composite(context) { InputCount = 2, Mode = CompositeMode.Plus };
_composite.SetValue((int)Property.Precision, BufferPrecision.PerChannel16Float);
disposer.Collect(_composite);
@@ -318,7 +320,7 @@ internal sealed class VolumetricLightEffectProcessor(IGraphicsDevicesAndContext
level?.SetInput(0, null, true);
if (_shaft is { } shaft)
{
for (var index = 0; index < VolumetricLightSettings.GoboLevelCount; index++)
for (var index = 0; index < VolumetricLightSettings.GoboLevelCount + VolumetricLightSettings.OccluderCapacity; index++)
shaft.SetInput(index, null, true);
}