phaser icon indicating copy to clipboard operation
phaser copied to clipboard

Dark edges around render texture snapshot

Open vforsh opened this issue 2 years ago • 5 comments

Version

Phaser 3.60 beta 3 and Phaser 3.55

Description

  1. Create render texture
  2. Draw text to it
  3. Add render texture to the scene -> no dark edges, all good
  4. Make render texture snapshot -> snapshot has dark edges around the text

rt-dark-edges

Example Test Code

Demo - https://codesandbox.io/s/text-dark-edges-7319v?file=/src/index.ts:0-618

import Phaser from "phaser"

class Demo extends Phaser.Scene {
	constructor() {
		super({})
	}

	preload() {}

	create() {
		let text = this.add.text(0, 0, "0123456789", {
			fontSize: "70px",
			fontStyle: "900",
		})

		this.add.rectangle(0, 300, 800, 400, 0xf7f5f5).setOrigin(0)

		let rt = this.add.renderTexture(0, 300, text.width, text.height)
		rt.draw(text)
		rt.snapshot((snapshot: HTMLImageElement) => {
			document.body.appendChild(snapshot)
		})
	}

	update() {}
}

var config = {
	type: Phaser.AUTO,
	parent: "phaser-example",
	width: 800,
	height: 600,
	scene: Demo,
}

var game = new Phaser.Game(config)

Additional Information

The issue is not specific to some Phaser versions. It was always present since the introduction of the snapshot method.

But it seems text-specific, when I draw shapes to the render texture, the resulting snapshot has no dark edges - https://codesandbox.io/s/text-dark-edges-forked-7ty5q?file=/src/index.ts

When I make a whole scene snapshot via renderer.snapshot text doesn't have dark edges.

Maybe this issue is somehow related to #5809.

vforsh avatar Dec 08 '21 08:12 vforsh

The dark edges are there because fillText will anti-alias the Text by default when drawing it to the Text Canvas. The WebGL texture that is created from this has pre-multiplied alpha set on it. This is the texture that is drawn to the Render Texture. When it gets composited into the Scene, the pre-multiplied alpha is blended with the background Camera color, and it all looks fine. If you make a snapshot, it gets drawn to a transparent texture instead, so the anti-aliasing stands out. If the RenderTexture is filled with a color first, like white, you won't notice it at all.

If we disable pre-multiplied alpha for the Text Canvas, then Text looks like this when added to a Scene:

image

But, when snapshoted, the black fringe is almost nonexistent because there are no aliasing pixels visible. The text is very jaggy, though, as a result.

You don't see it on Shapes because they don't have any aliasing.

You would see it if you created a CanvasTexture, drew some shapes to that, then drew that to a RT and snapshotted it, because again they would be aliased.

Not sure there is any possible fix for this. If we disable pma then Text will look arse in-game, and ok on a snapshot but super jaggy at small sizes.

photonstorm avatar Nov 01 '22 00:11 photonstorm

The dark edges around the text in the render texture snapshot might be caused by antialiasing artifacts. One way to fix this is by setting the antialias property of the render texture to false. Try modifying the code in the create method as follows:

create() { let text = this.add.text(0, 0, "0123456789", { fontSize: "70px", fontStyle: "900", });

this.add.rectangle(0, 300, 800, 400, 0xf7f5f5).setOrigin(0);

let rt = this.add.renderTexture(0, 300, text.width, text.height, {
  antialias: false, // disable antialiasing
});
rt.draw(text);
rt.snapshot((snapshot: HTMLImageElement) => {
  document.body.appendChild(snapshot);
});

} This should produce a snapshot of the render texture without the dark edges.

Hope it helps!

  • SlimHR

SlimHajRomdhane avatar Mar 25 '23 10:03 SlimHajRomdhane

Feels like the last comment was written by GPT

vforsh avatar Mar 25 '23 16:03 vforsh

Hahaha yes I am still a young software engineer so I try to help as much as I can! If you don’t like the use of ChatGPT I am really sorry. Best regards

On Sat, 25 Mar 2023 at 17:45 Vladislav Forsh @.***> wrote:

Feels like the last comment was written by GPT

— Reply to this email directly, view it on GitHub https://github.com/photonstorm/phaser/issues/5939#issuecomment-1483866950, or unsubscribe https://github.com/notifications/unsubscribe-auth/A332NRQUCBMPQI76LPB3CADW54OK5ANCNFSM5JTFKFKA . You are receiving this because you commented.Message ID: @.***>

SlimHajRomdhane avatar Mar 25 '23 16:03 SlimHajRomdhane

Unfortunately, ChatGPT has generated a useless answer here. There is no config object for RenderTexture, so this answer is complete rubbish. I've had to delete lots of similar answers today from you @SlimHajRomdhane and it has wasted a good 30 mins of my time. I appreciate what you're trying to do, but please stop, because it's just causing noise that benefits no one.

photonstorm avatar Mar 26 '23 17:03 photonstorm