1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CustomRenderTextureを使用してReflectionProbeで使用するBakeされたCubeMapを調整する

Posted at

はじめに

ReflectionProbeでBakeしたCubeMap使用するとライティングが変わっても反射結果はBakeしたときのライティングになってしまいます。(Bakeなので当たり前ではありますが..)

78626743-dcc4-4575-a1d9-0f31df25a46b.gif

ただそれだと時間の概念があるゲームだとライティングがリアルタイムに変わったりする場合があり使いづらいのでCustomRenderTextureを使用してBakeされたCubeMapをリアルタイムに調整していきたいと思います。

CustomRenderTextureの作成

CustomRenderTextureとは

Shaderを割り当てることが出来るRenderTextureになります。TextureなのでCubeMapにすることができるのでそれを活用してきます

作成

1.Create→Rendering→CustomRenderTextureでCustomRenderTextureを作成します

7044be18-2f18-4812-b43b-149e12645f86.png

2.CustomRenderTextureに割り当てるためのマテリアル作成し,下記のように設定します

d26a8231-39b5-4031-bc9c-0dd28a44953b.png

3.Shader実装
内容としてはCubeMapにDirectional Lightの色を乗算させてます


Shader "CustomRenderTexture/TestBlendShader"
{
    Properties
    {
        _Cube1 ("Cube1", CUBE) = "" {}
    }

    SubShader
    {
        Blend One Zero

        Pass
        {
            Tags
            {
                "LightMode" = "UniversalForward"
            }
            Name "ReflectionProbeBlend"

            HLSLPROGRAM
            //CustomRenderTextureのShaderを実装する際使用する
            #include "UnityCustomRenderTexture.cginc"

            #pragma vertex CustomRenderTextureVertexShader
            #pragma fragment frag
            #pragma target 3.0

            half4 _MainLightColor;
            UNITY_DECLARE_TEXCUBE(_Cube1);


            float4 frag(v2f_customrendertexture IN) : SV_Target
            {
                 float4 col;
                //MainLightの色と合成する
                float4 refColor1 = UNITY_SAMPLE_TEXCUBE(_Cube1, IN.direction);
                float4 c = clamp(_MainLightColor, 0, 1);
         
                //地面の部分だけ色を変える
                if (IN.direction.y <= 0.03f)
                {
                    col = refColor1 * c;
                }
                else
                {
                    col = refColor1;
                }
                return col;
            }
            ENDHLSL
        }
    }
}

CustomRenderTextureのShaderを実装する際はVertexShaderでCustomRenderTextureVertexShaderを指定してあげる必要があります

参考

4.マテリアルの設定

Cube1にBakeしたCubeMapを設定します

d5a30ecf-478a-4639-91d8-14d4c3a881ef.png

5.ReflectionProbeの設定

ff894d54-2ed5-4d0b-8564-21f0e09f8212.png

ReflectionProbeにCustomRenderTextureを割り当てます

完成

BakeされたCubeMapがライティングの影響を受けるようになりました

65e536cb-d8b2-4ec1-8c55-0f5f8fb6ebfe.gif

終わりに

CustomRenderTextureを使用することでBakeされたCubeMapの見た目を調整することが出来ました。

今回作成したShaderだと乗算しただけですが実現したい表現に応じて変えてけばBakeされたCubeMapでもリアルタイムな動的な変化のある反射表現が出来ると思います

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?