【Unity】关于DrawMeshInstanced does not support the shader 的问题

        在做某个功能的时候,需要使用 Graphics.DrawMeshInstanced 这个API,但是跑起来却报错了:

        DrawMeshInstanced does not support the shader '{ShaderName}' because it does not read any instanced properties. Try switching to DrawMeshInstancedProcedural if the shader is doing procedural instancing.

        看起来似乎是这个Shader不支持此API,但是没道理啊,DrawMeshInstanced 应该是适用性最好的接口,对Shader不需要什么特殊处理才对。

        然后我去翻了下Unity 的论坛:          Bug - Ver. 2021.3.9 - Newly introduced error for DrawMeshInstanced calls in codes - Unity ForumA lot of our projects and products rely on our custom GPU renderer based on Graphics.DrawMeshInstanced calls to render models efficiently. Now suddenly...https://forum.unity.com/threads/ver-2021-3-9-newly-introduced-error-for-drawmeshinstanced-calls-in-codes.1331346/      
        看起来怕是Unity自己搞出来的Bug哦……        

         从官方的回复看,这个Bug可以不用管(如果没有再打印出来的话)。事实上我测试的时候也是如此,虽然有报错,但是功能是正常的。而且只有第一次运行的时候会报错,之后就没啥问题了。

        既然能正常显示,所以我也就没有管这个报错了。

        PS1:虽说Unity说的在 2021.3.9 修复了这个Bug,但实际上似乎并没有。

        PS2:似乎是只有用类似 Shader Graph 这种连连看搞出来的 Shader 才会有这个问题。


另外一种情况:

        在编辑器下没有问题,但是在真机上报错。

        可能是因为Unity裁剪了GPU Instance的变体,在打包的时候 剔除了对应Shader导致的。这种需要进行如下设置:Edit > Project Settings > Graphics > Instancing Variants :设置为 Keep All:

         再打包就可以了。

猜你喜欢

转载自blog.csdn.net/cyf649669121/article/details/127528693