最近在做项目中遇到一个需求,需要将物体模型进行透明虚化效果。看了网上一些博客文章。已实现该需求,为此博主写篇文章记录一下该功能的实现操作。希望能帮助到大家,不足之处还望海涵。一起学习交流。
透明效果:
使用一个方块来进行操作。
实现的脚本
该脚本挂在物体父级上,因为获取的是该对象所有子物体的MeshRenderer。(改脚本主要是获取需要透明的所有MeshRenderer然后进行更改操作)
主要操作在于这YPTools.SetMaterialRenderingMode(mrs[j],YPTools.RenderingMode.Transparent); mrs[j].color = new Color(mrs[j].color.r, mrs[j].color.b, mrs[j].color.g, 0.1f);
public class EmptyControl : MonoBehaviour{private void Awake(){for (int i = 0; i < this.transform.childCount; i++){Transform wallTransform = this.transform.GetChild(i);MeshRenderer mr = wallTransform.GetComponent<MeshRenderer>();if (mr !=null){//MeshRenderer[] mr = wallTransform.gameObject.GetComponentsInChildren<MeshRenderer>(); Material[] mrs = mr.materials;for (int j = 0; j < mrs.Length; j++){Color mcolor = mrs[j].color;if (mcolor != null){//mcolor.a = 0.4f;mrs[j].color = new Color(65/255f,65/255f,65/255f,85/255f);//设置材质球颜色为灰色,方便透明时不那么明亮// 设置材质的颜色为半透明的颜色才能看到效果,如果材质有贴图,那么贴图也必须是半透明的YPTools.SetMaterialRenderingMode(mrs[j], YPTools.RenderingMode.Transparent);mrs[j].color = new Color(mrs[j].color.r, mrs[j].color.b, mrs[j].color.g, 0.1f);//wallTransform.gameObject.GetComponent<Renderer>().material.color = mcolor;}}} }}}
YPTools工具类
public class YPTools : MonoBehaviour{/// <summary>/// 设置Rendering Mode/// </summary>/// <param name="material"></param>/// <param name="renderingMode"></param>public static void SetMaterialRenderingMode(Material material, RenderingMode renderingMode){switch (renderingMode){case RenderingMode.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 = 2000;break;case RenderingMode.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 RenderingMode.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 RenderingMode.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;}}}public enum RenderingMode{Opaque,Cutout,Fade,Transparent,}
RenderingMode枚举对应材质这四种类型(更改类型为Transparent设置参数为0.1f就可以实现透明效果)
不透明效果:
YPTools.SetMaterialRenderingMode(mrs[j],YPTools.RenderingMode.Transparent);mrs[j].color = new Color(mrs[j].color.r, mrs[j].color.b, mrs[j].color.g, 0.1f);
将上面Transparent更改为Opaque然后在设置Material 材质颜色的a值为1
YPTools.SetMaterialRenderingMode(mrs[j],YPTools.RenderingMode.Opaque);mrs[j].color = new Color(mrs[j].color.r, mrs[j].color.b, mrs[j].color.g, 1f);
以上就是Unity对3D物体透明和不透明实现操作学习总结,也希望能帮助到大家。^ _ ^
如果觉得《Unity 3D物体模型的透明和不透明实现》对你有帮助,请点赞、收藏,并留下你的观点哦!