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

More than 1 year has passed since last update.

【Unity】Opaqueなマテリアルをランタイム上でTransparentに変更する

Posted at

はじめに

ランタイム上で不透明なマテリアルを半透明に切り替える方法についてメモを残します。

スクリプト

// ベースシェーダのサーフェスタイプを透明に設定
material.SetFloat("_Surface", 1);

// デフォルトのレンダータイプタグを 'Transparent' に上書き
material.SetOverrideTag("RenderType", "Transparent");

// マテリアルのレンダーキューを透明に設定
material.renderQueue = (int)RenderQueue.Transparent;

// ソースブレンドを SrcAlpha に設定
material.SetInt("_SrcBlend", (int)BlendMode.SrcAlpha);

// ディスティネーションブレンドを OneMinusSrcAlpha に設定
material.SetInt("_DstBlend", (int)BlendMode.OneMinusSrcAlpha);

// Zライトを 0 に設定
material.SetInt("_ZWrite", 0);

// アルファテスト無効
material.DisableKeyword("_ALPHATEST_ON");

// アルファブレンド有効
material.EnableKeyword("_ALPHABLEND_ON");

// アルファプレマルチプライ無効
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");

// マテリアルの色を取得
var color = material.color;

// 色のアルファ値設定
color.a = アルファ値(01f);

// マテリアル色更新
material.color = color;
4
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
4
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?