Howdy, Stranger!

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

mesh instancing example, not shader?

edited April 27 in Questions Posts: 977

is there one? thanks!

r

Comments

  • dave1707dave1707 Mod
    Posts: 9,109

    I’m not sure what you mean by mesh instancing. I have 20 mesh examples but not sure which one would be what you’re after.

  • Posts: 860

    @RonJeffries There is the built in example Mesh which illustrates a simple rotating multicoloured triangle.

    Here is another mesh demo from way back - I think it was from when I first looked at meshes but don’t know if I wrote it or someone on the forums did. Not sure is this is what you are after


    -- spritemesh viewer.mode=FULLSCREEN -- Use this function to perform your initial setup function setup() test=image(400,400) setContext(test) sprite(asset.builtin.Cargo_Bot.Clear_Button,200,200,400,400) setContext() m=mesh() img={} img[1]=readImage(asset.builtin.Cargo_Bot.Codea_Icon) img[2]=test img[3]=readImage(asset.builtin.Planet_Cute.Brown_Block) img[4]=readImage(asset.builtin.Planet_Cute.Character_Boy) img[5]=readImage(asset.builtin.Space_Art.Icon) m.texture=img[1] obj={} for i=1,20 do table.insert(obj,{x=math.random(WIDTH),y=math.random(HEIGHT),a=math.random(360),size=10+math.random(30),spin=-5+math.random(100)/10,xspd=-3+math.random(7),yspd=-3+math.random(7),xcoord=(math.random(2)-1)/2,ycoord=(math.random(2)-1)/2}) end count=0 curimg=math.random(3) end -- This function gets called once every frame function draw() background(40, 40, 50) count = count + 1 if count>200 then count=0 end if count==50 then curimg=math.random(#img) end if count<100 then m.texture=test background(86, 119, 45, 255) else m.texture=img[curimg] end m:clear() -- This sets a dark background color for i,s in pairs(obj) do local id=m:addRect(s.x,s.y,s.size,s.size,math.rad(s.a)) m:setRectTex(id,0,0,1,1) s.a = s.a + s.spin s.x = s.x + s.xspd s.y = s.y + s.yspd if s.x>WIDTH then s.x=0 end if s.x<0 then s.x=WIDTH end if s.y>HEIGHT then s.y=0 end if s.y<0 then s.y=HEIGHT end end m:draw() end
  • dave1707dave1707 Mod
    Posts: 9,109

    @RonJeffries If all you want is a simple mesh, here’s one.

    viewer.mode=FULLSCREEN
    
    function setup()
        m1=meshC(vec2(20,100),vec2(WIDTH-20,100),vec2(WIDTH/2,HEIGHT/2-20))
        m2=meshC(vec2(20,HEIGHT-100),vec2(WIDTH-20,HEIGHT-100),vec2(WIDTH/2,HEIGHT/2+20))
    end
    
    function draw()
        background(0)
        m1:draw()
        m2:draw()
    end
    
    meshC = class()
    
    function meshC:init(v1,v2,v3)
        self.m=mesh()
        self.m.vertices={v1,v2,v3}
        self.m:setColors(0,0,0,255)
        self.m:color(1,255,0,0)
        self.m:color(2,0,255,0)
        self.m:color(3,0,0,255)
    end
    
    function meshC:draw()
        self.m:draw()
    end
    
  • Posts: 977

    instancing is mentioned in mesh:draw. it's a thing where you can associate an array of parameters and say m:draw(n) and draw a raft of copies of the mesh in one call.

  • dave1707dave1707 Mod
    Posts: 9,109

    I don’t know if my iPad supports instancing and I’m not sure how it’s supposed to be used. The doc doesn’t give much info.

  • edited April 28 Posts: 122

    @RonJeffries check out the Roller Coaster example nvm that's not what you're asking for

  • SimeonSimeon Admin Mod
    Posts: 5,587

    @RonJeffries instanced drawing allows you to define the position of each mesh "instance" in a vertex shader. I don't have a small, clear example on me but I did find a very pretty example on the forums. I have cleaned up the errors in the code and made sure it runs:

    https://gist.github.com/TwoLivesLeft/3356c5e5786155595bbfac114d8fa22f

    The original thread by @SkyTheCoder is here:
    https://codea.io/talk/discussion/7158/3d-snow-using-opengl-instancing

    You use the mesh:buffer() method to access a named buffer which you populate. This buffer usually corresponds to an attribute in the shader used on the mesh, or a built-in mesh buffer

  • Posts: 972

    In case anyone doesn’t want to deal with gists, or just likes a nice little icon once in a while, here’s a zip to suit your needs.

    Speaking of which, how does everybody else copy the code in gists? I’ve been manually selecting the last word and then painstakingly scrolling up to the top of the file for years now, and every time I do it, I think there must be an easier way. Mustn’t there?

  • Posts: 2,118
    @UberGoober - varies a bit, some allow zip download of code others you have to select all text (I tend to do it from Raw file. Also some have filename listed so you can download individual files. Think it's up to the author's discretion - but it can be a pain.
  • Posts: 977

    thanks, @Simeon ... at least it runs. i was hoping for something simpler. does instancing require shaders?

  • SimeonSimeon Admin Mod
    Posts: 5,587

    @RonJeffries yes, though there's room for an abstraction over it

    Basically when you call mesh:draw(instanceCount), mesh will render that many copies of itself, but for each instance the shader will vary based on the index of the named attribute buffers. So this allows you to position each snow flake, for example, in the shader

    It's an efficient way to draw things like particles. Where you don't want to loop over each mesh, each vertex, and apply some colour or transformation. Instead you supply that information as a buffer and then inside your shader you have access to the index for that instance

  • Posts: 977

    Gotcha. Was nearly there after beating my head against the example last night. Thanks!

Sign In or Register to comment.