• 中文
  • 材质在运行时会一直替换造成闪烁现象?

一会材质名为Material,一会是材质被替换为的Material(Instance)

Related Discussions
...

恐怕我们需要有关您的问题的更多信息。
您使用的是哪个运行时? 我假设脊柱统一。
请更详细地描述导致您的问题的代码和场景设置是什么样的。


I'm afraid we will need more information about your problem.
Which runtime are you using? I assume spine-unity.
Please describe in more detail what your code and scene setup looks like that lead to your problem.

在Unity中 使用的自定义shader,只有Normal的插槽,在运行时角色的材质
一会是设置的默认材质,名字为:characterMaterial,
一会为默认材质的复制?名字为:characterMaterial(Instance)
造成了闪烁现象


相同的材质Material和Material(Instance)交替地被挂载在游戏对象上,造成了闪烁现象

通常这不会发生。 您是否通过脚本以任何方式访问或修改材料? 您能否分享所有访问 MeshRenderer 材质、设置覆盖材质或 MaterialPropertyBlocks 的相关代码? 除了 SkeletonAnimation 之外,您是否使用任何特殊组件?


Normaly this does not happen. Are you accessing or modifying the material in any way via scripting? Can you please share all relevant code that is accessing the material of the MeshRenderer, setting override materials or MaterialPropertyBlocks? Do you use any special components other than SkeletonAnimation?

I don't use another script to change the material. I tentatively solved this problem by commenting line 475 of SkeletonRenderer. cs . Is there a better solution? Here's the part of code in SkeletonRenderer. cs。

if (updateTriangles) { // Check if the triangles should also be updated.
            meshGenerator.FillTriangles(currentMesh);
            //line 475:
            meshRenderer.sharedMaterials = rendererBuffers.GetUpdatedSharedMaterialsArray();
}

我没有使用其他脚本修改材料,我通过注释掉SkeletonRenderer.cs的第475行代码暂时解决了这个问题,请问有没有更好的解决方案?这是SkeletonRenderer.cs中的那部分代码

请不要注释掉你提到的那一行,否则当有两个或多个图集页面纹理时,材料分配将无法正常工作,例如 与第一帧相比,绘制顺序发生了变化。

如果您说您不使用任何脚本来修改材质:您能否向我们发送一个仍然显示此问题的最小 Unity 项目? 您可以将其作为 zip 包发送至 contact@esotericsoftware.com,并简要提及此论坛帖子 URL,以便我们了解上下文。 然后我们可以看看为什么会创建材质实例。


Please do not comment out the line that you mentioned, otherwise material assignment when having two or more atlas page textures will not work properly when e.g. the draw order changes compared to the first frame.

If you say that you don't use any script to modify the materials: could you please send us a minimal Unity project that still shows this problem? You can send it as a zip package to contact@esotericsoftware.com, briefly mentioning this forum thread URL so that we know the context. Then we can have a look at why there are material instances created.

5 jours plus tard

抱歉,确实使用MaterialPropertyBlock.Set*** 修改了材质,请问该如何修正该BUG?

在角色被创建时,使用MaterialPropertyBlock.Set***修改了材质的属性,使用了render.materials.EnableKeyword(key)
其中
render是该GameObject的Renderer或MeshRenderer应该怎样避免实例化材质呢
for (int i = 0; i < _render.materials.Length; ++i)
{
_render.materials.EnableKeyword(key);
}

感谢您的附加信息! EnableKeyword 的使用解释了为什么要创建 Material 实例。

不幸的是,无法将 MaterialPropertyBlock 与自定义的 EnableKeyword 着色器关键字一起使用,因为在幕后着色器关键字有效地切换到不同的着色器。 每个变体(关键字的组合)都是单独编译的,并且需要单独的材质(绘制调用)。 如果不清楚,这个 Unity 论坛主题 用不同的语言解释它。

您要设置什么材料关键字? 一般来说,最好的解决方案是在骨架材质中预先设置相应的关键字,如果它在多个实例之间共享的话。


Thanks for the additional information! The use of EnableKeyword explains why a Material instance is created.

Unfortunately there is no way to use a MaterialPropertyBlock with a custom EnableKeyword shader keyword, since behind the scene shader keywords effectively switch to a different shader. Each variant (combination of keywords) is compiled separately and requires a separate Material (draw call). In case this was unclear, this Unity forum thread explains it in different words.

What material keyword are you trying to set? In general the best solution would be to set the respective keywords at the skeleton Material up front if it is shared across multiple instances anyway.