Coloring Specific Parts of a Model in Unity3d.

1 minute read

One of the features I’ve implemented is objects coloring. I’ve written a simple shader for that. I’ve used the alpha value to fill the desired spots on the model with the custom color. Also, I’ve done it for emission color as well ( We use it on our models a lot )

To use this shader erase desired parts from your texture map.

This is the texture I use on the ship above:

In addition you can use one more extra color for emission if needed. To use emission coloring add the emission map: erase everything on your diffuse map except spots you want for emission color. Make them black color save and attach to Emission Map inside the material. Thats it.

It may be not the best approach but it works.

Shader "Custom/CustomColorPainter" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_ColorEmission("ColorEmission", Color)=(1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_EmissionMap ("EmissionMap",2D) = "gray" {}
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200

		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;
        sampler2D _EmissionMap;
		struct Input {
			float2 uv_MainTex;
			float2 uv_EmissionMap;

		half _Glossiness;
		half _Metallic;
		fixed _AlphaChecker;
		fixed4 _Color;
		fixed4 _ColorEmission;

		// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
		// See for more information about instancing.
		// #pragma instancing_options assumeuniformscaling
			// put more per-instance properties here

		void surf (Input IN, inout SurfaceOutputStandard o) {
			_AlphaChecker = 0.6;

            fixed3 endColor = fixed3(0.0,0.0,0.0);
            fixed4 ca = tex2D(_EmissionMap, IN.uv_EmissionMap);

		    // normalColor stuff
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex);

			// emission stuff
			if (ca.a<=_AlphaChecker){
				o.Emission = half3(0.0,0.0,0.0);

			endColor = c.rgb;
        	o.Emission = _ColorEmission;
			endColor = _ColorEmission;

			if (c.a<_AlphaChecker)
            endColor = _Color;

			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Albedo = endColor.rgb;
			o.Alpha = c.a;
	FallBack "Diffuse"

Or take it from my github

Follow me at my Twitch channel where I’m showing creation process of the Battlecruiser.