facebook twitter hatena line email

Unity/3d/透明

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

以下のようなShaderを追加

Shader "Custom/SemiTransparent"
{
    Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
	}
	SubShader {
		Tags { "RenderType"="Transparent" "Queue"="Transparent"}
		LOD 200

		Pass{
  		  ZWrite ON
  		  ColorMask 0
		}

		CGPROGRAM
		#pragma surface surf Standard fullforwardshadows alpha:fade
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		fixed4 _Color;

		void surf (Input IN, inout SurfaceOutputStandard o) 
		{
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Metallic = 0;
			o.Smoothness = 0;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

参考:https://nn-hokuson.hatenablog.com/entry/2018/01/23/202530

Materialを新規追加して、そのMaterialのShaderに上記"Custom/SemiTransparent"のShaderを追加

c#のサンプル

TransparentAllUnderObject(obj);
public void TransparentAllUnderObject(GameObject obj)
{
    if (obj.GetComponent<SkinnedMeshRenderer>() != null)
    {
        Material[] materials = obj.GetComponent<SkinnedMeshRenderer>().materials;
        if (materials.Length > 0)
        {
            materials[0].shader = Shader.Find("Custom/SemiTransparent");
            materials[0].SetColor("_Color", new Color(1f, 1f, 1f, 0.8f));
        }
    }
    if (obj.GetComponent<MeshRenderer>() != null)
    {
        Material[] materials = obj.GetComponent<MeshRenderer>().materials;
        if (materials.Length > 0)
        {
            materials[0].shader = Shader.Find("Custom/SemiTransparent");
            materials[0].SetColor("_Color", new Color(1f, 1f, 1f, 0.8f));
        }
    }
    Transform transforms = obj.GetComponentInChildren<Transform>();
    if (transforms.childCount > 0)
    {
        foreach (Transform transform in transforms)
        {
            TransparentAllUnderObject(transform.gameObject); // 再帰処理
        }
    }
}