Browse Source

Add constructor options and equals method to FresnelParameters + tests.

Brian Zinn 5 years ago
parent
commit
02cd01b932

+ 105 - 24
src/Materials/fresnelParameters.ts

@@ -1,10 +1,71 @@
 import { DeepCopier } from "../Misc/deepCopier";
+import { DeepImmutable } from '../types';
 import { Color3 } from "../Maths/math.color";
 import { Engine } from "../Engines/engine";
 import { SerializationHelper } from "../Misc/decorators";
 import { Constants } from "../Engines/constants";
 
 /**
+ * Options to be used when creating a FresnelParameters.
+ */
+export type IFresnelParametersCreationOptions = {
+    /**
+     * Define the color used on edges (grazing angle)
+     */
+    leftColor?: Color3;
+
+    /**
+     * Define the color used on center
+     */
+    rightColor?: Color3;
+
+    /**
+     * Define bias applied to computed fresnel term
+     */
+    bias?: number;
+
+    /**
+     * Defined the power exponent applied to fresnel term
+     */
+    power?: number;
+
+    /**
+     * Define if the fresnel effect is enable or not.
+     */
+    isEnabled?: boolean;
+};
+
+/**
+ * Serialized format for FresnelParameters.
+ */
+export type IFresnelParametersSerialized = {
+    /**
+     * Define the color used on edges (grazing angle) [as an array]
+     */
+    leftColor: number[];
+
+    /**
+     * Define the color used on center [as an array]
+     */
+    rightColor: number[];
+
+    /**
+     * Define bias applied to computed fresnel term
+     */
+    bias: number;
+
+    /**
+     * Defined the power exponent applied to fresnel term
+     */
+    power: number;
+
+    /**
+     * Define if the fresnel effect is enable or not.
+     */
+    isEnabled: boolean;
+};
+
+/**
  * This represents all the required information to add a fresnel effect on a material:
  * @see http://doc.babylonjs.com/how_to/how_to_use_fresnelparameters
  */
@@ -28,22 +89,32 @@ export class FresnelParameters {
     /**
      * Define the color used on edges (grazing angle)
      */
-    public leftColor = Color3.White();
+    public leftColor: Color3;
 
     /**
      * Define the color used on center
      */
-    public rightColor = Color3.Black();
+    public rightColor: Color3;
 
     /**
      * Define bias applied to computed fresnel term
      */
-    public bias = 0;
+    public bias: number;
 
     /**
      * Defined the power exponent applied to fresnel term
      */
-    public power = 1;
+    public power: number;
+
+    public constructor(options: IFresnelParametersCreationOptions = {}) {
+        this.bias = (options.bias === undefined) ? 0 : options.bias;
+        this.power = (options.power === undefined) ? 1 : options.power;
+        this.leftColor = options.leftColor || Color3.White();
+        this.rightColor = options.rightColor || Color3.Black();
+        if (options.isEnabled === false) {
+            this.isEnabled = false;
+        }
+    }
 
     /**
      * Clones the current fresnel and its valuues
@@ -58,19 +129,31 @@ export class FresnelParameters {
     }
 
     /**
+     * Determines equality between FresnelParameters objects
+     * @param otherFresnelParameters defines the second operand
+     * @returns true if the power, bias, leftColor, rightColor and isEnabled values are equal to the given ones
+     */
+    public equals(otherFresnelParameters: DeepImmutable<FresnelParameters>): boolean {
+        return otherFresnelParameters &&
+            this.bias === otherFresnelParameters.bias &&
+            this.power === otherFresnelParameters.power &&
+            this.leftColor.equals(otherFresnelParameters.leftColor) &&
+            this.rightColor.equals(otherFresnelParameters.rightColor) &&
+            this.isEnabled === otherFresnelParameters.isEnabled;
+    }
+
+    /**
      * Serializes the current fresnel parameters to a JSON representation.
      * @return the JSON serialization
      */
-    public serialize(): any {
-        var serializationObject: any = {};
-
-        serializationObject.isEnabled = this.isEnabled;
-        serializationObject.leftColor = this.leftColor.asArray();
-        serializationObject.rightColor = this.rightColor.asArray();
-        serializationObject.bias = this.bias;
-        serializationObject.power = this.power;
-
-        return serializationObject;
+    public serialize(): IFresnelParametersSerialized {
+        return {
+            isEnabled: this.isEnabled,
+            leftColor: this.leftColor.asArray(),
+            rightColor: this.rightColor.asArray(),
+            bias: this.bias,
+            power: this.power
+        };
     }
 
     /**
@@ -78,16 +161,14 @@ export class FresnelParameters {
      * @param parsedFresnelParameters Define the JSON representation
      * @returns the parsed parameters
      */
-    public static Parse(parsedFresnelParameters: any): FresnelParameters {
-        var fresnelParameters = new FresnelParameters();
-
-        fresnelParameters.isEnabled = parsedFresnelParameters.isEnabled;
-        fresnelParameters.leftColor = Color3.FromArray(parsedFresnelParameters.leftColor);
-        fresnelParameters.rightColor = Color3.FromArray(parsedFresnelParameters.rightColor);
-        fresnelParameters.bias = parsedFresnelParameters.bias;
-        fresnelParameters.power = parsedFresnelParameters.power || 1.0;
-
-        return fresnelParameters;
+    public static Parse(parsedFresnelParameters: IFresnelParametersSerialized): FresnelParameters {
+        return new FresnelParameters({
+            isEnabled: parsedFresnelParameters.isEnabled,
+            leftColor: Color3.FromArray(parsedFresnelParameters.leftColor),
+            rightColor: Color3.FromArray(parsedFresnelParameters.rightColor),
+            bias: parsedFresnelParameters.bias,
+            power: parsedFresnelParameters.power || 1.0
+        });
     }
 }
 

+ 94 - 0
tests/unit/babylon/src/Materials/babylon.fresnelParameters.tests.ts

@@ -0,0 +1,94 @@
+/**
+ * Test Suite for FresnelParameters.
+ */
+describe('Babylon Material FresnelParameters', () => {
+
+    /**
+     * Loads the dependencies.
+     */
+    before(function(done) {
+        this.timeout(180000);
+        (BABYLONDEVTOOLS).Loader
+            .useDist()
+            .testMode()
+            .load(function() {
+                // Force apply promise polyfill for consistent behavior between chrome headless, IE11, and other browsers.
+                BABYLON.PromisePolyfill.Apply(true);
+                done();
+            });
+    });
+
+    describe('#FresnelParameters', () => {
+        it('empty constructor has default values', () => {
+            const subject = new BABYLON.FresnelParameters();
+            expect(subject.bias).to.equal(0, 'bias default is 0');
+            expect(subject.power).to.equal(1, 'power default is 1');
+            expect(subject.isEnabled).to.be.true;
+            expect(subject.leftColor.equals(BABYLON.Color3.White())).to.be.true;
+            expect(subject.rightColor.equals(BABYLON.Color3.Black())).to.be.true;
+        });
+
+        it('serialized empty constructor is serialized correctly', () => {
+            const subject = new BABYLON.FresnelParameters().serialize();
+            console.error(subject)
+            expect(subject).deep.equals({
+                isEnabled: true,
+                leftColor: [1, 1, 1],
+                rightColor: [0, 0, 0],
+                bias: 0,
+                power: 1
+            })
+        });
+
+        it('new FresnelParameters({...}) with options specified', () => {
+            const subject = new BABYLON.FresnelParameters({
+                bias: 1,
+                power: 0,
+                isEnabled: false,
+                leftColor: BABYLON.Color3.Black(),
+                rightColor: BABYLON.Color3.White()
+            });
+            expect(subject.bias).to.equal(1, 'created with 1');
+            expect(subject.power).to.equal(0, 'created with 0');
+            expect(subject.isEnabled).to.be.false;
+            expect(subject.leftColor.equals(BABYLON.Color3.Black())).to.be.true;
+            expect(subject.rightColor.equals(BABYLON.Color3.White())).to.be.true;
+        });
+
+        it('FresnelParameters.Parse({...}) with equality check', () => {
+            const subject = BABYLON.FresnelParameters.Parse({
+                isEnabled: true,
+                leftColor: [1, 1, 1],
+                rightColor: [0, 0, 0],
+                bias: 0,
+                power: 1
+            })
+
+            expect(new BABYLON.FresnelParameters().equals(subject)).to.be.true;
+        });
+
+        it('disabling FresnelParameters should mark materials as dirty (not ready)', () => {
+            const engine = new BABYLON.NullEngine({
+                renderHeight: 256,
+                renderWidth: 256,
+                textureSize: 256,
+                deterministicLockstep: false,
+                lockstepMaxSteps: 1
+            });
+
+            const scene = new BABYLON.Scene(engine);
+            const mesh = BABYLON.Mesh.CreateBox("mesh", 1, scene);
+            const material = new BABYLON.StandardMaterial("material", scene);
+            mesh.material = material;
+
+            const subject = new BABYLON.FresnelParameters();
+            material.refractionFresnelParameters = subject;
+
+            expect(scene._cachedMaterial).is.not.null;
+
+            // should mark materials as dirty and clear scene cache
+            subject.isEnabled = false;
+            expect(scene._cachedMaterial).is.null;
+        });
+    });
+})

+ 2 - 1
tests/unit/karma.conf.js

@@ -19,7 +19,6 @@ module.exports = function(config) {
             '!./**/*.d.ts',
             './Tools/DevLoader/BabylonLoader.js',
             './tests/unit/babylon/babylon.example.tests.js',
-            './tests/unit/babylon/src/Mesh/babylon.positionAndRotation.tests.js',
             './tests/unit/babylon/serializers/babylon.glTFSerializer.tests.js',
             './tests/unit/babylon/src/babylon.node.tests.js',
             './tests/unit/babylon/src/Animations/babylon.animation.tests.js',
@@ -29,9 +28,11 @@ module.exports = function(config) {
             './tests/unit/babylon/src/Loading/babylon.sceneLoader.tests.js',
             './tests/unit/babylon/src/PostProcesses/babylon.postProcess.tests.js',
             './tests/unit/babylon/src/Materials/babylon.material.tests.js',
+            './tests/unit/babylon/src/Materials/babylon.fresnelParameters.tests.js',
             './tests/unit/babylon/src/Meshes/babylon.dictionaryMode.tests.js',
             './tests/unit/babylon/src/Meshes/babylon.geometry.tests.js',
             './tests/unit/babylon/src/Meshes/babylon.mesh.vertexData.tests.js',
+            './tests/unit/babylon/src/Meshes/babylon.positionAndRotation.tests.js',
             './tests/unit/babylon/src/Misc/babylon.promise.tests.js',
             './tests/unit/babylon/src/Physics/babylon.physicsComponents.tests.js',
             { pattern: 'dist/preview release/**/*.js', watched: false, included: false, served: true },