Unity urp渲染管线下,动态修改材质球surfaceType

在项目中遇到了需要代码动态修改材质球的surfaceType,使其动态切换是否透明的需求。

urp渲染管线下,动态修改材质球的surfaceType,查了大部分帖子,都有一些瑕疵,可能会造成透明后阴影投射有问题。

其次在webgl平台上,可能还会造成透明物体仍会发生镜面反射的问题,进而出现模型曝光,与编辑器中展示效果差别过大

下面的方法解决了这类问题

cs 复制代码
 private void setTransparent(Material mat, bool isTransparent)
    {
        if (isTransparent)
        {
            //修改surfacetype为Transparent
            mat.SetFloat("_Surface", 1.0f);

            mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            mat.SetInt("_DstBlend",(int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            mat.SetInt("_ZWrite", 0);

            mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
            mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
            mat.SetShaderPassEnabled("ShadowCaster", false);
        }
        else
        {
            mat.SetFloat("_Surface", 0.0f);
            mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
            mat.SetInt("_ZWrite", 1);

            mat.renderQueue = -1;
            mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
            mat.SetShaderPassEnabled("ShadowCaster", true);
            
        }
    }
相关推荐
xcLeigh13 小时前
Unity基础:C#脚本的创建与挂载——让你的游戏物体活起来
游戏·unity·c#·教程
tanglinS16 小时前
双端面磨床汇总:面向工艺工程师的设备选型参考
大数据·运维·人工智能·自动化·材质
DaLiangChen1 天前
Vuforia 地面识别案例
unity·ar
江畔柳前堤2 天前
YOLO 目标检测全流程深度剖析
人工智能·yolo·目标检测·计算机视觉·unity·面试·vllm
魔术师Dix2 天前
一个轮子:Luban Skill 制作演示
游戏·unity·ai编程
xcLeigh2 天前
Unity基础:Light灯光组件入门——Directional Light、Point Light与Spotlight
unity·游戏引擎·教程
郝学胜-神的一滴3 天前
[简化版 GAMES 104] 现代游戏引擎 04:从0到1构建你的游戏世界
c++·游戏·unity·游戏引擎·软件工程·unreal engine
2401_859506243 天前
需求分析、节点确认、过程透明——我用做项目的思路选了一家玉雕工作室
材质
是嘟嘟啊3 天前
【Unity DOTS】EntityCommandBuffer 与 JobHandle
unity·ecs·dots·unity dots·job·burst·burstjob