Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Shaders in AGK2 BASIC Current Version Simple Examples

Author
Message
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 16th Jun 2017 20:30 Edited at: 6th Jul 2017 16:07
UPDATE: I created some very simple examples of creating and using shaders with AGK2 BASIC and attached the project to a post down below.

---------------------------------------------


Hello! I recently decided to get back to some more game dev or at least experiments.

I have an interest in learning shaders and have searched all around the forum and found things scattered here and there as well a big Shaders thread that has a lot of complex stuff and the ones I tested do not seem to work anymore.

Just wondering could someone create 5 simple AGK2 BASIC shader examples and post here?

Something like...

* Outlining a sprite with a glow that we change color in AGK2 code

* A fire FX that causes a sprite to appear like it is on fire (fire kind of over it, around it and through it)

* Same two shaders as above only examples are for 3D objects

* Fullscreen shader for posterization or scan lines or a plasma or whatever


It would be great to see ultra simple examples that JUST WORK "out of the box". Then we (those wanting to get into it and have a small amount of time) have a starting point and can start playing around.

Thank you!
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 17th Jun 2017 03:31 Edited at: 17th Jun 2017 03:34
Aha! I had to greatly simplify. Started with the Default Sprite Shader that is described in the AGK2 Shader documentation.
Then just played around with it. Guesswork mainly. Now I get it. I think. Pretty straightforward it seems. This Pixel / Fragment shader is actually called on a pixel-by-pixel basis. Which makes sense now that I think about it. lol

Anyway, I made a simple shader to change the color and then one to apply a tint. Just ultra basic stuff so far. I guess what I will do is after I have made a collection of tiny shaders growing in complexity I will post them in here to help others get a jump start.

Eventually, I should be at a point where I can figure out how to do edge detection (which should just be determining when the Alpha becomes 0 for a sprite image) and then be able to do a glow.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 17th Jun 2017 04:28
nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 17th Jun 2017 22:41
A great start for learning basic shader techniques is this tutorial on ShaderToy.

Use this tutorial to understand exactly what's going on and what exactly a shader is.
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 19th Jun 2017 02:09 Edited at: 19th Jun 2017 02:09
@nz0 that is a fantastic tutorial. I was familiar with ShaderToy but have never seen this tutorial before. This is exactly the kind of tutorial I like the best. Hands on code-oriented tutorial so I can play around. Makes it very easy to learn.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 5th Jul 2017 16:52
Now that I'm done with my tiny 3D invaders game for the "jam" type thing I will get my head back into the work I was doing on shaders and post the examples I came up with.

All very basic stuff but the Shaders thread has plenty of advanced things. The simple examples I will post might be a bridge between no shader knowledge and being able to understand other more complicated shaders.

I will try to do this to tonight or tomorrow.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 5th Jul 2017 19:58 Edited at: 5th Jul 2017 19:59
Just a brief note to say on the last half of my lunch break I started packaging my experiments up into a single project with the shaders split out into multiple files building progressively on the last.

The shader FX will all be visible on screen at the same time using sprites as a sort of step by step visual confirmation of what each shader is doing. I think I will definitely have this completed and posted tonight.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 6th Jul 2017 00:24 Edited at: 6th Jul 2017 00:27
Okay... attached to this post is a simple Shader Examples project in AGK2 BASIC. It is only 4 KB.

When run you should get this...


Look inside the media/shaders folder to find the 5 shaders that are used.

Like I said these are extremely simple shaders. The purpose of it is to give people a starting point. So many examples seem to jump straight into more advanced stuff and as a result a person doesn't have any opportunity to learn the fundamentals of how these things work. Hopefully these super simple shaders will make it easy for people to learn the basics as well as make it easy for them to experiment.

And now with this done the next time I get back to development I can focus on the Game Jam Template!

Anyway, I hope someone finds this useful.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)

Attachments

Login to view attachments
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 6th Jul 2017 01:23 Edited at: 6th Jul 2017 01:30
Thank you. Very enlightening
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 6th Jul 2017 16:10
@blink0k glad you liked it. I just figured when I first got into AGK2 and wanted to check out shaders I would have been thrilled to have a super simple & small project like this as a starting point. So hopefully it helps out others now and in the future.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Hughbus
AGK Backer
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location:
Posted: 7th Jul 2017 00:32
Yeah this is a fantastic little project to help anyone getting started with shaders in AGK. Thank you for sharing it with us all!
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 7th Jul 2017 00:56
Thanks @Hughbus I'm glad you all find it useful and appreciate me sharing it. One never knows if people will look at something and say "ah what a bunch of junk". lol
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Hughbus
AGK Backer
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location:
Posted: 7th Jul 2017 02:44
Hey Gar, ran into a few problems running the program (unedited) on my end. On both my PC and iPhone I was seeing these weird artefacts scrambling over the top of your sprites. Not sure what the cause would be, the pixel shaders look pretty simple.

Attachments

Login to view attachments
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 7th Jul 2017 02:49 Edited at: 7th Jul 2017 02:50
Hmm... in my very brief time with shaders I have seen artifacts (very much like noise) that come as a result of not initializing variables or not providing an output for a pixel (I think can't really remember on this last one).

Seems very odd you are getting it on the color bars example.

Thanks for letting me know. I haven't felt like diving into the game jam template yet so I will take a look at this project and see if I can reproduce the behavior you documented.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 7th Jul 2017 03:11
@Hughbus

When you get a chance please test this new project and see if you get the same behavior. I was unable to reproduce it but I tried to fix the things I thought might possibly cause it.

TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)

Attachments

Login to view attachments
Hughbus
AGK Backer
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location:
Posted: 7th Jul 2017 03:27
Gave it a quick test - seems to be exhibiting the same behavior
Hughbus
AGK Backer
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location:
Posted: 7th Jul 2017 03:41
Hey Gar, managed to solve the problem. switched all the incremental '+=' arithmetic to '='. I think what it was essentially doing was increment the value of newColour with the vec4 value and so cycling colours each fragment as it updates.

GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 7th Jul 2017 04:11 Edited at: 7th Jul 2017 04:12
@Hughbus Great work in sorting it out. Please post your updated project so people can have the more reliable one.

Feel free to change the name and title to AppGameKit Community's Simple Shader Examples and we can just run with this being a community project and maybe you or others can add on more shaders each just adding a little bit more.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Hughbus
AGK Backer
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location:
Posted: 7th Jul 2017 10:20
Here we go, updated Simple Shader Project with the tweaks. Your original actually worked on my laptop at home, so it must be a Graphics hardware specific issue. Either way, this version works on both

Attachments

Login to view attachments
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 8th Jul 2017 14:48 Edited at: 8th Jul 2017 14:52
Quick glance over your shader.
•1 if it helps readability create new variables.
•2 have only one gl_FragColor at the end of your shader, having more can cause those strange artifacts(gl_FragColor.rgb and gl_FragColor.a is okay)
•3 if you can use "*" except of "/" its a tiny bit faster : )

3.ps

Using AGKv2 Tier1
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 8th Jul 2017 18:01
Hey @janbo thanks!

I suspected the use of the gl_FragColor multiple times could be the cause of the artifacts on some machines and updated the shaders example up above.
I probably should update the original post though. I will do that. And will also include the update made by @Hughbus in the OP.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 8th Jul 2017 21:12
Ah okay.
by the way ...for "if (newColor.r > 1.0) newColor.r = 1.0;" there is the clamp function "newColor=clamp(newColor, 0.0, 1.0);"

Using AGKv2 Tier1
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 8th Jul 2017 21:47 Edited at: 8th Jul 2017 21:54
@janbo ha ha! Yeah I am sure there are many proprietary / domain specific functions and such. I always try to not learn much of that kind of thing and rely only on fundamentals. Some of it I use of course. But for the shaders I don't know any of that stuff and often a lot of these functions that are made seem kind of pointless to me although they do save a little typing.

I basically approach all of my development from the perspective of using the most basic code that can be easily ported from language to language. Of course most languages do have many such functions but if I am using only a limited amount of the proprietary stuff then I don't even need to worry about it and is less work for me when it comes time to port it something else. It's just a habit I got into long ago. Much like when I was developing in Unity I used a very different approach than the norm. Creating a space invaders game there I developed it very much like I do in AGK2 except I had to mess with their GameObjects and monobehaviours (well one monobehaviour anyway to get a hook into their callback system for Start, Update, etc) and work in the editor a bit here and there. Which is why I am now here.

Having said that with the shaders if I decide to pursue it more at some point I probably will take advantage of more of the command set. I just have no plans on doing any more work with shaders at this point. It was just a moment of curiosity thing that spawned this.

Too much to do! Too many things I want to do. lol And often (like today) I don't even feel like doing any development at all. It would be cool if I was working on the game jam template but alas I have no desire to do so. And this is another reason why I generally don't use much of the given languages / apis specialized functions. Because that requires me to spend time digging through documentation to learn what they are. And I have no desire to do that either any more than the bare minimum that I need to get stuff done.

I think a big part of it is having done that for decades I am just kind of burned out on spending time learning every new api. Thankfully, only a portion of each are actually necessary to know.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)

Login to post a reply

Server time is: 2024-11-22 07:31:35
Your offset time is: 2024-11-22 07:31:35