模板指令

在 1.0.4 版本加入.

在 1.3.0 版本发生变更: 模板(Stencil)操作已更新,以解决嵌套时出现的一些问题。现在,你**必须**使用StencilUnUse,并重复在StencilPush之后执行的操作。

模板指令允许您绘制并将当前绘制用作遮罩。它们提供的控制不如纯OpenGL那样多,但您仍然可以做出炫酷的效果!

可以使用以下3条指令来控制模板缓冲区:

  • StencilPush:推入一个新的模板层。此后的任何绘制都将用作遮罩。

  • StencilUse :现在绘制下一条指令,并使用模板对它们进行遮罩。

  • StencilUnUse:停止使用模板,即移除遮罩并正常绘制。

  • StencilPop:弹出当前模板层。

您应始终遵循此方案:

StencilPush

# PHASE 1: put any drawing instructions to use as a mask here.

StencilUse

# PHASE 2: all the drawing here will be automatically clipped by the
# mask created in PHASE 1.

StencilUnUse

# PHASE 3: put the same drawing instruction here as you did in PHASE 1

StencilPop

# PHASE 4: the stencil is now removed from the stack and unloaded.

限制

  • 在PHASE 1和PHASE 3中的绘制不得发生冲突,否则您将得到意外结果。

  • 一旦执行StencilPush操作,模板即被激活。

  • 一旦你正确弹出了所有模板层,模板即被停用。

  • 你不得在StencilPush / StencilPop之间自行使用模板。

  • 你可以在 StencilUse 之后、StencilPop 之前再推入另一个模板。

  • 您可以最多叠加128层模板(对于kivy版本低于1.3.0,则为8层)。

模板(Stencil)使用示例

以下是一个示例,采用kv风格:

StencilPush

# create a rectangular mask with a pos of (100, 100) and a (100, 100) size.
Rectangle:
    pos: 100, 100
    size: 100, 100

StencilUse

# we want to show a big green rectangle, however, the previous stencil
# mask will crop us :)
Color:
    rgb: 0, 1, 0
Rectangle:
    size: 900, 900

StencilUnUse

# you must redraw the stencil mask to remove it
Rectangle:
    pos: 100, 100
    size: 100, 100

StencilPop
class kivy.graphics.stencil_instructions.StencilPop

基类:Instruction

弹出模板堆栈。更多信息请参阅模块文档。

class kivy.graphics.stencil_instructions.StencilPush(**kwargs)

基类:Instruction

信息。

clear_stencil

clear_stencil 允许在 StencilPush 阶段禁用模板清除操作。此选项实质上禁用了函数 cgl.glClearStencil(0)cgl.glClear(GL_STENCIL_BUFFER_BIT) 的调用。

如果为 True,则在 StencilPush 阶段会清理模板;如果为 False,则不会清理。

备注

**强烈建议**设置 clear_stencil=False 以提高性能并减少 GPU 使用(尤其是在有数百条指令的情况下)。然而,如果出现任何副作用(例如 StencilPush 的伪影或不准确行为),建议通过 clear_stencil=True 重新启用清除指令。

在 2.3.0 版本加入.

class kivy.graphics.stencil_instructions.StencilUnUse

基类:Instruction

使用当前模板缓冲区来取消设置遮罩。

class kivy.graphics.stencil_instructions.StencilUse(**kwargs)

基类:Instruction

更多信息。

func_op

确定用于 glStencilFunc() 的模板操作。可以是 'never'、'less'、'equal'、'lequal'、'greater'、'notequal'、'gequal' 或 'always' 之一。

默认情况下,操作符设置为“等于”。

在 1.5.0 版本加入.