Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Alpha overlap — Gideros Forum

Alpha overlap

I need to achieve this effect:


Two objects is overlapping on a layer that has alpha < 1, and the intersection part is twice darker then the original. Is there a way to fix it? I tried to apply "setBlendMode" to the layer with all combinations of src and dst modes, but no result.

Problem representasion:
application:setBackgroundColor(0x323232)
 
local layer = Sprite.new()
stage:addChild(layer)
 
local shadow1 = Pixel.new(0, 1, 300, 50)
shadow1:setPosition(100, 225)
layer:addChild(shadow1)
 
local shadow2 = Pixel.new(0, 1, 50, 300)
shadow2:setPosition(225, 100)
layer:addChild(shadow2)
 
layer:setAlpha(0.3)

Comments

  • @rrraptor sorry I cannot answer your question but I have a dirty hack, create a third shadow?!
    application:setBackgroundColor(0x323232)
     
    local layer = Sprite.new()
    layer:setAlpha(0.3)
     
    local shadow1 = Pixel.new(0, 1, 300, 50)
    shadow1:setPosition(100, 225)
    layer:addChild(shadow1)
     
    local shadow2 = Pixel.new(0, 1, 50, 125)
    shadow2:setPosition(225, 100)
    layer:addChild(shadow2)
     
    local shadow3 = Pixel.new(0, 1, 50, 150)
    shadow3:setPosition(225, 275)
    layer:addChild(shadow3)
     
    stage:addChild(layer)

    Best bad answer of the week?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • olegoleg Member
    @rrraptor
    Set alpha = 1 -On dark stripes
    Move dark stripes under layer background
    Apply blending mode to the background

    Likes: MoKaLux

    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
    +1 -1 (+1 / -0 )Share on Facebook
  • basically there is no way to fix this using two rectangles because of the way alpha blending actually works.

    The only solution I can think of right now is using 5 rectangles but that's kind of a performance hit.

    Dislikes: Yan

    +1 -1 (+0 / -1 )Share on Facebook
  • keszeghkeszegh Member
    edited August 2019 Accepted Answer
    render both to a rendertarget with alpha=1 and then put on screen a bmp with this rendertarget as texture with alpha=0.3. i think there is no other real solution.
    see the following discussion about the same question:
    http://forum.giderosmobile.com/discussion/6972/question-about-setting-alpha-of-a-mesh-uniformly-an-open-problem-for-you-to-solve#latest
    there actually is a way using stencils which may work in your case.
  • @keszegh yeah, i though about that, but will it cause performance issues with a large number of objects, because they can move?
    Hmm..or i could render static shadows into a different render target once (some sort of baking shadows xD), and dynamic shadows every frame (?) into a another render target buffer, then draw static shadows on it.
  • constant many rendertargets maintained per frame should be fine i guess but you better try it on the weakest machine you target.
  • If all of the crosses are going to be the same you would only need to maintain one RenderTarget.
  • hgy29hgy29 Maintainer
    I just recalled about this ‘problem’ and I think it could be solved with stencil easily. I’ll try to post a solution tomorrow, unless someone else does it meanwhile :)

    Likes: MoKaLux, keszegh

    +1 -1 (+2 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    Accepted Answer
    It works!
    I just added a few stencil commands at the bottom of the OP original code.
    application:setBackgroundColor(0x323232)
     
    local layer = Sprite.new()
    stage:addChild(layer)
     
    local shadow1 = Pixel.new(0, 1, 300, 50)
    shadow1:setPosition(100, 225)
    layer:addChild(shadow1)
     
    local shadow2 = Pixel.new(0, 1, 50, 300)
    shadow2:setPosition(225, 100)
    layer:addChild(shadow2)
     
    layer:setAlpha(0.3)
     
    --Use stencil for drawing every shadow pixel only once:
    --1) Clear stencil on layer (before drawing children)
    layer:setStencilOperation{ stencilClear=true, stencilWriteMask=1 }
    --2) Only draw pixels where stencil is 0, and increment stencil when drawn
    shadow1:setStencilOperation{ stencilClear=false, stencilMask=1, stencilWriteMask=1, 
    	stencilRef=0, stencilFunc=Sprite.STENCIL_EQUAL,
    	depthPass=Sprite.STENCIL_INCR,
    	stencilFail=Sprite.STENCIL_KEEP}
    shadow2:setStencilOperation{ stencilClear=false, stencilMask=1, stencilWriteMask=1, 
    	stencilRef=0, stencilFunc=Sprite.STENCIL_EQUAL,
    	depthPass=Sprite.STENCIL_INCR,
    	stencilFail=Sprite.STENCIL_KEEP}
    +1 -1 (+5 / -0 )Share on Facebook
Sign In or Register to comment.