Unity Shaders and Effects Cookbook (7-3) blend with vertex colors in the terrain

Source: Internet
Author: User

One of the most common scenarios for using vertex information is to create a more realistic terrain or environment that is blended into different textures by using an RGBA channel that uses vertex colors. This way, you don't have to import a map as a mixed map.


Create a new scene and import a flat mesh model with the book.

New material, new shader. Select shader in the material and assign the material to the model.


The shader code is as follows:

Shader "Cookbookshaders/chapt7-3/vertexheightmapblend" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_secondtex ( "Second Texture", 2D) = "White" {}_heightmap ("Heightmap", 2D) = "White" {}_value ("Value", Range (1,20)) =3}subshader {Tags { "Rendertype" = "Opaque"}lod 200cgprogram#pragma surface surf Lambert vertex:vertsampler2d _maintex;sampler2d _secondtex ; sampler2d _heightmap;float _value;struct Input {float2 uv_maintex;float2 uv_secondtex;float3 vertexcolor;}; void Vert (inout appdata_full v,out Input o) {unity_initialize_output (input,o); O.vertexcolor=v.color.rgb;} void Surf (Input in, InOut surfaceoutput o) {half4 firstcolor=tex2d (_maintex,in.uv_maintex); Half4 secondcolor=tex2d (_ Secondtex,in.uv_secondtex); float4 heightcolor=tex2d (_heightmap,in.uv_maintex); float redchannel=1- IN.VERTEXCOLOR.R; The vertex color R also stores the height data, 1 minus the height data stored in the height graph is the ratio of float RHEIGHT=HEIGHTCOLOR.R * REDCHANNEL; The r actual data of the height graph float invertheight=1-heightcolor.r;float finalheight= (invertheight * redchannel) *4;float finalBlend= Saturate (rheight + finalheight);//calculates vertex mix attenuation values, which allows us to add more level of detail to the blend texture float hardness= ((1-IN.VERTEXCOLOR.G) * _value) +1;finalblend=pow ( finalblend,hardness);//Finalblend as the coefficient of linear interpolation float3 finalcolor=lerp (firstcolor,secondcolor,finalblend); Albedo=finalcolor;o. ALPHA=FIRSTCOLOR.A;} ENDCG} FallBack "Diffuse"}
transfer from http://www.thisisgame.com.cn Http://blog.csdn.net/huutu

Effect:



Principle:

Originally, we should have a picture store used to do mixed data, but more than one picture needs more system resources to handle. Since there are color data in the vertex data, we can brush the data to the top color when we do the model, saving this picture.


Sample Project Download

http://pan.baidu.com/s/1bpfrk9h


Unity Shaders and Effects Cookbook (7-3) blend with vertex colors in the terrain

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.