Is updating the entire VBO on each frame the most efficient way to draw many changing unique triangles?

不想你离开。 提交于 2019-12-05 15:39:25

I took a look at your code and you've got a good bit of inefficiency in your JavaScript. For example, this line which is executed repeatedly:

convertedVerts = convertedVerts.concat(getConvertedVerts(pxVerts, zIndex, color));

can be extremely inefficient: a naïve execution of your program takes O(n^2) time where n is the number of vertices, because each time that line runs it copies elements into a new array.

By changing it to the following, I got much improved performance on Firefox and Safari (Chrome doesn't seem to care, perhaps because it optimizes the copying away):

convertedVerts.push.apply(convertedVerts, getConvertedVerts(pxVerts, zIndex, color));

Better still would be to change getConvertedVerts so that it takes the array to push onto rather than creating and returning one.

However, the way your program should probably be written is to allocate a Float32Array once, and then write new values directly into it (pass the array and a starting index around) in each frame. Avoid creating new arrays, and especially avoid creating new Float32Arrays, every frame, much less every vertex.

I would also recommend that you get rid of the intermediate canvas-emulation steps (like constructing an rgba() color and then parsing it again) — there is no reason to have any string-manipulation at all in your code, though this isn't nearly as much of an issue as the algorithmic inefficiency above.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!