Camera2D on a renderTarget (mixing items affected and not affected by a camera on the same program)

The camera examples found at use extend(camera) which applies the camera to the whole window.

But what if you want to apply the camera transformation to a renderTarget? Maybe you want to have part of the window affected by the camera but draw other items unaffected by it.

The simple trick is not to call extend(camera) (which would transform everything) but to apply the camera transformation to the render target instead, before drawing anything into it.


import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.isolatedWithTarget
import org.openrndr.draw.renderTarget
import org.openrndr.shape.Rectangle

fun main() = application {
    program {
        // Create a renderTarget where to draw things. It will be controlled by the camera.
        val rt = renderTarget(290, 440) {
        // Create camera
        val cam = Camera2D()

        // Add mouse listeners to the camera

        extend {
            // Draw onto the renderTarget
            drawer.isolatedWithTarget(rt) {
                // Calling ortho required if size differs from window size
                // Apply camera transformation
                view = cam.view
                // PINK background
                // Draw something
                rectangle(Rectangle.fromCenter(, 200.0, 100.0))

            // GRAY background for the window

            // Draw the renderTarget (with the camera previously applied to it) twice
            drawer.image(rt.colorBuffer(0), 20.0, 20.0)
            drawer.image(rt.colorBuffer(0), rt.width + 40.0, 20.0)

            // Draw things not affected by the camera
  , -0.5), 300.0)

I’ll this or similar examples to orx-camera.

1 Like