• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by GamesDeveloper12 · Jul 27, 2015 at 07:15 PM · shadertexturematerialcolor

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:

alt text

The end result would then look like the following example:

alt text

(Ignore the black border, thats just so the image can be seen on the page)

screen-shot-2015-07-27-at-200927.png (8.8 kB)
idea-1.png (21.7 kB)
Comment

People who like this

0 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexi123454 · Jul 27, 2015 at 08:16 PM 0
Share

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?

avatar image GamesDeveloper12 · Jul 27, 2015 at 08:18 PM 0
Share

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.

avatar image tanoshimi · Jul 28, 2015 at 08:32 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image

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)

T$$anonymous$$s would create a property for the material that's a color (defaulted to w$$anonymous$$te), 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 t$$anonymous$$ngs 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 w$$anonymous$$te 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 anyt$$anonymous$$ng black in the texture mask will be Color1, and anyt$$anonymous$$ng else will be Color2:

 Shader "CustomColorPattern"
 {
     Properties
     {
         _MainTex ("Mask Texture", 2D) = "w$$anonymous$$te" {}
         _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.

Comment

People who like this

0 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GamesDeveloper12 · Jul 27, 2015 at 09:08 PM 0
Share

thats fantastic, i was intending to use this on a 3d cube. Would it still work ok ?

avatar image alexi123454 · Jul 27, 2015 at 11:17 PM 0
Share

This shader is specifically for sprites, it probably won't render correctly on a cube. Try this for 3d objects:

 Shader "CustomColorPattern" {
     Properties{
         _Color1("Main Color 1", Color) = (1,1,1,1)
         _Color2("Main Color 2", Color) = (1,1,1,1)
         _MainTex("Mask Texture", 2D) = "white" {}
     }
 
         SubShader{
         Tags{ "RenderType" = "Opaque" }
         LOD 100
 
         Pass{
         CGPROGRAM
 #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_MATRIX_MVP, v.vertex);
         o.texcoord = v.texcoord;
         o.color = v.color;
         UNITY_TRANSFER_FOG(o,o.vertex);
         return o;
     }
 
     sampler _MainTex;
 
     fixed4 frag(v2f i) : COLOR
     {
         fixed4 c = tex2D(_MainTex, 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
     }
     }
 
 }
 
avatar image GamesDeveloper12 · Jul 28, 2015 at 08:13 AM 0
Share

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.

avatar image alexi123454 · Jul 28, 2015 at 11:30 AM 0
Share

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 MainTex.

avatar image

Answer by Tasarran · Jul 27, 2015 at 10:33 PM

T$$anonymous$$s 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 w$$anonymous$$te 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...

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing Eye Colour (Colour only non-white parts of a texture?) 2 Answers

Double Layer Material 0 Answers

Is there a way to blend textures into a material based on a noise map? 1 Answer

Changing two different objects renderer colour 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges