プログラムの素

スマートフォンアプリ開発に携わっているペーペープログラマのブログです

Unity5で半透明オブジェクトに影を投影する方法

Unity5で半透明オブジェクトに影を投影する方法をメモ。

Standard ShaderにおいてRendering ModeFadeTransparentに設定すると、オブジェクトの影が描画されなくなってしまいます。
OpaqueCutoutなら描画されるみたいです)

  • Opaque

f:id:famme_fatale:20150318121647p:plain

  • Transparent

f:id:famme_fatale:20150318121702p:plain

Unityのドキュメントに以下のような記述があります。

次に,不透明のオブジェクトのみがシャドウを投影します/されます。すなわち,ビルトインの Transparent またはパーティクル シェーダを用いた場合シャドウは出来ません。

なので、半透明オブジェクトに影を投影させるためにはちょっと工夫が必要でした。

Rendering ModeCutoutなら影が描画されるみたいなので、Cutoutでシェーダーを記述します。

新規でShaderを作成するとStandard Shaderベースでシェーダーが作成されます。
それをちょっと改良して以下のように記述します。

Shader "Custom/Test" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
        _Cutout ("Cutout", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "Queue"="AlphaTest" "IgnoreProjector"="true" "RenderType"="TransparentCutout" }
        Cull Off
        Blend SrcAlpha OneMinusSrcAlpha
        Offset -1, -1
        LOD 200
        
        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows alphatest:_Cutout

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

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

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

まずはプロパティに変数を追加します。

_Cutout ("Cutout", Range(0,1)) = 0.0

Cutoutのシェーダーにはアルファ値の閾値が必要です。
アルファ値がこの閾値未満の場合にはピクセルが描画されないようになります。

続いてTags等の部分は以下のようにします。

Tags { "Queue"="AlphaTest" "IgnoreProjector"="true" "RenderType"="TransparentCutout" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Offset -1, -1

最後にオプションとして以下を記述します。

#pragma surface surf Standard fullforwardshadows alphatest:_Cutout

alphatest:変数名と記載することでCutoutシェーダーとして機能することになります。
詳細はこちらに記載があります。

このシェーダーを用いて地面にマテリアルを適用した結果が以下になります。

f:id:famme_fatale:20150318123752p:plain

f:id:famme_fatale:20150318123800p:plain

正しい方法かはわかりませんが、無事半透明オブジェクトにも影を投影することができました。

そもそも半透明オブジェクトに対して影が投影されるのがおかしいのかな。。。