AppGameKit Classic

AppGameKit Classic

评价数不足
Using Pixel or Fragment Shaders
由 Jammy 制作
This is a simple guide to using pixel or fragment (they are the same thing) shaders in AGK2.

The shader used in this example came from this tutorial here.

http://corbinpercy.com/2d-fractals-part-2-fractal-tutorial-using-shadertoy/

No media is needed.

Just

1) Crate a new project in AGK2 the Copy and paste the AGK2 code.
2) Open Notepad, Copy and paste the Shader code. Save it as Kaliset.ps into your projects media folder.
3) Run your program by hitting F5
   
奖励
收藏
已收藏
取消收藏
AGK2 code
`Create a new project then copy and paste this code. ( It will need the Kaliset.ps shader, in the media folder to run without error)

// Project: Pixel Shader turorial 1
// Created: 2015-01-05 by JammySoft

// set window properties
SetWindowTitle( "Pixel Shader turorial 1" )
SetWindowSize( 1024, 768, 0 )
SetSyncRate(0,0) ` this is unlimited frames per second

// set display properties
SetVirtualResolution( 1024, 768 )
`SetOrientationAllowed( 1, 1, 1, 1 )

` load a shader
` as we are using a sprite to display the shader we can work with pixel shaders and ignore vertex shaders at the moment.
Shader = LoadSpriteShader( "Kaliset.ps" )


` create a full screen sprite to apply the shader
CreateImageColor( 1, 255,0, 0,255 ) `this just creates an image of 1 pixel and colours it.

Sprite=CreateSprite(1) `make a sprite and call it sprite
SetSpritePositionByOffset(Sprite,0,0)

`assign the shader to the sprite.
SetSpriteShader( Sprite, Shader )
SetSpriteSize(Sprite,1024,768) ` make the sprite full screen

SetShaderConstantByName( Shader,"iResolution",1024,768,0,0 ) ` tell the shader the screen resolution
do
SetShaderConstantByName( Shader,"iGlobalTime",Timer()/2,0,0,0 ) `tell the shader what time it is - so it can update its animations
PrintC("F.P.S. = ")
Print( ScreenFPS() )
Sync()
loop

end
Kaliset Shader
//Here is the shader code modified from an original tutorial that was here http://corbinpercy.com/2d-fractals-part-2-fractal-tutorial-using-shadertoy/ (Website unfortunately no longer active).

Copy this code into Notepad and save it as "Kaliset.ps" into your projects media folder.


uniform vec3 iResolution; // viewport resolution (in pixels)
uniform float iGlobalTime; // shader playback time (in seconds)
uniform float iChannelTime[10]; // channel playback time (in seconds)
uniform vec3 iChannelResolution[10]; // channel resolution (in pixels)
uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down)
uniform sampler2D iTexture; // input channel 2D
uniform vec4 iDate; // (year, month, day, time in seconds)

const int iterations=12;

void main(void)
{
//this takes the pixel we are working with to determine where it is on the screen
vec2 z = gl_FragCoord.xy / iResolution.xy;

//fixes aspect ratio in favor of symmety. comment it and see what it looks like
z.y*=iResolution.y/iResolution.x;

//gloobywavez
z.x += sin(z.y*2.0+iGlobalTime * .2)/10.0;
//zooom
//z*= 1.2 + sin(iGlobalTime*.15);

//pan
// z+=vec2(sin(iGlobalTime*.2),cos(iGlobalTime*.01));

//rotate
//z=vec2(z.x*cos(iGlobalTime*.2)- z.y*sin(iGlobalTime*.2),z.y*cos(iGlobalTime*.2));

vec2 c=vec2(0.5, 1.1);

float average=0.;
float l=length(z);
float prevl;
for (int i=0; i<iterations; i++)
{
//kaliset base form
z=abs(z)/dot(z,z) -c;

//this is another function that can be iterated to produce some different fractals
//comment out the previous kaliset and experiment with values with this one!!
//z = abs(z)/(z.x*z.y)-c;

prevl=l;
l=length(z);

average+=abs(l-prevl);
}

//get the average length based upon the amount of iterations elapsed. multiply it to adjust "definition"
average/=float(iterations) * 15.;

//color fluctuation
average+=iGlobalTime*0.08;

vec3 myColor=vec3(0.2,0.21,.62);
vec3 finalColor;

//set the colors!
finalColor.r = (fract(float(average)/myColor.r));
finalColor.g = (fract(float(average)/myColor.g));
finalColor.b = (fract(float(average)/myColor.b));

gl_FragColor = vec4(finalColor,1.0);
}
8 条留言
Resourceful 2024 年 5 月 25 日 上午 1:34 
Resourceful 2021 年 4 月 16 日 上午 2:57 
I just found this post
when i tried to run it said it could not fined "Kaliset.ps"
after I move it into the media folder it worked

Collateral Damage 2021 年 4 月 13 日 上午 10:35 
For the current AGK2 version LoadFullScreenShader must be used.
Carhd 2020 年 7 月 8 日 下午 12:37 
@Jammy thank you for the tutorial.
Jammy  [作者] 2020 年 7 月 8 日 上午 1:39 
Thanks for the cool comments. Text updated to reflect that the original tutorial I worked from`s website is no longer active.
Carhd 2020 年 7 月 7 日 下午 9:28 
the website doesn't exist.
Conrad 2018 年 6 月 7 日 上午 5:21 
Great! Nice retro fractal effect. I recommend changing sprite position from SetSpritePositionByOffset(Sprite,0,0) to SetSpritePosition(Sprite,0,0) (so the sprite appears over the whole window and not just the top left corner), and set sync rate to 60 (SetSyncRate(60,0) instead of SetSyncRate(0,0)) so you don't overheat! :D Nice post Jammy!
JLansing 2015 年 1 月 5 日 下午 5:59 
HOLY! Something tells me I'm going to be playing catch up for the next few years. Thanks for sharing this! I look forward to seeing your future work as well. Feel free to add me on Steam. I make maps for Anarchy Arcade / Gmod. I don't ask a ton of questions, don't worry. lol