프로그래밍 정리/Unity

[유니티][Unity] 셰이더 스크립트에서 변경 후 되지 않는 이유

주누다 2016. 11. 2. 05:20
반응형
Unity5부터 추가된 Standard 쉐이더에는 4가지 렌더모드가 존재합니다.

Opaque, Cutout, Fade, Transparent 입니다.

이 쉐이더 모드를 스크립트에서 전환할 수 있습니다.



mat.SetFloat("_Mode", 2);


그런데 모드를 전환해도 쉐이더 속성은 변하지 않습니다. 이유는 저도 잘 모르겠네요. 5.2.2f1 기준입니다.

그래서 각종 속성을 직접 바꿔줘야 합니다.



public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode)
{
switch (blendMode)
{
case BlendMode.Opaque:
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = -1;
break;

...
}


이런 방법을 거쳐야 합니다.

저 자세한 코드는 링크를 통해 확인하세요.



원문 링크




===========================================================================



public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode)
{
	switch (blendMode)
	{
		case BlendMode.Opaque:
			material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
			material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
			material.SetInt("_ZWrite", 1);
			material.DisableKeyword("_ALPHATEST_ON");
			material.DisableKeyword("_ALPHABLEND_ON");
			material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			material.renderQueue = -1;
			break;
		case BlendMode.Cutout:
			material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
			material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
			material.SetInt("_ZWrite", 1);
			material.EnableKeyword("_ALPHATEST_ON");
			material.DisableKeyword("_ALPHABLEND_ON");
			material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			material.renderQueue = 2450;
			break;
		case BlendMode.Fade:
			material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
			material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
			material.SetInt("_ZWrite", 0);
			material.DisableKeyword("_ALPHATEST_ON");
			material.EnableKeyword("_ALPHABLEND_ON");
			material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			material.renderQueue = 3000;
			break;
		case BlendMode.Transparent:
			material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
			material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
			material.SetInt("_ZWrite", 0);
			material.DisableKeyword("_ALPHATEST_ON");
			material.DisableKeyword("_ALPHABLEND_ON");
			material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
			material.renderQueue = 3000;
			break;
	}
}



============================================================================

ex)

skinMeshRenders[i].materials[j].shader = Shader.Find("Standard");

                skinMeshRenders[i].materials[j].SetFloat("_Mode", 2.0f);


                skinMeshRenders[i].materials[j].SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);

                skinMeshRenders[i].materials[j].SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);

                skinMeshRenders[i].materials[j].SetInt("_ZWrite", 0);

                skinMeshRenders[i].materials[j].DisableKeyword("_ALPHATEST_ON");

                skinMeshRenders[i].materials[j].EnableKeyword("_ALPHABLEND_ON");

                skinMeshRenders[i].materials[j].DisableKeyword("_ALPHAPREMULTIPLY_ON");

                skinMeshRenders[i].materials[j].renderQueue = 3000;


                Color temp_color = skinMeshRenders[i].materials[j].color;

                temp_color.a = alpha;

                skinMeshRenders[i].materials[j].color = temp_color;




반응형