3D snow using OpenGL instancing

Hey everyone, sorry I haven’t posted in a while, I’ve been having a lot of troubles getting my iPad to charge, and it might be a while until I can use Codea again (unless it comes out for Mac OS X :wink: ). Anyway, in the spirit of holiday things, I made a new demo of 3D snow, this time using OpenGL’s instancing to increase quality and performance. It generates a single mesh of randomly positioned and rotated triangles covering a wide and short area, and draws it a bunch of times using instancing to create a ton of snow without much of a hit to performance. It runs at 60 FPS on an iPad Air, rendering 90,000 vertices (I think I could get it to 120K or 150K but it went a little below 60 FPS), which is pretty good.


View on Github

View on Pastebin


Also, if you turn up the “stretch” parameter, toggle the “night” parameter, and increase the “timeSpeed” parameter, it looks like you’re traveling at warp speed.

That’s beautiful @SkyTheCoder, really nice effect

Very nice, good to see you back

i get this error

Main:40: bad argument #1 to 'shader' (Shader not found)
stack traceback:
	[C]: in function 'shader'
	Main:40: in function 'setup'

@Jmv38 Sorry, I forgot to include a function in another library - add this to the end of the Utility tab:

-- Function tweak to make shaders easier, so I can call shader("X") instead of shader(Shaders.X.vS, Shaders.X.fS)
local _shader = shader
function shader(...)
    if select("#", ...) == 1 then
        local data = Shaders[select(1, ...)]
        if data ~= nil then
            return _shader(data.vS, data.fS)
        else
            return _shader(...)
        end
    else
        return _shader(...)
    end
end

Edit: Updated gist and paste to include function.

thank you @SkyTheCoder now works great.
Really nice.
why

draw()
...
    collectgarbage()
    collectgarbage()
    collectgarbage()
end

?
wouldnt 1 be enough?
And in the draw function…? usually it is bad for fps.

@Jmv38 I didn’t think it was bad for FPS. I can’t really remember why, but I just got into the habit of putting three collectgarbage() calls in a row at the end of the draw function, to prevent memory leaks slowing things down. Try removing them and see if you can increase the number of triangles per mesh without affecting the FPS.

@SkyTheCoder i was just asking out of curiosity. You program is very fast, so the collectgarbage() doesnt harm, here.

What is interesting is that on my Air 2, I can get to 401 instances at 60FPS, but at 402, ie just one more, FPS drops to about 3!

@Ignatz I think it has something to do with the maximum number of instances at a time, it seemed to be around 400. Try increasing the number of triangles per mesh and see if you can push it further, like here:

    ...local vspread = 5
    for i = 1, **200** do
        local rel...

I can get to 255 at 60 Fps

Really nice and very beautiful. The matrix transforms in the draw loop seem a little heavy though. I wonder whether it might be possible to pre-calculate some paths for the instances and store them in a look-up table?