Tiled animated texture:
This shader loads a tiled texture and can play the textures in a row.
Dude, it's texture animation directly on the GPU for AppGameKit!
You won't belive the speed difference by using this shader...
There are some out-of bounds errors that I try to fix, so it should be possible to use this shader without activating the imageUV
scrolling. Still, this shader is the most powerful solution for texture animation...
GLSL shaders are very powerful. I never realised, that we could actually build our games in GLSL and use AppGameKit as a GLSL Player
It is very creepy, that you can even build editors in glsl.. :S
I would really like to see more math commands like matrix4 in AppGameKit, so I could create some serious GLSL shaders for AppGameKit with it.
This code does require a 4x4 tiled texture called
test.png in the media folder, it's in the attachments.
AGK2 T1:
// Project: Animated Texture
// Created: 2016-05-04
// set window properties
SetWindowTitle( "Animated Texture" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
tex= LoadImage("test.png")
box= CreateObjectBox(5,5,5)
shader=LoadShader("anitex.vs","anitex.ps")
SetObjectColor(box,255,100,100,255)
SetObjectImage(box,tex,0)
SetImageWrapU(tex,1)
SetImageWrapV(tex,1)
SetObjectShader(box,shader)
// image tiles for the shader
tx = 4
ty = 4
SetObjectShaderConstantByName( box, "Animation", tx, ty, tx*ty, 1.0-(1.0/(tx*ty)) )
SetObjectShaderConstantByName( box, "m_Speed",1,0,0,0)
do
print(timer())
SetObjectShaderConstantByName( box, "Time",timer(),0,0,0)
Print( ScreenFPS() )
Sync()
loop
ps:
uniform sampler2D texture0;
varying highp vec3 posVarying;
varying mediump vec3 normalVarying;
varying mediump vec2 uvVarying;
varying mediump vec3 lightVarying;
mediump vec3 GetPSLighting( mediump vec3 normal, highp vec3 pos );
mediump vec3 ApplyFog( mediump vec3 color, highp vec3 pointPos );
void main()
{
mediump vec3 norm = normalize(normalVarying);
mediump vec3 light = lightVarying + GetPSLighting( norm, posVarying );
mediump vec3 color = texture2D(texture0, uvVarying).rgb * light;
color = ApplyFog( color, posVarying );
gl_FragColor = vec4(color,1.0);
}
vs:
attribute highp vec3 position;
attribute mediump vec3 normal;
attribute mediump vec2 uv;
varying highp vec3 posVarying;
varying mediump vec3 normalVarying;
varying mediump vec2 uvVarying;
varying mediump vec3 lightVarying;
uniform highp mat3 agk_WorldNormal;
uniform highp mat4 agk_World;
uniform highp mat4 agk_ViewProj;
uniform mediump vec4 uvBounds0;
uniform mediump vec4 Animation;
mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );
uniform float Time;
uniform float m_Speed;
void main()
{
int iNumTilesU = int(Animation.x);
int iNumTilesV = int(Animation.y);
int numTilesTotal = int(Animation.z);
int selectedTile = 1;
selectedTile += int(Time*float(m_Speed));
// calculate the offset (fract is here, to keep the calcuations inside 0.0 - 1.0)
uvVarying.x = - (1.0 - float((uv.x + mod(float(selectedTile), float(iNumTilesU))) / float(iNumTilesU))); ///selectedTile;
uvVarying.y = (-uv.y - float(selectedTile / iNumTilesU)) / float(iNumTilesV)*-1.0; ///selectedTile;
highp vec4 pos = agk_World * vec4(position,1.0);
gl_Position = agk_ViewProj * pos;
mediump vec3 norm = normalize(agk_WorldNormal * normal);
posVarying = pos.xyz;
normalVarying = norm;
lightVarying = GetVSLighting( norm, posVarying );
}
EDIT:
Found the lines to stay in bounds:
(if you don't want to use SetImageWrapU/ V and want have precise calculations over a longer period of time )
update in vs:
uvVarying.x = (uv.x + mod(float(selectedTile), Animation.x)) / Animation.x;
uvVarying.y = (uv.y + mod(float(selectedTile / int(Animation.x)),Animation.y) ) / Animation.y;
BTW: Has anyone a normal map shader for AppGameKit, that works with the new light and fog functions mentioned in "Shaders in AGK"?
https://www.appgamekit.com/documentation/guides/13_shaders.htm
Feel free to see my little advert/ demonstration for more GLSL shaders in this thread: