Updating custom geometries

I have a lot of custom geoemtries that are rebuild quite-often (voxel chunks). When I’ve recreated the geometry I do the following to re-create the model. Is this the correct way of doing this in Urho3d or am I doing something that gives me overhead performance wise? (Note the “isBuilt” check first where I for the first time create the object otherwise just updates the index/vertex buffers)

    if(!iisBuilt) {
        int bb = 0;
        if(size_x > size_y && size_x > size_z) {
            bb = size_x;
        } else if(size_y > size_x && size_y > size_z) {
            bb = size_y;
        } else {
            bb = size_z;
        model->SetBoundingBox(BoundingBox(0, (float)bb));

        elements.Push(VertexElement(TYPE_VECTOR3, SEM_POSITION));
        elements.Push(VertexElement(TYPE_VECTOR3, SEM_NORMAL));
        elements.Push(VertexElement(TYPE_VECTOR3, SEM_COLOR));

        geom->SetVertexBuffer(0, vb);

        model->SetGeometry(0, 0, geom);
        node->SetPosition(Vector3(from_x, from_y, from_z));
        object = node->CreateComponent<StaticModel>();
    } else {
    vb->SetSize(vertices.Size(), elements);

    ib->SetSize(vertices.Size(), false);

    geom->SetDrawRange(TRIANGLE_LIST, 0, vertices.Size());

    Vector<SharedPtr<VertexBuffer> > vertexBuffers;
    Vector<SharedPtr<IndexBuffer> > indexBuffers;

    PODVector<unsigned> morphRangeStarts;
    PODVector<unsigned> morphRangeCounts;
    model->SetVertexBuffers(vertexBuffers, morphRangeStarts, morphRangeCounts);

    ResourceCache* cache=GetSubsystem<ResourceCache>();

Any input would be helpful! Thanks

Here’s what I do for updating geometry (thread safe)

Tested it on voxel rendering and runs pretty fast when updating

Actually… I might need to change where I do the MutexLock in that code since you want your critical blocks to be small… Still, currently it works well.

Thanks for sharing, I will compare your code with mine later and try it out :slight_smile:

I tried to make new custom geometry inside a thread, but after I added it to the scene, it crashes, any idea ?
@Victor: I think your project is similar to mine :slight_smile:

Hey there! As long as you’re not attempting any rendering, generating the vertex buffers, or setting of textures inside of another thread you should be fine. All graphical aspects of the code should be done in the main thread, however, calculations (for normals, tangents, voxels chunks, etc), can be put onto another thread.

I hope that helps! :slight_smile:

I create node, create Custom Geometry and create the vertices, apply materials… inside an other thread

I load textures and techniques in the main thread.

I do all the rest of the loading an other thread and I add it to the scene in the main thread.

How is it not possible to create the vertex buffers in an other thread ?

How is it not possible to create the vertex buffers in an other thread ?

You can’t set buffer data from a thread that isn’t the main thread.

Actually, I can ! I just succeded.
Below is the solution for the main thread side. For the other thread, you just have not to commit CustomGeometry…

    Urho3D::Node* tmpNode;
    if (pipeline->try_pop(tmpNode)) {
      Urho3D::PODVector<Urho3D::CustomGeometry*> components;
      tmpNode->GetComponents<Urho3D::CustomGeometry>(components, true);
      for (auto compo : components) {
1 Like

Interesting, what renderer are you using?~ nvm, I overlooked the commit.