合成で素材の不透明度を保てるようにする

このコミットが含まれているのは:
routersys committed 2026-10-19 00:28:53 +09:00
1 親 91592d4e03
コミット db0dd16f4d
3ファイル変更
+120 -3

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

+3 -1
ファイルの表示
@@ -6,7 +6,7 @@ SamplerState PageSampler : register(s1);
cbuffer Constants : register(b0)
{
float amount : packoffset(c0.x);
float pad0 : packoffset(c0.y);
float keepTransparent : packoffset(c0.y);
float pad1 : packoffset(c0.z);
float pad2 : packoffset(c0.w);
};
@@ -24,5 +24,7 @@ float4 main(
float4 page = PageTexture.SampleLevel(PageSampler, uv1.xy, 0);
page.rgb = min(page.rgb, page.a.xxx);
if (keepTransparent > 0.5)
page *= source.a;
return lerp(source, page, amount);
}
+102 -1
ファイルの表示
@@ -9,18 +9,20 @@ namespace MangaScreentone.Tests;
public sealed class MangaScreentoneCustomEffectTests
{
const int AmountIndex = 0;
const int KeepTransparentIndex = 1;
const int Width = 40;
const int Height = 24;
static readonly Bgra Blue = Bgra.Opaque(255, 0, 0);
static readonly Bgra HalfPage = new(0, 0, 0, 128);
static Rendering Render(IGraphicsDevicesAndContext devices, ID2D1Image source, ID2D1Image page, float amount)
static Rendering Render(IGraphicsDevicesAndContext devices, ID2D1Image source, ID2D1Image page, float amount, bool keepTransparent = false)
{
using var effect = new MangaScreentoneCustomEffect(devices);
effect.SetInput(0, source, true);
effect.SetInput(1, page, true);
effect.Amount = amount;
effect.KeepTransparent = keepTransparent;
using var output = effect.Output;
return Rendering.Capture(devices, output);
}
@@ -168,4 +170,103 @@ public sealed class MangaScreentoneCustomEffectTests
Assert.True(WithinRounding(Mixed(Bgra.Transparent, Blue.Premultiplied(), 0.5f), rendering[10, 10]), $"{rendering[10, 10]}");
Assert.Equal(Bgra.Transparent, rendering[45, 10]);
}
[Fact]
public void TransparencyIsNotKeptUntilItIsTurnedOn()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
using var effect = new MangaScreentoneCustomEffect(context);
Assert.Equal(0f, effect.GetFloatValue(KeepTransparentIndex));
}
[Theory]
[InlineData(true, 1f)]
[InlineData(false, 0f)]
public void KeepingTransparencyIsReportedAsAFlag(bool keep, float expected)
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
using var effect = new MangaScreentoneCustomEffect(context);
effect.KeepTransparent = !keep;
effect.KeepTransparent = keep;
Assert.Equal(expected, effect.GetFloatValue(KeepTransparentIndex));
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void AnOpaquePageCoversTheTransparentPartsOfTheSourceOnlyWhenTransparencyIsNotKept(bool keep)
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
var pageColor = Bgra.Opaque(10, 20, 30);
using var source = new SourceImage(context, Width, Height, (x, _) => x < 10 ? Bgra.Transparent : Blue);
using var page = SourceImage.Solid(context, Width, Height, pageColor);
var rendering = Render(context, source.Bitmap, page.Bitmap, 1f, keep);
Assert.All(rendering.Coordinates(), point => Assert.Equal(point.X < 10 && keep ? Bgra.Transparent : pageColor, rendering[point.X, point.Y]));
}
[Fact]
public void ASemiTransparentSourceScalesThePageByItsOpacityWhenTransparencyIsKept()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
using var source = SourceImage.Solid(context, Width, Height, new Bgra(0, 0, 255, 128));
using var page = SourceImage.Solid(context, Width, Height, Bgra.Opaque(200, 100, 50));
var expected = new Bgra(100, 50, 25, 128);
var rendering = Render(context, source.Bitmap, page.Bitmap, 1f, true);
Assert.All(rendering.Coordinates(), point => Assert.True(WithinRounding(expected, rendering[point.X, point.Y]), $"({point.X}, {point.Y}) {rendering[point.X, point.Y]}"));
}
[Fact]
public void ThePageOutsideTheSourceIsNotDrawnWhenTransparencyIsKept()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
using var source = SourceImage.Solid(context, Width, Height, Blue);
using var page = SourceImage.Solid(context, 8, 8, Bgra.Opaque(10, 20, 30));
using var moved = Translate(context, page.Bitmap, 50f, 4f);
using var movedOutput = moved.Output;
var rendering = Render(context, source.Bitmap, movedOutput, 1f, true);
Assert.Equal(Bgra.Transparent, rendering[53, 7]);
Assert.Equal(Bgra.Transparent, rendering[45, 10]);
}
[Fact]
public void ThePartialAmountKeepsTransparentPartsTransparentWhenTransparencyIsKept()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
var pageColor = Bgra.Opaque(10, 20, 30);
using var source = new SourceImage(context, Width, Height, (x, _) => x < 10 ? Bgra.Transparent : Blue);
using var page = SourceImage.Solid(context, Width, Height, pageColor);
var rendering = Render(context, source.Bitmap, page.Bitmap, 0.5f, true);
Assert.Equal(Bgra.Transparent, rendering[3, 5]);
Assert.True(WithinRounding(Mixed(pageColor, Blue.Premultiplied(), 0.5f), rendering[20, 5]), $"{rendering[20, 5]}");
}
[Fact]
public void WithoutAmountTheSourcePassesThroughEvenWhenTransparencyIsKept()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
using var source = new SourceImage(context, Width, Height, (x, _) => x < 10 ? Bgra.Transparent : Blue);
using var page = SourceImage.Solid(context, Width, Height, Bgra.Opaque(10, 20, 30));
var rendering = Render(context, source.Bitmap, page.Bitmap, 0f, true);
Assert.All(rendering.Coordinates(), point => Assert.Equal(source[point.X, point.Y], rendering[point.X, point.Y]));
}
}
+15 -1
ファイルの表示
@@ -11,6 +11,8 @@ internal sealed class MangaScreentoneCustomEffect(IGraphicsDevicesAndContext dev
{
public float Amount { set => SetValue((int)EffectImpl.Properties.Amount, value); }
public bool KeepTransparent { set => SetValue((int)EffectImpl.Properties.KeepTransparent, value ? 1f : 0f); }
[CustomEffect(2)]
private sealed class EffectImpl : D2D1CustomShaderEffectImplBase<EffectImpl>
{
@@ -27,6 +29,17 @@ internal sealed class MangaScreentoneCustomEffect(IGraphicsDevicesAndContext dev
}
}
[CustomEffectProperty(PropertyType.Float, (int)Properties.KeepTransparent)]
public float KeepTransparent
{
get => _cb.KeepTransparent;
set
{
_cb.KeepTransparent = value > 0.5f ? 1f : 0f;
UpdateConstants();
}
}
public EffectImpl() : base(ShaderResourceUri.Get("MangaScreentone"))
{
}
@@ -84,7 +97,7 @@ internal sealed class MangaScreentoneCustomEffect(IGraphicsDevicesAndContext dev
private struct ConstantBuffer
{
public float Amount;
public float Pad0;
public float KeepTransparent;
public float Pad1;
public float Pad2;
}
@@ -92,6 +105,7 @@ internal sealed class MangaScreentoneCustomEffect(IGraphicsDevicesAndContext dev
public enum Properties
{
Amount = 0,
KeepTransparent = 1,
}
}
}