Created
October 20, 2013 03:52
-
-
Save dermotbalson/7064857 to your computer and use it in GitHub Desktop.
Smoke puff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- flak | |
function setup() | |
shellbursts={} | |
counter=0 | |
nextExplosion=math.random()*2+1 | |
end | |
function draw() | |
nextExplosion=nextExplosion-DeltaTime | |
if nextExplosion<0 then | |
local s=Smoke(vec2(math.random(100,700),math.random(400,700)),256*(.25+.75*math.random()),2,7) | |
table.insert(shellbursts,s) | |
nextExplosion=math.random()*5+2 | |
end | |
background(117, 154, 181, 255) | |
for i,s in pairs(shellbursts) do | |
if s:draw()==false then table.remove(shellbursts,i) end | |
end | |
end | |
Smoke=class() | |
Smoke.img=nil | |
function Smoke:init(pos,size,explodeSecs,fadeSecs) | |
self.pos=pos | |
if Smoke.img==nil then Smoke.createImage(512) end | |
self.f={frac=0,fade=1} | |
local t1=tween(explodeSecs,self.f,{frac=1},{ easing = tween.easing.quadOut } ) | |
local t2=tween(fadeSecs,self.f,{fade=0},{ easing = tween.easing.linear } ) | |
tween.sequence(t1,t2) | |
self.m=mesh() | |
self.m.texture=Smoke.img | |
local w=Smoke.img.width | |
local x1,y1,x2,y2=-size/2,-size/2,size/2,size/2 | |
local tx1,ty1=math.random()*(1-size/w),math.random()*(1-size/w) | |
local tx2,ty2=tx1+size/w,ty1+size/w | |
v,t={},{} | |
v[1]=vec2(x1,y1) t[1]=vec2(tx1,ty1) | |
v[2]=vec2(x2,y1) t[2]=vec2(tx2,ty1) | |
v[3]=vec2(x2,y2) t[3]=vec2(tx2,ty2) | |
v[4]=vec2(x1,y2) t[4]=vec2(tx1,ty2) | |
v[5]=vec2(x1,y1) t[5]=vec2(tx1,ty1) | |
v[6]=vec2(x2,y2) t[6]=vec2(tx2,ty2) | |
self.m.vertices=v | |
self.m.texCoords=t | |
self.m:setColors(color(255)) | |
self.m.shader=shader(smokeShader.vertexShader,smokeShader.fragmentShader) | |
self.m.shader.frac=self.f.frac | |
self.m.shader.fade=self.f.fade | |
self.m.shader.size=size/w | |
self.m.shader.centre=vec2((tx1+tx2)/2,(ty1+ty2)/2) | |
self.timer=0 | |
end | |
function Smoke:draw() | |
self.m.shader.frac=self.f.frac | |
self.m.shader.fade=self.f.fade | |
pushMatrix() | |
translate(self.pos.x,self.pos.y) | |
self.m:draw() | |
popMatrix() | |
if self.f.fade>0 then return true else return false end | |
end | |
function Smoke.createImage(size) | |
local min,max=100,255 | |
local m=hmap.create(size,size,0.3) | |
local z1,z2=0,0 | |
for i=0,m.w do | |
for j=0,m.h do | |
z1=math.min(z1,m[i][j]) | |
z2=math.max(z2,m[i][j]) | |
end | |
end | |
local img=image(m.w,m.h) | |
local f=(max-min)/(z2-z1) | |
for i=1,m.w do | |
for j=1,m.h do | |
local intensity=min+f*(m[i][j]-z1) | |
img:set(i,j,color(intensity)) | |
end | |
end | |
Smoke.img=img | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment