Original shader from shadertoy: Bloom.blend (92.1 KB)
Modified shader using rendered screen result:
The result differs from the original shader (I can’t match up the colors and sRGB).
For normal color schemes the colorRange at line 9 in _Image.fs has to be set to a much lower value. Bloom2.blend (92.3 KB)
I have been trying to modify the shader as well ( not complete ) . My python script was almost as same as yours. I bindcoded the image of one filter to another one.
But this line 34 void mainImage( out vec4 fragColor, in vec2 fragCoord ) kinda solved the problem I had hahas…
Hi, someone else asked me to use a similar method with 2D filters offscreens, then I share 2 new files using a backbuffer from a previous frame to do some effects. I tried to keep the files as simple as possible and the shaders are simple too but could be improved. bufferPreviousFrame.blend (511.9 KB) MotionBlur.blend (515.8 KB)
These files could be done with bge using ImageRender I think but it’s for upbge as it has an API for 2DFilter offscreens
Refactored the bufferPreviousFrame code into a Python component, could be better maybe but it is okay for what it does
# Credits: inspired from HG1 work on a bloom filter ported in bge
# I guess this can be used to create motionblur effects...
# refact: wkk.py
import bge
shader = '''\
// Simple shader usecase
uniform vec2 mouse;
//uniform vec2 resolution;
uniform sampler2D bb;
uniform float bgl_RenderedTextureWidth;
uniform float bgl_RenderedTextureHeight;
vec2 resolution = vec2(bgl_RenderedTextureWidth, bgl_RenderedTextureHeight);
void main() {
float radius = .1;
vec2 lightDir = mouse - (gl_FragCoord.xy / resolution.xy);
lightDir.x *= resolution.x / resolution.y;
float D = length(lightDir);
vec2 L = normalize(lightDir);
float md = max(D - radius, 0.) / radius + 1.;
float at = 1. / (md*md);
at = (at - .25)/(1.-.25);
at = max(at, 0.);
gl_FragColor = vec4(vec3(at), 1.) + 0.975 * texture2D(bb, (gl_FragCoord.xy / resolution.xy));
}
'''
class BufferThing(bge.types.KX_PythonComponent):
args = {}
def start(self, args):
scene = self.object.scene
self.filter = scene.filterManager.addFilter(
0, bge.logic.RAS_2DFILTER_CUSTOMFILTER, shader)
# In this case we use the same shader for backbuffer and the main shader
self.buffer = scene.filterManager.addFilter(
1, bge.logic.RAS_2DFILTER_CUSTOMFILTER, shader)
self.buffer.addOffScreen(2, hdr=bge.render.HDR_HALF_FLOAT)
self.isFirstFrame = True
def update(self):
mouse = bge.logic.mouse
if self.isFirstFrame:
self.isFirstFrame = False
return # skip first frame
# The texture is the result of what was stored in the offscreen at the previous frame (backbuffer)
self.filter.setTexture(0, self.buffer.offScreen.colorBindCodes[0], "bb")
self.filter.setUniform2f("mouse", mouse.position[0], 1 - mouse.position[1])
# Prepare the backbuffer image for the next frame (send it needed uniforms) (will be available in next logic frame)
self.buffer.setTexture(0, self.buffer.offScreen.colorBindCodes[0], "bb")
self.buffer.setUniform2f("mouse", mouse.position[0], 1 - mouse.position[1])
I was able to re-write this (my first real big glsl script!)
I had to make it update the buffer and then push the mip mapped image into the buffer without mipmapping it again mixed into the bloom attachment mip mapped
Abuffer.fs