Mobile vertex blend with normal map?

Hey everyone,

Does anyone know of a mobile shader that has the effect of taking two materials and blending them together based on vertex colour? You can use this for things like blending a dirt road into grass.

Black vertex -
Diffuse A
Normal A

White vertex -
Diffuse B
Normal B

If there aren’t any around I might have a shot at hacking one together.

Okay, here’s what I have so far. I created a strumpy shader and then hacked parts of it into the Bump Spec shader that comes with Unity. Tested on device and it seems to run fine. If you are shader savvy and feel that this shader needs improvement, by all means feel free to fix it up :smile:

You define the textures on your model by painting vertex colours onto it. In this case; black for grass, white for pavement.

// Blend 2 textures together based on vertex colours

Shader "Mobile/Bump Specular Blend" {
Properties {
	_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
	_MainTexA ("Base (RGB) Gloss (A)", 2D) = "white" {}
	_BumpMapA ("Normalmap", 2D) = "bump" {}
	_MainTexB ("Base (RGB) Gloss (A)", 2D) = "white" {}
	_BumpMapB ("Normalmap", 2D) = "bump" {}
}
SubShader { 
	Tags { "RenderType"="Opaque" }
	LOD 250
	
CGPROGRAM
#pragma surface surf MobileBlinnPhong exclude_path:prepass halfasview
// Add noforwardadd to use 1 directional light + vertex lights.

inline fixed4 LightingMobileBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten)
{
	fixed diff = max (0, dot (s.Normal, lightDir));
	fixed nh = max (0, dot (s.Normal, halfDir));
	fixed spec = pow (nh, s.Specular*128) * s.Gloss;
	
	fixed4 c;
	c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten*2);
	c.a = 0.0;
	return c;
}

sampler2D _MainTexA;
sampler2D _MainTexB;
sampler2D _BumpMapA;
sampler2D _BumpMapB;
half _Shininess;

struct Input {
	float2 uv_MainTexA;
	float2 uv_MainTexB;
	float4 color : COLOR;
	float2 uv_BumpMapA;
	float2 uv_BumpMapB;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 Tex2D0 = tex2D(_MainTexA, IN.uv_MainTexA);
	fixed4 Tex2D1 = tex2D(_MainTexB, IN.uv_MainTexB);
	fixed4 Lerp0=lerp(Tex2D0,Tex2D1,IN.color);

	float4 Tex2D2=tex2D(_BumpMapA,(IN.uv_BumpMapA.xyxy).xy);
	float4 UnpackNormal0=float4(UnpackNormal(Tex2D2).xyz, 1.0);
	float4 Tex2D3=tex2D(_BumpMapB,(IN.uv_BumpMapB.xyxy).xy);
	float4 UnpackNormal1=float4(UnpackNormal(Tex2D3).xyz, 1.0);
	float4 Lerp1=lerp(UnpackNormal0,UnpackNormal1,IN.color);

	o.Albedo = Lerp0.rgb;
	o.Gloss = Lerp0.a;
	o.Alpha = Lerp0.a;
	o.Specular = _Shininess;
	o.Normal = Lerp1;

}
ENDCG
}

FallBack "Mobile/Bumped Specular"
}

hello
i m trying to animate wrinkle map on cloth.
your shader is what i m looking for, but i miss one feature that i miss to achieve , not sure if it s possible in unity, you can see on that video they are blending several material with normal map and diffuse, the result is quite convincing, any idea how they made those effect?

https://www.youtube.com/watch?v=foGfXQ73jHw

thanks

I know this is an old post…but did you update this shader…it’s just what i need…only don’t understand how you get the blending on the vertex collor…could you shortly explain??

could you also make it…that it takes it’s position from a image with coloring

Or you could just use my vertex toolkit:

1 Like