Pixelate shader/filter for UPBGE - how to make edges of objects not blurry

I’ve been having some trouble with using this pixel shader for my game. The edges of objects aren’t sharp or crisp, like when I play and record my game at 240p (with no shader), vs recording it at 960p with the shader applied. Recording it at a lower resolution yields the correct result of what I’m trying to achieve. Is there a way to mitigate the blurriness of the outlines of objects at higher resolutions with the shader applied?

The problem:
image

What is the resolution when you go full screen? Maybe it needs to be dividable by 4 in the script?

That doesn’t work. Is there a way to get a sharper effect regardless of screen size?

set the AA(anti alias) higher

Tried that via the properties panel, but setting it higher does the opposite of what I need, I need it completely off to get the pixelation effect.

320x240 to 1280x960

I have no idea how to code glsl shader but I did google for an hour just because I wanted to know if this is possible to fix. It seems it should be in an interpolation mode but I have no idea how to add it to the shader.

GL_NEAREST is needed to add.
https://www2.cs.sfu.ca/~haoz/teaching/htmlman/texparameter.html

1 Like

Ah, thank you! I cam across this as well and have reached the same wall as how to use this since I barely know how GLSL works as well.

The only other method I could find was someone recommended was to use RenderToTexture, and I tried to do that but there doesn’t seem to be a way to force blender (in this mode) to render without AA, and it seems like it’s tied to the window size. I looked through the API and the “scale” function is supposed to implement nearest neighbor scaling for video textures, but I am not sure how to implement it.
https://blender.stackexchange.com/questions/81160/video-texture-module-resolution

Also here’s an example of how using RenderToTexture does lowers the resolution, but it’s still blurry
.
image

I’ve been considering even making some sort of wrapper as an extension of the game exe itself to just resize the game that way and circumvent this problem, but I have no idea how to do that either.

here’s another closeup of the thing where there is a slight transparency that happens along the edges that has been driving me nuts.
image

So far the best way I can think of fixing this is by altering the pixelate filter in the OP to make the edges not blurry but I can’t figure it out.

Add this script in the code to get rid of all mipmapping

from bge import render
render.setMipmapping(render.RAS_MIPMAP_NONE)

It seems to fix the problem.

This works for all textures in the image except for the video (Render-to-texture) texture
image

Could you share me that file so I can have a look?

r2t_attempt_09.blend (834.7 KB)

Sure (I was using UPBGE)

I got nothing. Sorry. :confused: Tried the mipmap false thingy and it does nothing.

1 Like

Same here, thank you for your help so far though! I will update here if I find a solution

1 Like

any updates?

Technically, I don’t think it’s possible not to get that anti aliasing effect with that filter, it samples a square containing multiples pixels and averages the values.
With the render-to-texture method, it’s not a matter of mipmapping but filtering. It is possible to change the filtering from linear to nearest using the OpenGL Wrapper module, bgl.

from bge import logic, texture
import bgl

controller = logic.getCurrentController()
owner = controller.owner

objects = logic.getCurrentScene().objects

cameraName = 'cam'

scene = logic.getCurrentScene()
camera = scene.cameras[cameraName]


if "RenderToTexture" not in owner:
    renderToTexture = texture.Texture(owner, 0)
    
    source = texture.ImageRender(scene, camera)
    source.capsize = [70, 70]
    
    renderToTexture.source = source
    
    owner["RenderToTexture"] = renderToTexture
    
#disable linear filtering
bgl.glActiveTexture(bgl.GL_TEXTURE0 + owner["RenderToTexture"].bindId)
bgl.glBindTexture(bgl.GL_TEXTURE_2D, owner["RenderToTexture"].bindId)

bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)
bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)

#without the following line, in UPBGE, textures will be missing in the rendered texture
bgl.glActiveTexture(bgl.GL_TEXTURE0)

owner["RenderToTexture"].refresh(True)

render-to-texture-nearest-filtering.blend (364.9 KB)

1 Like

For those interested, I made a pixelate filter that gives you sharp edges. You can check it on my answer on this thread.