Can I create an image and draw to it in Godot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kitfox

I’d like to create an image that is made by compositing tiles from another source image. While I could do this just by adding a lot of sprites to the screen, I feel it would make more sense to composite an offscreen image and then use that as the source of a single sprite. (This image is meant to be a background terrain, with things like rivers and roads drawn upon it.) Is there a way to do this? The docs have quite a bit of info of using a tile set to make independent sprites, but I’m having trouble finding anything for compositing(ie, creating your own image on the fly).

you can collect and categorize tiles in a TileSet
you can then draw with that TileSet to many layers of TileMap
Using TileMaps — Godot Engine (stable) documentation in English

is that what you mean? you can also create images by setting individual pixels but using TileMaps would be easier

_image.create(int(size.x), int(size.y), false, Image.FORMAT_RGBA8)
_image.lock()
for y in range(size.y):
	for x in range(size.x):
		var actualX := int(x + offset.x)
		var actualY := int(y + offset.y)
		_image.set_pixel(x, y, _level.getMapColor(actualX, actualY))
_image.unlock()
_image.expand_x2_hq2x()
_image.expand_x2_hq2x()
_imageTexture.create_from_image(_image)

rakkarage | 2020-07-26 22:49

Thanks. That looks helpful. I’m going to need to read up to see how to load my initial image and then how to draw my _imageTexture to the screen.

kitfox | 2020-07-27 00:48

this is the minimal code that you need to draw pixels to screen using an image

Minimum code to draw an image to screen in Godot · GitHub

kasthor | 2021-03-27 17:50