Skip to content

Instantly share code, notes, and snippets.

@Vavius
Created November 11, 2015 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vavius/d25cfec7beecb47c1d8e to your computer and use it in GitHub Desktop.
Save Vavius/d25cfec7beecb47c1d8e to your computer and use it in GitHub Desktop.
local vsh = [=[
attribute vec4 position;
attribute vec2 uv;
attribute vec4 color;
varying MEDP vec4 colorVarying;
varying MEDP vec2 uvVarying;
void main () {
gl_Position = position;
uvVarying = uv;
colorVarying = color;
}
]=]
local fsh = [=[
varying MEDP vec4 colorVarying;
varying MEDP vec2 uvVarying;
uniform float progress;
uniform float angleOffset;
uniform sampler2D sampler;
#define M_PI 3.141592653589793
#define M_2_PI 6.283185307179586
void main () {
float a_rad = M_PI - atan(uvVarying[1] - 0.5, uvVarying[0] - 0.5);
a_rad = a_rad / M_2_PI;
a_rad = mod(angleOffset + a_rad, 1.0);
// float a = step(progress, a_rad);
// replace with proper step func
%s
LOWP vec4 color = texture2D ( sampler, uvVarying ) * a;
gl_FragColor = color * colorVarying;
}
]=]
local normal = "float a = step(progress, a_rad);"
local inverse = "float a = step(a_rad, progress);"
local function CreateProgram(inv)
program = MOAIShaderProgram.new()
program:setVertexAttribute ( 1, 'position' )
program:setVertexAttribute ( 2, 'uv' )
program:setVertexAttribute ( 3, 'color' )
local step = inv and inverse or normal
local fragment = string.format(fsh, step)
program:load(vsh, fragment)
program:reserveUniforms(2)
program:declareUniform(1, "progress", MOAIShaderProgram.UNIFORM_FLOAT)
program:declareUniform(2, "angleOffset", MOAIShaderProgram.UNIFORM_FLOAT)
return program
end
local function CircleProgressShader(inverse)
local shader = MOAIShader.new()
shader:setProgram(CreateProgram(inverse))
shader:setAttr(1, 1)
shader:setAttr(2, 0)
return shader
end
local function floorTo(x, snap)
return snap * math.floor(x / snap)
end
local function SetProgress(shader, p, steps)
local p = 1 - p
if steps then
p = floorTo(p, 1 / steps)
end
shader:setAttr(1, p)
end
local function SetOffset(shader, o)
shader:setAttr(2, o)
end
--=====================================================================--
-- Sample scene
--=====================================================================--
MOAISim.openWindow ( "test", 320, 480 )
viewport = MOAIViewport.new ()
viewport:setSize ( 320, 480 )
viewport:setScale ( 320, -480 )
layer = MOAILayer2D.new ()
layer:setViewport ( viewport )
MOAISim.pushRenderPass ( layer )
gfxQuad = MOAIGfxQuad2D.new ()
gfxQuad:setTexture ( "moai.png" )
gfxQuad:setRect ( -64, -64, 64, 64 )
gfxQuad:setUVRect ( 0, 0, 1, 1 )
prop = MOAIProp.new ()
prop:setDeck ( gfxQuad )
layer:insertProp ( prop )
shader = CircleProgressShader ()
prop:setShader ( shader )
thread = MOAICoroutine.new ()
thread:run ( function ()
progress = 0
while true do
SetProgress ( shader, progress % 1 )
progress = progress + 0.01
coroutine.yield ()
end
end )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment