Page 3 of 4

Re: Shader Help Thread

PostPosted: Wed Mar 28, 2018 5:14 am
by dpJudas
When a shader runs it can have multiple outputs. Each such output is called a gbuffer. Traditionally, the first output was called the frame buffer (or color buffer), but in a modern engine the output from the scene shaders rarely go directly to screen - instead, when the postprocessing shaders run they get those buffers as texture input and eventually the final output end up in the system frame buffer.

The shader producing the initial output, the "hardware shaders" as they are called in GZDoom, don't have to always output pre-shaded information. They could be made to output anything you'd like. As it is today, the shaders already output extra gbuffers when SSAO is active: the normal vector of the face and the color of the fog. Naturally, postprocessing shaders would have to also be able to read what the hardware shaders wrote.

Re: Shader Help Thread

PostPosted: Wed Mar 28, 2018 7:28 am
by Pixel Eater
Ok it sounds like we are on the same page (I just don't know the lingo :P ).

So I need a "hardware shader" from before the lighting is added to output a texture into a g-buffer which my post-processing shader can then read and compare with the regular input texture. By calculating the difference it should be possible to change colour depending on depth (rainbow fog anyone?).

Seeing as this is not on the horizon anyhow, should I make a feature suggestion anyway in case it gets placed in the "on-hold" queue?

Thanks for taking the time to write that BTW :cheers:

Re: Shader Help Thread

PostPosted: Wed Mar 28, 2018 8:02 am
by dpJudas
You're welcome. :)

I think this feature request is probably too big to get implemented by the usual suspects. It requires a lot of work.

Re: Shader Help Thread

PostPosted: Wed Mar 28, 2018 2:57 pm
by Pixel Eater
Ah, no worries then. I'm happy just knowing it's doable B-)

Re: Shader Help Thread

PostPosted: Thu Jun 14, 2018 12:46 am
by Paynamia
Is it at all possible to apply shaders to models at this point? I know it can be managed in Zandronum (and I think older versions of GZDoom) by placing a texture in the textures folder, but it doesn't seem to work in the current version of GZD.

Re: Shader Help Thread

PostPosted: Sun Jul 01, 2018 11:05 am
by Diode
Paynamia wrote:Is it at all possible to apply shaders to models at this point? I know it can be managed in Zandronum (and I think older versions of GZDoom) by placing a texture in the textures folder, but it doesn't seem to work in the current version of GZD.


Apply a shader to a texture used as a model's skin and it will work.

Re: Shader Help Thread

PostPosted: Sat Jul 21, 2018 5:41 am
by Diode
So, I've been trying to fake lighting on 3D models in areas without attenuated lights. Which is difficult when you have no clue about the calculations needed and next to no real coding ability. Oh well.



Here's a model in sector light, but I'm using a matcap texture to fake the look of a dynamic light coming from the camera . It looks pretty convincing for the most part, but the mapping is wrong - if we view the model from the other side, we can see that the highlight is no longer in the center, but around the edges:



It isn't working correctly because... This is an environment mapping shader:

Code: Select allExpand view
vec4 ProcessTexel()
{
   vec3 eyedir = normalize(uCameraPos.xyz-pixelpos.xyz);
   vec3 reflected = reflect(eyedir.xyz,normalize(vWorldNormal.xyz));   
   float m = 2.8284271247461903 * sqrt(reflected.z+ 1.0);
   return getTexel(reflected.xy / m + 0.5);
}


What needs to be changed to get the mapping to properly shift with the camera?

Re: Shader Help Thread

PostPosted: Thu Sep 06, 2018 10:55 pm
by Dr. Van Nostrand
I've been beating my head against this for hours and could use a hand if anyone's feeling charitable.

This is my attempt at a simple conversion of the "warp1" shader that comes with gzdoom to a post-processing shader:

Code: Select allExpand view
void main()
{
   vec2 texCoord = vTexCoord.st;

   const float pi = 3.14159265358979323846;
   vec2 offset = vec2(0,0);

   offset.y = sin(pi * 2.0 * (texCoord.x + timer * 0.125)) * 0.1;
   offset.x = sin(pi * 2.0 * (texCoord.y + timer * 0.125)) * 0.1;

   texCoord += offset;
   
   FragColor = vec4(texCoord.rgba);
   
}


At compile time I get this error:
0(18) : error C1031: swizzle mask element not present in operand "rgba

Where have I gone wrong exactly?

Re: Shader Help Thread

PostPosted: Thu Sep 06, 2018 11:00 pm
by Pixel Eater
Try 'FragColor = texCoord'.

If that doesn't work, try dropping the "v" from vTexCoord?

Re: Shader Help Thread

PostPosted: Thu Sep 06, 2018 11:03 pm
by Dr. Van Nostrand
Thanks Pixel Eater. I tried both recommendations but now get:

0(14) : error C1035: assignment of incompatible types

Re: Shader Help Thread

PostPosted: Thu Sep 06, 2018 11:07 pm
by Pixel Eater
Oh, hold on FragColor wants a colour not a coordinate. Instead try 'FragColor = texture( InputTexture, texCoord )'.

Re: Shader Help Thread

PostPosted: Thu Sep 06, 2018 11:20 pm
by Dr. Van Nostrand
Thanks again, now we're making some progress. I can get the shader to compile now with this code:

Code: Select allExpand view
uniform float timer;

void main()
{
   vec2 texCoord = TexCoord.st;

   const float pi = 3.14159265358979323846;
   vec2 offset = vec2(0,0);

   offset.y = sin(pi * 2.0 * (texCoord.x + timer * 0.125)) * 0.1;
   offset.x = sin(pi * 2.0 * (texCoord.y + timer * 0.125)) * 0.1;

   texCoord += offset;
   
   FragColor = texture(InputTexture, texCoord);
   
}


but the effect isn't what I expected. The screen is warped but doesn't update... if I stand still the warp effect doesn't move at all. I must be missing something with the timer...

Re: Shader Help Thread

PostPosted: Thu Sep 06, 2018 11:38 pm
by Pixel Eater
Ah, right. You'll need to add 'Uniform float timer' to the shader block in your GLDefs file which will look something like this:
Code: Select allExpand view
HardwareShader PostProcess scene
{
    Name "Shader Name"
    Shader "Shader Filename.fp" 330
    Uniform float timer
    Enabled
}

And in Zscript add 'Shader.SetUniform1f( players[ consoleplayer ], "Shader Name", "timer", gametic + e.FracTic )' into a RenderOverlay event handler.
Code: Select allExpand view
class "ShaderHandler" : EventHandler
{
   override void RenderOverlay( RenderEvent e )
   {
      Shader.SetUniform1f( players[consoleplayer], "Shader Name", "timer", gametic + e.FracTic );
   }
}

Re: Shader Help Thread

PostPosted: Fri Sep 07, 2018 12:06 am
by Dr. Van Nostrand
I'm not sure why but it's still not working. Here's a pk3 with all of the files. Thanks again for your help!

Re: Shader Help Thread

PostPosted: Fri Sep 07, 2018 12:16 am
by Pixel Eater
No problems, it just needed the event handler declared :)