- Home /
How can I change the colour of part of a texture?
Say I have a basic diffuse texture for a sword and another texture filling a region on that sword.
During gameplay the player enchants the sword to have fire powers or water powers.
How would I go about passing red (fire) or blue (water) respectively to a shader so when rendering the model, the region is filled with the colour passed.
if its any help to the question, alpha doesn't matter but is preferable, the textures in question are pixel art textures, so a 1-bit texture defining the regions is the plan. And I know my way around graphics programming, but I'm fairly new to unity.
Any and all help is appreciated, Thanks in advanced, Bombshell
EDIT: when looking for an answer I came across the shader lab and gave it a go but its not working, the customizable colours aren't showing on the model
Shader "Unlit/ColorMod" {
Properties {
_Color1 ("Color R", Color) = (1,0,0,1)
_Color2 ("Color G", Color) = (0,1,0,1)
_Color3 ("Color B", Color) = (0,0,1,1)
_Color4 ("Color A", Color) = (1,1,1,0)
_MainTex ("Base (RGB)", 2D) = "white" { }
_DecalTex ("Decal (RGBA)", 2D) = "white" { }
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Color1;
float4 _Color2;
float4 _Color3;
float4 _Color4;
sampler2D _MainTex;
sampler2D _DecalTex;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
float4 _MainTex_ST;
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
return o;
}
half4 frag (v2f i) : COLOR
{
float4 texcol = tex2D (_MainTex, i.uv);
float4 deccol = tex2D (_DecalTex, i.uv);
float4 temp = float4(0,0,0,0);
temp += _Color1 * deccol.r;
temp += _Color2 * deccol.g;
temp += _Color3 * deccol.b;
temp += _Color4 * deccol.a;
return texcol + temp;
}
ENDCG
}
}
}
Answer by Bombshell93 · Jul 16, 2012 at 01:01 AM
for anyone wondering, I managed to get the shader working, this is a no lighting shader. I have also commented on it so its more use, however slight.
Shader "Unlit/ColorMod" {
Properties {
_Color1 ("Color R", Color) = (1,0,0,1) //Color R - opacity based on the Red channel of the Decal texture
_Color2 ("Color G", Color) = (0,1,0,1) //Color G - opacity based on the Green channel of the Decal texture
_Color3 ("Color B", Color) = (0,0,1,1) //Color B - opacity based on the Blue channel of the Decal texture
_Color4 ("Color A", Color) = (1,1,1,0) //Color A - opacity based on the Alpha channel of the Decal texture
_MainTex ("Base (RGB)", 2D) = "white" { } //Base (RGB) - the base texture
_DecalTex ("Decal (RGBA)", 2D) = "white" { } //Decal (RGBA) - the decal texture
}
SubShader {
Pass {
CGPROGRAM //Shader Start, Vertex Shader named vert, Fragment shader named frag
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
//Link properties to the shader
float4 _Color1;
float4 _Color2;
float4 _Color3;
float4 _Color4;
sampler2D _MainTex;
sampler2D _DecalTex;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
float4 _MainTex_ST;
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex); //Transform the vertex position
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex); //Prepare the vertex uv
return o;
}
half4 frag (v2f i) : COLOR
{
float4 texcol = tex2D (_MainTex, i.uv); //base texture
float4 deccol = tex2D (_DecalTex, i.uv); //decal texture
float4 temp = float4(0,0,0,0);
temp += _Color1 * _Color1.a * deccol.r; //get the Red channels color
temp += _Color2 * _Color2.a * deccol.g; //get the Green channels color
temp += _Color3 * _Color3.a * deccol.b; //get the Blue channels color
temp += _Color4 * _Color4.a * deccol.a; //get the Alpha channels color
return lerp(texcol, temp, temp.a); //mix the base texture and the new decal
}
ENDCG //Shader End
}
}
}
Answer by Muuskii · Jul 15, 2012 at 09:08 PM
I actually is possible to use a function called setPixels to target certain pixels and change them how you like, but it's probably not the best option for what you're looking for.
A quick search yields: http://forum.unity3d.com/threads/143595-Press-a-button-and-change-the-texture-of-an-object
Where they talk about a script you can use to change the material during runtime. Just make three textures, apply each one to a new material and then switch between them. Or you could make one big texture and change the UV information to access different parts of it. . .
Your answer
Follow this Question
Related Questions
How do I build A texture from decals? 0 Answers
Why is my UV'd texture displaying wrapped with the wrong scale? 0 Answers
Transparent shader and scrolling 0 Answers
LineRenderer unchanging texture rotation 0 Answers
Black Texture on Initial Load 1 Answer