当前位置:   article > 正文

unity 实现3d模型渐隐(修改材质透明度)_unity房屋渐隐

unity房屋渐隐

前言:看到此功能首先想到的是写shader,但是我对shader一知半解。网上找了几个大佬分享的做渐隐的shader也只是能实现渐隐而已,无法实现unity模型材质Standard的各种效果,我又不会修改Standard shader,所以只能退而求其次。

如有大佬明白如何用shader实现,烦请告知,感激不尽

下面说下我的实现方式:

由于模型材质Rendering Mode大都是Opaque,无法实现渐隐。

关于Rendering Mode,这里不做解释,不懂得转这里渲染模式 (Rendering Mode) - Unity 手册

实现思路:因为模型材质Rendering Mode模式不一样,所以要在做渐隐之前先将模型参数存起来,以便模型渐隐后可以复原。

模型参数存储很简单,我是用的字典。但是获取材质Rendering Mode以及修改材质Rendering Mode就比较冷门了。下面挂上代码实现方式

获取材质Rendering Mode:

  1. // 获取材质的Rendering Mode
  2. public RenderingFadeMode GetMaterialRenderingFadeMode(Material material)
  3. {
  4. RenderingFadeMode rm = RenderingFadeMode.Opaque;
  5. int one = material.GetInt("_SrcBlend");
  6. int two = material.GetInt("_DstBlend");
  7. int three = material.GetInt("_ZWrite");
  8. bool oneKeyword = material.IsKeywordEnabled("_ALPHATEST_ON");
  9. bool twoKeyword = material.IsKeywordEnabled("_ALPHABLEND_ON");
  10. bool threeKeyword = material.IsKeywordEnabled("_ALPHAPREMULTIPLY_ON");
  11. if (one == 1 & two == 0 && three == 1 && oneKeyword == false && twoKeyword == false && threeKeyword == false)
  12. rm = RenderingFadeMode.Opaque;
  13. else if (one == 1 & two == 0 && three == 1 && oneKeyword == true && twoKeyword == false && threeKeyword == false)
  14. rm = RenderingFadeMode.Cutout;
  15. else if (one == 5 & two == 10 && three == 0 && oneKeyword == false && twoKeyword == true && threeKeyword == false)
  16. rm = RenderingFadeMode.Fade;
  17. else if (one == 1 & two == 10 && three == 0 && oneKeyword == false && twoKeyword == false && threeKeyword == true)
  18. rm = RenderingFadeMode.Transparent;
  19. return rm;
  20. }

修改材质Rendering Mode:

  1. // 设置材质的Rendering Mode
  2. public void SetMaterialRenderingFadeMode(Material material, RenderingFadeMode RenderingFadeMode)
  3. {
  4. switch (RenderingFadeMode)
  5. {
  6. case RenderingFadeMode.Opaque:
  7. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  8. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  9. material.SetInt("_ZWrite", 1);
  10. material.DisableKeyword("_ALPHATEST_ON");
  11. material.DisableKeyword("_ALPHABLEND_ON");
  12. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  13. material.renderQueue = -1;
  14. break;
  15. case RenderingFadeMode.Cutout:
  16. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  17. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  18. material.SetInt("_ZWrite", 1);
  19. material.EnableKeyword("_ALPHATEST_ON");
  20. material.DisableKeyword("_ALPHABLEND_ON");
  21. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  22. material.renderQueue = 2450;
  23. break;
  24. case RenderingFadeMode.Fade:
  25. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  26. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  27. material.SetInt("_ZWrite", 0);
  28. material.DisableKeyword("_ALPHATEST_ON");
  29. material.EnableKeyword("_ALPHABLEND_ON");
  30. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  31. material.renderQueue = 3000;
  32. break;
  33. case RenderingFadeMode.Transparent:
  34. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  35. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  36. material.SetInt("_ZWrite", 0);
  37. material.DisableKeyword("_ALPHATEST_ON");
  38. material.DisableKeyword("_ALPHABLEND_ON");
  39. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  40. material.renderQueue = 3000;
  41. break;
  42. }
  43. }

以上实现我也是第一次做,网上查了很久

各位同仁如有更好实现方式,烦请告知

项目工程在此,需要自取

链接:https://pan.baidu.com/s/1JYDG6DdS38rf6P4gGK28Bg 
提取码:84z7

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/116332?site
推荐阅读
相关标签
  

闽ICP备14008679号