Shader——地形纹理混合

作者:追风剑情 发布于:2015-9-15 20:51 分类:Shader

函数 功能描述
lerp(a,b,f) 涉及线性插值(1-f)*a+b*f,在这里,a和b可以匹配为一个矢量或标题类型,f可以是一个标量或者与a、b同类型的矢量。

一、创建shader、material、Plane

  1. Shader "Custom/TerrainBlend" {
  2. Properties {
  3. _MainTint ("Diffuse Tint", Color) = (1,1,1,1)
  4. _ColorA ("Terrain Color A", Color) = (1,1,1,1)
  5. _ColorB ("Terrain Color B", Color) = (1,1,1,1)
  6. _RTexture ("Red Channel Texture", 2D) = "" {}
  7. _GTexture ("Green Channel Texture", 2D) = "" {}
  8. _BTexture ("Blue Channel Texture", 2D) = "" {}
  9. _ATexture ("Alpha Channel Texture", 2D) = "" {}
  10. _BlendTex ("Blend Texture", 2D) = "" {}
  11. }
  12. SubShader {
  13. Tags { "RenderType"="Opaque" }
  14. LOD 200
  15. CGPROGRAM
  16. #pragma surface surf Lambert
  17.  
  18. float4 _MainTint;
  19. float4 _ColorA;
  20. float4 _ColorB;
  21. sampler2D _RTexture;
  22. sampler2D _GTexture;
  23. sampler2D _BTexture;
  24. sampler2D _ATexture;
  25. sampler2D _BlendTex;
  26.  
  27. struct Input {
  28. float2 uv_RTexture;
  29. float2 uv_GTexture;
  30. float2 uv_BTexture;
  31. float2 uv_ATexture;
  32. float2 uv_BlendTex;
  33. };
  34.  
  35. void surf (Input IN, inout SurfaceOutput o) {
  36. //纹理采样
  37. float4 blendData = tex2D (_BlendTex, IN.uv_BlendTex);
  38. float4 rTexData = tex2D (_RTexture, IN.uv_RTexture);
  39. float4 gTexData = tex2D (_GTexture, IN.uv_GTexture);
  40. float4 bTexData = tex2D (_BTexture, IN.uv_BTexture);
  41. float4 aTexData = tex2D (_ATexture, IN.uv_ATexture);
  42. //使用lerp()函数(线性插值)对纹理进行混合。
  43. float4 finalColor;
  44. finalColor = lerp(rTexData, gTexData, blendData.g);
  45. finalColor = lerp(finalColor, bTexData, blendData.b);
  46. finalColor = lerp(finalColor, aTexData, blendData.a);
  47. finalColor.a = 1.0;
  48. o.Albedo = finalColor.rgb * _MainTint.rgb;
  49. o.Alpha = finalColor.a;
  50. }
  51. ENDCG
  52. }
  53. FallBack "Diffuse"
  54. }

t2.png

二、运行效果

t1.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号