2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Unity]ShaderGraphでBooleanを使ってノードを切り替える

Last updated at Posted at 2024-05-26

概要

ShaderGraphでBooleanを使ったノード切り替えについてまとめます。コードで切り替えるとき少し癖がありました

本文

ShaderGraph内でBooleanプロパティをつないだBranchで赤色と青色を切り替えます。
スクリーンショット 2024-05-27 000409.png

Cube対してChangeColorスクリプトを張り付けます

スクリーンショット 2024-05-27 003551.png


using UnityEngine;

public class ChangeColor : MonoBehaviour
{
    [SerializeField] private bool isRed;
    
    void Start()
    {
        GetComponent<Renderer>().material.SetInt("_ColorChange", isRed ? 1 : 0);
    }
}

InspectorからisRedフラグをON/OFFすることでシェーダーの機能を切り替えることができます。

マテリアルにはSetBooleanみたいなメソッドは無いようで、SetIntを使って1もしくは0を指定することでtrue/falseを切り替えられます。

Booleanでの切り替えは、内部的にはtrue側とfalse側の両方が計算されているらしいので処理負荷が気になる方は検討が必要そうです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?