Unity3d Shaderlab compressed mixed texture map
Textures can be used to store large amounts of data, we can package multiple images on a single RGBA texture, and then extract these elements through shader code ,
we'll be able to use each image's RGBA the message of the channel as a separate texture.
This usage scenario, especially on the ground, has obvious advantages, so let's say how to build a common four texture mixed shader.
Then please county create a shader and material ball bar. Here we go directly:
1, modify Properties
{
setting up the GUI;
_maintint ("Diffuse Tint", Color) = (1,1,1,1)
_colora ("Terrain Color A", color) = (1,1,1,1)
_colorb ("Terrain Color B", color) = (1,1,1,1)
_rtexture ("Red Channel Texture", 2D) = ""
_gtexture ("Green Channel Texture", 2D) = ""
_btexture ("Blue Channel Texture", 2D) = ""
_atexture ("Alpha Channel Texture", 2D) = ""
_blendtexture ("Blend Texture", 2D) = ""
}
2,subshader declaration variable
#pragma surface surf Lambert
variables ;
FLOAT4 _maintint;
FLOAT4 _colora;
FLOAT4 _colorb;
Sampler2d _rtexture;
Sampler2d _gtexture;
Sampler2d _btexture;
Sampler2d _atexture;
Sampler2d _blendtexture;
3. Modify the Input structure
struct Input {
FLOAT2 uv_rtexture;
FLOAT2 uv_gtexture;
FLOAT2 uv_btexture;
FLOAT2 uv_atexture;
FLOAT2 uv_blendtexture;
};
4, modify the Invincible surf function
void Surf (Input in, InOut surfaceoutput o) {
Get texture message ;
FLOAT4 blenddata = tex2d (_blendtexture,in.uv_blendtexture);
FLOAT4 rtexdata = tex2d (_rtexture,in.uv_rtexture);
FLOAT4 gtexdata = tex2d (_gtexture,in.uv_gtexture);
FLOAT4 btexdata = tex2d (_btexture,in.uv_btexture);
FLOAT4 atexdata = tex2d (_atexture,in.uv_atexture);
to mix textures ;
FLOAT4 Finalcolor;
Finalcolor = Lerp (RTEXDATA,GTEXDATA,BLENDDATA.G);
Finalcolor = Lerp (FINALCOLOR,BTEXDATA,BLENDDATA.B);
Finalcolor = Lerp (FINALCOLOR,ATEXDATA,BLENDDATA.A);
Finalcolor.a=1;
calculate Hue ;
FLOAT4 terrainlayers = lerp (_COLORA,_COLORB,BLENDDATA.R) * *;
Finalcolor *= terrainlayers;
Finalcolor = saturate (Finalcolor);
Half4 C = tex2d (_maintex, In.uv_maintex);
O.albedo = Finalcolor.rgb*_maintint.rgb;
O.alpha = FINALCOLOR.A;
}
////////////////////////////////////////////////////
These are all the steps of our hybrid texture Shader .
Let's look at the effect directly in unity .
It uses lerp (a,b,f). If we want to get the median between 1 and 2 , we can assign the third function of the lerp function to a value of 0.5 , he will return 1.5 ,
This function is good for blending textures, because in a RGBA the value of a single channel in a texture is a single-precision floating-point value, typically a range of values 0~1 between.
We will provide the grass texture soil texture as well as the mixed texture Red channel 3 values as lerp function parameters, so that we get the object surface each pixel value is the correct blend color.
Code Start-------------------------------------------------------------------------------
Shader "91ygame/textureblend" {
Properties {
Setting up the GUI;
_maintint ("Diffuse Tint", Color) = (1,1,1,1)
_colora ("Terrain Color A", color) = (1,1,1,1)
_colorb ("Terrain Color B", color) = (1,1,1,1)
_rtexture ("Red Channel Texture", 2D) = ""
_gtexture ("Green Channel Texture", 2D) = ""
_btexture ("Blue Channel Texture", 2D) = ""
_atexture ("Alpha Channel Texture", 2D) = ""
_blendtexture ("Blend Texture", 2D) = ""
}
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD 200
Cgprogram
#pragma surface surf Lambert
Variable
FLOAT4 _maintint;
FLOAT4 _colora;
FLOAT4 _colorb;
Sampler2d _rtexture;
Sampler2d _gtexture;
Sampler2d _btexture;
Sampler2d _atexture;
Sampler2d _blendtexture;
struct Input {
FLOAT2 uv_rtexture;
FLOAT2 uv_gtexture;
FLOAT2 uv_btexture;
FLOAT2 uv_atexture;
FLOAT2 uv_blendtexture;
};
void Surf (Input in, InOut surfaceoutput o) {
Get texture message;
FLOAT4 blenddata = tex2d (_blendtexture,in.uv_blendtexture);
FLOAT4 rtexdata = tex2d (_rtexture,in.uv_rtexture);
FLOAT4 gtexdata = tex2d (_gtexture,in.uv_gtexture);
FLOAT4 btexdata = tex2d (_btexture,in.uv_btexture);
FLOAT4 atexdata = tex2d (_atexture,in.uv_atexture);
to mix textures;
FLOAT4 Finalcolor;
Finalcolor = Lerp (RTEXDATA,GTEXDATA,BLENDDATA.G);
Finalcolor = Lerp (FINALCOLOR,BTEXDATA,BLENDDATA.B);
Finalcolor = Lerp (FINALCOLOR,ATEXDATA,BLENDDATA.A);
finalcolor.a=1.0;
Calculate hue;
FLOAT4 terrainlayers = lerp (_COLORA,_COLORB,BLENDDATA.R) * *;
Finalcolor *= terrainlayers;
Finalcolor = saturate (Finalcolor);
Half4 C = tex2d (_maintex, In.uv_maintex);
O.albedo = Finalcolor.rgb*_maintint.rgb;
O.alpha = FINALCOLOR.A;
}
Endcg
}
FallBack "Diffuse"
}
Code End--------------------------------------------------------------------------------
Unity3d Shaderlab compressed Mixed texture map