- Home /
Shader question
Hi,
i've made a small shader that takes a color map with RGBA channels and also takes 4 diffuse textures as channels and multiply each color channel on the map with each diffuse texture to blend them together on a piece of mesh terrain.
Now the problem is i also want to use the alpha channel for the 4th texture. Now i've come to the problem that the map.a is applying the texture to the opacity value of 1 and not that of 0.
Here's a picture of it:
Now if a create a float value for the alpha channel and do
float a = m.a - 1;
It applies to the correct regions directed by the color map. But the texture itself is screwed in a way:
How can i fix this problem shown in pic2 ?
Here's the shader code:
Shader "Exile/terrain/exile_4lyr_terrain" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_Layer1 ("Layer1", 2D) = "white" {}
_Layer2 ("Layer2", 2D) = "white" {}
_Layer3 ("Layer3", 2D) = "white" {}
_Layer4 ("Layer4", 2D) = "white" {}
_Map ("Map", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _Layer1;
sampler2D _Layer2;
sampler2D _Layer3;
sampler2D _Layer4;
sampler2D _Map;
float4 _Color;
struct Input {
float2 uv_Layer1;
float2 uv_Layer2;
float2 uv_Layer3;
float2 uv_Layer4;
float2 uv_Map;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 l1 = tex2D (_Layer1, IN.uv_Layer1);
half4 l2 = tex2D (_Layer2, IN.uv_Layer2);
half4 l3 = tex2D (_Layer3, IN.uv_Layer3);
half4 l4 = tex2D (_Layer4, IN.uv_Layer4);
half4 m = tex2D (_Map, IN.uv_Map);
float a = m.a - 1;
o.Albedo = ((l1.rgb * m.r) + (l2.rgb * m.g) + (l3.rgb * m.b) + (l4.rgb * a)) * _Color.rgb;
}
ENDCG
}
FallBack "Diffuse"
}
what is in the alpha channel of the map texture? all 0? because if it's all zero, you're applying an alpha of -1 (0-1)... and -1 isn't a good alpha to apply and causes this effect
I'm not sure what effect you're trying to achieve, perhaps you just want to invert the alpha? float a = 1 - m.a;
Yes i want to invert the alpha but it gives me that that particular channel is very bright like this :
I'm afraid we can't see the image you've attached, but the colour will start looking more and more white/bright the more colour you add together. If that channel is adding too much colour, just reduce it with float a = 1 - m.a; a *= 0.6;
i' ve edited it , and that seems logical but when i add the float (also with slider) it basically stays bright but yea only the intensity of how much is shown of that particular texture changes.
Answer by GrahamReeves · Jun 06, 2014 at 01:44 PM
Everytime you add more colour from the different layers you're adding brightness, so it will ONLY get brighter and brighter.
Perhaps this will work if you want to "merge" the different layers as they appear. This method should mean if you only have one layer in an area, it will be full colour and not merged with the others and I think should have a similar brightness all over.
void surf (Input IN, inout SurfaceOutput o) {
half4 l1 = tex2D (_Layer1, IN.uv_Layer1);
half4 l2 = tex2D (_Layer2, IN.uv_Layer2);
half4 l3 = tex2D (_Layer3, IN.uv_Layer3);
half4 l4 = tex2D (_Layer4, IN.uv_Layer4);
half4 m = tex2D (_Map, IN.uv_Map);
// not sure if you want this or not:
// m.a = 1 - m.a;
// don't let intensity of all layers go over 1
float AlphaWeight = m.r + m.g + m.b + m.a;
if ( AlphaWeight > 1 )
m *= 1.0 / AlphaWeight;
o.Albedo = ((l1.rgb * m.r) + (l2.rgb * m.g) + (l3.rgb * m.b) + (l4.rgb * m.a)) * _Color.rgb;
}