3
2

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 3 years have passed since last update.

【小ネタ】Universal Render Pipeline/UnlitシェーダのSurfaceModeをスクリプトからTransparentに変更する方法

Last updated at Posted at 2021-06-02

動的にMaterialを作成した後で、半透明化する設定方法が、調べてもなかなか見つからなかったので、メモしておきます。

マテリアル作成csファイルの一部
material = new Material (Shader.Find ("Universal Render Pipeline/Unlit"));
material.SetFloat("_Surface", (float)BaseShaderGUI.SurfaceType.Transparent);
BaseShaderGUI.SetupMaterialBlendMode(material); //<-これが大事!

BaseShaderGUI.SetupMaterialBlendMode()を呼ばないと所定のBlandModeなどの設定が適用されないのでご注意。

ただ、BaseShaderGUIはUnityEditor用なのでEditorではない環境に向けては、BaseShaderGUI.SetupMaterialBlendMode()の中身を引っ張てくる必要が有ります。
という事で、Runtimeで行いたい場合はこちら。

material = new Material(Shader.Find("Universal Render Pipeline/Unlit"));
// Alpha Blend
material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
// General Transparent Material Settings
material.SetOverrideTag("RenderType", "Transparent");
material.SetFloat("_ZWrite", 0.0f);
material.renderQueue = (int)RenderQueue.Transparent;
material.renderQueue += material.HasProperty("_QueueOffset") ? (int)material.GetFloat("_QueueOffset") : 0;
material.SetShaderPassEnabled("ShadowCaster", false);

AlphaBlend以外のブレンディングが必要な場合は、別途切り替える必要が有りますが、まぁそこはBaseShaderGUI.SetupMaterialBlendMode()を見た方が早いということで。。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?