Render target as a texture resource

I started working on an example on how to use RTs in GUI and noticed that it’s a bit difficult since there is no way to hook a render target into an atlas. There’s a PR now on GH (Add attachment texture resource path into resource.get_render_target_info by Jhonnyg · Pull Request #9057 · defold/defold · GitHub) that enables you to use RTs together with atlases via scripts, like so:

go.property("rt_atlas", resource.atlas())
go.property("rt", resource.render_target())

local function render_target_to_atlas(self)
	local rt_info = resource.get_render_target_info(self.rt)
	local atlas_info = resource.get_atlas(self.rt_atlas)
	local rt_w = rt_info.attachments[1].width
	local rt_h = rt_info.attachments[1].height
	
	atlas_info.texture = rt_info.attachments[1].texture
	atlas_info.animations[1].width = rt_w
	atlas_info.animations[1].height = rt_h
	atlas_info.geometries[1].vertices = { 0, rt_h, 0, 0, rt_w, 0, rt_w, rt_h }
	atlas_info.geometries[1].uvs = { 0, rt_h, 0, 0, rt_w, 0, rt_w, rt_h }
	
	resource.set_atlas(self.rt_atlas, atlas_info)
end

The example I’m tinkering with looks like this:

And works like this:

  • create a RT resource and assign into the .render file
  • create a dummy atlas and assign it to the GUI (and assign the texture to the box node)
  • use the code above to change the backing texture of the atlas to the RT (and update the animation data so we can get correct UV’s, this is not needed if you have a dummy texture of the same size as the RT)
  • render the “map” into the render target with a custom material that does the postprocessing
3 Likes

Thank you so much for your work on this, looking forward to playing with the example and getting it to suit my needs :slight_smile: