Open MartinSStewart opened 5 years ago
I've done some research. It looks like WebGL 1* only supports instancing via ANGLE_instanced_arrays
. Unfortunately this doesn't suppprt my idea of having an array of uniform groups and running the vertex shader on all the attributes once for each group of uniforms. As I understand it, you instead include an additional attribute array that only advances after all of the "normal" attributes have been used by the vertex shader.
Since we need to use attribute arrays instead of an array of uniforms groups to get instancing to work, my guess is that a new function and new shader type will be needed. Something like this:
manyEntities :
InstanceShader attributes perInstanceAttributes uniforms varyings
-> Shader {} uniforms varyings
-> Mesh attributes
-> List perInstanceAttributes -- List.length here determines how many instances we draw. Also unlike attributes, perInstanceAttributes will change often.
-> uniforms -- Same data is used for all instances
-> Entity
I imagine adding a new shader type is a substantial change. Maybe even requires changes to the compiler. I think it's worthwhile but I imagine it won't be added anytime soon.
I haven't worked with WebGL much so someone let me know if I'm totally off track or if there's a better solution.
*I'm assuming this package targets WebGL 1 because version 2 isn't supported on Safari or Edge yet.
@MartinSStewart thanks for investigating this use case! I'm not exactly sure if we should add custom api for each custom use case like this, or we should rather model WebGL api to be lower level than it is now.
By the way, there is a post on discourse about rendering particles. I linked to this issue from there.
Good point. Thanks for the link!
Both methods for instanced drawing:
are well supported now, maybe something like WebGL.entityInstanced
could be added?
@David-Klemenc sorry for the late reply. The functions that you linked to WebGL2, whilst this package only supports WebGL.
Support for instancing, aka being able to draw the same mesh many times with different uniforms, would be very useful for my project.
Here's an example of my current work around to be able to efficiently draw many instances of a single mesh (I've excluded some details but this is the general idea).
It's very repetative code and if I want a new shader that also draws multiple instances, I need to copy paste this code.
Also while it's faster than making one draw call per instance, having to do the following in the vertex shader is probably expensive.
I admit, I haven't entirely thought out how the elm-webgl API should be modified in order to support instancing. My initial idea is to change the uniform parameter in
entity
to a list. I'm happy to discuss how this might be organized.