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 2021 in Questions Posts: 1,364

is there one? thanks!



  • dave1707dave1707 Mod
    Posts: 10,047

    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: 911

    @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: 10,047

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

    function setup()
    function draw()
    meshC = class()
    function meshC:init(v1,v2,v3)
    function meshC:draw()
  • Posts: 1,364

    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: 10,047

    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 2021 Posts: 323

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

  • SimeonSimeon Admin Mod
    Posts: 5,781

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

    The original thread by @SkyTheCoder is here:

    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: 1,819

    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,724
    @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: 1,364

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

  • SimeonSimeon Admin Mod
    Posts: 5,781

    @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: 1,364

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

Sign In or Register to comment.