setBlending method

dynamic 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

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

    return;
  }

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

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

        currentBlendEquation = AddEquation;
        currentBlendEquationAlpha = AddEquation;
      }

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

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

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

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

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

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

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

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

          default:
            print('THREE.WebGLState: Invalid blending: $blending');
            break;
        }
      }

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

      currentBlending = blending;
      currentPremultipledAlpha = premultipliedAlpha;
    }

    return;
  }

  // custom blending

  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;
}