Is there a way to create a new texture dynamically by combining 2 textures, one displayed on top of another?
Combining 2 textures into 1?
- Edited
You could show them both in a viewport, then take the image displayed and save it as another texture. Or you could take the data from each texture and use your own code to combine them into another.
Yeah, you could do it with a shader and combine multiple textures. I forget what the limit is, but it's probably around 16 textures or somewhere around there. If you just want to do it once (not in real-time, such as animating) then you could use the Texture and Image functions, such as get_pixel and get_pixel to mix two textures and save them as another texture. This will be easier than using a shader.
Thanks for the answers, although what I'm getting from this is basically "try to avoid needing to do this", which I just did.
It depends on what you want to do but, in general, anything you can pre-process, either in GIMP or Blender, will be easier and faster than trying to do it in real-time in Godot.
You know, you could also solve this by just combining the textures beforehand in a program like GIMP or Inkscape. Making the resource have it baked in is a simple and extremely low-CPU usage workaround. Best of luck.
I realize this is an old thread, but at the risk of necro'ing, there is a better solution than shaders, etc.
var file_A: String = "res://bits/lane_grass.svg"
var file_B: String = "res://bits/lane_text_jp.svg"
var bitmap_A: TextureRect = TextureRect.new()
var bitmap_B: TextureRect = TextureRect.new()
bitmap_A.texture = load(file_A)
bitmap_B.texture = load(file_B)
var image_A: Image = bitmap_A.texture.get_image()
var image_B: Image = bitmap_B.texture.get_image()
# Here's where the magic happens
image_A.blend_rect(image_B, Rect2i(Vector2.ZERO, Vector2(1900, 850)), Vector2i(0, 0))
var texture_A = ImageTexture.create_from_image(image_A)
var the_bitmap: TextureRect = TextureRect.new()
the_bitmap = TextureRect.new()
the_bitmap.texture = texture_A
return the_bitmap
Basically, I'm loading in TextureRect resources from a file (note they are also SVG files), grabbing the textures directly, then grabbing the Images from them - blitting the images together, then creating a new ImageTexture from the Image, and assigning it to a TextureRext.texture for usage.
Because blend_rect()
does alpha-masking, this combines the two bitmaps as an overlay.
It's possible there are better ways to do this, but this is what I came up with. And when I tried other things, like directly loading an Image, I'd get warning/errors:
var image_B: Image = Image.load_from_file(file_B)
Loaded resource as image file, this will not work on export: 'res://image_B.png'.
Instead, import the image file as an Image resource and load it normally as a resource.
I don't doubt that pre-manipulating things in Gimp, etc, would work. But in my use-case, I'm working from SVG files with multiple layers, which I want to use selectively, to construct a final game-bitmap. For instance, there's Layers with English text, and Layers with other languages, all of which I export to different SVG files. Then the game will combine the layers, as needed. Yeah, I could create in advance a bunch of bitmaps with every possible combination of layer-elements I want, but that's a major pain, I'd rather combine elements.