I’m trying to play with mesh instancing and I’m getting really odd visual artifacts if I go over maybe 200 or so instances. Am I misunderstanding how this works or is there a bug somewhere?
function setup()
width = 50
height = 50
numInstances = width * height
m = mesh()
m.shader = shader(vert, frag)
m:addRect(0,0,5,5)
m:setColors(color(255,200,0))
transform = m:buffer("transform")
transform:resize(numInstances)
transform.instanced = true
for i=0,(numInstances-1) do
local x = i % width
local y = math.floor(i / width)
transform[i+1] = matrix():translate(6 + x*6, 6 + y*6)
end
end
function draw()
background(40, 40, 50)
m:draw(numInstances)
end
vert=[[
#version 300 es
uniform mat4 modelViewProjection;
in mat4 transform;
in vec4 position;
in vec4 color;
out lowp vec4 vColor;
void main()
{
vColor = color;
gl_Position = modelViewProjection * (transform * position);
}
]]
frag=[[
#version 300 es
in lowp vec4 vColor;
out lowp vec4 fragColor;
void main()
{
fragColor = vColor;
}
]]
Here’s an example showing an array of meshes with a texture.
function setup()
size=20
m=mesh()
for x=1,WIDTH/size do
for y=1,HEIGHT/size do
m:addRect(x*size,y*size,size,size*2)
end
end
m.texture="Planet Cute:Character Cat Girl"
end
function draw()
background(40, 40, 50)
m:draw()
end
That’s the same visual result I’m going for, yes, but not the purpose of this experiment. I’m trying to use instancing in order to do the same thing and it’s glitching out. What I don’t know is if I’m instancing wrong or if Codea has a bug.
I did some more experimenting and it looks like there’s a bug or limitation somewhere. Any more than 256 instances will start corrupting the visuals. Perhaps internally Codea is storing the instance count as a byte?
I don’t think vertex count is the issue - there are only 6 in the mesh. Instancing is a technique for telling the GPU to redraw the same mesh some number of times in order to avoid using a lot of vertices in the mesh itself or needing so many draw calls.
@BigZaphod in the code you had initially posted, you have not enabled the instancing extension, reference the first line of both of the vert shaders @dave1707 posted:
#extension GL_EXT_draw_instanced: enable
you also need to then use the built in gl_InstanceIDEXT attribute to access the instance number (Dave uses this in the yOffset float in his shaders)