🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Need help with a grab Shader

Started by
7 comments, last by trsh 4 years, 6 months ago

How can I make grab Shader similar to https://docs.unity3d.com/Manual/SL-GrabPass.html in GLSL.

The idea, is that you grab exact pixels from background (ex. from composited renderTarget) and put on rendered object. So it's like 100% transparency, because it always has the pixels right behind. Later if you disort the pixels, can give very cool effects like https://www.shadertoy.com/view/Mdc3Rl.

SO far I have texture of background (plane to renderTarget), and object cube right before it. My vertex shader for cube is gl_Position = WVP * spos; and I have no idea how do the frag shader to copy the pixels from background. Any help appreciated.


Advertisement

You need to attach the rendered scene (aka the render texture) to the shader and then simply transform the texture coords not in UV units but in pixels so that your cube is "pixel perfect" aligned to the render texture.

I did something similar for a high-performance debug console where I handled my text buffers as an R8-Texture. I then read the character from the buffer, determined the screen position in pixels to place the line to and mapped a 16x16 character (128x128 pixel) texture font to the fragments where the text should be placed instead.

float2 rasterPosition = float2
(
    _RenderSpace.x + (_ScreenParams.x / 2.0) + (_RenderSpace.z * (v.vertex.x + 0.5)),
    _RenderSpace.y + (_ScreenParams.y / 2.0) + (_RenderSpace.w * (v.vertex.y + 0.5))
);
vertex = float4
(
    (2.0 * (rasterPosition.x / _ScreenParams.x)) - 1.0,
    _ProjectionParams.x * (2.0 * (rasterPosition.y / _ScreenParams.y) - 1.0),
    _ProjectionParams.y,
    1.0
);

This maps the fragments to be rendered into screenspace (pixels) instead of renderspace so you then can simply transfrom your render texture UVs to it

Shaarigan said:

You need to attach the rendered scene (aka the render texture) to the shader and then simply transform the texture coords not in UV units but in pixels so that your cube is "pixel perfect" aligned to the render texture.

I did something similar for a high-performance debug console where I handled my text buffers as an R8-Texture. I then read the character from the buffer, determined the screen position in pixels to place the line to and mapped a 16x16 character (128x128 pixel) texture font to the fragments where the text should be placed instead.

float2 rasterPosition = float2
(
    _RenderSpace.x + (_ScreenParams.x / 2.0) + (_RenderSpace.z * (v.vertex.x + 0.5)),
    _RenderSpace.y + (_ScreenParams.y / 2.0) + (_RenderSpace.w * (v.vertex.y + 0.5))
);
vertex = float4
(
    (2.0 * (rasterPosition.x / _ScreenParams.x)) - 1.0,
    _ProjectionParams.x * (2.0 * (rasterPosition.y / _ScreenParams.y) - 1.0),
    _ProjectionParams.y,
    1.0
);

This maps the fragments to be rendered into screenspace (pixels) instead of renderspace so you then can simply transfrom your render texture UVs to it

Read your explanation, but don't get it. :(

My Shaders are like and that's probably stupid :/ because ain't working

//vertex
gl_Position = WVP * spos;
mposs = (WVP * spos).xy;
// frag
vec2 uv = gl_FragCoord.xy / vec2(1680.0, 1050.0);
fragColor.rgb = texture(snapshot, mposs.xy+uv.xy).rgb;
fragColor.a = 1.0;
float2 rasterPosition = float2
(
    _RenderSpace.x + (_ScreenParams.x / 2.0) + (_RenderSpace.z * (v.vertex.x + 0.5)),
    _RenderSpace.y + (_ScreenParams.y / 2.0) + (_RenderSpace.w * (v.vertex.y + 0.5))
);
vertex = float4
(
    (2.0 * (rasterPosition.x / _ScreenParams.x)) - 1.0,
    _ProjectionParams.x * (2.0 * (rasterPosition.y / _ScreenParams.y) - 1.0),
    _ProjectionParams.y,
    1.0
);

What is is this syntax for? Doesn't look like GLSL. Whats renderSpace? Whats ScreenParams?

Simply as:

vec2 uv = gl_FragCoord.xy / screenSize;
    fragColor.rgb = texture(snapshot, uv.xy).rgb;
    fragColor.a = 1.0;
trsh said:
Whats renderSpace? Whats ScreenParams?

RenderSpace is a uniform value that describes the offset and size of the plane you want to render to. Depending on your render matrix this can be {0, 0} to {1, 1} or even {-Screen.Width / 2, -Screen.Height / 2} to {Screen.Width / 2, Screen.Height / 2}. ScreenParams is also a uniform that describes the Width and Height of the physical screen currently rendered to.

The syntax is CG but shouldn't be difficult to translate to GLSL or SPIR-V shader

Thanks bro

This topic is closed to new replies.

Advertisement