Animating textures with hardware shaders

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.

Animating textures with hardware shaders

Postby boris » Fri Jun 12, 2015 4:16 pm

Some post on DW made me play with GZDoom's hardware shaders, and I realized you can use them to animate textures - or more specifically build compound textures where only parts are animated (unlike the swirling textures). I'm not sure if this has been done before, so I'll explain how it works. First, here's a video of what it looks like:



On the left only the stripes are animated. This is not done with multiple frames, so the animation is very smooth. On the right side is a green stone wall, with two layers of blood running down at different speeds.

As only one texture is processed by the shader I'm using a little trick - I'm just putting all the different parts I need in the same texture. The textures you see actually look like this:

Image Image

Then I'm utilizing the fact that the shaders can access other parts of the texture and do something with the retrieved information. For example in the blood fall texture I'm taking the blood parts, constantly scroll them a little and then blend them over the stone and each other. The shaders look like this:

Silver texture
Spoiler:


Blood texture
Spoiler:


I'm not really good with shaders, so the code could most likely be improved. It should also be possible to make more advanced, cooler looking effects. The example map from the video is attached to this post.
There's one "problem" though. You have to make sure that the line you place the texture on is no longer than the compound texture result. In the case of the example textures that is 64 units. That means if you want to place them on a 128 unit long wall you have to split that wall in 64 unit parts. If you fail to do so you'll see the unanimated parts of the texture.
You do not have the required permissions to view the files attached to this post.
boris
I post less than Manc and Hobo
 
Joined: 15 Jul 2003

Re: Animating textures with hardware shaders

Postby boris » Sat Jun 13, 2015 1:45 am

boris wrote:There's one "problem" though. You have to make sure that the line you place the texture on is no longer than the compound texture result. In the case of the example textures that is 64 units. That means if you want to place them on a 128 unit long wall you have to split that wall in 64 unit parts. If you fail to do so you'll see the unanimated parts of the texture.

Looks like this can be fixed by modifying to shift all coordinates left when the x component is biggerthan 1/3.
boris
I post less than Manc and Hobo
 
Joined: 15 Jul 2003

Re: Animating textures with hardware shaders

Postby Nash » Sat Jun 13, 2015 1:49 pm

The fact that the texture continues to animate even when the game is paused/console is open/menu is open prevents me from ever taking shaders in GZDoom seriously and I won't even consider using them...
User avatar
Nash
AKA Nash Muhandes! Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: Animating textures with hardware shaders

Postby Graf Zahl » Sat Jun 13, 2015 2:28 pm

The fact that all other texture animations also continue while the game is paused must mean then that you cannot take ZDoom as a whole seriously, right?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
 
Joined: 19 Jul 2003
Location: Germany

Re: Animating textures with hardware shaders

Postby Gez » Sat Jun 13, 2015 2:38 pm

Nash wrote:The fact that the texture continues to animate even when the game is paused/console is open/menu is open prevents me from ever taking shaders in GZDoom seriously and I won't even consider using them...

Have you missed that this happens to all animated textures in ZDoom, regardless of how they're animated? Does that prevent you from taking (G)ZDoom seriously and from considering using it?

The only way to have animations that pause when the game pause is to have your animation handled by something that is tied to game logic, like ACS or DECORATE code. Or to use a port that's not derived from ZDoom.
Gez
 
 
 
Joined: 06 Jul 2007

Re: Animating textures with hardware shaders

Postby edward850 » Sat Jun 13, 2015 4:14 pm

Nash wrote:The fact that the texture continues to animate even when the game is paused/console is open/menu is open prevents me from ever taking shaders in GZDoom seriously and I won't even consider using them...

Have you missed how this has been a thing since 1993?
Honestly it's hard to take you seriously sometimes.

Edit: Well then. My mind is playing tricks on me again.
Last edited by edward850 on Sat Jun 13, 2015 5:36 pm, edited 1 time in total.
User avatar
edward850
[netcode intensifies]
 
Joined: 19 Jul 2005
Location: New Zealand

Re: Animating textures with hardware shaders

Postby Gez » Sat Jun 13, 2015 5:23 pm

edward850 wrote:Have you missed how this has been a thing since 1993?


It's not vanilla behavior. I don't feel like looking back for when exactly ZDoom started doing that. It's old, but not "older than ZDoom itself" old.
Gez
 
 
 
Joined: 06 Jul 2007

Re: Animating textures with hardware shaders

Postby edward850 » Sat Jun 13, 2015 5:35 pm

Actually, yeah. That's really weird, because I could have sworn I had seen it manifest from there, and yet not even Boom exhibits it, or the old ZDoom 1.17c. What I'm then probably remembering is the HUD continues to animate.
But then that begs the question as to where this does come from? Best guess is the interpolation code needed a different timer?
User avatar
edward850
[netcode intensifies]
 
Joined: 19 Jul 2005
Location: New Zealand

Re: Animating textures with hardware shaders

Postby NeuralStunner » Sat Jun 13, 2015 7:05 pm

Since AnimDefs frames can use "partial tics", I guess it was necessary. That capability might come from Boom, I don't know.
User avatar
NeuralStunner
Not "Neutral"
 
 
 
Joined: 21 Jul 2009
Location: capital N, capital S, no space
Discord: NeuralStunner#4201
Operating System: Windows Vista/7/2008 64-bit
Graphics Processor: nVidia (Modern GZDoom)

Re: Animating textures with hardware shaders

Postby Caligari87 » Sat Jun 13, 2015 7:31 pm

This is really cool, I just wish I could actually make sense of how that shader code works, or at least get a "for dummies" reference. Wouldn't mind trying my hand at a few.

8-)
User avatar
Caligari87
User Accounts Assistant
 
Joined: 26 Feb 2004
Discord: Caligari87#3089
Github ID: caligari87

Re: Animating textures with hardware shaders

Postby Graf Zahl » Sun Jun 14, 2015 1:45 am

NeuralStunner wrote:Since AnimDefs frames can use "partial tics", I guess it was necessary. That capability might come from Boom, I don't know.



That's precisely the reason. Animation speed is uncoupled from the playsim, not only for partial tics but also to allow animating menu graphics.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
 
Joined: 19 Jul 2003
Location: Germany

Re: Animating textures with hardware shaders

Postby boris » Sun Jun 14, 2015 2:38 am

Caligari_87 wrote:This is really cool, I just wish I could actually make sense of how that shader code works, or at least get a "for dummies" reference. Wouldn't mind trying my hand at a few.

8-)

As I said I'm not really good with shaders myself, but I'll try to explain how the shaders works (so don't expect this to be the ideal or even good way). The shader computes the color of one pixel on your screen and not of the actual texture. If you look closely you'll see that the animation works on a sub-texture-pixel level.

Here's a breakdown of the silver texture shader:

Code: Select allExpand view
vec2 background = gl_TexCoord[0].st;
vec2 foreground = gl_TexCoord[0].st;
vec2 highlight = gl_TexCoord[0].st;

This sets three 2-dimensional vector variables. gl_texCoord[0].st gets the x/y components of the current texture position. They are all the same, but will then be modified point to other pixels. background will be the blue part, foreground the silver part, highlight the light stripe part. It's important to understand that the values are not in real texture dimensions (in this example192x128), but in the range of 0.0 to 1.0, where (0.0, 0.0) is the top left and (1.0, 1.0) is the bottom right. So the actual texture has these positions:
Image
(note that the x values written as 0.333 and 0.666 are actually 1/3 and 2/3)

Code: Select allExpand view
background.x += 1.0/3.0;

This adds an offset of 1/3 (one third) of the texture dimension to the x position of the background vector. That means if the topmost leftmost pixel (0, 0) is processed, this vector will now point to (0.333..., 0), or to the topmost leftmost pixel of the blue part of the texture.

Code: Select allExpand view
highlight.x += 1.0/3.0*2.0;

This adds 2/3 (two thirds) of the texture dimension to the x position of the highlight vector. That means if the topmost leftmost pixel (0, 0) is processed, this vector will now point to (0.333..., 0), or to the topmost leftmost pixel of the highlight part of the texture.

Code: Select allExpand view
highlight.y += timer * 0.5;

This modifies the y position of the vector pointing to a pixel on the highlight part of the texuture. It takes the game time into account, so as time goes on the position will change, which will give the illusion that the texture is scrolling. It's multiplied by 0.5 (or divided by 2) so that it doesn't scroll superfast.
Let's say we're processing the topmost leftmost pixel of the texture (0, 0), at time 0 highlight.y will be 0. At time 0.1 highlight.y will be 0.05. At time 0.2 highlight.y will be 0.1 and so on.

Code: Select allExpand view
highlight.y = highlight.y - floor(highlight.y);

As said as the beginning, the texture dimension is between 0.0 and 1.0, so this makes sure highlight.y stays in that range. This is necessary because the value of highlight.y computed in the previous step grows bigger and and bigger as time goes on. For example a time 314.6 highlight.y will be 157.3, which will be reduced to 0.3 here. So basically it starts back at 0 once 1.0 is reached.

Code: Select allExpand view
vec4 backgroundTexel = getTexel(background);
vec4 forgroundTexel = getTexel(foreground);
vec4 highlightTexel = getTexel(highlight);

Gets the texel information at the position of each in RGBA format (I guess getTexel is a function defined by GZDoom). So at this point the shader has color information of three different pixels from the texture.

The following part says what color the pixel shown on the screen shoud have.

Code: Select allExpand view
if(forgroundTexel.a > 0)
   return forgroundTexel * color;

This checks if the pixel we want to show is transparent or not. If it is not transparent the silver texture will be displayed, otherwise we'll go into the else branch below. Effectively this will draw everything but the "windows" of the texture. It's multiplied by color (which is supplied to the shader) to take light level, color, fog etc. into account.
Code: Select allExpand view
else
   return (backgroundTexel * highlightTexel) * color;

If the alpha of foregroundTexel is 0 the shader will draw the light. It will do so by multiplying the background (blue) color with the highlight color. The highlight color is in a range of black and white, so multiplying it with the background color makes the blue either brighter or darker. Finally it's multiplied with the color supplied to the shader.

And that's pretty much it. Hope it helps.
boris
I post less than Manc and Hobo
 
Joined: 15 Jul 2003

Re: Animating textures with hardware shaders

Postby Nash » Sun Jun 14, 2015 11:53 am

Wow so much hostility in this thread, I didn't mean what I said in a bad way of course. :D Everyone here is aware by now that ZDoom is still my engine of choice despite me complaining about this and that - and probably will be forever - not even those fancy new AAA engines could attract me.

Yeah I'm aware that animated textures used to be one with the playsim because in older versions of ZDoom, such was the case and then somewhere along the way this was changed... which is normally not a big deal at all for normal Doom play but for non-Doom projects, and depending on what specific purpose, the continuously animating textures when the game is paused can be either undesirable or even "immersion breaking"... of course my comment was spoken in a "non-Doom-project" context but eh, I'll live with it. :)
User avatar
Nash
AKA Nash Muhandes! Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: Animating textures with hardware shaders

Postby Tormentor667 » Sun Jun 14, 2015 12:12 pm

Tarin ftw!
User avatar
Tormentor667
needs more detail
 
Joined: 16 Jul 2003
Location: Germany

Re: Animating textures with hardware shaders

Postby kodi » Tue Jun 16, 2015 11:29 am

I'll save this for later and see what can be done with it. Really neat!

Kinda OT: There's an idea I've been pondering about shaders, could they be used to rotate sprites and textures smoothly?
User avatar
kodi
Proscrastinator
 
 
 
Joined: 06 May 2013

Next

Return to Editing (Archive)

Who is online

Users browsing this forum: No registered users and 1 guest