Create mesh at runtime (SOLVED)

Is it possible to create a mesh in runtime?

1 Like

Using buffers?

2 Likes

@selimanac Thanks so much! The Point Primitive Example seems to be no more available on github.

Anyway, I am able to create a buffer, set it to a mesh and manipulate the buffer in runtime. What I would like to do is: create multiple meshes at runtime, each one with its own buffer. It seems to me that this is not possible at the moment.

In my use case, I can cap the maximum number of meshes I need and create them all in the editor.

Do you see any other solution?

Thanks!

1 Like

I believe it should be possible, @mozokevgen might have a tip for you, in Cozy Cut, there were countless new meshes in runtime created!

1 Like

Thanks for the link. Very good talk from @mozokevgen ; I have already watched it in the past. I am not sure but it seems to me that it is not doing what I need. Or, more probably, I have not been able to explain myself in a clear way. Let me try again.

I would like to create meshes in runtime but each mesh should have its own buffer.

I tried the following:

  1. I create a game object with a mesh and a factory for this game object
  2. I create some instances of this gameobject via factory.create
  3. I modify the buffer of a mesh of one of these instanced gameobject
  4. The mesh of ALL instances is modified.

The final result is clearly what one should expect since the various instances are sharing the same mesh resource. For this reason it seems to me that what I want is impossible at the moment. What is needed is something like: resource.create_mesh.

As I already said, for my use case, I plan to have a small number (say 16?) of meshes so I can create all of them in the editor.

Thanks!

1 Like

Hmm, I believe this should be working. This could be a bug.

I’m not able to try this right now, but if you are using the built-in model material, it should be using world space. Perhaps you can try it with local vertex space.

2 Likes

@selimanac Thanks!

When I wrote ā€œinstancedā€ I didn’t mean it in the technical sense, I should have wrote ā€œcreatedā€œ by factory.create.

On the other hand, I am quite confused about ā€œinstanceā€ and the problem I am facing. All instances (in the technical sense) share the same local data geometry. So this should not be related to what I would like to implement.

I hope this makes sense…

Thanks again.

1 Like

Are you creating a new buffer for each mesh or are they sharing the same mesh?

2 Likes

I have: (1) a gameobject with a mesh component, this mesh has its buffer (2) a factory for this gameobject.

I use factory.create to create two gameobjects, let’s call them go_A and go_B. I create a new buffer in code and I set this buffer to the mesh of go_A. I see that also go_B uses the same geometry of go_A.

I hope this is somehow clearer. I apologize for not being able to explain my context.

Thanks!

1 Like

You don’t change mesh for go
You take mesh resource from go. And change resource.

If you need unique mesh for go, you need to make new resource and change resource for go instance

3 Likes

How do you set the buffer?
WIthout seeing the code, it sounds like you’re just replacing the shared resource, using resource.set_buffer() (Like @d954mas says).

Instead, you should use the vertices property of the mesh, to set a custom mesh:

go.set(mesh_url, “vertices“, mybuffer)
3 Likes

Thanks @Mathias_Westerdahl @d954mas

Yes, I think what you said is the point! But while this works

    local res = go.get(mesh_url, "vertices")

    resource.set_buffer(res, buf)

when I try

go.set(mesh_url, ā€œverticesā€, buf)

I get the following error:

ERROR:GAMEOBJECT: Properties type can not be determined.
ERROR:SCRIPT: main/main.script:365: the property ā€˜vertices’ of ā€˜(null)’ must be a hash

In both fragments, the buffer buf is created via buffer.create, mesh_url is valid and is set as

local mesh_url = msg.url(nil, self.id1, ā€œmeshā€)

What am I doing wrong?

Thanks!

1 Like

You need to create a buffer resource, and you’ll get the resource path hash back. Use that to set the ā€œverticesā€.

3 Likes

@Mathias_Westerdahl Thank you! Now everything works! And I can create as many meshes at runtime as I need! Great!

Thank you to everyone who replied and helped me!

3 Likes

I’m a bit late for the discussion, but here is small example of mesh + buffer resource creation:

example_1 is the simpliest. All other has some additional manipulations.

I have a feeling, that I need to pack my presentation in some kind of a ā€œgeneral moduleā€, so it could be easy reused…

8 Likes