如何更改kivy RenderContext的片段着色器?

wee*_*not 1 glsl post-processing kivy

我有一个kivy应用程序,我希望在特定RenderContext上绘制的所有内容都应用着色器后处理效果(类似于文档中kivy-examples中的EffectWidget示例中演示的效果).

worldWidget 的构造函数中,我创建了RenderContext

self.prc = RenderContext()
Run Code Online (Sandbox Code Playgroud)

设置其投影矩阵(这是有效的)

self.prc['projection_mat'] = proj_mat
Run Code Online (Sandbox Code Playgroud)

然后尝试将其片段着色器设置为默认片段着色器的最小副本,该着色器应该只将十分之一变为不透明(基本上使屏幕变暗).

self.prc.shader.fs = """
#ifdef GL_ES
    precision highp float;
#endif

/* Outputs from the vertex shader */
varying vec4 frag_color;
varying vec2 tex_coord0;

/* uniform texture samplers */
uniform sampler2D texture0;

void main (void){
    gl_FragColor = 0.1*frag_color * texture2D(texture0, tex_coord0);
}
"""
Run Code Online (Sandbox Code Playgroud)

如果此着色器的代码不正确,则程序不会运行,抱怨着色器编译错误,这表示正在编译着色器.但是,我没有看到着色器的任何影响.绘制的所有内容prc都是绘制的,但是在正常的不透明度下.我究竟做错了什么?谢谢你的时间!


编辑!

我被要求提供一个完整的可运行示例.以下程序绘制两个矩形.左侧的Rectangle有自己的RenderContext,不受灰度后处理影响(它绘制为红色)的影响.右边的矩形没有自己的RenderContext中,它正确地受到影响后处理.

这是代码:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import *
from kivy.graphics.opengl import *
from kivy.graphics.shader import *
from kivy.core.window import Window
from kivy.graphics.transformation import Matrix

from kivy.logger import Logger

class World(Widget) :
    def __init__(self, **kwargs):
        Logger.debug('world.init()')

        # Parent RenderContext for subsuming all other render contexts
        self.prc=RenderContext()
        proj_mat = Matrix()
        proj_mat.look_at(0.,0.,1., # eye position coords
                         0.,0.,0.,  # looking at these coords
                         0,1.,0)    # a vector that points up

        if Window.height > Window.width :
            self.xRadius = float(Window.width)/Window.height
            self.yRadius = 1.0
            proj_mat.scale(1.0/self.xRadius,1.0,1.0)
        else :
            self.xRadius = 1.0
            self.yRadius = float(Window.height)/Window.width
            proj_mat.scale(1.0,1.0/self.yRadius,1.0)

        self.prc['projection_mat'] = proj_mat

        ## an effect shader used to make objects monochromatic (grayscale)
        self.prc.shader.fs = """
#ifdef GL_ES
precision highp float;
#endif

/* Outputs from the vertex shader */
varying vec4 frag_color;
varying vec2 vTexCoords0;

/* uniform texture samplers */
uniform sampler2D texture0;

uniform vec2 resolution;
uniform float time;

void main() {
  vec4 rgb = texture2D(texture0, vTexCoords0);
  float c = (rgb.x + rgb.y + rgb.z) * 0.3333;
  gl_FragColor = vec4(c, c, c, 1.0);
}
"""

        if not self.prc.shader.success :
            raise Exception('Effect shader compile failed.')

        self.canvas = self.prc

        ## Left Rectangle drawn with its own RenderContext
        ## this is not affected by the effect shader (if it were, it would be drawn as white)
        ## but it is affected by the parent's projection matrix
        self.spriteRC = RenderContext(use_parent_projection=True)
        self.spriteRC.add(Color(1,0,0,1))
        self.spriteRC.add(Rectangle(pos=(-0.25,0.0),size=(0.1,0.1)))

        ## Right Rectangle object drawn directly to the canvas
        ## this **is** affected by the effect shader
        self.canvas.add(Color(1,0,0,1))
        self.canvas.add(Rectangle(pos=(0.25,0),size=(0.1,0.1)))
        self.canvas.add(self.spriteRC)



        super(World, self).__init__(**kwargs)

class GameApp(App):
    def build(self):
        w = World()
        fl = FloatLayout()
        fl.add_widget(w)
        return fl

if __name__ == '__main__':
    GameApp().run()
Run Code Online (Sandbox Code Playgroud)

tit*_*ito 5

着色器无法堆叠在管道中.只会使用最新的绑定,这不是Kivy的限制,但这就是OpenGL的工作原理.即:

self.c1 = RenderContext()
self.c2 = RenderContext()
self.c2.add(Rectangle())
self.c1.add(self.c2)
Run Code Online (Sandbox Code Playgroud)

矩形将仅由最新的着色器处理,即c2中的着色器.

为了获得矩形的特定着色器,然后使用c1着色器处理输出,请使用Framebuffer(Fbo是RenderContext子类)!

self.c1 = RenderContext()
self.c2 = Fbo(size=...)
self.c2.add(Rectangle())
self.c1.add(self.c2)  # this is just for triggering the render from c1 when c2 content changes
self.c1.add(Rectangle(size=self.c2.size, texture=self.c2.texture))
Run Code Online (Sandbox Code Playgroud)

我在这里错过了所有Color和其他参数,但它只是用于演示.您可以像在RenderContext上一样更改Fbo上的着色器.