Set a mesh texture (SOLVED)

Is it possible to set the image of a mesh (tex property) to the texture of an atlas?

I have tried to get the following

local atlas = resource.get_atlas(ATLAS_URL)
local atlas_texture = atlas.texture
go.set(MESH_URL, “tex”, atlas_texture)

I can confirm that the atlas data and the atlas_texture are correct. But the last line gives

ERROR:GAMEOBJECT: Properties can not be of type ‘string’.
ERROR:SCRIPT: main/play/play.script:255: the property ‘tex’ of ‘/TEST_mesh_atlas#mesh’ must be a number

Any help? What am I doing wrong?

Try “tex0” instead of “tex”

1 Like

Same error with “tex0”

Actually, I think the reference is wrong. It’s supposed to be “texture0”, “texture1”, “texture2” and so forth

1 Like

With “texture0” I have the error “must be a hash” (previously was “must be a number”). But still an error.

The following code works:

local atlas = resource.get_atlas("/main/common/images/EDIT_common.a.texturesetc")
local atlas_texture = atlas.texture
go.set(MESH_URL, “texture0”, hash(atlas_texture))

You need “texture0” and also to hash the texture got from the atlas.

Thanks to @jhonny.goransson for the great help as usual!

1 Like

Resources are referenced using hashes.
Also note that you passed in the path when creating the resource.

@Mathias_Westerdahl Thanks!

I have to admit I don’t quite understand what you mean with “Also note that you passed in the path when creating the resource.”. I called URL something that is a path in

local atlas = resource.get_atlas(ATLAS_URL)

in my first post?

Thank you again!

This isn’t a url. It’s a path. It is also the path that is returned in the atlas.texture.

1 Like

Yes, thanks!

Still a (maybe final) question.

The following fragment of code is correctly setting the texture of an atlas to a mesh. The atlas is created by the engine via the editor.

First I use go.property to force the engine to load the atlas during the initialization of the script:

go.property(“my_atlas”, resource.atlas(ATLASH_PATH)

Then, in a function, I use self.my_atlas to access the atlas data via resource.get_atlas:

local atlas = resource.get_atlas(self.my_atlas)
local atlas_texture = atlas.texture
go.set(MESH_URL, texture0, hash(atlas_texture))

Is this a real intended use of resource.atlas and resource.get_atlas? Or it is working for some strange coincidence I don’t even see?

Thank you!

“resource.atlas” (and the other resource properties) are there for you to be able to load a resource into your bundle.
If we didn’t have a reference, we wouldn’t know what content to build and bundle with the game.

The property “texture0” of the mesh, is there so that it may be read or set during runtime. Useful for changing a texture.

Note that you hash the path unnecessarily.
I refer to my previous statement: "Resources are referenced using hashes."

self.my_atlas is a resource path hash. All our resource properties are.
SO, you can do this:

go.set(MESH_URL, texture0, self.my_atlas)
2 Likes

Thank you for the clear explanation!

@Mathias_Westerdahl

I have tested your suggestion of using

go.set(MESH_URL, texture0, self.my_atlas)

and it does NOT work. I get the error: go.set failed because the value is unsupported

Then I have tried

local atlas = resource.get_atlas(self.my_atlas)
local atlas_texture = atlas.texture
go.set(MESH_URL, texture0, atlas_texture)

removing the hash and passing the texture directly. And still it does NOT work. I get the error: the property ‘texture0’ of ‘/TEST_mesh_atlas#mesh’ must be a hash.

So, there is something I don’t understand in your suggestion… What am I missing?

Can you please share a minimal project showing what you are trying to do? I will try to create a new example as well for our examples page.

1 Like

I’m sorry I tricked you a little bit, and I didn’t read your code in enough detail.

The texture0 accepts a texture (.texturec). It does not accept an atlas nor a tilesource.
And your example gets the texture name and hashes that. And that’s why it works.

Perhaps in the future, we can get a resource.get_texture(atlas)function to facilitate this.

2 Likes

We have resource.get_atlas(atlas_path)(API reference (resource))

You can get the texture from that:

local atlas_info = resource.get_atlas(path)
local texture_res = atlas_info.texture
1 Like

Yes, that’s what he did.
However, it’s not always returning a hash: script_resource.cpp

1 Like

@britzl @Mathias_Westerdahl @jhonny.goransson Thank you very much for all your answers! Your support is really amazing and this makes Defold a unique engine!

As I wrote above, my aim was to set the texture of an atlas (created in the editor) as texture of a mesh. I have successfully achieved this as follows. I repeat somehow a previous post of mine in order to clearly have a solution at the end of this thread for future use.

First I defined a property in order to have the atlas included in the build / bundle:

go.property(“my_atlas”, resource.atlas(ATLAS_PATH)

Then I set the atlas texture to a mesh via the following lines:

local atlas = resource.get_atlas(self.my_atlas)
go.set(MESH_URL, texture0, hash(atlas.texture))

And everything works as expected! A general reference for the resource API is of course API reference (resource)

Thank you again!

2 Likes