Is it possible to create a mesh in runtime?
Using buffers?
@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!
I believe it should be possible, @mozokevgen might have a tip for you, in Cozy Cut, there were countless new meshes in runtime created!
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:
- I create a game object with a mesh and a factory for this game object
- I create some instances of this gameobject via factory.create
- I modify the buffer of a mesh of one of these instanced gameobject
- 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!
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.
@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.
Are you creating a new buffer for each mesh or are they sharing the same mesh?
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!
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
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)
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!
You need to create a buffer resource, and youāll get the resource path hash back. Use that to set the āverticesā.
@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!
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ā¦