- Home /
Multiple Texture Colours
I am trying to create a shader and material with two colours so i can use the same texture and easily change the colour scheme. I want to be able to change both the inner and outer colour.
What i would like the material shader to show:
The end result would then look like the following example:
(Ignore the black border, thats just so the image can be seen on the page)
You're going to have to be a little more specific about what you're having trouble with. Do you need help making the two color properties? Do you need help with making that color pattern? Does the shader need to have lighting and shadows, or is it meant for until sprites. Does the shader also need to display a texture in addition to the colors?
I need help with making the two color properties and the pattern. It doesnt need lighting or shadows . The texture should be the two colours in the arrangement shown in the example as it is intended to be used as the top face of a cube with the colours randomly picked at runtime.
If you want to assign this texture only to one face of a cube, you're going to have to unwrap the UV coordinates into a proper map that can be used by the shader. And, if you're doing that, you may as well just assign two different materials in your modelling program - one for the blue section and one for the rest of the cube - and then use Unity's built-in unlit color shaders. What modelling program do you use? It's pretty trivial in Blender.
Answer by alexi123454 · Jul 27, 2015 at 09:01 PM
Adding properties to a shader isn't too difficult, you just need to add them to the properties section at the top of the shader. You can add a color with the line formatted as such:
_PropertyName ("PropertyNameShownInInspector, Color) = (1,1,1,1)
This would create a property for the material that's a color (defaulted to white), named "Property Name Shown In Inspector" in the inspector. To access the property in the shader, the variable name is "_PropertyName".
Causing the colors to appear exactly as shown can be a little difficult, as shaders require you to define things in percentages or pixels, depending on what value you're looking at. The easiest way to define exactly the pattern you have about would be for the shader to take in a texture mask (a black and white version of where you want the colors to be).
Here is the completed shader (a lightly edited version of the official built-in sprite shader), where anything black in the texture mask will be Color1, and anything else will be Color2:
Shader "CustomColorPattern"
{
Properties
{
_MainTex ("Mask Texture", 2D) = "white" {}
_Color1 ("Main Color 1", Color) = (1,1,1,1)
_Color2 ("Main Color 2", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color1;
fixed4 _Color2;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = tex2D(_MainTex, IN.texcoord);
//c.rgb *= c.a;
if (c.r == 0 && c.g == 0 && c.b == 0)
{
c = _Color1;
}
else
{
c = _Color2;
}
return c;
}
ENDCG
}
}
}
Pretty much all of the relevant stuff is in the "frag" function towards the bottom.
thats fantastic, i was intending to use this on a 3d cube. Would it still work ok ?
This shader is specifically for sprites, it probably won't render correctly on a cube. Try this for 3d objects:
Shader "CustomColorPattern" {
Properties{
_Color1("$$anonymous$$ain Color 1", Color) = (1,1,1,1)
_Color2("$$anonymous$$ain Color 2", Color) = (1,1,1,1)
_$$anonymous$$ainTex("$$anonymous$$ask Texture", 2D) = "white" {}
}
SubShader{
Tags{ "RenderType" = "Opaque" }
LOD 100
Pass{
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
UNITY_FOG_COORDS(0)
};
fixed4 _Color1;
fixed4 _Color2;
v2f vert(appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
o.texcoord = v.texcoord;
o.color = v.color;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
sampler _$$anonymous$$ainTex;
fixed4 frag(v2f i) : COLOR
{
fixed4 c = tex2D(_$$anonymous$$ainTex, i.texcoord);
if (c.r == 0 && c.g == 0 && c.b == 0)
{
c = _Color1;
}
else
{
c = _Color2;
}
UNITY_APPLY_FOG(i.fogCoord, c);
UNITY_OPAQUE_ALPHA(c.a);
return c;
}
ENDCG
}
}
}
thanks, i have changed the material to use this shader, but the cube is always light purple no matter what mask or colours i use.
Have a look at the unity console window, is it saying there's an error? If not, try clicking on the shader and looking at it in the inspector. There should be a little list of information, followed by a Properties list with Color1, Color2, and $$anonymous$$ainTex.
Answer by Tasarran · Jul 27, 2015 at 10:33 PM
This would probably be better done by just using two sprites, or a sprite over the top of a colored plane.
You make the Sprite basically a white and transparent version of the overlay pattern, put it over the blue field, and you have control over the color of it through the .color attribute of the Sprite...
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Double Layer Material 0 Answers
Changing Eye Colour (Colour only non-white parts of a texture?) 2 Answers
Changing two different objects renderer colour 1 Answer
Car paint looks not good? 1 Answer