地形混合shader
1.四个贴图混合
1 Shader "Custom/BlendTex_surface" { 2 Properties { 3 _RTexture("Red Channel Texture", 2D) = "" {} 4 _GTexture("Green Channel Texture", 2D) = "" {} 5 _BTexture("Blue Channel Texture", 2D) = "" {} 6 _ATexture("Alpha Channel Texture", 2D) = "" {} 7 _Mask("Mask(RG)",2D) = ""{} 8 } 9 SubShader { 10 Tags { "RenderType"="Opaque" } 11 LOD 200 12 13 CGPROGRAM 14 #pragma surface surf Lambert 15 #pragma target 4.0 16 17 sampler2D _RTexture; 18 sampler2D _GTexture; 19 sampler2D _BTexture; 20 sampler2D _ATexture; 21 22 sampler2D _Mask; 23 24 struct Input { 25 float2 uv_RTexture; 26 float2 uv_GTexture; 27 float2 uv_BTexture; 28 float2 uv_ATexture; 29 float2 uv_Mask; 30 }; 31 32 void surf (Input IN, inout SurfaceOutput o) { 33 float4 rTexData = tex2D(_RTexture, IN.uv_RTexture); 34 float4 gTexData = tex2D(_GTexture, IN.uv_GTexture); 35 float4 bTexData = tex2D(_BTexture, IN.uv_BTexture); 36 float4 aTexData = tex2D(_ATexture, IN.uv_ATexture); 37 float4 blendData = tex2D(_Mask, IN.uv_Mask); 38 39 float4 finalColor; 40 //根据blendData.g 将 RTexture 和 GTexture 混合 41 finalColor = lerp(rTexData, gTexData, blendData.g);//原本为g 42 //根据blendData.b 将 BTexture 混合 43 finalColor = lerp(finalColor, bTexData, blendData.b);//原本为b 44 //根据blendData.a 将 ATexture 混合 45 finalColor = lerp(finalColor, aTexData , blendData.a);//原本为a 46 finalColor = saturate(finalColor); 47 o.Albedo = finalColor.rgb; 48 o.Alpha = finalColor.a; 49 } 50 ENDCG 51 } 52 FallBack "Diffuse" 53 }
1 Shader "Custom/BlendTex_vertfrag" 2 { 3 Properties 4 { 5 Tex0 ("Layer 0 (R)", 2D) = "white" {} 6 Tex1 ("Layer 1 (G)", 2D) = "white" {} 7 Tex2 ("Layer 2 (G)", 2D) = "white" {} 8 Tex3 ("Layer 3 (A)", 2D) = "white" {} 9 _Control ("Control (RGBA)", 2D) = "red" {} 10 _IllumFactor ("Illumin Factor", Range(1,2)) = 1 11 } 12 13 SubShader 14 { 15 Tags{ "Queue" = "Geometry+110" } 16 Pass 17 { 18 19 Tags{"LightMode" = "ForwardBase" } 20 21 CGPROGRAM 22 #pragma vertex vert 23 #pragma fragment frag 24 #pragma target 2.0 25 #include "UnityCG.cginc" 26 27 sampler2D _Control; 28 sampler2D Tex0; 29 sampler2D Tex1; 30 sampler2D Tex2; 31 sampler2D Tex3; 32 half _IllumFactor; 33 34 half4 Tex0_ST; 35 half4 Tex1_ST; 36 half4 Tex2_ST; 37 half4 Tex3_ST; 38 half4 _Control_ST; 39 40 41 struct v2f 42 { 43 float4 pos : SV_POSITION; 44 half2 uv_Tex0:TEXCOORD0; 45 half2 uv_Tex1:TEXCOORD1; 46 half2 uv_Tex2:TEXCOORD2; 47 half2 uv_Tex3:TEXCOORD3; 48 half2 uv_Control:TEXCOORD4; 49 }; 50 51 struct appdata 52 { 53 float4 vertex : POSITION; 54 float4 texcoord : TEXCOORD0; 55 }; 56 57 58 v2f vert (appdata v) 59 { 60 float4 worldPos = mul(unity_ObjectToWorld, v.vertex); 61 62 v2f o; 63 64 o.uv_Control = v.texcoord.xy; 65 o.uv_Tex0 = TRANSFORM_TEX(v.texcoord ,Tex0); 66 o.uv_Tex1 = TRANSFORM_TEX(v.texcoord ,Tex1); 67 o.uv_Tex2 = TRANSFORM_TEX(v.texcoord, Tex2); 68 o.uv_Tex3 = TRANSFORM_TEX(v.texcoord, Tex3); 69 o.pos = mul(UNITY_MATRIX_MVP, float4(v.vertex.xyz, 1)); 70 71 return o; 72 } 73 74 75 fixed4 frag(v2f i) : COLOR 76 { 77 fixed4 splat_control = tex2D (_Control, i.uv_Control.xy); 78 79 fixed4 albedo = fixed4(1,1,1,1); 80 fixed4 Tex0Color = tex2D(Tex0, i.uv_Tex0); 81 //albedo.rgb = splat_control.r * Tex0Color.rgb;...用这个会出现不明黑色阴影 82 fixed4 Tex1Color = tex2D(Tex1, i.uv_Tex1); 83 //albedo.rgb += splat_control.g * Tex1Color.rgb; 84 85 fixed4 Tex2Color = tex2D(Tex2, i.uv_Tex2).rgba; 86 //albedo.rgb += splat_control.b * Tex2Color; 87 fixed4 Tex3Color = tex2D(Tex3, i.uv_Tex3).rgba; 88 //albedo.rgb += splat_control.a * Tex3Color; 89 90 albedo = lerp(Tex0Color,Tex1Color, splat_control.g); 91 albedo = lerp(albedo,Tex2Color, splat_control.b); 92 albedo = lerp(albedo,Tex3Color, splat_control.a); 93 94 return albedo* _IllumFactor; 95 } 96 97 ENDCG 98 } 99 } 100 FallBack "Diffuse" 101 }
2.四个贴图(加法线)混合
1 Shader "Custom/BlendNormalTex_surface" { 2 Properties { 3 _RTexture("Red Channel Texture", 2D) = "" {} 4 _GTexture("Green Channel Texture", 2D) = "" {} 5 _BTexture("Blue Channel Texture", 2D) = "" {} 6 _ATexture("Alpha Channel Texture", 2D) = "" {} 7 8 _RNormalTex ("RNormal Map", 2D) = "bump" {} 9 _GNormalTex ("GNormal Map", 2D) = "bump" {} 10 _BNormalTex ("BNormal Map", 2D) = "bump" {} 11 _ANormalTex ("ANormal Map", 2D) = "bump" {} 12 13 _RNormalIntensity ("RNormal Map Intensity", Range(-1,1)) = 1 14 _GNormalIntensity ("GNormal Map Intensity", Range(-1,1)) = 1 15 _BNormalIntensity ("BNormal Map Intensity", Range(-1,1)) = 1 16 _ANormalIntensity ("ANormal Map Intensity", Range(-1,1)) = 1 17 _IllumFactor ("Illumin Factor", Range(1,2)) = 1 18 19 20 21 _Mask("Mask(RG)",2D) = ""{} 22 } 23 SubShader { 24 Tags { "RenderType"="Opaque" } 25 LOD 200 26 27 CGPROGRAM 28 #pragma surface surf Lambert 29 // #pragma surface surf Lambert vertex:vert 30 #pragma target 4.0 31 32 sampler2D _RTexture,_GTexture,_BTexture,_ATexture; 33 sampler2D _RNormalTex,_GNormalTex,_BNormalTex,_ANormalTex; 34 float _RNormalIntensity,_GNormalIntensity,_BNormalIntensity,_ANormalIntensity; 35 float _IllumFactor; 36 37 sampler2D _Mask; 38 39 struct Input { 40 float2 uv_RTexture; 41 float2 uv_GTexture; 42 float2 uv_BTexture; 43 float2 uv_ATexture; 44 float2 uv_RNormalTex; 45 float2 uv_GNormalTex; 46 float2 uv_BNormalTex; 47 float2 uv_ANormalTex; 48 float2 uv_Mask; 49 // float3 m_normal; 50 }; 51 void surf (Input IN, inout SurfaceOutput o) { 52 float4 rTexData = tex2D(_RTexture, IN.uv_RTexture); 53 float4 gTexData = tex2D(_GTexture, IN.uv_GTexture); 54 float4 bTexData = tex2D(_BTexture, IN.uv_BTexture); 55 float4 aTexData = tex2D(_ATexture, IN.uv_ATexture); 56 float4 blendData = tex2D(_Mask, IN.uv_Mask); 57 58 float4 finalColor; 59 //根据blendData.g 将 RTexture 和 GTexture 混合 60 finalColor = lerp(rTexData, gTexData, blendData.g); 61 //根据blendData.b 将 BTexture 混合 62 finalColor = lerp(finalColor, bTexData, blendData.b); 63 //根据blendData.a 将 ATexture 混合 64 finalColor = lerp(finalColor, aTexData , blendData.a); 65 finalColor = saturate(finalColor); 66 67 68 float3 rNormalMap = UnpackNormal(tex2D(_RNormalTex, IN.uv_RNormalTex)); 69 rNormalMap.xy*=_RNormalIntensity; 70 float3 gNormalMap = UnpackNormal(tex2D(_GNormalTex, IN.uv_GNormalTex)); 71 gNormalMap.xy*=_GNormalIntensity; 72 float3 bNormalMap = UnpackNormal(tex2D(_BNormalTex, IN.uv_BNormalTex)); 73 bNormalMap.xy*=_BNormalIntensity; 74 //bNormalMap.z=sqrt(1.0-saturate(dot(bNormalMap.xy,bNormalMap.xy)));这个似乎并没有效果 75 float3 aNormalMap = UnpackNormal(tex2D(_ANormalTex, IN.uv_ANormalTex)); 76 aNormalMap.xy*=_ANormalIntensity; 77 78 float3 finalNormal; 79 //根据blendData.g 将 RTexture 和 GTexture 混合 80 finalNormal = lerp(rNormalMap, gNormalMap, blendData.g); 81 //根据blendData.b 将 BTexture 混合 82 finalNormal = lerp(finalNormal, bNormalMap, blendData.b); 83 //根据blendData.a 将 ATexture 混合 84 finalNormal = lerp(finalNormal, aNormalMap , blendData.a); 85 86 o.Albedo =finalColor*_IllumFactor; 87 o.Alpha = finalColor.a; 88 o.Normal=finalNormal; 89 //o.Normal=rNormalMap*blendData.r+gNormalMap*blendData.g+bNormalMap*blendData.b+aNormalMap*blendData.a; 90 } 91 ENDCG 92 } 93 FallBack "Diffuse" 94 }
1 Shader "Custom/BlendNormalTex_vertfrag" 2 { 3 Properties 4 { 5 Tex0 ("Layer 0 (R)", 2D) = "white" {} 6 Tex1 ("Layer 1 (G)", 2D) = "white" {} 7 Tex2 ("Layer 2 (G)", 2D) = "white" {} 8 Tex3 ("Layer 3 (A)", 2D) = "white" {} 9 10 _Normal0("Normal 0 (A)", 2D) = "bump" {} 11 _Normal1("Normal 1 (B)", 2D) = "bump" {} 12 _Normal2("Normal 2 (G)", 2D) = "bump" {} 13 _Normal3("Normal 3 (R)", 2D) = "bump" {} 14 15 _BumpScale0("BumpScale 0", Range(-1.0,1.0)) = 1.0 16 _BumpScale1("BumpScale 1", Range(-1.0, 1.0)) = 1.0 17 _BumpScale2("BumpScale 2", Range(-1.0, 1.0)) = 1.0 18 _BumpScale3("BumpScale 3", Range(-1.0, 1.0)) = 1.0 19 _Control ("Control (RGBA)", 2D) = "red" {} 20 _IllumFactor ("Illumin Factor", Range(1,2)) = 1 21 } 22 23 SubShader 24 { 25 Tags{ "Queue" = "Geometry+110" } 26 Pass 27 { 28 29 Tags{"LightMode" = "ForwardBase" } 30 31 CGPROGRAM 32 #pragma vertex vert 33 #pragma fragment frag 34 #pragma target 2.0 35 #include "UnityCG.cginc" 36 #include "Lighting.cginc" 37 38 sampler2D _Control; 39 //贴图及纹理 40 sampler2D Tex0,Tex1,Tex2,Tex3; 41 half4 Tex0_ST,Tex1_ST,Tex2_ST,Tex3_ST; 42 //法线贴图及纹理 43 sampler2D _Normal0,_Normal1,_Normal2,_Normal3; 44 half4 _Normal0_ST,_Normal1_ST,_Normal2_ST,_Normal3_ST; 45 half _BumpScale0, _BumpScale1, _BumpScale2, _BumpScale3; 46 47 half _IllumFactor; 48 half4 _Control_ST; 49 50 51 struct v2f 52 { 53 float4 pos : SV_POSITION; 54 float3 normal:NORMAL; 55 float4 tangnent:TANGENT; 56 half4 uv_Tex0:TEXCOORD0; 57 half4 uv_Tex1:TEXCOORD1; 58 half4 uv_Tex2:TEXCOORD2; 59 half4 uv_Tex3:TEXCOORD3; 60 half2 uv_Control:TEXCOORD4; 61 half3 lightDir:TEXCOORD5; 62 half3 viewDir:TEXCOORD6; 63 }; 64 65 struct appdata 66 { 67 float4 vertex : POSITION; 68 float4 texcoord : TEXCOORD0; 69 float4 tangent:TANGENT; 70 float3 normal:NORMAL; 71 }; 72 73 74 v2f vert (appdata v) 75 { 76 v2f o; 77 o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 78 o.uv_Control = v.texcoord.xy; 79 o.uv_Tex0.xy = TRANSFORM_TEX(v.texcoord ,Tex0); 80 o.uv_Tex0.zw = TRANSFORM_TEX(v.texcoord ,_Normal0); 81 82 o.uv_Tex1.xy = TRANSFORM_TEX(v.texcoord ,Tex1); 83 o.uv_Tex1.zw = TRANSFORM_TEX(v.texcoord ,_Normal1); 84 85 o.uv_Tex2.xy = TRANSFORM_TEX(v.texcoord, Tex2); 86 o.uv_Tex2.zw = TRANSFORM_TEX(v.texcoord ,_Normal2); 87 88 o.uv_Tex3.xy = TRANSFORM_TEX(v.texcoord, Tex3); 89 o.uv_Tex3.zw = TRANSFORM_TEX(v.texcoord ,_Normal3); 90 91 TANGENT_SPACE_ROTATION; 92 o.lightDir=mul(rotation,ObjSpaceLightDir(v.vertex)).xyz; 93 o.viewDir=mul(rotation,ObjSpaceViewDir(v.vertex)).xyz; 94 95 return o; 96 } 97 fixed4 frag(v2f i):SV_Target 98 { 99 fixed3 tangentLightDir=normalize(i.lightDir); 100 fixed3 tangentViewDir=normalize(i.viewDir);//暂时没用到,用于补充高光 101 102 103 fixed4 splat_control = tex2D (_Control, i.uv_Control.xy); 104 105 106 fixed3 tangentNormal0=UnpackNormal(tex2D(_Normal0, i.uv_Tex0.zw)); 107 tangentNormal0.xy*=_BumpScale0; 108 109 fixed3 tangentNormal1=UnpackNormal(tex2D(_Normal1, i.uv_Tex1.zw)); 110 tangentNormal1.xy*=_BumpScale1; 111 112 fixed3 tangentNormal2=UnpackNormal(tex2D(_Normal2, i.uv_Tex2.zw)); 113 tangentNormal2.xy*=_BumpScale2; 114 115 fixed3 tangentNormal3=UnpackNormal(tex2D(_Normal3, i.uv_Tex3.zw)); 116 tangentNormal3.xy*=_BumpScale3; 117 118 fixed4 albedo = 0.0f; 119 120 fixed4 Tex0Color = tex2D(Tex0, i.uv_Tex0); 121 fixed4 Tex1Color = tex2D(Tex1, i.uv_Tex1); 122 fixed4 Tex2Color = tex2D(Tex2, i.uv_Tex2); 123 fixed4 Tex3Color = tex2D(Tex3, i.uv_Tex3); 124 125 albedo = lerp(Tex0Color*max(0, dot(tangentNormal1, tangentLightDir)),Tex1Color*max(0, dot(tangentNormal1, tangentLightDir)), splat_control.g); 126 albedo = lerp(albedo,Tex2Color*max(0, dot(tangentNormal2, tangentLightDir)), splat_control.b); 127 albedo = lerp(albedo,Tex3Color*max(0, dot(tangentNormal3, tangentLightDir)), splat_control.a); 128 129 return albedo*_IllumFactor; 130 } 131 ENDCG 132 } 133 } 134 FallBack "Diffuse" 135 }
效果:
albedo = lerp(Tex0Color,Tex1Color, splat_control.g);
//splat_control.g*Tex0Color+(1-splat_control.g)*Tex1Color
工程文件:https://files.cnblogs.com/files/luxishi/BlendShader.rar