I have a script that dynamically creates a set of meshes to render a lightning bolt between two points. Starting to look good but since all the meshes are really well suited for triangle strip rendering I was curious if it was possible to render them like that. I’ve heard Unity uses triangle strip optimization methods of some type anyways so just not sure how to access that functionality.
Yeah, the mesh class feels like a bit of a black box, it would be great if one of the devs could explain all of that so we know the most optimal way to use it. Untiy 4 added some additional methods and enums to that class as well but thus far no documentation explaining best use or how exactly the engine reacts to changes in mesh instances internally.
I have a 2D system using the mesh class for a strategy game (1000+ units on mobiles, gameobjects were way too slow) but I’m still not sure if I’m being fully optimal with it. I can’t afford to to build the system several different ways just to see if using the mesh class this or that way might be faster - mystery meat coding is not fun.
Although my biggest tip for mesh class performance would be, if you can avoid setting new triangle indices each frame then that will help. Just set unused triangle vertices to use the same position (thus creating a zero size triangle) and not touching the [ ]triangles array each frame should help a lot. My system uses a kind of buffer and only changes the underlying triangle list every batch of 16 sprites (every 64 verts) - and basically anything else you can do to avoid allocations prompting garbage collection. For example, if you have lots of point lists for each lighting bolt and the contents/size of those lists are changing every frame then don’t add/remove points/class-references directly. Instead wrap that array/list with some methods and a counter then upon removal decrement it’s counter and re-assign the last point/class-ref in the list to the item that was just “removed” thus you avoid the internal array copy upon item removal. The copy itself might not slow you down, it’s the overhead of the garbage collector will start to sap CPU time, especially on mobiles!.
There is this undocumented function of Mesh class.
It works like SetTriangles function, but you need to feed the triangles array as a trianglestrip.
EDIT: And this is to get them:
Unity 4 has SetIndices, which can use MeshTopology.TriangleStrip. So instead of doing mesh.triangles = myTriangles, you use SetIndices instead.
–Eric
Hi,
How does it works if I want to update multiple polygons trails using the same material in a single mesh ? I mean, how is made the separation in the triangles strip indices from a trail to another ?
Am I forced to define a submesh for each trail, even if they all use the same material ?
MeshTopology.TriangleStrip no longer exists. If it did, I expect you could use degenerate triangles.
–Eric
Ok thanks, problem solved ^^
Pardon me for bumping this so much later, but it is not clear to me how to write triangle strips rather than complete triangles. Am I able to programmatically write triangle strips into the mesh at runtime or do I need to do those kind of operations in openGL?
Thanks,
r
You can directly access the triangle indices, vertices, uv’s, etc, through the Mesh class. The triangles array is simply an array of ints that point anywhere in the vertex array, and you can make them form strips easily enough.
Thanks, although I would like to keep them as strips for rendering, instead of loading and array of triangles and running the optimize method. Ideally, I would like to reduce what is being passed over the bus to the renderer, as I am finding lag on mobile.
r
Alright, after writing a long treatise on why you were being dense, I actually googled what made triangle strips special, and now I have the same questions as you I don’t see how one could use triangle strips via the Mesh API.
It’s not currently possible, since MeshTopology.TriangleStrip was removed for reasons unknown after an all-too-brief existence.
–Eric
It’s quite probable I was being dense, and I don’t mind being led to ‘the light’… So thanks for taking the time.
I’m guessing that I can fire the vertex list directly at OpenGl, but I was hoping to hear that I could register the triangles for collisions and ray-casting as well. I would be curious why the calls were removed, as I would gladly trade unconformed geometry for reduced latency.
Thanks again,
r
There isn’t any direct access to OpenGL in Unity. There is the GL class, but despite the name it’s not really OpenGL (after all it has to work on Direct3D too), and it’s nowhere near as efficient as the Mesh class since you have to manage everything manually using scripting.
–Eric
You can have direct access by writing a plugin. The plugin can access the Direct-X / Open GL apis directly.