Change a range of colors
Asked Answered
C

6

0

Hello everyone,

I'm looking for a solution to use one texture for all nations color. I see i can do that by shader code, but the basis don't allow me to modify an array of pixel.

It seems we need to absolutely to respect somes color to work correctly but it seems to be possible to modify that, even the "shadow" (dark pixels) in the texture, but how ?

shader_type canvas_item;

uniform vec4 oldcol1 : hint_color;
uniform vec4 newcol1 : hint_color;

void fragment(){
	vec4 currentColor = texture(TEXTURE, UV);
	
	if (currentColor == oldcol1)
	{
		COLOR = newcol1;
	}
	else
	{
		COLOR = currentColor;
	}
}

This is what i was able to do with this code:

This is what i want to have when i generate a vehicle:

Compare answered 1/12, 2022 at 15:17 Comment(0)
L
0

The code from that video would work, but there is far too much shadow / shading in your sprites to property mask out a color without issues on the edge.

Lout answered 2/12, 2022 at 16:38 Comment(0)
C
0

I have found an interresting tutorial where we can change the color by shader but it's not perfect:

shader_type canvas_item;

uniform vec4 skinMaskCol: hint_color = vec4(1.0, 0.0, 1.0, 1.0);
uniform vec4 skinCol: hint_color = vec4(1.0);
uniform float tolerance: hint_range(0.0, 1.0) = 0.2;

void fragment(){
	vec4 image = texture(TEXTURE, UV);
	vec3 color = image.rgb;

	float alpha = image.a;
	float skinMaskLen = length(skinMaskCol.rgb);
	float cLen = length(color);

	vec3 maskNorm = skinMaskCol.rgb / skinMaskLen * cLen;
	vec3 skinColNorm = skinCol.rgb / skinMaskLen * cLen;

	float skinDist = distance(color, maskNorm) * 5.0;
	color = mix(skinColNorm, color, step(tolerance, skinDist));

	COLOR = vec4(color, alpha);
}

Only condition is to set to magenta what i want to modify, but the color expant anyway to the darker pixels... and some artefact still here.

Compare answered 2/12, 2022 at 14:35 Comment(0)
L
0

You have the basic idea, but it won't work as the last step in the shader because of interpolation.

This happens when an image is scaled (up or down) which almost always happens when filtering is enabled. It would work for a pixel art game, like if there was no filtering.

The alternative is to generate the sprites before you send them to the shader (create a new texture in code). This is slow, so should only be done on startup. But that will remove the fringing.

Lout answered 2/12, 2022 at 14:53 Comment(0)
C
0

Hi, Lout

And if i want absolutely keep this code like that, what may i "sacrifice" as details or something else ?

Except make X texture sprite for X nation color, there are anoter options i can explore ?

Compare answered 2/12, 2022 at 16:16 Comment(0)
L
0

The code from that video would work, but there is far too much shadow / shading in your sprites to property mask out a color without issues on the edge.

Lout answered 2/12, 2022 at 16:38 Comment(0)
C
0

I have drastically decrease the shadow from the 3D software i use to generate the texture and the result is almost perfect. The camera's zoom distance will help to "hide" the imperfections.

Thank you, Lout

Compare answered 5/12, 2022 at 15:7 Comment(0)
D
0

just render a monochrome/b&w lighting pass and then a rgb 'splatmask' pass of the sprite. It would make things much easier for you and you didn't have to compromise onthe lighting & shading.

Diarmid answered 5/12, 2022 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.