Pixel perfect for plane with texture?

For sprites, there’s a pixel perfect mode that was added following this thread: Sprite pixel art artifact issue?

Is it possible to apply the same technique to a plane with a texture on it (which behaves like a billboarded sprite)?

As you can see in this PG: https://playground.babylonjs.com/#4G38H4#2024 there’s artifacts visible when moving the camera.

Updated: https://playground.babylonjs.com/#4G38H4#2027
Here is a comparison of the plane with texture, and a sprite that has pixelperfect enabled - the pixel ratios remain the same when zooming in and out, while the plane with texture warps.

If you toggle “use alpha from diffuse texture” the artifacts go away. Below a comparison between the image and the render in bjs.

2 Likes

Hey, thanks, but that’s not what I mean - my playground wasn’t clear. I’ve updated the playground: https://playground.babylonjs.com/#4G38H4#2024

The idea is to keep the ‘pixelated’ look of the image but without the artifacting when moving the camera. There’s a shader that does this for the pixelperfect mode for sprites, but I do not know if it’s usable in this case and how to do so.

Is this what you are looking for?
texture.samplingMode = BABYLON.Texture.NEAREST_SAMPLINGMODE

1 Like

For the ‘pixel look’ yes :slight_smile: but the texture is already set to nearest sampling mode in the PG. There’s artifacts when I move the camera while using that mode.

Sorry :wink:

What type of artifacts? When you zoom out so far that the shader is “struggling” to decide which pixel to draw so it skips a whole pixel row/column in the texture?
Why are you explicitly turning off mipmaps when loading the texture? Do you have a specific reason for it? That’s the first thing that could help you.

The third parameter has to be false to create mipmaps.
let texture = new BABYLON.Texture("https://i.imgur.com/clj2AYB.png", scene, false, true, BABYLON.Constants.TEXTURE_NEAREST_SAMPLINGMODE);


A solution if you wan to have an overall pixelated look is to use a postprocess:

Source: Node Material Pixelation - #4 by thompsoni

1 Like

That’s a cool effect, I might use that :slight_smile:

But the issue is indeed the ‘squashing’ or ‘stretching’ of a row or column of pixels of a pixelated texture when the plane is moved closer or further from the camera (so not really getting things more pixellated). In the PG the ears and torso for example seem to vary in width, which is what I’m trying to minimize.

For comparison (sprite):
https://playground.babylonjs.com/#5BQ0GN#5 (this has artifacts - well that’s what I’ll call it)

Fixed:
https://playground.babylonjs.com/#5BQ0GN#8 (shader)
https://playground.babylonjs.com/#5BQ0GN#11 (pixel perfect added after PR to add above shader fix)

So I’m looking to implement this mode for a billboarded plane with a pixelated texture.

1 Like

Is this better?

1 Like

I tried the changing the mipmap setting but it’s the same.

See new PG for comparison:

The lower (sprite) retains the correct pixel ratio while the upper (texture) does not when zooming in and out. I’m trying to get the plane with the texture to behave the same as the sprite (as I’m using such planes as planted sprites).

Ok, let me create a material plugin for this.

This one?

1 Like

Looks better! But there’s still some pixel rows being added when moving the camera…
There’s also these black pixels at the top of the plane that appear/disappear which are caused by the same issue I believe.
I’m not as experienced as you when it comes to shaders (not really sure what you added code does :slight_smile: ), but maybe you can see what the difference is with this PG’s shader code: https://playground.babylonjs.com/#5BQ0GN#8 ?

Shading chef, keep 'em coming :laughing:

1 Like

That’s because the texture UV were not clamped.

Check this one. Any closer? :smiley: EDIT: still not there… I’ll back on Monday and I’ll crack this nut :slight_smile:

The one in the middle is the sprite, the texture is around the sprite.

2 Likes

Nice! Thanks for looking into this.

I’ve been trying a bit myself but no luck :frowning:
Don’t know if this helps any, but there’s a ‘enablePixelPerfectMode’ on FxaaPostProcess - doesn’t do anything for me but I might be doing something wrong.
And also in this thread: Sprite pixel art artifact issue? it is said that the filtering needs to be set to bilineair for the effect to work.

I have to completely take over the shaders to rule out any unwanted uv/color modification (so no plugin) and use custom shaders.

Sprite + Texture side by side example. It seems to be the same for me.

That looks great and exactly like the sprite!
The only problem I have is that I animate the plane’s texture by setting the uScale and vScale and changing the uOffset and vOffset at set times (pretty much like using a sprite sheet). With the shadermaterial this is not possible - is there any way to get this working with your solution?

It’s easy to add the uv scaling and offset to the shader:

3 Likes

Cool!
That seems to work perfectly.
I only have an issue in my own project which somehow is replacing the assigned texture with another one but I can’t reproduce it in the playground. Is there a specific order in which the shader, scene etc. must be created for this to work properly?
Anyway, that’s a project issue - so marking as solved :slight_smile:

I suggest you to create a new topic on the forum with your question. Thanks!