美文网首页Unity技术分享Unity技术VR/AR分享
Unity半透明物体,无法烘焙正确的阴影

Unity半透明物体,无法烘焙正确的阴影

作者: 水影sta | 来源:发表于2019-01-09 20:52 被阅读0次

    不知道这是不是Unity的bug

    如下图,这是正确的显示,树叶开启了alphatest,light配置如下图

    image.png image.png image.png

    如果我们把shader的_MainTex,改名为_Diffuse(改properties里面),神奇的事情发生了,实时模式正常,但是烘焙之后,阴影不正常了!所以,经验教训是,漫反射贴图,老老实实命名为MainTex。。。。。

    image.png

    ASE版的Shader如下:

    // Made with Amplify Shader Editor
    // Available at the Unity Asset Store - http://u3d.as/y3X 
    Shader "Custom/Leaf"
    {
        Properties
        {
            _MainTex("MainTex", 2D) = "white" {} //是的,就是这里,一定要用_MainTex)
            _MainColor("MainColor", Color) = (1,1,1,0)
            _Cutoff( "Mask Clip Value", Float ) = 0.5
            _SpeedTurbulence("SpeedTurbulence", Float) = 0.5
            _Turbulence("Turbulence", Float) = 1
            [HideInInspector] _texcoord( "", 2D ) = "white" {}
            [HideInInspector] __dirty( "", Int ) = 1
        }
    
        SubShader
        {
            Tags{ "RenderType" = "TransparentCutout"  "Queue" = "AlphaTest+0" "IgnoreProjector" = "True" "IsEmissive" = "true"  }
            Cull Off
            CGPROGRAM
            #include "UnityShaderVariables.cginc"
            #pragma target 3.0
            #pragma surface surf Unlit keepalpha addshadow fullforwardshadows noshadow exclude_path:deferred vertex:vertexDataFunc 
            struct Input
            {
                float2 uv_texcoord;
            };
    
            uniform float _SpeedTurbulence;
            uniform float _Turbulence;
            uniform float4 _MainColor;
            uniform sampler2D _MainTex;
            uniform float4 _MainTex_ST;
            uniform float _Cutoff = 0.5;
    
    
            float3 mod3D289( float3 x ) { return x - floor( x / 289.0 ) * 289.0; }
    
            float4 mod3D289( float4 x ) { return x - floor( x / 289.0 ) * 289.0; }
    
            float4 permute( float4 x ) { return mod3D289( ( x * 34.0 + 1.0 ) * x ); }
    
            float4 taylorInvSqrt( float4 r ) { return 1.79284291400159 - r * 0.85373472095314; }
    
            float snoise( float3 v )
            {
                const float2 C = float2( 1.0 / 6.0, 1.0 / 3.0 );
                float3 i = floor( v + dot( v, C.yyy ) );
                float3 x0 = v - i + dot( i, C.xxx );
                float3 g = step( x0.yzx, x0.xyz );
                float3 l = 1.0 - g;
                float3 i1 = min( g.xyz, l.zxy );
                float3 i2 = max( g.xyz, l.zxy );
                float3 x1 = x0 - i1 + C.xxx;
                float3 x2 = x0 - i2 + C.yyy;
                float3 x3 = x0 - 0.5;
                i = mod3D289( i);
                float4 p = permute( permute( permute( i.z + float4( 0.0, i1.z, i2.z, 1.0 ) ) + i.y + float4( 0.0, i1.y, i2.y, 1.0 ) ) + i.x + float4( 0.0, i1.x, i2.x, 1.0 ) );
                float4 j = p - 49.0 * floor( p / 49.0 );  // mod(p,7*7)
                float4 x_ = floor( j / 7.0 );
                float4 y_ = floor( j - 7.0 * x_ );  // mod(j,N)
                float4 x = ( x_ * 2.0 + 0.5 ) / 7.0 - 1.0;
                float4 y = ( y_ * 2.0 + 0.5 ) / 7.0 - 1.0;
                float4 h = 1.0 - abs( x ) - abs( y );
                float4 b0 = float4( x.xy, y.xy );
                float4 b1 = float4( x.zw, y.zw );
                float4 s0 = floor( b0 ) * 2.0 + 1.0;
                float4 s1 = floor( b1 ) * 2.0 + 1.0;
                float4 sh = -step( h, 0.0 );
                float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
                float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
                float3 g0 = float3( a0.xy, h.x );
                float3 g1 = float3( a0.zw, h.y );
                float3 g2 = float3( a1.xy, h.z );
                float3 g3 = float3( a1.zw, h.w );
                float4 norm = taylorInvSqrt( float4( dot( g0, g0 ), dot( g1, g1 ), dot( g2, g2 ), dot( g3, g3 ) ) );
                g0 *= norm.x;
                g1 *= norm.y;
                g2 *= norm.z;
                g3 *= norm.w;
                float4 m = max( 0.6 - float4( dot( x0, x0 ), dot( x1, x1 ), dot( x2, x2 ), dot( x3, x3 ) ), 0.0 );
                m = m* m;
                m = m* m;
                float4 px = float4( dot( x0, g0 ), dot( x1, g1 ), dot( x2, g2 ), dot( x3, g3 ) );
                return 42.0 * dot( m, px);
            }
    
    
            void vertexDataFunc( inout appdata_full v, out Input o )
            {
                UNITY_INITIALIZE_OUTPUT( Input, o );
                float3 ase_vertexNormal = v.normal.xyz;
                float simplePerlin3D6 = snoise( ase_vertexNormal );
                v.vertex.xyz += ( ase_vertexNormal * sin( ( simplePerlin3D6 * _SpeedTurbulence * v.color.b * _SinTime.y ) ) * v.color.g * _Turbulence );
            }
    
            inline half4 LightingUnlit( SurfaceOutput s, half3 lightDir, half atten )
            {
                return half4 ( 0, 0, 0, s.Alpha );
            }
    
            void surf( Input i , inout SurfaceOutput o )
            {
                float2 uv_MainTex = i.uv_texcoord * _MainTex_ST.xy + _MainTex_ST.zw;
                float4 tex2DNode3 = tex2D( _MainTex, uv_MainTex );
                o.Emission = ( _MainColor * tex2DNode3 ).rgb;
                o.Alpha = 1;
                clip( tex2DNode3.a - _Cutoff );
            }
    
            ENDCG
        }
        Fallback "Diffuse"
        CustomEditor "ASEMaterialInspector"
    }
    /*ASEBEGIN
    Version=16100
    2054.4;28.8;1523;789;1224.154;382.9904;1.291887;True;True
    Node;AmplifyShaderEditor.NormalVertexDataNode;11;-942.7629,-12.9262;Float;False;0;5;FLOAT3;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    Node;AmplifyShaderEditor.NoiseGeneratorNode;6;-703.6547,58.08059;Float;False;Simplex3D;1;0;FLOAT3;0,0,0;False;1;FLOAT;0
    Node;AmplifyShaderEditor.RangedFloatNode;34;-700.8823,144.6862;Float;False;Property;_SpeedTurbulence;SpeedTurbulence;3;0;Create;True;0;0;False;0;0.5;0.5;0;0;0;1;FLOAT;0
    Node;AmplifyShaderEditor.VertexColorNode;35;-690.6451,219.2309;Float;False;0;5;COLOR;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    Node;AmplifyShaderEditor.SinTimeNode;31;-657.2269,390.2851;Float;False;0;5;FLOAT4;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    Node;AmplifyShaderEditor.SimpleMultiplyOpNode;33;-443.5016,108.2539;Float;False;4;4;0;FLOAT;0;False;1;FLOAT;0;False;2;FLOAT;0;False;3;FLOAT;0;False;1;FLOAT;0
    Node;AmplifyShaderEditor.ColorNode;1;-301.5,-341;Float;False;Property;_MainColor;MainColor;1;0;Create;True;0;0;False;0;1,1,1,0;0.6037736,0.6037736,0.6037736,1;True;0;5;COLOR;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    Node;AmplifyShaderEditor.SinOpNode;36;-305.0663,94.7501;Float;False;1;0;FLOAT;0;False;1;FLOAT;0
    Node;AmplifyShaderEditor.RangedFloatNode;39;-304.8699,227.7952;Float;False;Property;_Turbulence;Turbulence;4;0;Create;True;0;0;False;0;1;1;0;0;0;1;FLOAT;0
    Node;AmplifyShaderEditor.SamplerNode;3;-389.5,-175;Float;True;Property;_MainTex;MainTex;0;0;Create;True;0;0;False;0;None;0b68d99f4e0dbfb42a5568dc11cf6bee;True;0;False;white;Auto;False;Object;-1;Auto;Texture2D;6;0;SAMPLER2D;;False;1;FLOAT2;0,0;False;2;FLOAT;0;False;3;FLOAT2;0,0;False;4;FLOAT2;0,0;False;5;FLOAT;1;False;5;COLOR;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    Node;AmplifyShaderEditor.SimpleMultiplyOpNode;5;1.5,-239;Float;False;2;2;0;COLOR;0,0,0,0;False;1;COLOR;0,0,0,0;False;1;COLOR;0
    Node;AmplifyShaderEditor.SimpleMultiplyOpNode;38;-124.3465,33.92128;Float;False;4;4;0;FLOAT3;0,0,0;False;1;FLOAT;0;False;2;FLOAT;0;False;3;FLOAT;0;False;1;FLOAT3;0
    Node;AmplifyShaderEditor.StandardSurfaceOutputNode;0;189,-231;Float;False;True;2;Float;ASEMaterialInspector;0;0;Unlit;Custom/Leaf;False;False;False;False;False;False;False;False;False;False;False;False;False;False;True;False;False;False;False;False;Off;0;False;-1;0;False;-1;False;0;False;-1;0;False;-1;False;0;Masked;0.5;True;True;0;False;TransparentCutout;;AlphaTest;ForwardOnly;True;True;True;True;True;True;True;True;True;True;True;True;True;True;True;True;True;0;False;-1;False;0;False;-1;255;False;-1;255;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;False;2;15;10;25;False;0.5;False;0;5;False;-1;10;False;-1;0;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;0;0,0,0,0;VertexOffset;True;False;Cylindrical;False;Relative;0;;2;-1;-1;-1;0;False;0;0;False;-1;-1;0;False;-1;0;0;0;15;0;FLOAT3;0,0,0;False;1;FLOAT3;0,0,0;False;2;FLOAT3;0,0,0;False;3;FLOAT;0;False;4;FLOAT;0;False;6;FLOAT3;0,0,0;False;7;FLOAT3;0,0,0;False;8;FLOAT;0;False;9;FLOAT;0;False;10;FLOAT;0;False;13;FLOAT3;0,0,0;False;11;FLOAT3;0,0,0;False;12;FLOAT3;0,0,0;False;14;FLOAT4;0,0,0,0;False;15;FLOAT3;0,0,0;False;0
    WireConnection;6;0;11;0
    WireConnection;33;0;6;0
    WireConnection;33;1;34;0
    WireConnection;33;2;35;3
    WireConnection;33;3;31;2
    WireConnection;36;0;33;0
    WireConnection;5;0;1;0
    WireConnection;5;1;3;0
    WireConnection;38;0;11;0
    WireConnection;38;1;36;0
    WireConnection;38;2;35;2
    WireConnection;38;3;39;0
    WireConnection;0;2;5;0
    WireConnection;0;10;3;4
    WireConnection;0;11;38;0
    ASEEND*/
    //CHKSM=091D50A3401A3E8F8982A6B5C26F6077EB899117

    相关文章

      网友评论

        本文标题:Unity半透明物体,无法烘焙正确的阴影

        本文链接:https://www.haomeiwen.com/subject/mdhnrqtx.html