Change model texture in runtime (SOLVED)

I think this is coming soon based on mentions of being able to dynamically change textures and materials at runtime.

9 Likes

I am waiting too for this feature …

1 Like

@britzl just got this issue in my project. I am downloading many png images from a server and I need to display them on the screen. I am using model component with a quad mesh. When I load an image and set it as a texture for one object on the screen, all objects get this image.
Is it still impossible to create a new separate resource for a model and load it with downloaded data?

What are the images/how are they being used?

Images are PNG icons for some sort of a catalog. I create a list view of items with a name label and an icon. Game objects are created with factory.create(), so they share the resource. I want to change the texture resource individually, but they change altogether.

You can try gui.new_texture. I don’t use it, but looks like it can change texture in runtime. Model still can’t change texture. PNG file save (SOLVED)

Are the sizes of the images consistent? https://github.com/subsoap/imagebatch might help as a starting point possibly.

1 Like

Good stuff, but still won’t solve the issue entirely.

Hm. Any way to use gui texture in the game object world?

Transfer world coord to gui coords every frame, to update gui pos. Or create and attach gui to go. Not good way but no normal way, for changing texture.

I think it must have feature for engine. Plz add it. @sven @britzl

1 Like

that’s necessary feature.

1 Like

Hm. Any way to use gui texture in the game object world?

Not an expert but I do think you should be able to kinda do it with some workarounds.

  1. Copy the GUI material from the builtins folder and changed the tag to tile. This will make it move with the camera (will be rendered in worldspace), but the positions changes from a parent go will not propegate to the gui. Therefore:
  2. Create a script that tracks the delta position of the go and send that to the gui. This isn’t free because you would have to send a message to the gui each frame.

parent_gui.script

local position

function init(self)
	position = go.get_position()
	self.action = {x=position.x, y=position.y, dx=0, dy=0}
end

function update(self, dt)
	position = go.get_position()
	self.action.dx = self.action.x - position.x
	self.action.dy = self.action.y - position.y

	self.action.x = position.x
	self.action.y = position.y
	
	msg.post(".", "parent_update", self.action)
end

world_gui.gui_script

function init(self)
	self.root = gui.get_node("root")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("parent_update") then
		local p = gui.get_position(self.root)
		p.x = p.x - message.dx
		p.y = p.y - message.dy
		gui.set_position(self.root, p)
	end
end

Collection Setup
09

2 Likes

Is changing individual model textures at runtime still in the backlog?

It may be possible now with the resources update but that is something I have not gotten around to testing.

1 Like

You can use resource properties to change texture: https://defold.com/manuals/script-properties/#resource-properties

3 Likes

Amazing, thanks!

Is it possible to change the texture of a material at runtime?
(or is this a new question?)

You can set the material of a model, or the individual textures of a model

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