• RuntimesUnity
  • 请问在Unity中如何将spine文件作为一个动态遮罩?

比如发射出来的粒子只会显示在spine动画内,或者背景只会在spine动画范围内显示?感谢!

  • cjx 님이 이 게시물을 좋아합니다..
Related Discussions
...

使用骨架作为遮罩需要使用(或编写)写入模板缓冲区的特殊着色器。

您想要屏蔽什么组件? 它是一个 UI(Canvas)组件,还是一个普通的 Sprite 或 MeshRenderer?

最近有一个关于这个主题的论坛帖子(英文),您可以在这里找到它:
https://esotericsoftware.com/forum/d/24416-problem-with-sprite-mask
这里还有另一个旧线程:
https://esotericsoftware.com/forum/d/16082-animated-maskunity-help/2

我将在下面的单独帖子中发布第一个线程答案的机器翻译。



Using a skeleton as a mask would require using (or writing) a special shader which writes to the stencil buffer.

What component do you want to mask? Is it a UI (Canvas) component, or a normal Sprite or MeshRenderer that you want to mask?

There has recently been a forum thread (in English) on this topic, you can find it here:
https://esotericsoftware.com/forum/d/24416-problem-with-sprite-mask
And also another older thread here:
https://esotericsoftware.com/forum/d/16082-animated-maskunity-help/2

I will post a machine translation of the first thread's answer below in a separate posting.

  • cjx님이 이에 답장했습니다.

    帖子的机器翻译:

    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    基本上,遮罩交互是使用模板缓冲区实现的,遮罩写入(递增或设置非 0 的值)到模板缓冲区,遮罩对象读取模板值,并且仅在等于 0 且可见性设置为“外部遮罩”或 对于“内部掩码”,大于 0(或“==”或“>=”某个值以支持嵌套掩码层次结构)。

    虽然我们不知道如何执行此操作的干净官方方法,但您需要编写或使用写入模板缓冲区的着色器。 写入操作可能会增加模板缓冲区值(请参阅文档页面此处)或将其设置为常量值,例如 1(如果在 第一掩模级。

    如果其他组件可能依赖于要设置的某个模板值,那么在将模板着色器与其他蒙版组件混合时请注意潜在的副作用。

    ..

    写完上面的帖子后,我想到的一件事是,您可能想要向模板缓冲区写入一个固定值而不是增加它,因为 Spine 骨架很容易具有重叠部分。
    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~



    Machine translation of the postings:
    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    Basically mask interaction is implemented using the stencil buffer, masks writing (incrementing or setting a value other than 0) to the stencil buffer, masked objects reading the stencil value and only rendering when either equal to 0 with visibility set to Outside mask or larger than 0 (or == or >= a certain value to support nested mask-hierarchies) for Inside mask.

    While we don't know a clean official way how to do this, you would need to write or use a shader which writes to the stencil buffer. Likely the write operation should increment the stencil buffer value (see the documentation page here) or set it to a constant value like 1 if on the first mask-level.

    Just watch out for potential side effects when mixing your stencil shader with other Mask components, if perhaps other components rely on a certain stencil value to be set.

    ..

    One thing that came to my mind after having written the above posting is that you might want to write a fixed value to the stencil buffer and not increment it, since a Spine skeleton can easily have overlapping parts.
    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    Harald 您所提供的两个链接内容失效,看不到。是否有新的链接可以提供?

    @cjx 很遗憾得知这些链接对您不起作用。 所有链接在我这边都工作正常,也许它们被您的提供商或国家/地区阻止。

    哪个链接不起作用? 在上面的帖子中,有两个关于 ShaderLab Stencil 缓冲区使用的论坛链接和一个 Unity 文档链接:

    "Problem with Sprite Mask"
    https://esotericsoftware.com/forum/d/24416-problem-with-sprite-mask

    "Animated Mask/Unity (HELP)"
    https://esotericsoftware.com/forum/d/16082-animated-maskunity-help/2

    "ShaderLab 命令:模板"
    https://docs.unity3d.com/cn/2021.3/Manual/SL-Stencil.html



    Sorry to hear the links didn't work for you. All links work fine on my end, perhaps they are blocked by your provider or country.

    Which of the links did not work? In the postings above there are two forum links and one Unity documentation link about ShaderLab Stencil buffer usage:

    "Problem with Sprite Mask"
    https://esotericsoftware.com/forum/d/24416-problem-with-sprite-mask

    "Animated Mask/Unity (HELP)"
    https://esotericsoftware.com/forum/d/16082-animated-maskunity-help/2

    "ShaderLab 命令:模板"
    https://docs.unity3d.com/cn/2021.3/Manual/SL-Stencil.html

    • cjx님이 이에 답장했습니다.

      Harald 非常感谢您的帮助!祝你生活愉快!

      Harald 不过我有在其他国家打开这个链接,依旧显示Sorry!he page you requested could not be found. 🙁。请问能提供图片或者PDF吗?给你添麻烦了。

      很遗憾听到它仍然不适合你。 上面三个链接哪一个不起作用? 下面的论坛链接是“Sprite Mask 问题”和“Animated Mask/Unity(帮助)”? 或者统一链接?

      Sorry to hear it still does not work for you. Which of the three links above do not work? The forum links below "Problem with Sprite Mask" and "Animated Mask/Unity (HELP)"? Or the Unity link?