facebook twitter hatena line email

「Unity/3d/透明」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行2: 行2:
  
 
以下のようなShaderを追加
 
以下のようなShaderを追加
 +
<pre>
 +
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;
 +
};
  
https://nn-hokuson.hatenablog.com/entry/2018/01/23/202530
+
fixed4 _Color;
  
Materialを新規追加して、そのMaterialのShaderに上記Shaderを追加
+
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"
 +
}
 +
</pre>
 +
参考:https://nn-hokuson.hatenablog.com/entry/2018/01/23/202530
  
 +
Materialを新規追加して、そのMaterialのShaderに上記"Custom/SemiTransparent"のShaderを追加
  
 +
==c#のサンプル==
 
<pre>
 
<pre>
GameObject body = zombieObj.transform.Find("Z_Body").gameObject;
+
AllUnderObject(obj);
Material[] materials = body.GetComponent<SkinnedMeshRenderer>().materials;
+
public void AllUnderObject(GameObject obj)
materials[0].shader = Shader.Find("Custom/SemiTransparent");
+
{
materials[0].SetColor("_Color", new Color(1f, 1f, 1f, 0.5f));
+
    Debug.Log("name=" + obj.name);
 +
    if (obj.GetComponent<SkinnedMeshRenderer>() != null)
 +
    {
 +
        Material[] materials = obj.GetComponent<SkinnedMeshRenderer>().materials;
 +
        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;
 +
        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)
 +
        {
 +
            AllUnderObject(transform.gameObject); // 再帰処理
 +
        }
 +
    }
 +
}
 
</pre>
 
</pre>

2021年11月24日 (水) 12:29時点における版

(注意:手動では透明にできたが、コードで変更ができない・・)

以下のような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#のサンプル

AllUnderObject(obj);
public void AllUnderObject(GameObject obj)
{
    Debug.Log("name=" + obj.name);
    if (obj.GetComponent<SkinnedMeshRenderer>() != null)
    {
        Material[] materials = obj.GetComponent<SkinnedMeshRenderer>().materials;
        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;
        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)
        {
            AllUnderObject(transform.gameObject); // 再帰処理
        }
    }
}