LoginSignup
9
7

More than 5 years have passed since last update.

【Unity】画像のクロスフェードをシェーダで行う

Last updated at Posted at 2017-03-25

Unity C# で静止画をクロスフェードします。

・環境
Unity 5.5.0f3

シェーダ

CrossFade.shader
Shader "Custom/CrossFade" {

    Properties{
        _Color("Main Color", Color) = (1,1,1,1)
        _Blend("Blend", Range(0, 1)) = 0
        _Texture1("Texture 1", 2D) = ""
        _Texture2("Texture 2", 2D) = ""

    }

    SubShader{
        Pass{
            SetTexture[_Texture1]
            SetTexture[_Texture2]{
                ConstantColor(1,1,1,[_Blend])
                Combine texture Lerp(constant) previous
            }
        }
    }

}

サンプルプログラム

CrossFadeSample.cs
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class CrossFadeSample : MonoBehaviour
{
    // クロスフェードに使用するImageオブジェクト
    [SerializeField]
    private Image ImageUI;

    // テクスチャ1枚目
    [SerializeField]
    private Texture Texture1;

    // テクスチャ2枚目
    [SerializeField]
    private Texture Texture2;

    // 変化の速度 (秒)
    [SerializeField]
    private float Speed = 2.0f;


    void Start()
    {
        // Imageのマテリアルを取得
        Material material = ImageUI.GetComponent<Image>().material;

        // ブレンド率を0.0に設定
        material.SetFloat("_Blend", 0.0f);

        // テクスチャを設定する
        material.SetTexture("_Texture1", Texture1);
        material.SetTexture("_Texture2", Texture2);

        // ブレンド率を操作する (DoTweenを使用しています)
        material.DOFloat(1.0f, "_Blend", Speed).SetLoops(-1, LoopType.Yoyo);
    }
}

サンプルプロジェクトファイルはこちら
https://github.com/Necotarou/CrossFadeSample

9
7
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
9
7