setBlending method

void setBlending(
  1. int blending, [
  2. int? blendEquation,
  3. int? blendSrc,
  4. int? blendDst,
  5. int? blendEquationAlpha,
  6. int? blendSrcAlpha,
  7. int? blendDstAlpha,
  8. bool? premultipliedAlpha,
])

Implementation

void setBlending(int blending,
    [int? blendEquation,
    int? blendSrc,
    int? blendDst,
    int? blendEquationAlpha,
    int? blendSrcAlpha,
    int? blendDstAlpha,
    bool? premultipliedAlpha]) {
  if (blending == NoBlending) {
    if (currentBlendingEnabled) {
      disable(WebGL.BLEND);
      currentBlendingEnabled = false;
    }

    return;
  }

  if (!currentBlendingEnabled) {
    enable(WebGL.BLEND);
    currentBlendingEnabled = true;
  }

  if (blending != CustomBlending) {
    if (blending != currentBlending || premultipliedAlpha != currentPremultipledAlpha) {
      if (currentBlendEquation != AddEquation || currentBlendEquationAlpha != AddEquation) {
        gl.blendEquation(WebGL.FUNC_ADD);

        currentBlendEquation = AddEquation;
        currentBlendEquationAlpha = AddEquation;
      }

      if (premultipliedAlpha != null && premultipliedAlpha) {
        switch (blending) {
          case NormalBlending:
            gl.blendFuncSeparate(WebGL.ONE, WebGL.ONE_MINUS_SRC_ALPHA, WebGL.ONE, WebGL.ONE_MINUS_SRC_ALPHA);
            break;

          case AdditiveBlending:
            gl.blendFunc(WebGL.ONE, WebGL.ONE);
            break;

          case SubtractiveBlending:
            gl.blendFuncSeparate(WebGL.ZERO, WebGL.ONE_MINUS_SRC_COLOR, WebGL.ZERO, WebGL.ONE);
            break;

          case MultiplyBlending:
            gl.blendFuncSeparate(WebGL.ZERO, WebGL.SRC_COLOR, WebGL.ZERO, WebGL.SRC_ALPHA);
            break;

          default:
            console.error('WebGLState: Invalid blending: $blending');
            break;
        }
      }
      else {
        switch (blending) {
          case NormalBlending:
            gl.blendFuncSeparate(WebGL.SRC_ALPHA, WebGL.ONE_MINUS_SRC_ALPHA, WebGL.ONE, WebGL.ONE_MINUS_SRC_ALPHA);
            break;

          case AdditiveBlending:
            gl.blendFunc(WebGL.SRC_ALPHA, WebGL.ONE);
            break;

          case SubtractiveBlending:
            gl.blendFuncSeparate(WebGL.ZERO, WebGL.ONE_MINUS_SRC_COLOR, WebGL.ZERO, WebGL.ONE);
            break;

          case MultiplyBlending:
            gl.blendFunc(WebGL.ZERO, WebGL.SRC_COLOR);
            break;

          default:
            console.error('WebGLState: Invalid blending: $blending');
            break;
        }
      }

      currentBlendSrc = null;
      currentBlendDst = null;
      currentBlendSrcAlpha = null;
      currentBlendDstAlpha = null;

      currentBlending = blending;
      currentPremultipledAlpha = premultipliedAlpha;
    }

    return;
  }

  blendEquationAlpha = blendEquationAlpha ?? blendEquation;
  blendSrcAlpha = blendSrcAlpha ?? blendSrc;
  blendDstAlpha = blendDstAlpha ?? blendDst;

  if (blendEquation != currentBlendEquation || blendEquationAlpha != currentBlendEquationAlpha) {
    gl.blendEquationSeparate(equationToGL[blendEquation]!, equationToGL[blendEquationAlpha]!);

    currentBlendEquation = blendEquation;
    currentBlendEquationAlpha = blendEquationAlpha;
  }

  if (blendSrc != currentBlendSrc ||
      blendDst != currentBlendDst ||
      blendSrcAlpha != currentBlendSrcAlpha ||
      blendDstAlpha != currentBlendDstAlpha) {
    gl.blendFuncSeparate(factorToGL[blendSrc]!, factorToGL[blendDst]!, factorToGL[blendSrcAlpha]!, factorToGL[blendDstAlpha]!);

    currentBlendSrc = blendSrc;
    currentBlendDst = blendDst;
    currentBlendSrcAlpha = blendSrcAlpha;
    currentBlendDstAlpha = blendDstAlpha;
  }

  currentBlending = blending;
  currentPremultipledAlpha = null;
}