From c8987c9515bfe03258acbcaf11e6d50297c2ba6b Mon Sep 17 00:00:00 2001 From: Marcus Cazzola Helms Date: Tue, 28 Oct 2025 16:54:58 +0100 Subject: [PATCH] add anti aliasing option --- h2d/RenderContext.hx | 3 +++ h3d/scene/RenderContext.hx | 6 ++++++ h3d/shader/Texture.hx | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/h2d/RenderContext.hx b/h2d/RenderContext.hx index 86a1413bef..a494fbc48c 100644 --- a/h2d/RenderContext.hx +++ b/h2d/RenderContext.hx @@ -151,6 +151,9 @@ class RenderContext extends h3d.impl.RenderContext { cameraStackIndex = 0; filterStack = []; filterStackIndex = 0; + #if anti_aliasing + globals.set("global.antiAliasing", 0); + #end } override function dispose() { diff --git a/h3d/scene/RenderContext.hx b/h3d/scene/RenderContext.hx index 349eba2ae6..80ba969e20 100644 --- a/h3d/scene/RenderContext.hx +++ b/h3d/scene/RenderContext.hx @@ -46,6 +46,9 @@ class RenderContext extends h3d.impl.RenderContext { @global("global.modelViewInverse") var globalModelViewInverse : h3d.Matrix; @global("global.previousModelView") var globalPreviousModelView : h3d.Matrix; @global("global.frame") var globalFrame : Int; + #if anti_aliasing + @global("global.antiAliasing") var globalAntiAliasing : Int; + #end var allocPool : h3d.pass.PassObject; var allocFirst : h3d.pass.PassObject; @@ -67,6 +70,9 @@ class RenderContext extends h3d.impl.RenderContext { cachedShaderList = []; cachedPassObjects = []; initGlobals(); + #if anti_aliasing + globalAntiAliasing = 0; + #end } public function setCamera( cam : h3d.Camera ) { diff --git a/h3d/shader/Texture.hx b/h3d/shader/Texture.hx index 1b431cd926..b7d04f3c25 100644 --- a/h3d/shader/Texture.hx +++ b/h3d/shader/Texture.hx @@ -6,6 +6,12 @@ class Texture extends hxsl.Shader { @input var input : { var uv : Vec2; }; + + #if anti_aliasing + @global var global: { + var antiAliasing: Int; + }; + #end @const var additive : Bool; @const var killAlpha : Bool; @@ -22,7 +28,21 @@ class Texture extends hxsl.Shader { } function fragment() { + #if anti_aliasing + var c = if (global.antiAliasing == 1) { + var dx = dFdx(input.uv); + var dy = dFdy(input.uv); + var c0 = texture.get(input.uv + 0.25 * dx + 0.25 * dy); + var c1 = texture.get(input.uv + 0.25 * dx - 0.25 * dy); + var c2 = texture.get(input.uv - 0.25 * dx + 0.25 * dy); + var c3 = texture.get(input.uv - 0.25 * dx - 0.25 * dy); + (c0 + c1 + c2 + c3) * 0.25; + } else { + texture.get(calculatedUV); + } + #else var c = texture.get(calculatedUV); + #end if( killAlpha && c.a - killAlphaThreshold < 0 ) discard; if( additive ) pixelColor += c;