Unity Shaders and Effects Cookbook 7-3 在地形中使用顶点颜色做混合

               

使用顶点信息 最 常见的情况之一 就是创建更加真实的地形或者环境,通过使用顶点颜色的RGBA通道 混合到不同的纹理中。这样就不用再导入一张贴图作为混合贴图。


新建场景,导入随书的一个平面网格模型。

新建材质,新建Shader。在材质中选择Shader,将材质赋值给模型。


Shader代码如下:

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 200    CGPROGRAM  #pragma surface surf Lambert vertex:vert  sampler2D _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; //顶点颜色 R 中也存储高度数据,1减去,就是高度图中存储的高度数据占比   float rHeight=heightColor.r * redChannel; //高度图的R实际数据   float invertHeight=1-heightColor.r;   float finalHeight=(invertHeight * redChannel)*4;   float finalBlend=saturate(rHeight + finalHeight);   //计算顶点混合衰减值,这使我们能够为混合纹理增加更多一级细节   float hardness=((1-IN.vertexColor.g) * _Value)+1;   finalBlend=pow(finalBlend,hardness);   //将finalBlend作为线性插值的系数   float3 finalColor=lerp(firstColor,secondColor,finalBlend);   o.Albedo=finalColor;   o.Alpha=firstColor.a;  }  ENDCG }  FallBack "Diffuse"}
转自http://www.thisisgame.com.cn http://blog.csdn.net/huutu

效果:



原理:

扫描二维码关注公众号,回复: 5105872 查看本文章

原本我们应该要有一张图片存储 用来做混合的数据,但是多一张图就需要更多系统资源来处理。既然顶点数据中有color 数据,那么我们在做模型的时候可以把数据刷到顶点颜色中,就省了这一张图。


示例项目下载

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


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43667944/article/details/86651657
今日推荐