赞
踩
前言:看到此功能首先想到的是写shader,但是我对shader一知半解。网上找了几个大佬分享的做渐隐的shader也只是能实现渐隐而已,无法实现unity模型材质Standard的各种效果,我又不会修改Standard shader,所以只能退而求其次。
如有大佬明白如何用shader实现,烦请告知,感激不尽
下面说下我的实现方式:
由于模型材质Rendering Mode大都是Opaque,无法实现渐隐。
关于Rendering Mode,这里不做解释,不懂得转这里渲染模式 (Rendering Mode) - Unity 手册
实现思路:因为模型材质Rendering Mode模式不一样,所以要在做渐隐之前先将模型参数存起来,以便模型渐隐后可以复原。
模型参数存储很简单,我是用的字典。但是获取材质Rendering Mode以及修改材质Rendering Mode就比较冷门了。下面挂上代码实现方式
获取材质Rendering Mode:
- // 获取材质的Rendering Mode
- public RenderingFadeMode GetMaterialRenderingFadeMode(Material material)
- {
- RenderingFadeMode rm = RenderingFadeMode.Opaque;
-
- int one = material.GetInt("_SrcBlend");
- int two = material.GetInt("_DstBlend");
- int three = material.GetInt("_ZWrite");
- bool oneKeyword = material.IsKeywordEnabled("_ALPHATEST_ON");
- bool twoKeyword = material.IsKeywordEnabled("_ALPHABLEND_ON");
- bool threeKeyword = material.IsKeywordEnabled("_ALPHAPREMULTIPLY_ON");
-
- if (one == 1 & two == 0 && three == 1 && oneKeyword == false && twoKeyword == false && threeKeyword == false)
- rm = RenderingFadeMode.Opaque;
- else if (one == 1 & two == 0 && three == 1 && oneKeyword == true && twoKeyword == false && threeKeyword == false)
- rm = RenderingFadeMode.Cutout;
- else if (one == 5 & two == 10 && three == 0 && oneKeyword == false && twoKeyword == true && threeKeyword == false)
- rm = RenderingFadeMode.Fade;
- else if (one == 1 & two == 10 && three == 0 && oneKeyword == false && twoKeyword == false && threeKeyword == true)
- rm = RenderingFadeMode.Transparent;
-
- return rm;
- }
修改材质Rendering Mode:
- // 设置材质的Rendering Mode
- public void SetMaterialRenderingFadeMode(Material material, RenderingFadeMode RenderingFadeMode)
- {
- switch (RenderingFadeMode)
- {
- case RenderingFadeMode.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 RenderingFadeMode.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 RenderingFadeMode.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 RenderingFadeMode.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;
- }
- }
以上实现我也是第一次做,网上查了很久
各位同仁如有更好实现方式,烦请告知
项目工程在此,需要自取
链接:https://pan.baidu.com/s/1JYDG6DdS38rf6P4gGK28Bg
提取码:84z7
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。