Skip to content
RblSb edited this page Jan 24, 2020 · 3 revisions

Render to texture is a handy feature which lets us draw stuff into image and then use resulting image instead of redrawing said stuff every frame. This can be used to speed up rendering, perform shader effects,...

To begin, create an image of desired size using Image.createRenderTarget(). You can then draw to this image as you would into framebuffer - for example by image.g2 (2D) or image.g4 (3D). When you are done, don't forget to draw the image itself into framebuffer.

import kha.Image;
import kha.Color;
...
final image = Image.createRenderTarget(256, 256);
final g = image.g2;
g.begin(); // sets up everything for rendering into the texture
g.color = Color.Red;
g.fillRect(20, 20, 50, 50);
g.end(); // after the end call, the image can be used just like any other image

Remember that you cannot start drawing to texture inside of other g.begin...g.end() block. Do g.end() before that action or use some callback system to call your texture creation after g.end()

To unload image from gpu memory use image.unload().