Is it possible to change fill and stroke colors from SVG to generate different cached, colorized textures?

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

I have hairstyle graphics in my project that vary by style, length, and color. The idea is to make modular characters with them, for cutout animation 2D characters. Each style and length is a SVG file. I made the graphics using Inkscape.

Now, to make all possible combinations of hair+beard styles, that’s 14 hair, 5 beards, 19 total. 7 lengths and let’s say 8 colors. Simple math of 19 x 7 x 8 equals 1064 files. That is, if I used PNG files. It seems like way too many, and I do not want to modulate shaded grayscale hair (there are 3 cel shades in vector - normal, shade and highlight) nor use shaders because of performance reasons.

The other way I thought of, is to somehow manipulate the values Godot reads from a SVG file, so for example, instead of reading “fill:#ffffff”, replace it as a different color instead, set by a palette in GDScript, and generate an ImageTexture from that on the fly. This way would allow to have way more than 8 colors, without increasing the number of files and only keeping it as 19 x 7 x 1 = 133, with just grayscale SVG files, and only use them as reusable sources for colorized hair textures.

Is this even possible without modifying Godot’s source code? Are there any alternatives? It may not even be feasible if Godot does something while importing the SVG file that makes this approach tricky.

Any help is greatly appreciated!

If modulating the entire hair isn’t an option because you need different hues / saturations for shadows and highlights, you could go around it by splitting the image in three regions (normal/shade/highlight) and modulating them separately.

skysphr | 2022-01-25 09:01

I wished the mods would reject this question because then I looked at the Godot docs saying SVG import isn’t even too good. Oh well, I suppose I can do what you’re saying, as it’s just 19x7x3 file-wise and doesn’t grow with more colors because of modulate. Performance might be a little, maybe negligibly impacted with more characters on screen because of extra sprites, I wish it was easier to combine modulated sprites into one texture for this reason. Thank you for the comment.

Naonlily | 2022-01-25 10:02

You can render to texture by using a viewport.

skysphr | 2022-01-25 11:51

Very good. After a little research I built a small texture generating demo and a stress test using a viewport. It works well and I consider this solved, thank you.

Naonlily | 2022-01-25 14:28