瀏覽代碼

Alpha Mode

sebavan 6 年之前
父節點
當前提交
b5878bb789
共有 3 個文件被更改,包括 123 次插入1 次删除
  1. 40 0
      src/Engines/constants.ts
  2. 80 1
      src/Engines/engine.ts
  3. 3 0
      src/LibDeclarations/webgl.d.ts

+ 40 - 0
src/Engines/constants.ts

@@ -28,6 +28,46 @@ export class Constants {
      * Alpha will be set to SRC ALPHA + (1 - SRC ALPHA) * DEST ALPHA
      */
     public static readonly ALPHA_SCREENMODE = 10;
+    /**
+     * Defines that alpha blending to SRC + DST
+     * Alpha will be set to SRC ALPHA + DST ALPHA
+     */
+    public static readonly ALPHA_ONEONE_ONEONE = 11;
+    /**
+     * Defines that alpha blending to SRC * DST ALPHA + DST
+     * Alpha will be set to 0
+     */
+    public static readonly ALPHA_ALPHATOCOLOR = 12;
+    /**
+     * Defines that alpha blending to SRC * (1 - DST) + DST * (1 - SRC)
+     */
+    public static readonly ALPHA_REVERSEONEMINUS = 13;
+    /**
+     * Defines that alpha blending to SRC + DST * (1 - SRC ALPHA)
+     * Alpha will be set to SRC ALPHA + DST ALPHA * (1 - SRC ALPHA)
+     */
+    public static readonly ALPHA_SRC_DSTONEMINUSSRCALPHA = 14;
+    /**
+     * Defines that alpha blending to SRC + DST
+     * Alpha will be set to SRC ALPHA
+     */
+    public static readonly ALPHA_ONEONE_ONEZERO = 15;
+
+    /** Defines that alpha blending equation a SUM */
+    public static readonly ALPHA_EQUATION_ADD = 0;
+    /** Defines that alpha blending equation a SUBSTRACTION */
+    public static readonly ALPHA_EQUATION_SUBSTRACT = 1;
+    /** Defines that alpha blending equation a REVERSE SUBSTRACTION */
+    public static readonly ALPHA_EQUATION_REVERSE_SUBTRACT = 2;
+    /** Defines that alpha blending equation a MAX operation */
+    public static readonly ALPHA_EQUATION_MAX = 3;
+    /** Defines that alpha blending equation a MIN operation */
+    public static readonly ALPHA_EQUATION_MIN = 4;
+    /**
+     * Defines that alpha blending equation a DARKEN operation:
+     * It takes the min of the src and sums the alpha channels.
+     */
+    public static readonly ALPHA_EQUATION_DARKEN = 5;
 
     /** Defines that the ressource is not delayed*/
     public static readonly DELAYLOADSTATE_NONE = 0;

+ 80 - 1
src/Engines/engine.ts

@@ -195,6 +195,8 @@ export class EngineCapabilities {
     };
     /** Max number of texture samples for MSAA */
     public maxMSAASamples = 1;
+    /** Defines if the blend min max extension is supported */
+    public blendMinMax: boolean;
 }
 
 /** Interface defining initialization parameters for Engine class */
@@ -842,7 +844,9 @@ export class Engine {
     /** @hidden */
     protected _alphaState = new _AlphaState();
     /** @hidden */
-    protected _alphaMode = Engine.ALPHA_DISABLE;
+    protected _alphaMode = Engine.ALPHA_ADD;
+    /** @hidden */
+    protected _alphaEquation = Engine.ALPHA_DISABLE;
 
     // Cache
     /** @hidden */
@@ -1555,6 +1559,20 @@ export class Engine {
             }
         }
 
+        if (this._webGLVersion > 1) {
+            this._caps.blendMinMax = true;
+        }
+        else {
+            const blendMinMaxExtension = this._gl.getExtension('EXT_blend_minmax');
+            if (blendMinMaxExtension != null) {
+                this._caps.blendMinMax = true;
+                this._gl.MIN = blendMinMaxExtension.MAX_EXT;
+                this._gl.MAX = blendMinMaxExtension.MIN_EXT;
+            } else {
+                this._caps.blendMinMax = false;
+            }
+        }
+
         // Depth buffer
         this.setDepthBuffer(true);
         this.setDepthFunctionToLessOrEqual();
@@ -3980,6 +3998,27 @@ export class Engine {
                 this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE_MINUS_SRC_COLOR, this._gl.ONE, this._gl.ONE_MINUS_SRC_ALPHA);
                 this._alphaState.alphaBlend = true;
                 break;
+
+            case Constants.ALPHA_ONEONE_ONEONE:
+                this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE, this._gl.ONE, this._gl.ONE);
+                this._alphaState.alphaBlend = true;
+                break;
+            case Constants.ALPHA_ALPHATOCOLOR:
+                this._alphaState.setAlphaBlendFunctionParameters(this._gl.DST_ALPHA, this._gl.ONE, this._gl.ZERO, this._gl.ZERO);
+                this._alphaState.alphaBlend = true;
+                break;
+            case Constants.ALPHA_REVERSEONEMINUS:
+                this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE_MINUS_DST_COLOR, this._gl.ONE_MINUS_SRC_COLOR, this._gl.ONE_MINUS_DST_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA);
+                this._alphaState.alphaBlend = true;
+                break;
+            case Constants.ALPHA_SRC_DSTONEMINUSSRCALPHA:
+                this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE_MINUS_SRC_ALPHA);
+                this._alphaState.alphaBlend = true;
+                break;
+            case Constants.ALPHA_ONEONE_ONEZERO:
+                this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE, this._gl.ONE, this._gl.ZERO);
+                this._alphaState.alphaBlend = true;
+                break;
         }
         if (!noDepthWriteChange) {
             this.setDepthWrite(mode === Engine.ALPHA_DISABLE);
@@ -3996,6 +4035,46 @@ export class Engine {
         return this._alphaMode;
     }
 
+    /**
+     * Sets the current alpha equation
+     * @param equation defines the equation to use (one of the Engine.ALPHA_EQUATION_XXX)
+     */
+    public setAlphaEquation(equation: number): void {
+        if (this._alphaEquation === equation) {
+            return;
+        }
+
+        switch (equation) {
+            case Constants.ALPHA_EQUATION_ADD:
+                this._alphaState.setAlphaEquationParameters(this._gl.FUNC_ADD, this._gl.FUNC_ADD);
+                break;
+            case Constants.ALPHA_EQUATION_SUBSTRACT:
+                this._alphaState.setAlphaEquationParameters(this._gl.FUNC_SUBTRACT, this._gl.FUNC_SUBTRACT);
+                break;
+            case Constants.ALPHA_EQUATION_REVERSE_SUBTRACT:
+                this._alphaState.setAlphaEquationParameters(this._gl.FUNC_REVERSE_SUBTRACT, this._gl.FUNC_REVERSE_SUBTRACT);
+                break;
+            case Constants.ALPHA_EQUATION_MAX:
+                this._alphaState.setAlphaEquationParameters(this._gl.MAX, this._gl.MAX);
+                break;
+            case Constants.ALPHA_EQUATION_MIN:
+                this._alphaState.setAlphaEquationParameters(this._gl.MIN, this._gl.MIN);
+                break;
+            case Constants.ALPHA_EQUATION_DARKEN:
+                this._alphaState.setAlphaEquationParameters(this._gl.MIN, this._gl.FUNC_ADD);
+                break;
+        }
+        this._alphaEquation = equation;
+    }
+
+    /**
+     * Gets the current alpha equation.
+     * @returns the current alpha equation
+     */
+    public getAlphaEquation(): number {
+        return this._alphaEquation;
+    }
+
     // Textures
 
     /**

+ 3 - 0
src/LibDeclarations/webgl.d.ts

@@ -45,6 +45,9 @@ interface WebGLRenderingContext {
     UNSIGNED_INT_24_8: number;
     DEPTH24_STENCIL8: number;
 
+    MIN: number;
+    MAX: number;
+
     /* Multiple Render Targets */
     drawBuffers(buffers: number[]): void;
     readBuffer(src: number): void;