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);
            
        }
    }
相关推荐
Clank的游戏栈4 小时前
Unity多线程渲染指令队列设计与集成技术详解
windows·unity·游戏引擎
胜天半子_王二_王半仙12 小时前
godot源码编译
游戏引擎·godot
Thomas_YXQ12 小时前
Unity3D IK解算器技术分析
开发语言·搜索引擎·unity·全文检索·unity3d·lucene
Tandy12356_13 小时前
Godot开发2D冒险游戏——第二节:主角光环整起来!
游戏引擎·godot
星火撩猿20 小时前
常见游戏引擎介绍与对比
unity·ue5·游戏引擎·godot
sky_smile_Allen21 小时前
[Unity]-[UI]-[Prefab] 关于Unity UGUI 的布局及组件讲解
ui·unity·游戏引擎
虾球xz1 天前
游戏引擎学习第244天: 完成异步纹理下载
c++·学习·游戏引擎
太妃糖耶1 天前
URP-利用矩阵在Shader中实现物体的平移和缩放
unity·矩阵
Magnum Lehar1 天前
ApophisZerg游戏引擎项目目录展示
人工智能·vscode·编辑器·游戏引擎
Tandy12356_1 天前
Godot开发2D冒险游戏——第一节:主角登场!
python·游戏引擎·godot