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 ) https://gyazo.com/421523ac33f1c4e0cb8ea7e0cf4cf3d4

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.

https://gyazo.com/62b74bf1ca147f66db31a3e995a87d9c

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

		CGPROGRAM
		// 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 https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
		// #pragma instancing_options assumeuniformscaling
		UNITY_INSTANCING_CBUFFER_START(Props)
			// put more per-instance properties here
		UNITY_INSTANCING_CBUFFER_END

		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;
			}
			else{
        	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;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

Or take it from my github

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

Comments