Change model texture in runtime (SOLVED)

Is it possible to do this for a mesh?
Can the sprite be replaced with a mesh?

  local resource_path = go.get("#sprite", "texture0")
  local header = { width=self.width, height=self.height, type=resource.TEXTURE_TYPE_2D, format=resource.TEXTURE_FORMAT_RGB, num_mip_maps=1 }
  resource.set_texture( resource_path, header, self.buffer )

Have looked at Pkeods awesome atlas dynamic image loading, but its a little different to what I need - setting mesh texture at runtime.

1 Like

Sorry to reply to myself but it does appear to work. Still testing.
Reference: https://defold.com/ref/stable/resource/

1 Like

Hey @dlannan, so there is possibility to change a mesh at runtime? Did you manage to do so? :wink:

Hey @Pawel. I have been away from Defold for a good 8 months (working in games business resulting in very little spare time). From memory, yes, I did make a mesh extension to do this - which allowed me to load gltfs among other things. Iā€™ll dig it up over the weekend and get back to you.

From memory, it kind of cheats :slight_smile: I make a dummy mesh object, then it fills the buffer at run time, but it does mean you can load in different gltfs into the same mesh, thus, I would expect it to handle mesh modification (which is essentially modifying those buffers).

Will get back to you.

4 Likes

Thank you very much then for responding! :heart:

Iā€™m thinking about primitive implementation of LOD, so I could have high res model and low res model and I could switch between them, instead of factoring new game objects and removing old :wink:

1 Like

Sorry I havent gotten back to this sooner. I really need to clone myself atm.
Interesting use for a LOD, Im not sure the performance would be there if you were writing verts when switching. I guess you might be referring to populating some buffers, then switching them using some factor?

1 Like

Hey @Pawel. I finally got to have a look at this. I did a large amount of bits and pieces in this space:

The more interesting project it eh MeshPool - which makes a bunch of dummy temp files for use as a modifiable mesh at runtime.
GLTF Loader has a thing called geomextension (C++) that allows direct manipulation of vertexbuffers at runtime - https://github.com/dlannan/defold-tools/blob/main/glTFLuaLoader/geomextension/src/myextension.cpp

I think I have a newer geomextension somewhere (findingā€¦)

This project I havet checked for a long time (not sure it builds because of newton) but the geomextension and gltfloader should be good to use.

Hope that helps. Will check these projects tonight, to highlight the code sections to look at.

3 Likes

Ok. Back again. If you look at the project n defold-tools called GLTF Lua Loader the method that sets the mesh data is called: geom:makeMesh
In this a couple of key methods from the extension are used:
Lines 34->36 of geometry-utils.lua:

geomextension.setbufferbytesfromtable( meshbuf, "position", indices, verts )
if(normals) then geomextension.setbufferbytesfromtable( meshbuf, "normal", indices, normals ) end
if(uvs) then geomextension.setbufferbytesfromtable( meshbuf, "texcoord0", indices, uvs ) end 

As can be seen this sets the meshbuffers position indexes, normals and texcoord. You can also set verts. In file gltfloader.lua line 169:

geomextension.setbufferfloatsfromtable(bv.byteOffset or 0, bv.byteLength or 0, buffer.data, gltf.verts)

In the geomextensions there are a number of helper functions for this. Iā€™ll make geomextensions a proper extensions repo so people can just include the lib in Defold projects - will look at this on the weekend.

All the best everyone (and yes, Im getting back into Defold after a crazy time in gamesā€¦ our whole studio got erm ā€˜let goā€™. :slight_smile: ā€¦ cant say anymore).

4 Likes

Thank you so much! :heart: Now, I need to find some time to get back to this and come back with eventual questions! :sweat_smile:

No problems. I started geomextension standalone lib last night. Should have something tonight sometime. Will post here.

1 Like

Ok. Here is geomextension.

The example modifies the mesh at runtime and creates a new cube everyframe with a different size. Obviously youā€™d normally use scale, but this is a simple way to show how to update meshes at runtime. I was going to add a vid, but that really doesnt make sense, since it looks like its scaling :slight_smile:
I havent tried including this as an extension in another project. Will give it a try, but it should be ok.

3 Likes