Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: subspark on Sat 15/11/2008 09:57:02

Title: Possibility for a Displacement Plugin? [Cool Example included]
Post by: subspark on Sat 15/11/2008 09:57:02
Would it be possible for a plugin to be written that allows screen buffer distortion?
Have a look at this effect done in Adobe After Effects:

Displacement:
(http://www.shuugouteki.net/paul/Images/displacement.gif)

Result:
(http://www.shuugouteki.net/paul/Images/underwater.gif)

Is this possible in AGS?

Cheers,
Sparx.
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: Anteater on Sat 15/11/2008 18:03:50
I'd say it's absolutely possible with a plugin, but some work would be required.
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: Gilbert on Sat 15/11/2008 18:09:13
Actually, I think it is even possible without a plugin. Just grab a dynamic of the whole screen and do whatever modification you want to it. It may be quite slow though.
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: GarageGothic on Sat 15/11/2008 18:29:21
I wrote a module (not yet released to the public) that allows you to turn Objects into "EffectObjects" that can work as lenses, mirrors and/or just provide distortion of whatever background/object/character is behind it. For a water distortion effect similar to what you describe it uses simple sine functions performed on columns and/or rows of pixels. Using a graphical displacement map would make it much, much slower as you would need to call GetPixel on every pixel of the displacement map. As is, it's still pretty tough on the CPU, but keeps decent framerates as long as you don't use too many or too large effect objects in the same scene.

For full-screen manipulation a plugin would be preferable, and absolutely necessary if you want to use displacement maps. I don't think it should be too difficult to code, but I don't have any real experience with plugins as I prefer using modules.
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: subspark on Sat 15/11/2008 22:16:53
Would anyone be up for the challenge? It would be really cool to see some games made with real-time distortion effects such as the ones described. You could even do forcefields or shockwaves! 8)

Cheers,
Sparx.
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: BOYD1981 on Sat 15/11/2008 22:38:46
i'm no expert on the subject but what with the DirectX engine couldn't this sort of stuff be done if shader support was implemented?
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: subspark on Sun 16/11/2008 13:14:20
Thats kind of what I'm thinking. But something that could be run (in theory) on the PC equivilant of a PSP Processor.

Cheers,
Sparx. :)
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: Dualnames on Tue 18/11/2008 09:47:21
If someone manages to write anything to do that effect we will have all AGS games under-water!!
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: subspark on Tue 18/11/2008 23:51:24
QuoteIf someone manages to write anything to do that effect we will have all AGS games under-water!!
Or with cool room to room transitions! ;D

Sparx.
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: Ryan Timothy B on Wed 19/11/2008 05:12:21
One thing I really could see this being used for, is heat waves.  Like in Final Fantasy 3 where you fight in the desert with the heat waves in the background.
I love the idea of having this effect.
I could probably do a simple version of this (with hack programming of course--nothing but. :P), but definitely nothing like you've shown here.  Especially with the displacement background.

Rather than me explaining how I would attempt to do this the overcomplicated way, I'll just show a picture.

(http://www.bryvis.com/images/other/agsf/distorteffect.png)

I would place a bunch x and y coords on the screen (Green), connect them all in the form of a triangle (incomplete demonstration pic above), and have the x and y coords randomly move around +/- 1 or 2 pixels on both x and y randomly.  But never letting them break the triangle by going through itself.  Then I'd check each pixel in the VERY first background (always keeping a fresh copy of this background for reference) and align them to their knew mathematical location in the new triangle.  Wouldn't be too, too, hard but I bet AGS would hate it and your computer would as well.

Oh, and you'd have to keep a radius limit as to where the x+y coords could move and not beyond (you'd have to keep the x and y coords from the original triangle location to where it is now).  Otherwise the effect would literally walk off the screen.
What you'd do with the 'stretched' pixel I wouldn't have a clue.  And that is what would make the background look like poo. :P
Title: Re: Displacement Plugin? [Cool Example Included]
Post by: subspark on Wed 19/11/2008 09:25:24
Hey thats a pretty neat approximation! Good going Ryan. Does your method work or are you still testing it?

Good suggestion Ryan. Heatwaves are also very appropriate for screen displacement effects.
Also you could import the full-screen displacenment frames as an 8 bit grayscale 32 color set of sprites and add them into a looping view.
The image compression would certainly free up some more resources maybe even relax the proccessor a bit too?

Cheers,
Sparx.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: Ryan Timothy B on Thu 20/11/2008 03:55:44
Nah it was just an Idea of How I would do this myself.  I only slapped some lines on your pic to give a visual explanation on how I'd do this, IF I were to attempt it.

It definitely wouldn't look as good as your Adobe effect, but I think it would still pass as water/heat etc just as long as the triangles didn't stretch or shrink too much.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: subspark on Thu 20/11/2008 06:05:02
Well we can always animated backgrounds and have custom displacement loops for characters. But a plugin that allows dynamic solution would be extremely beneficial.
Cheers,
Sparx.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: Dualnames on Fri 21/11/2008 09:45:23
Still I think Radiant, is definetely a coding beast concerning raw draw cool effects..ask him..or make him do it ;D
Title: Re: Displacement Plugin [Cool Example Included]
Post by: subspark on Fri 21/11/2008 13:20:03
Radiant? You about buddy? I'd like to get your thoughts on such a plugin.

Cheers,
Sparx.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: Dualnames on Fri 21/11/2008 13:28:07
Well, thing is if it can be done internally with AGS Engine, I could definetely give it a shot. But concerning plugins, well I couldn't. I think the plugin is definetely something that would help AGS engine benefit.

As for the displacement, wondering does the displacement affect the characters, it should.
It should be done using Rawdraw taking input from Screenshot. However, what my concern and main difficult and obstacle as how to code is, what exactly does displacement do? Takes parts of the screen and moves them? Cause if it's that(as a main idea), it is definetely possible AGS internally.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: GarageGothic on Fri 21/11/2008 14:13:38
Dualnames: It's definitely possible to do in-engine (see my previous post), and quite easy scripting wise. Only horribly slow to do full-screen - and even worse when using Direct3D mode. Even grabbing a single screenshot to DynamicSprite causes my framerate to stutter in Direct3D mode, I couldn't imagine at what pace the game would run if doing it every loop.

The most realistic solution would be to use pre-rendered animation frames for the distorted background, either done in an external application such as AfterEffects or dynamically generated upon room entry and only apply real-time distortion to the characters/objects. I should be able to add character support to my effect module if enough people are truly interested.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: on Fri 21/11/2008 14:37:46
Let's say one would create five distorted images upon entering the room, and store these as the five possible cycling backgrounds- it wouldn't be as fluid an animation, but should save processor power. I like the general idea of a distortion plugin very much, but I see Dualnames point- it's eye candy that's easy to overuse.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: Dualnames on Fri 21/11/2008 21:44:06
I'll give it a shot..and if it succeeds at any point, I'll give it out. As for Ghost's words, well, yes it could get misused, but that happens with most of the cool things, even SSH star wars scroller can easily achieve the same effect. Just see how many games have Star Wars scrolling text effect.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: Ryan Timothy B on Fri 21/11/2008 22:19:17
Quote from: GarageGothic on Fri 21/11/2008 14:13:38
[..]Even grabbing a single screenshot to DynamicSprite causes my framerate to stutter in Direct3D mode, I couldn't imagine at what pace the game would run if doing it every loop.[..]

I don't understand why you'd need to take a screenshot between each frame.  Explain this to me if you will.
I'm not too advanced with AGS yet, so I there may be limitations I'm not aware of yet.  But I know how I'd do it in a program like Turbo Pascal, and it's very similar to how I'd do it in AGS.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: GarageGothic on Fri 21/11/2008 22:46:42
Well, if you want to distort the full screen as in subspark's example, you need a DynamicSprite copy of the current screen (including objects and characters at their current position, with their current animation frame and current lighting) to then distort. Arguably you only need to grab the screen when something has changed, but since there's no easy way to determine when this has happened, we do it every frame.

Creating a DynamicSprite from a screenshot rather than just creating it from the background is much slower in Direct3D mode as the data must be read back from the video card's memory rather than system RAM. For this reason, instead of grabbing a screenshot to DynamicSprite, my EffectModule replicates the area behind the object in question by grabbing part of the background and then cycling through all the characters and objects in the room, checking if they're overlapping the object and if so, sizing and tinting their graphics to match the actual objects/characters) using DynamicSprite functions and then drawing them in baseline order to the background sprite. This method improves the speed considerably compared to using a screenshot, but I think it could cause quite a slowdown if used full-screen in a room with more than one or two characters and a few objects.

This is also why I suggest pre-distorting the background graphics (storing the frames as DynamicSprites you could have much more than 5 animation frames, though you would probably have to delete and then re-render the frames when saving/restoring to avoid 20+ MB savegame files). Running the realtime distortion only on characters should allow decent framerates.

Title: Re: Displacement Plugin [Cool Example Included]
Post by: subspark on Sat 22/11/2008 00:35:42
There's been some great responses from everybody here. It's nice to see everybody experimenting either in their heads or in practice.

QuoteIt's eye candy that's easy to overuse.
Sure. But the same could be said for laser guns in movies or the fade transitions that Chris implemented into the modern AGS. ;)
"Something great is always remade because it is great and doesn't perish upon birth." I forget which philosopher said that.

Cheers,
Sparx.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: Trent R on Sat 22/11/2008 01:47:56
QuoteIt's eye candy that's easy to overuse.
So are lens flares, but everybody knows that you shouldn't use them. If they try to, it's immediately shot down once it reaches the CL, GiP, or Database.

And I'd like to add that I'd love to see it, even though I wouldn't use it (anytime soon at least)

~Trent
Title: Re: Displacement Plugin [Cool Example Included]
Post by: subspark on Sat 22/11/2008 10:26:31
Thanks for that.  :P
Title: Re: Displacement Plugin [Cool Example Included]
Post by: on Mon 24/11/2008 13:22:59
Hmm, then let me rephrase: It's easy to overuse, and thus, it should be highly customizeable.

That's what I really had in mind, sorry if it came over the wrong way.
Title: Re: Displacement Plugin [Cool Example Included]
Post by: Trent R on Mon 24/11/2008 18:11:45
Not at all, I got what you meant. And I meant that if it does become a trend, it will control itself through the forums mentioned.


~Trent
Title: Re: Possibility for a Displacement Plugin?
Post by: Gilbert on Mon 24/11/2008 19:44:17
Since the subject of this thread was so misleading in a way that made people think such a plugin has been made, I have decided to change it to something more reasonable. :P
Title: Re: Possibility for a Displacement Plugin?
Post by: subspark on Mon 24/11/2008 20:22:43
Thanks Gilbet. Somehow I lost my question mark when I added [Cool Example Included] next to the original  subject.

Cheers,
Sparx.