2016-03-24 7 views
1

私はScatterLayoutをたくさん使っています(スキャッタの中に散らばっているなど)。私を逃れる1つの機能は、ある種のアルファマスクを散布キャンバスに適用することです。アイデアは、背景画像や他の単純な深度のトリックに対応する単純な形状のノックアウトを実行することです。Kivy、Maskウィジェットキャンバス

これは基本的なOpenGLやkivy.graphics.stencil_instructionsで実行できると思います。私は、OpenGLの重いコマンド(私はそれらをデバッグする方法はわかりません)が特に気に入っていませんが、単純なコマンドをウィジェットクラスにラップするだけで問題ありません。だからここ

は、私が他のソースから取得しています何ですが、私は(四角形のように)これ以上の原始的な固形物を取りたい:

(コードは未テストです!)

テクスチャベースの方向で更新
with self.canvas: 

     # Hopefully I can build my "transparency mask" manually with a Texture 
     texture = Texture.create(size=(64, 64)) 
     size = 64 * 64 * 3 
     buf = [int(x * 255/size) for x in range(size)] 
     buf = b''.join(map(chr, buf)) 
     texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte') 

     StencilPush() 

     Rectangle(texture=texture, pos=self.pos, size=(64, 64)) 

     #use mask 
     StencilUse() 

     """ 
     Will we get an image based mask on all drawing commands placed here? 
     """ 

     StencilPop() 
+0

あなたは[この回答](http://stackoverflow.com/questions/35802203/how-do-i-mask-an-image-in-kivy-using-python)ではうまくいきませんか? – KeyWeeUsr

+0

@KeyWeeUsrあなたの投稿は新しいアイデアを生み出しましたが、まだテストしていません。私は私の更新された考えを反映するために私の質問を編集しました。 – user2097818

答えて

1

はまさに答えです。あなた自身のカスタムPNGを使ってみてください。あなたはそれをあなたが好きなように複雑にし、美しく動作させます。私は、アプリケーションのために静的なPNGを使用していないので、レンダリング順序の問題に遭遇した場合には、私がまだ再オープンするかもしれませんが、問題を解決するための重要なコードを含めるつもりです。

fs_multitexture = ''' 
$HEADER$ 

// New uniform that will receive texture at index 1 
uniform sampler2D texture1; 

void main(void) { 

    // multiple current color with both texture (0 and 1). 
    // currently, both will use exactly the same texture coordinates. 
    gl_FragColor = frag_color * \ 
     texture2D(texture0, tex_coord0) * \ 
     texture2D(texture1, tex_coord0); 
} 
''' 

そして、そのテクスチャフィルタリングプロセスを示し最低限Widgetクラスを:私はあまりにも遠くこれを剥奪している場合は、ドキュメント文字列として含める必要があり、非常に単純なシェーダの定義は、(それが自動的にロードされますが、上記のリンクを参照してくださいこの簡単な魔法を作ります:

class MultitextureWidget(Widget): 
    def __init__(self, **kwargs): 
     self.canvas = RenderContext() 
     self.canvas.shader.fs = fs_multitexture 
     with self.canvas: 
      Color(1, 1, 1) 
      BindTexture(source='mtexture2.png', index=1) 
      Rectangle(size=(150, 150), source='mtexture1.png', pos=(500, 200)) 

     self.canvas['texture1'] = 1 
     super(MultitextureWidget, self).__init__(**kwargs) 
     Clock.schedule_interval(self.update_glsl, 0) 

    def update_glsl(self, *largs): 
     self.canvas['projection_mat'] = Window.render_context['projection_mat'] 
     self.canvas['modelview_mat'] = Window.render_context['modelview_mat']