- Joined
- Jul 31, 2017
- Messages
- 500
This snippet demonstrates the basics of texture coordinate animation.
Cerberus:
Strict
' ST-Animation / UV-Animation
' (in Cerberus X, texture coordinates are called ST)
Import mojo2
Function Main:Int()
New MyApp
Return 0
End
Class MyApp Extends App
Field canvas:Canvas
' material (texture and shader)
Field mat:Material
' vertex model and texture data
' 4 vertices, 2 coordinates each
Field verts:Float[4 * 2]
Field texcs:Float[4 * 2]
' primitives (quads, in this case)
' 1 quad, 4 coordinates
Field quads:Int[1 * 4]
' texture offset
Field s0:Float
Method OnCreate:Int()
SetUpdateRate(60)
canvas = New Canvas
' material must be loaded via Material.Load to disable texture clamping
mat = Material.Load("flower.png", Texture.Filter, null)
' initialize model data
' starting top/left going clockwise, x and y coordinate each
Local x:Float = 100.0
Local y:Float = 50.0
Local w:Float = 256.0
Local h:Float = 256.0
verts[00*2+0] = x
verts[00*2+1] = y
verts[01*2+0] = x + w
verts[01*2+1] = y
verts[02*2+0] = x + w
verts[02*2+1] = y + h
verts[03*2+0] = x
verts[03*2+1] = y + h
' init quad
' keep in mind to have vertices clockwise arranged in quad
quads[00*4+0] = 0
quads[00*4+1] = 1
quads[00*4+2] = 2
quads[00*4+3] = 3
Return 0
End
Method OnUpdate:Int()
' move texture (the '+1) Mod 1' helps keeping the value in 0..1)
s0 = (s0 + 0.01 + 1) Mod 1
Return 0
End
Method OnRender:Int()
' update tex coords
' starting top/left going clockwise, s and t coordinate each
texcs[00*2+0] = 0.0 - s0
texcs[00*2+1] = 0.0
texcs[01*2+0] = 1.0 - s0
texcs[01*2+1] = 0.0
texcs[02*2+0] = 1.0 - s0
texcs[02*2+1] = 1.0
texcs[03*2+0] = 0.0 - s0
texcs[03*2+1] = 1.0
' draw quad
canvas.DrawIndexedPrimitives(4, 1, verts, texcs, quads, mat)
canvas.Flush()
Return 0
End
End