/// _unity make gameobject transparent">
当前位置:   article > 正文

Unity模型透明功能(HDRP)_unity make gameobject transparent

unity make gameobject transparent

先整理一下原来透明怎么设置的

1.Build-in

获取透明的Shader,修改模型材质的Shader,修改颜色参数透明度,修改材质是否为空

  1. /// <summary>
  2. /// 将游戏物体设置成透明的(包括子物体也都设置成透明的)
  3. /// </summary>
  4. /// <param name="go"></param>
  5. /// <param name="transparent"></param>
  6. /// <param name="color"></param>
  7. /// <param name="isSpecular"></param>
  8. /// <param name="removeTexture"></param>
  9. /// <returns></returns>
  10. public static bool SetTransparent(this GameObject go, Color color, bool isSpecular, bool removeTexture = false)
  11. {
  12. Renderer render = go.GetComponent<Renderer>();
  13. if (render == null) return false;
  14. Shader shader = ShaderHelper.GetTransparentShader(isSpecular);//ShaderHelper是以前Buldin时的工具,现在没什么用,里面其实就是Shader.Find()了
  15. for (int i = 0; i < render.materials.Length; i++)
  16. {
  17. SetTransparentMaterial(render, i, shader, color, removeTexture);
  18. //StardardShaderSet.SetMaterialRenderingMode(mm, RenderingMode.Transparent);
  19. }
  20. return true;
  21. }
  22. /// <summary>
  23. /// 修改材质,达到透明的效果
  24. /// </summary>
  25. /// <param name="render"></param>
  26. /// <param name="i"></param>
  27. /// <param name="shader"></param>
  28. /// <param name="color"></param>
  29. /// <param name="removeTexture"></param>
  30. private static void SetTransparentMaterial(Renderer render, int i, Shader shader, Color color, bool removeTexture)
  31. {
  32. //Material mm = new Material(render.materials[i]);
  33. //Material mm = render.materials[i];
  34. //render.materials[i] = mm;
  35. render.materials[i].shader = shader;
  36. render.materials[i].color = color;
  37. if (removeTexture)
  38. {
  39. render.materials[i].mainTexture = null;
  40. }
  41. }

2.URP

修改URP/Lit材质的_SurfaceType,_Mode等参数。

参考:https://answers.unity.com/questions/1608815/change-surface-type-with-lwrp.html?_ga=2.257083661.724107984.1607480798-60239732.1590568025

  1. using UnityEngine;
  2. using System;
  3. public class DisplayController : MonoBehaviour
  4. {
  5. public enum SurfaceType
  6. {
  7. Opaque,
  8. Transparent
  9. }
  10. public enum BlendMode
  11. {
  12. Alpha,
  13. Premultiply,
  14. Additive,
  15. Multiply
  16. }
  17. public Material wallMaterial;
  18. public void ChangeWallTransparency(bool transparent)
  19. {
  20. if (transparent)
  21. {
  22. wallMaterial.SetFloat("_Surface", (float)SurfaceType.Transparent);
  23. wallMaterial.SetFloat("_Blend", (float)BlendMode.Alpha);
  24. }
  25. else
  26. {
  27. wallMaterial.SetFloat("_Surface", (float)SurfaceType.Opaque);
  28. }
  29. SetupMaterialBlendMode(wallMaterial);
  30. }
  31. void SetupMaterialBlendMode(Material material)
  32. {
  33. if (material == null)
  34. throw new ArgumentNullException("material");
  35. bool alphaClip = material.GetFloat("_AlphaClip") == 1;
  36. if (alphaClip)
  37. material.EnableKeyword("_ALPHATEST_ON");
  38. else
  39. material.DisableKeyword("_ALPHATEST_ON");
  40. SurfaceType surfaceType = (SurfaceType)material.GetFloat("_Surface");
  41. if (surfaceType == 0)
  42. {
  43. material.SetOverrideTag("RenderType", "");
  44. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  45. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  46. material.SetInt("_ZWrite", 1);
  47. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  48. material.renderQueue = -1;
  49. material.SetShaderPassEnabled("ShadowCaster", true);
  50. }
  51. else
  52. {
  53. BlendMode blendMode = (BlendMode)material.GetFloat("_Blend");
  54. switch (blendMode)
  55. {
  56. case BlendMode.Alpha:
  57. material.SetOverrideTag("RenderType", "Transparent");
  58. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  59. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  60. material.SetInt("_ZWrite", 0);
  61. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  62. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  63. material.SetShaderPassEnabled("ShadowCaster", false);
  64. break;
  65. case BlendMode.Premultiply:
  66. material.SetOverrideTag("RenderType", "Transparent");
  67. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  68. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  69. material.SetInt("_ZWrite", 0);
  70. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  71. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  72. material.SetShaderPassEnabled("ShadowCaster", false);
  73. break;
  74. case BlendMode.Additive:
  75. material.SetOverrideTag("RenderType", "Transparent");
  76. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  77. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
  78. material.SetInt("_ZWrite", 0);
  79. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  80. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  81. material.SetShaderPassEnabled("ShadowCaster", false);
  82. break;
  83. case BlendMode.Multiply:
  84. material.SetOverrideTag("RenderType", "Transparent");
  85. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.DstColor);
  86. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  87. material.SetInt("_ZWrite", 0);
  88. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  89. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  90. material.SetShaderPassEnabled("ShadowCaster", false);
  91. break;
  92. }
  93. }
  94. }
  95. }

3.HDRP

参考:https://forum.unity.com/threads/hdrp-make-surface-transparent-in-runtime.663025/#post-4439554

It seems like the proper thing to do is to have 2 materials, one opaque and one transparent, and switch materials on the mesh as needed.

根据查到的资料,运行过程中修改_SurfaceType,没有效果,建议给模型设置一个透明的材质,切换材质来达到透明的效果。

测试发现也可以使用将透明材质的属性全部Copy过来,一样能够透明,需要将原材质的信息备份(颜色、材质),Copy透明材质,还原原信息。

  1. var color=material.GetColor("_BaseColor");
  2. var texture=material.GetTexture("_BaseColorMap");
  3. if(Transparent_Mat==null){
  4. Transparent_Mat=Resources.Load<Material>("HDRPLit_Transparent_Double");
  5. }
  6. if(EnableTransparent==true){
  7. Material m=GameObject.Instantiate(Transparent_Mat);
  8. var materialBack=new Material(material);//备份材质,后续还原用
  9. backMaterials.Add(material,materialBack);//backMaterials是个Dictionary<Material,Material>,存一下材质的备份。
  10. //还原的时候 material.CopyPropertiesFromMaterial(materialBack)
  11. material.CopyPropertiesFromMaterial(m);
  12. material.SetColor("_BaseColor", color);
  13. material.SetTexture("_BaseColorMap", texture);
  14. //透明也要有金属、光滑效果的话,其他属性也要设置一下。
  15. }

由于历史原因,项目中多处存在透明代码,适用于不同的渲染关系,有空找个时间统一一下,写一个统一处理三种情况的代码。

4.公司相关代码说明

TransparentHelper:最早的Build-in的透明功能

StartdarMaterialController:URP和HDRP的透明功能,调用StardarShaderMatHelper.SetMaterialRenderingMode

ModelChange:工艺流程的透明功能,调用StardarShaderMatHelper.SetMaterialRenderingMode

 

 

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

闽ICP备14008679号