Unity Shaders and Effects Cookbook (2-4) compressing and blending texture maps: Storing interpolation information using grayscale graphs

Source: Internet
Author: User
Tags scale image

This section read a few times to slowly understand.

The first is the gray chart, why is called grayscale, because the picture of R, G, B is stored in the same data, open unity to adjust the color to see more intuitive.


As can be seen, when R, G, B three values of the same time, the picture is only black and white, and lost other colors, so we call the grayscale map.


When do I use a grayscale image?

This article turns from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

For example, height map, at the point of this coordinate, high altitude, recorded as 1, altitude must not recorded as 0. This data is stored in the RGB three channels, so that the more white the higher, the darker the lower.


It's all about the production and application of gray-scale graphs.

And then, when we get the grayscale graph out, it is found that a graph of R, G, B, a all store the same data is too wasted ..., so you can put different gray-scale images, such as the 4 gray-scale image of the data, respectively, stored in a picture of R, G, B, a, so it does not waste, reducing the file volume. This is what the book says about texture compression.

For example, my lawn, stone, sand, water four pictures, to be on the same terrain, to mix, then there is this kind of data, grassland and stone mixing coefficient A, and then the mixture coefficient of sand and B, and then the mixing coefficient of water flow c. The A, B, and C three data are stored in a picture of the R, G, B three channels inside. When mixing, we can remove the color data of 4 images in shader, and then use the TEX2D function to remove the blending coefficients of the current coordinates. This is convenient.


This is an example of what the book says.

First look at the example image of texture compression



Enter into this section of the example

This section has four images of grass, stone, sand, water, and then a graph that stores the blending coefficients of these 4 graphs.

This article turns from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

The following picture stores the mixing factor, pretty, notice that the R, G, and B of the graph represent the mixing coefficients of different pictures.



The following will be through the Shader, first read the grass, stone, sand, water flow four pictures of the color data, and then read out the mixed coefficient data, and then mixed.

Shader "cookbookshaders/packing and blending textures" {Properties {_shitou_texture ("Shitou Texture", 2D) = "White" {}_ Cao_texture ("Cao Texture", 2D) = "White" {}_shazi_texture ("Shazi Texture", 2D) = "White" {}_niba_texture ("Niba Texture", 2D) = "White" {}_blendtexture ("Blend Texture", 2D) = "White" {}}subshader {Tags {"rendertype" = "Opaque"}lod 200cgprogram#  pragma surface surf lambertsampler2d _shitou_texture;    Stone sampler2d _cao_texture;//grass sampler2d _shazi_texture;    Sand sampler2d _niba_texture; Mud sampler2d _blendtexture; A gray-scale image of the coefficients of the above picture and graph is recorded, for example, the R-channel records the mixing coefficients of the stone and the grass, and the G-channel records the coefficients of mixing the above mixed results with the sand. Gray-scale graph actually it does not exist as a picture, but as a record of data, such as the recording of 5 images of mixed information, such as recording 4 images of the alpha, such as the height of the calculation of the terrain, altitude is recorded as 1, elevation must not recorded as 0.struct Input {float2 uv_ SHITOU_TEXTURE;FLOAT2 uv_cao_texture;float2 uv_shazi_texture;float2 uv_niba_texture;float2 uv_BlendTexture;}; void Surf (Input in, InOut surfaceoutput o) {float4 blenddata=tex2d (_blendtexture,in.uv_blendtexture);// Read all the mixed data from the grayscale graph recording the mixing coefficients float4 shitoudata=tex2d (_shitou_texture,in.uv_shitou_texture);//read out the color data of the stone's coordinates; FLOAT4 caodata=tex2d (_cao_texture,in.uv_cao_texture); Float4 shazidata=tex2d (_shazi_texture, in.uv_shazi_texture); float4 nibadata=tex2d (_niba_texture,in.uv_niba_texture); FLOAT4 finalcolor;// Mixed stone and grass Finalcolor = Lerp (SHITOUDATA,CAODATA,BLENDDATA.R); The stone and grass are interpolated according to the values in the R channel in the grayscale map. Finalcolor = Lerp (FINALCOLOR,SHAZIDATA,BLENDDATA.G); finalcolor = Lerp (FINALCOLOR,NIBADATA,BLENDDATA.B); Finalcolor.a=1;o. Albedo = Finalcolor.rgb;o. Alpha = FINALCOLOR.A;} ENDCG} FallBack "Diffuse"}

The effect after mixingThis article turns from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn


Notice that this section is in the LERP function.

Lerp (a,b,f) linear interpolation (1-f) *a + b*f

such as Lerp (1,2,0.5) =1.5


Sample Project Package Download:

Http://pan.baidu.com/s/1dFFIfnV


Unity Shaders and Effects Cookbook (2-4) compressing and blending texture maps: Storing interpolation information using grayscale graphs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.