Kaynağa Gözat

Simplification As A SceneComponent

sebastien 7 yıl önce
ebeveyn
işleme
7a85889b64

+ 0 - 19
src/Mesh/babylon.mesh.ts

@@ -2189,25 +2189,6 @@
         }
 
         /**
-         * Simplify the mesh according to the given array of settings.
-         * Function will return immediately and will simplify async. It returns the Mesh.  
-         * @param settings a collection of simplification settings.
-         * @param parallelProcessing should all levels calculate parallel or one after the other.
-         * @param type the type of simplification to run.
-         * @param successCallback optional success callback to be called after the simplification finished processing all settings.
-         */
-        public simplify(settings: Array<ISimplificationSettings>, parallelProcessing: boolean = true, simplificationType: SimplificationType = SimplificationType.QUADRATIC, successCallback?: (mesh?: Mesh, submeshIndex?: number) => void): Mesh {
-            this.getScene().simplificationQueue.addTask({
-                settings: settings,
-                parallelProcessing: parallelProcessing,
-                mesh: this,
-                simplificationType: simplificationType,
-                successCallback: successCallback
-            });
-            return this;
-        }
-
-        /**
          * Optimization of the mesh's indices, in case a mesh has duplicated vertices.   
          * The function will only reorder the indices and will not remove unused vertices to avoid problems with submeshes.   
          * This should be used together with the simplification to avoid disappearing triangles.  

+ 104 - 1
src/Mesh/babylon.meshSimplification.ts

@@ -1,4 +1,56 @@
 module BABYLON {
+    export interface Scene {
+        /** @hidden (Backing field) */
+        _simplificationQueue: SimplificationQueue;
+
+        /**
+         * Gets or sets the simplification queue attached to the scene
+         * @see http://doc.babylonjs.com/how_to/in-browser_mesh_simplification
+         */
+        simplificationQueue: SimplificationQueue;
+    }
+
+    Object.defineProperty(Scene.prototype, "simplificationQueue", {
+        get: function (this:Scene) {
+            if (!this._simplificationQueue) {
+                this._simplificationQueue = new SimplificationQueue();
+                let component = this._getComponent(SceneComponentConstants.NAME_SIMPLIFICATIONQUEUE) as SimplicationQueueSceneComponent;
+                if (!component) {
+                    component = new SimplicationQueueSceneComponent(this);
+                    this._addComponent(component);
+                }
+            }
+            return this._simplificationQueue;
+        },
+        set: function (this:Scene, value: SimplificationQueue) {
+            this._simplificationQueue = value;
+        },
+        enumerable: true,
+        configurable: true
+    });
+
+    export interface Mesh {
+        /**
+         * Simplify the mesh according to the given array of settings.
+         * Function will return immediately and will simplify async. It returns the Mesh.  
+         * @param settings a collection of simplification settings.
+         * @param parallelProcessing should all levels calculate parallel or one after the other.
+         * @param type the type of simplification to run.
+         * @param successCallback optional success callback to be called after the simplification finished processing all settings.
+         */
+        simplify(settings: Array<ISimplificationSettings>, parallelProcessing: boolean, simplificationType: SimplificationType, successCallback?: (mesh?: Mesh, submeshIndex?: number) => void): Mesh;
+    }
+
+    Mesh.prototype.simplify = function(settings: Array<ISimplificationSettings>, parallelProcessing: boolean = true, simplificationType: SimplificationType = SimplificationType.QUADRATIC, successCallback?: (mesh?: Mesh, submeshIndex?: number) => void): Mesh {
+        this.getScene().simplificationQueue.addTask({
+            settings: settings,
+            parallelProcessing: parallelProcessing,
+            mesh: this,
+            simplificationType: simplificationType,
+            successCallback: successCallback
+        });
+        return this;
+    }
 
     /**
      * A simplifier interface for future simplification implementations.
@@ -699,7 +751,6 @@
             }
         }
 
-
         private vertexError(q: QuadraticMatrix, point: Vector3): number {
             var x = point.x;
             var y = point.y;
@@ -746,4 +797,56 @@
             return error;
         }
     }
+
+    /**
+     * Defines the simplification queue scene component responsible to help scheduling the various simplification task
+     * created in a scene
+     */
+    export class SimplicationQueueSceneComponent implements ISceneComponent {
+        /**
+         * The component name helpfull to identify the component in the list of scene components.
+         */
+        public readonly name = SceneComponentConstants.NAME_SIMPLIFICATIONQUEUE;
+
+        /**
+         * The scene the component belongs to.
+         */
+        public scene: Scene;
+
+        /**
+         * Creates a new instance of the component for the given scene
+         * @param scene Defines the scene to register the component in
+         */
+        constructor(scene: Scene) {
+            this.scene = scene;
+        }
+
+        /**
+         * Registers the component in a given scene
+         */
+        public register(): void {
+            this.scene._beforeCameraUpdateStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERAUPDATE_SIMPLIFICATIONQUEUE, this, this._beforeCameraUpdate);
+        }
+
+        /**
+         * Rebuilds the elements related to this component in case of
+         * context lost for instance.
+         */
+        public rebuild(): void {
+            // Nothing to do for this component
+        }
+
+        /**
+         * Disposes the component and the associated ressources
+         */
+        public dispose(): void {
+            // Nothing to do for this component
+        }
+
+        private _beforeCameraUpdate(): void {
+            if (this.scene._simplificationQueue && !this.scene._simplificationQueue.running) {
+                this.scene._simplificationQueue.executeNext();
+            }
+        }
+    }
 } 

+ 0 - 16
src/babylon.scene.ts

@@ -966,12 +966,6 @@
          */
         public VRHelper: VRExperienceHelper;
 
-        /**
-         * Gets or sets the simplification queue attached to the scene
-         * @see http://doc.babylonjs.com/how_to/in-browser_mesh_simplification
-         */
-        public simplificationQueue: SimplificationQueue;
-
         // Private
         private _engine: Engine;
 
@@ -1240,11 +1234,6 @@
                 this.attachControl();
             }
 
-            //simplification queue
-            if (SimplificationQueue) {
-                this.simplificationQueue = new SimplificationQueue();
-            }
-
             //collision coordinator initialization. For now legacy per default.
             this.workerCollisions = false;//(!!Worker && (!!BABYLON.CollisionWorker || BABYLON.WorkerIncluded));
 
@@ -4525,11 +4514,6 @@
                 this.actionManager.processTrigger(ActionManager.OnEveryFrameTrigger);
             }
 
-            //Simplification Queue
-            if (this.simplificationQueue && !this.simplificationQueue.running) {
-                this.simplificationQueue.executeNext();
-            }
-
             if (this._engine.isDeterministicLockStep()) {
                 var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(this._engine.getDeltaTime(), Scene.MaxDeltaTime)) + this._timeAccumulator;
 

+ 3 - 1
src/babylon.sceneComponent.ts

@@ -10,6 +10,7 @@
         public static readonly NAME_BOUNDINGBOXRENDERER = "BoundingBoxRenderer";
         public static readonly NAME_PARTICLESYSTEM = "ParticleSystem";
         public static readonly NAME_GAMEPAD = "Gamepad";
+        public static readonly NAME_SIMPLIFICATIONQUEUE = "SimplificationQueue";
 
         public static readonly STEP_ISREADYFORMESH_EFFECTLAYER = 0;
 
@@ -26,7 +27,8 @@
 
         public static readonly STEP_AFTERRENDERINGGROUPDRAW_EFFECTLAYER_DRAW = 0;
 
-        public static readonly STEP_BEFORECAMERAUPDATE_GAMEPAD = 0;
+        public static readonly STEP_BEFORECAMERAUPDATE_SIMPLIFICATIONQUEUE = 0;
+        public static readonly STEP_BEFORECAMERAUPDATE_GAMEPAD = 1;
 
         public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER = 0;
         public static readonly STEP_AFTERCAMERADRAW_LENSFLARESYSTEM = 1;