#Pallet swapper code not working
1 messages · Page 1 of 1 (latest)
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_TeamColor1 ("Team Shade 1", Color) = (0,0,1,1)
_TeamColor2 ("Team Shade 2", Color) = (0,0,0.7,1)
_TeamColor3 ("Team Shade 3", Color) = (0,0,0.4,1)
}
SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
LOD 200
Pass
{
ZWrite Off
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _TeamColor1;
fixed4 _TeamColor2;
fixed4 _TeamColor3;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed3 magenta1 = fixed3(1.0, 0.6, 1.0);
fixed3 magenta2 = fixed3(1.0, 0.2, 1.0);
fixed3 magenta3 = fixed3(0.6, 0.0, 0.6);
float tol = 0.01;
if (distance(col.rgb, magenta1) < tol)
col.rgb = _TeamColor1.rgb;
else if (distance(col.rgb, magenta2) < tol)
col.rgb = _TeamColor2.rgb;
else if (distance(col.rgb, magenta3) < tol)
col.rgb = _TeamColor3.rgb;
return col;
}
ENDCG
}
}
}
Did you try to increase the tolerance ?
Also, maybe try to convert your hard-coded value to sRGB value as it might be the issue here (hard coded color is linear when the sampled texture is sRGB)
It should be close to color = pow(color, 0.454545)
Thanks let me try that
I tried it and there is some progress it converts a certain shade of brown but not actually any of the magenta shades I defined:
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_TeamColor1 ("Team Shade 1", Color) = (0, 0, 1, 1)
_TeamColor2 ("Team Shade 2", Color) = (0, 0, 0.7, 1)
_TeamColor3 ("Team Shade 3", Color) = (0, 0, 0.4, 1)
}
SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
LOD 100
Pass
{
ZWrite Off
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _TeamColor1;
fixed4 _TeamColor2;
fixed4 _TeamColor3;
// Prelinearized (gamma corrected) magenta shades
float3 magenta1 = pow(float3(1.0, 0.6, 1.0), 0.454545); // Light
float3 magenta2 = pow(float3(1.0, 0.2, 1.0), 0.454545); // Mid
float3 magenta3 = pow(float3(0.6, 0.0, 0.6), 0.454545); // Dark
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
```
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
float3 linearCol = pow(col.rgb, 2.2); // gamma correct the sampled color
float tolerance = 0.01;
if (distance(linearCol, magenta1) < tolerance)
col.rgb = _TeamColor1.rgb;
else if (distance(linearCol, magenta2) < tolerance)
col.rgb = _TeamColor2.rgb;
else if (distance(linearCol, magenta3) < tolerance)
col.rgb = _TeamColor3.rgb;
return col;
}
ENDCG
}
}
}```
Since you've moved the magenta variables outside the function, be aware HLSL treats global variables as external inputs by default (set by Shader.SetGlobalVector in C#) and will initalise them as 0. You need to mark them as static to avoid that behaviour and keep the values you've hardcoded
Also not sure if you want both pow(x,2.2) and pow(x,0.45). You want both colours in the same colorspace so should only need to convert one.
Thanks let me try this