@swf2es/render 0.1.41 → 0.1.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -568,6 +568,12 @@ const layerColor = (row: string) => ` if (${row} >= 0 && front.a > 0.0) {
568
568
  }
569
569
  `;
570
570
 
571
+ /**
572
+ * Added to a blend program's uMode where its target is a layer drawn for
573
+ * its blend mode alone (SceneList.fresh), which BLEND_FRONT takes apart.
574
+ */
575
+ export const FRESH = 64;
576
+
571
577
  /** A composited layer's pixel, `front`, coloured by its row's colour transform where it has one (uRow not -1). */
572
578
  export const LAYER_COLOR = layerColor("uRow");
573
579
 
@@ -576,18 +582,41 @@ export const LAYER_COLOR = layerColor("uRow");
576
582
  * (uBack, a copy of it, whose texel at the fragment is uBackOffset short of
577
583
  * it) into finalColor: normal, layer, add and screen as `front` alone, GL's
578
584
  * blending computing them; the separable modes blend straight colours and
579
- * composite source over, the others have formulas of their own.
585
+ * composite source over, the others have formulas of their own: invert
586
+ * ignores the source's colour, inverting the clear part of what is below
587
+ * to white, and subtract adds alphas, as BitmapData.draw's do.
588
+ *
589
+ * In a layer drawn for its blend mode alone (FRESH added to uMode,
590
+ * SceneList.fresh), adl draws `front` as it is, in any mode, over a pixel
591
+ * nothing in the layer has drawn, where subtract, invert, alpha and erase
592
+ * would leave black or nothing: an inverted shape in an inverted clip
593
+ * shows. A pixel drawn and then cleared, by erase or alpha, blends by the
594
+ * formulas, so they mark it DRAWN_CLEAR, clear but for a red no
595
+ * premultiplied colour has, which the formulas read as clear and
596
+ * compositeFragment drops, as Flash's premultiplied pixels of no alpha
597
+ * have no colour.
580
598
  */
581
- const BLEND_FRONT = ` if (uMode == ${NORMAL} || uMode == ${LAYER} || uMode == ${SCREEN} || uMode == ${ADD}) {
599
+ const BLEND_FRONT = ` int mode = uMode & ${FRESH - 1};
600
+ bool fresh = uMode >= ${FRESH};
601
+ if (mode == ${NORMAL} || mode == ${LAYER} || mode == ${SCREEN} || mode == ${ADD}) {
582
602
  finalColor = front;
583
603
  return;
584
604
  }
585
605
 
586
606
  vec4 back = texelFetch(uBack, ivec2(gl_FragCoord.xy) - uBackOffset, 0);
607
+ if (fresh && back == vec4(0.0)) {
608
+ finalColor = front;
609
+ return;
610
+ }
611
+
612
+ if (back.a == 0.0) {
613
+ back = vec4(0.0);
614
+ }
615
+
587
616
  vec3 cb = straight(back);
588
617
  vec3 cf = straight(front);
589
618
  vec3 b;
590
- switch (uMode) {
619
+ switch (mode) {
591
620
  case ${MULTIPLY}: b = cb * cf; break;
592
621
  case ${LIGHTEN}: b = max(cb, cf); break;
593
622
  case ${DARKEN}: b = min(cb, cf); break;
@@ -595,16 +624,19 @@ const BLEND_FRONT = ` if (uMode == ${NORMAL} || uMode == ${LAYER} || uMode == $
595
624
  case ${OVERLAY}: b = hardlight(cf, cb); break;
596
625
  case ${HARDLIGHT}: b = hardlight(cb, cf); break;
597
626
  case ${SUBTRACT}:
598
- finalColor = vec4(max(back.rgb - front.rgb, 0.0), front.a + back.a * (1.0 - front.a));
627
+ finalColor = vec4(max(back.rgb - front.rgb, 0.0), min(front.a + back.a, 1.0));
599
628
  return;
600
629
  case ${INVERT}:
601
- finalColor = vec4((back.a - back.rgb) * front.a + back.rgb * (1.0 - front.a), back.a);
630
+ finalColor = vec4(
631
+ (1.0 - back.rgb) * front.a + back.rgb * (1.0 - front.a),
632
+ front.a + back.a * (1.0 - front.a)
633
+ );
602
634
  return;
603
635
  case ${ALPHA}:
604
- finalColor = front.a > 0.0 ? back * front.a : back;
636
+ finalColor = drawn(front.a > 0.0 ? back * front.a : back, fresh);
605
637
  return;
606
638
  case ${ERASE}:
607
- finalColor = back * (1.0 - front.a);
639
+ finalColor = drawn(back * (1.0 - front.a), fresh);
608
640
  return;
609
641
  default:
610
642
  finalColor = front + back * (1.0 - front.a);
@@ -616,7 +648,9 @@ const BLEND_FRONT = ` if (uMode == ${NORMAL} || uMode == ${LAYER} || uMode == $
616
648
  `;
617
649
 
618
650
  /** BLEND_FRONT's helpers. */
619
- const BLEND_HELPERS = `vec3 straight(vec4 c) { return c.a > 0.0 ? c.rgb / c.a : vec3(0.0); }
651
+ const BLEND_HELPERS = `const vec4 DRAWN_CLEAR = vec4(1.0 / 255.0, 0.0, 0.0, 0.0);
652
+ vec3 straight(vec4 c) { return c.a > 0.0 ? c.rgb / c.a : vec3(0.0); }
653
+ vec4 drawn(vec4 c, bool fresh) { return fresh && c.a < 0.5 / 255.0 ? DRAWN_CLEAR : c; }
620
654
  vec3 hardlight(vec3 cb, vec3 cf) {
621
655
  vec3 multiply = 2.0 * cb * cf;
622
656
  vec3 screen = 1.0 - 2.0 * (1.0 - cb) * (1.0 - cf);
@@ -649,6 +683,10 @@ void main(void) {
649
683
  ivec2 frag = ivec2(gl_FragCoord.xy);
650
684
  ivec2 at = ivec2(frag.x - uOffset.x, uFlip ? uOffset.y - frag.y : frag.y - uOffset.y);
651
685
  vec4 front = texelFetch(uLayer, at, 0);
686
+ if (front.a == 0.0) {
687
+ front = vec4(0.0);
688
+ }
689
+
652
690
  ${LAYER_COLOR}
653
691
  ${BLEND_FRONT}}
654
692
  `;
@@ -143,6 +143,8 @@ export interface Open {
143
143
  object?: KernelObject | null;
144
144
  /** Whether it is opaque throughout, as the stage is (Commands.opaqueStage). */
145
145
  opaque?: boolean;
146
+ /** Whether it is a layer drawn for its blend mode alone (SceneList.fresh). */
147
+ fresh?: boolean;
146
148
  }
147
149
 
148
150
  /** A whole layer's object from params `p` from `at` (plan.ts, layerParams): null for none. */