Drawing sprite to a render target in order to use it as a texture later on

Hi

I need to draw a sprite to a render target so that later on I can use it to mask the rendering of another sprite. However, in order for this to work, I need to draw the sprite to the full width and height of the render target.

The sprite’s world position can be anything, so first I need to get the vertex shader to output “local sprite coordinates” to the render target. I’m pretty sure that in the vertex shader I can just ignore the view_proj matrix and just output the position as it goes in

gl_Position = vec4(position.xyz, 1.0);

Also, in the render script I’m pretty sure I have to set an orthographic projection and the size of the viewport to the size of the sprite

render.set_view(vmath.matrix4())
render.set_projection(vmath.matrix4_orthographic(0, 32, 0, 32, -1, 1))
render.set_viewport(0, 0, 32, 32)
. . . -- create render target here of size 32x32
render.enable_render_target(self.mask_render_target)
render.draw(self.mask_pred)
render.disable_render_target(self.mask_render_target)

However, this doesn’t seem to work. Have I missed something, are my assumptions about “local sprite coordinates” wrong?

Did you manage to solve this?

One thing to note here is that the sprite vertices are batched, in order to create one draw call, as opposed to one draw call per sprite. Thus the sprite vertices are in world space.

Although the material has the option to select World or Local space, we haven’t yet implemented Local space for sprites.

Another option would be to draw a quad model (.dae), where the Local space would be respected.

I see, thank you :slight_smile: I’ll try that today and up date this thread

Not yet!

Is it actually possible and if so, is it planned to implement sooner or later? Also asking about tilemaps, as I came across it with it.

Local space would currently mean that each sprite will become a separate draw call.
(a remedy for that would be to also implement instancing)

What we want to do is to improve on the vertex format of the sprites.
How that would work is not designed yet, and we also don’t have it planned in a near future.

I think a first step would be to put the vertex color in the vertex format, as opposed to having it as a shader uniform (i.e. we don’t want color to break the batching).

As for rendering a quad fullscreen, there are a few ways to do it.

  1. scale the sprite accordinly to fit the screen (“sprite.set_scale()”)
  2. using projected coordinates in the shader (i.e. figuring out how to output vertices with x/y equal to -1 or 1. essentially not using the model/view/projection matrix for the (single) sprite)

What’s your use case @Pawel ?

4 Likes