Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

necotarou
Unityをコネコネしてますよ。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away