Creating a texture from off screen textures

Hi, with the community’s help I am about to complete porting my first quiz game from Unity to Defold, and I’m now planning on porting more of my games and I think there may be only one thing I don’t know how to do.

The game has an avatar designer, with the user choosing features. In Unity I could create a rendertexture off screen, turn on the needed feature textures, save the texture for use and turn off the rendertexture to save draw calls.

Is there a way to achieve this in Defold? Currently my project only uses GUIs (one for menu, question, answer, etc) in one collection and render scripts are so over my head (I plugged one in via an example and it works - that’s all I know!)

Much help to everyone who has helped me to this point :smile:

2 Likes

Yea that’s possible, we have a “render target” which the same thing as a render texture, but currently you need to enable / disable it via render scripts to render to it. But I think we need some more info on how your setup currently looks like in unity to provide info on how to do it in defold

3 Likes

Excellent. I just opened Unity for the first time in a while and, well, I’ve been spoiled using Defold!

As you can see from the screenshot, my two avatars are off screen in a render canvas with a render camera.

When I needed to generate the avatar I turned off the face textures not needed, and generated the texture in code. I could then use the texture in game, and turn off the avatar render canvases to save them getting drawn (even though they are off screen, but you never know)

I’m not the greatest coder in the world, so I probably copied the generated code from somewhere else to use.

1 Like

Hi, I’ve had a look at render target (Mastering Render Targets) but - and as I said I’m not much of a coder - it renders the screen camera and I want to render an off-screen area. Would this be the general idea though?

Hey there,

Check out Part 3 of Mastering Render Targets, there we learn how to change the location and size of the virtual camera, known as the projection, that renders to the target. In the tutorial we set the projection to the position and size of the window object so that everything behind it gets rendered to the target, but you can set the projection to any location including off screen.

Say we had a game object positioned off screen at position -130, 360 with a size of 192 x 192 pixels:

Then in the pipeline render script we can set the projection to that location and size before drawing to the target and everything captured by that projection will be rendered to the target:

-- Set the projection to the offscreen logo.
local logo_half = 192 / 2
local window_proj = vmath.matrix4_orthographic(-130 - logo_half, -130 + logo_half, 360 - logo_half, 360 + logo_half, self.near, self.far)
render.set_projection(window_proj)

-- Set the render target to our target.
render.set_render_target(self.target)
-- Clear the render target with the games clear color.
render.clear({[render.BUFFER_COLOR_BIT] = self.clear_color, [render.BUFFER_DEPTH_BIT] = 1, [render.BUFFER_STENCIL_BIT] = 0})
-- Draw all objects with the "tile" tag in their material.
render.draw(self.tile_pred, {frustum = frustum})
-- Reset the render target.
render.set_render_target(render.RENDER_TARGET_DEFAULT)
2 Likes

Thank you so much, this is great!

1 Like

No problem! Best of luck with your project.

1 Like