LoginSignup
2
4

More than 5 years have passed since last update.

Unity NGUIでUISprite等に色の加算合成を行う

Posted at

動作環境

Unity5.5.0f3
NGUI 3.11.1

手順

手順は以下の通りになります。

  1. NGUIのシェーダーをカスタマイズする
  2. UISprite、UITextureのマテリアルを上記シェーダーを利用したマテリアルに差し替える
  3. C#側からマテリアルのパラメータをコントロール

3番目の方法は以前書いた記事に書いてあります。

NGUIのシェーダーをカスタマイズする

NGUIの「Unlit/Transparent Colored」シェーダーに加算合成の機能を追加する形で作ったのが以下のシェーダーです。コメントした箇所が元のシェーダーに対して追加した部分です。


Shader "Unlit/Transparent Colored Add"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
        //加算合成する色
        _Color ("Tint", Color) = (1,1,1,1)
        //加算合成する割合
        _AddPercentage ("Add %", Range (0.0,1.0)) = 1.0
    }

    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
            "DisableBatching" = "True"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            Offset -1, -1
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag           
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            float _AddPercentage;

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                half2 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
            };

            v2f o;

            v2f vert (appdata_t v)
            {
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = v.texcoord;
                //加算合成
                o.color.rgb = v.color.rgb + _Color.rgb * _AddPercentage;
                o.color.a = v.color.a * _Color.a;
                return o;
            }

            fixed4 frag (v2f IN) : SV_Target
            {
                return tex2D(_MainTex, IN.texcoord) * IN.color;
            }
            ENDCG
        }
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
            "DisableBatching" = "True"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            Offset -1, -1
            //ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMaterial AmbientAndDiffuse

            SetTexture [_MainTex]
            {
                Combine Texture * Primary
            }
        }
    }
}

C#側からマテリアルのパラメータをコントロール

下記がC#側からマテリアルの加算のパラメータをコントロールするスクリプトです。該当するUISprite等にアタッチします。

UIControllAdd.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIControllAdd : MonoBehaviour {
/// <summary>
    /// 加算する割合
    /// </summary>
    /// <value>The add parcentage.</value>
    public float addParcentage {
        get {
            return m_addParcentage;
        }

        set {
            m_addParcentage = value;

        }
    }

    /// <summary>
    /// 加算の割合
    /// </summary>
    [SerializeField, Range(0f,1f)] float    m_addParcentage;

    void Awake()
    {
        var widget = GetComponent<UIWidget>();
        widget.onRender = SetControllAddCB;
    }

    void SetControllAddCB(Material mat)
    {
        mat.SetFloat("_AddPercentage",m_addParcentage);
    }
}

以上。

2
4
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
2
4