在Unity中动态处理大量的对象/网格

huangapple go评论77阅读模式
英文:

Dynamically work with a very large number of objects/meshes in unity

问题

I want to render a large number (>10.000) of similar (but not completely identical) objects. In addition, I would like to be able to color and move the objects individually. These objects are currently cylinders with different amount of vertices/triangles.

My Questions for the Experts here is what would be the most effcient way of rendering and interacting with that many objects.

CombineInstance

What i have done so far is created a MeshManager class which takes each individual mesh and combines them with CombineInstance to a whole new mesh. If there are too many vertices a second, third,.. CombineInstance is created. The class nows in which combined mesh which individual mesh (and the corresponding vertices) is stored. The Problem is if i now want to color each mesh i color the corresponding vertice.

Example:
This is done for each Mesh (i.e. 10.000 Times)

Color[] newColor = mesh.colors;

// Run through all vertices of mesh
for (int vertexID = indexRange[0]; vertexID <= indexRange[1]; vertexID++)
{
    newColor[vertexID] = color[0];
}

mesh.SetColors(newColor);

This is really slow for many objects with many vertices! Especially the mesh.colors call seems to be slow.

Individual GameObjects

Much faster to color and move but no batch effect which means many draw calls. I have then below 30 FPS which makes it not feasible.

GPU instancing

As i can not ensure that i have the same exactly the same object (vertices/triangles) the DrawMeshInstanced and DrawMeshInstancedIndirect Methods can not be used. So Compute Shaders are not an option

As i want to run this on the Hololens performance is important. Did i miss something? Are there any Unity tricks?

英文:

Question:

I want to render a large number (>10.000) of similar (but not completely identical) objects. In addition, I would like to be able to color and move the objects individually. These objects are currently cylinders with different amount of vertices/triangles.

My Questions for the Experts here is what would be the most effcient way of rendering and interacting with that many objects.

What i tried/thought of:

CombineInstance

What i have done so far is created a MeshManager class which takes each individual mesh and combines them with CombineInstance to a whole new mesh. If there are too many vertices a second, third,.. CombineInstance is created. The class nows in which combined mesh which individual mesh (and the corresponding vertices) is stored. The Problem is if i now want to color each mesh i color the corresponding vertice.

Example:
This is done for each Mesh (i.e. 10.000 Times)

Color[] newColor = mesh.colors;

// Run through all vertices of mesh
for (int vertexID = indexRange[0]; vertexID <= indexRange[1]; vertexID++)
{
    newColor[vertexID] = color[0];
}

mesh.SetColors(newColor);

This is really slow for many objects with many vertices! Especially the mesh.colors call seems to be slow.

Individual GameObjects

Much faster to color and move but no batch effect which means many draw calls. I have then below 30 FPS which makes it not feasible.

GPU instancing

As i can not ensure that i have the same exactly the same object (vertices/triangles) the DrawMeshInstanced and DrawMeshInstancedIndirect Methods can not be used. So Compute Shaders are not an option


As i want to run this on the Hololens performance is important. Did i miss something? Are there any Unity tricks?

答案1

得分: 1

实体-组件-系统 正在由Unity开发,用于描述你提到的使用情况。 这是一个相当深奥的领域,尽管最终可能是你寻找的东西。

> 实体组件系统(ECS)架构将标识(实体)、数据(组件)和行为(系统)分离。该架构专注于数据。系统读取组件数据流,然后将数据从输入状态转换为输出状态,实体然后进行索引。

有关渲染的用例示例可以在这里这里找到,例如。

英文:

The Entity-Component-System is being developed by Unity for the very use case that you describe. It is quite the rabbit hole to go down into, though it might ultimately be what you are looking for.

> An Entity Component System (ECS) architecture separates identity (entities), data (components), and behavior (systems). The architecture focuses on the data. Systems read streams of component data, and then transform the data from an input state to an output state, which entities then index.

Examples for rendering as a use case can be found here and here, for instance.

huangapple
  • 本文由 发表于 2023年3月9日 21:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定