Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by Bombshell93 · Jul 15, 2012 at 08:30 PM · shadertexturerpgcustomizationcolour

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
     }
 }

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
     }
 }

}

Comment
Add comment · 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
0

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

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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