Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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 ;) ). 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.

Comments

  • SimeonSimeon Admin Mod
    Posts: 4,837

    That's beautiful @SkyTheCoder, really nice effect

  • IgnatzIgnatz Mod
    Posts: 5,396

    Very nice, good to see you back

  • Jmv38Jmv38 Mod
    Posts: 3,295

    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'
    
  • edited December 2015 Posts: 1,976

    @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.

  • Jmv38Jmv38 Mod
    edited December 2015 Posts: 3,295

    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.

  • Posts: 1,976

    @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.

  • Jmv38Jmv38 Mod
    Posts: 3,295

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

  • IgnatzIgnatz Mod
    edited December 2015 Posts: 5,396

    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!

  • Posts: 1,976

    @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...
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    I can get to 255 at 60 Fps

  • Posts: 2,020

    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?

Sign In or Register to comment.