简单的双面自定义着色器看起来很奇怪

时间:2018-05-23 12:12:54

标签: unity3d 3d shader

我对Shaders编程很新,我试图创建一个着色器:

  • 允许使用2个对应的法线贴图(淡入/淡出)更改2个纹理
  • 双面(从表面两侧显示网格)

以下是我的最终结果:

  Shader "Custom/TextureBlend" {
         Properties {
             _Color ("Color", Color) = (1,1,1,1)
             _Blend ("Texture Blend", Range(0,1)) = 0.0
             _MainTex ("Albedo (RGB)", 2D) = "white" {}
             _MainTex2 ("Albedo 2 (RGB)", 2D) = "white" {}
             _Glossiness ("Smoothness", Range(0,1)) = 0.5
             _Metallic ("Metallic", Range(0,1)) = 0.0
             _BumpMap ("Bumpmap", 2D) = "bump" {}
             _BumpMap2 ("Bumpmap", 2D) = "bump" {}
         }
         SubShader {
             Tags { "RenderType"="Opaque" }
             LOD 200
              Cull Off
              ZTest LEqual

             CGPROGRAM
             // Physically based Standard lighting model, and enable shadows on all light types
             #pragma surface surf Standard fullforwardshadows

             // Use shader model 3.0 target, to get nicer looking lighting
             #pragma target 3.0

             sampler2D _MainTex;
             sampler2D _MainTex2;
             sampler2D _BumpMap;
             sampler2D _BumpMap2;

             struct Input {
                 float2 uv_MainTex;
                 float2 uv_MainTex2;
                 float2 uv_BumpMap;
                 float2 uv_BumpMap2;
             };

             half _Blend;
             half _Glossiness;
             half _Metallic;
             fixed4 _Color;

             void surf (Input IN, inout SurfaceOutputStandard o) {
                 // Albedo comes from a texture tinted by color
                 fixed4 c = lerp (tex2D (_MainTex, IN.uv_MainTex), tex2D (_MainTex2, IN.uv_MainTex2), _Blend) * _Color;
                 o.Albedo = c.rgb;
                 // Metallic and smoothness come from slider variables
                 o.Metallic = _Metallic;
                 o.Smoothness = _Glossiness;
                 o.Alpha = c.a;
                 fixed4 n = lerp (tex2D (_BumpMap, IN.uv_BumpMap), tex2D (_BumpMap2, IN.uv_BumpMap2), _Blend) * _Color;
                 o.Normal = n.rgb;
             }
             ENDCG
         }
         FallBack "Diffuse"
     }

它起作用,但由于某种原因,表面上的纹理看起来暗淡(在网格的另一侧通常看不到它甚至更暗),并且法线贴图几乎看不见。

在左边,您可以在右侧看到带有着色器的平面 - 标准着色器:

enter image description here

此外反射看起来很奇怪,如果我将金属滑块向右移动你会看到它:

enter image description here

我会非常感激任何帮助,因为我对着色器编程很陌生 非常感谢你提前!

1 个答案:

答案 0 :(得分:1)

法线贴图存储矢量信息,而常规纹理是无符号的,因此您需要使用UnpackNormal()辅助方法as shown in the samples。您也不需要乘以Color。

最后的代码将是:

fixed4 n0 = tex2D(_BumpMap, IN.uv_BumpMap);
fixed4 n1 = tex2D(_BumpMap2, IN.uv_BumpMap2);
o.Normal = UnpackNormal(lerp(n0, n1, _Blend)).xyz;