Nested snapshots: invalidate removing objects not part of the snapshot

Been running into an issue with nested snapshots which I’d greatly appreciate help with.

The basic setup is:

Snapshot 1 contains two groups (group 1 and group 2). In game, the purpose of this snapshot is to frame buffer the entire game scene for post processing.

The lower group (group 1) contains Snapshot 2 (which would be a ground-level FX layer in game), and the upper group (group 2) contains objects.

The code invalidates both snapshots each frame to update them.

However calling invalidate on Snapshot 2 also causes some of the objects from group 2 to not be rendered, which doesn’t make sense. Confusingly, if the position in the group stack of the Snapshot and Objects are swapped so that snapshot 2 is on top, everything is rendered correctly. 

Is this a Corona bug? I’ve coded a minimal sample project to reproduce the issue.

Thanks - Sean

– Begin sample code

– parameters

_H = display.contentHeight; _W = display.contentWidth;

display.setStatusBar(display.HiddenStatusBar)

– Snapshot 1

local ss1 = display.newSnapshot(_W, _H)

– Make snapshot coordinates correspond to real screen coordinates so that it properly fills screen

– and drawing to the snapshot is the same as drawing to real screen coords

ss1.anchorX = 0; ss1.anchorY = 0

ss1.group.x = -0.5 * _W; ss1.group.y = -0.5 * _H

local grp1 = display.newGroup()

ss1.group:insert(grp1)

local grp2 = display.newGroup()

ss1.group:insert(grp2)

– Blue background

local ss1_rect = display.newRect(0.5 * _W, 0.5 * _H, _W, _H)

grp1:insert(ss1_rect)

ss1_rect:setFillColor (0, 0, 0.5)

– Grid of objects

for i=1, 9 do

for j=1, 9 do

  local object = display.newRect(i/10 * _W, j/10 * _H, 20, 20)

  grp2:insert(object)

end

end

– Snapshot 2

local ss2 = display.newSnapshot(_W, _H)

grp1:insert(ss2)

ss2.anchorX = 0; ss2.anchorY = 0

ss2.group.x = -0.5 * _W; ss2.group.y = -0.5 * _H

– Green background

local ss2_rect = display.newRect(0.5 * _W, 0.5 * _H, _W, _H)

ss2.group:insert(ss2_rect)

ss2_rect:setFillColor (0, 0.5, 0, 0.5)

function onFrame()

ss2:invalidate() – Comment this out and the grid of squares reappears

ss1:invalidate()

end

Runtime:addEventListener(‘enterFrame’, onFrame)

– End sample code

Hi @sean181,

I’m a bit confused on your usage of nested snapshots here. If “ss1” is supposed to be the snapshot to “frame buffer the entire game scene for post processing”, then what is the second snapshot used for? You say it’s a “ground-level FX layer in game” but why does it need to be a snapshot? What kind of effects or items are you planning to render in there, and what is the specific usage for making it a snapshot? Basically, snapshots are for extremely specific usage cases so I want to understand that you’re using them with the proper goals and requirements.

Best regards,

Brent

Hi Brent,

In game the SS2 layer contains shadows which are projected from characters and objects. using the snapshot in this case allows all the shadows to be composited together so that overlapping shadows blend into one uniformly coloured larger shadow. Alpha is then applied to this layer + a mask that cuts off the shadows at walls or void areas in the scene.

Thanks for looking into this,

Sean

Should I submit this as a bug?

Hi Sean,

I haven’t gotten a chance to inspect this too deeply yet… hopefully I can today or tomorrow. Thanks for your patience.

Brent

Update - Was trying to find a workaround to this today, and interestingly the setup

Snapshot -> Group -> Snapshot

exhibits the culling problem, but

Snapshot -> Snapshot

does not.

So I can work around the problem by reorganising the (complicated) group structure, but it’s still really strange.

Hi @sean181,

I’m a bit confused on your usage of nested snapshots here. If “ss1” is supposed to be the snapshot to “frame buffer the entire game scene for post processing”, then what is the second snapshot used for? You say it’s a “ground-level FX layer in game” but why does it need to be a snapshot? What kind of effects or items are you planning to render in there, and what is the specific usage for making it a snapshot? Basically, snapshots are for extremely specific usage cases so I want to understand that you’re using them with the proper goals and requirements.

Best regards,

Brent

Hi Brent,

In game the SS2 layer contains shadows which are projected from characters and objects. using the snapshot in this case allows all the shadows to be composited together so that overlapping shadows blend into one uniformly coloured larger shadow. Alpha is then applied to this layer + a mask that cuts off the shadows at walls or void areas in the scene.

Thanks for looking into this,

Sean

Should I submit this as a bug?

Hi Sean,

I haven’t gotten a chance to inspect this too deeply yet… hopefully I can today or tomorrow. Thanks for your patience.

Brent

Update - Was trying to find a workaround to this today, and interestingly the setup

Snapshot -> Group -> Snapshot

exhibits the culling problem, but

Snapshot -> Snapshot

does not.

So I can work around the problem by reorganising the (complicated) group structure, but it’s still really strange.