[Effect] mode7

Share your Construct 2 effect files

Post » Thu Oct 30, 2014 10:41 pm

The renderer is fully premultiplied, meaning the color rgb values are multiplied by their alpha components. For example 50% opacity red is actually (0.5, 0, 0, 0.5), and will still render as full brightness red at 50% opacity instead of a dark "half red" at 50% opacity due to the renderer blend configuration. It's easy to mess up premultiplied alpha in shaders.

I can't figure out why right now but there seems to be some kind of built in fog calculation which screws up the premultiplied alpha. I don't know why that would need to be in the shader anyway, since if it's linear you can fake it easily with a sprite. If you remove the fog then the alpha blend works correctly.

Just replace the last few (broken) lines of the shader:

Code: Select all
    vec4 color = texture2D(samplerFront, vec2(mod(xx * scale_x, 1.0), mod(yy * scale_y, 1.0)));
   
    //add some fog in the distance
    gl_FragColor = vec4(color.rgb/color.a, color.a*(abs(pz)*15.0));


with this, which skips the fog calculation:

Code: Select all
gl_FragColor = texture2D(samplerFront, vec2(mod(xx * scale_x, 1.0), mod(yy * scale_y, 1.0)));


Top tip: GPUs aren't good at conditionals. The shader would probably be a lot faster on low-end hardware if the "single_image" conditional were removed. It could be made as two separate shaders instead, each with the mode hard coded on/off. Then you have a blazing fast shader: one texture lookup per fragment, no conditionals, no loops... it should even run well on mobile!
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Thu Oct 30, 2014 10:44 pm

Oh, and remove this line at the top - it's a completely redundant texture lookup which isn't referenced anywhere!

Code: Select all
vec4 front = texture2D(samplerFront, vTex);


That should double your rendering throughput :P

Also, tagging @Aurel
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Thu Oct 30, 2014 11:00 pm

Thanks you SO MUCH @Ashley, transparencies are BACK in business! : ))
And the shader seems faster, too!

The additive blending is still missing, though, but that's so much better already : )))
I'm investigating right now for the blending, maybe I'm doing something wrong somewhere...
(but blendings are OK if the mode7 effect is disabled, even with 4 others effects activated)
Image | @AurelRegard on twitter
B
19
S
6
G
1
Posts: 307
Reputation: 2,495

Post » Thu Oct 30, 2014 11:48 pm

Image

Nope, definitely no luck with the blendings. (Can't wait to have this fixed to record a clean video!)

I tried:
- remove all other effects, on the layout and on all layers
- try forcing own texture on/off
- check at the fallback blendings

Argl, so close! Any idea? Could that come from the shader too? I guess this one is on my side, but I don't see where to look for...
Image | @AurelRegard on twitter
B
19
S
6
G
1
Posts: 307
Reputation: 2,495

Post » Fri Oct 31, 2014 12:00 pm

Have you got a minimal repro to play with?
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Fri Oct 31, 2014 12:03 pm

@Aurel would you mind uploading the fixed shader file if you manage to get it all sorted? Thanks.
B
37
S
19
G
18
Posts: 613
Reputation: 10,231

Post » Fri Oct 31, 2014 2:31 pm

Of course!

I'm working on the minimal CAPX, should we find something, I'll post the shader and the CAPX, so everyone can have it.

@Ashley working on it as we speak!
Last edited by Aurel on Fri Oct 31, 2014 3:23 pm, edited 1 time in total.
Image | @AurelRegard on twitter
B
19
S
6
G
1
Posts: 307
Reputation: 2,495

Post » Fri Oct 31, 2014 3:20 pm

@Ashley

Image

Minimum CAPX available here (6mo): https://www.dropbox.com/s/2y3i4af1ldtze ... .capx?dl=0

What's inside?

- a sprite, in additive mode, on a layer.
- a bright background layer so we can see the addtive effect.

Result:
- additive blending works ok if mode7 is desactivated on the sprite layer.
- additive blending doesn't work if mode7 is activated on the sprite layer.
- additive blending works if applicated on the full layer, though. But not on sprites.
Image | @AurelRegard on twitter
B
19
S
6
G
1
Posts: 307
Reputation: 2,495

Post » Fri Oct 31, 2014 6:53 pm

@R0J0hound A friend of mine just simplified the shader the way Ashley said, so if you're using only "single_image", it's much faster.
Once we got this blending issue sorted, are you ok with me posting it here?
I don't want to be unrespectful with your work.
Image | @AurelRegard on twitter
B
19
S
6
G
1
Posts: 307
Reputation: 2,495

Post » Sat Nov 01, 2014 3:35 pm

@Aurel - it's working correctly. What's happening is:

1. Layer has effect so is rendering to its own texture
2. Layer texture starts out transparent black (0,0,0,0)
3. Additive blended object renders on top of transparent black. This (AFAIK) correctly fills in the alpha, so you end up with opaque black.
4. Mode7 effect processes on the layer texture, which correctly distorts the black text.

To fix it you can try setting the layer with the mode7 effect also to 'additive' blend. It will process the mode7 effect, then additively blend the result on to the background (since blend modes come last). This only works if everything on that layer has additive blend, but that's a good idea anyway since it improves rendering performance.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

PreviousNext

Return to Effects

Who is online

Users browsing this forum: No registered users and 0 guests