瀏覽代碼

Merge pull request #3515 from RaananW/execute-once

executeOnceBeforeRender including optional timeout
David Catuhe 7 年之前
父節點
當前提交
9ee5d348ae
共有 1 個文件被更改,包括 28 次插入1 次删除
  1. 28 1
      src/babylon.scene.ts

+ 28 - 1
src/babylon.scene.ts

@@ -1924,6 +1924,33 @@
             this.onAfterRenderObservable.removeCallback(func);
             this.onAfterRenderObservable.removeCallback(func);
         }
         }
 
 
+        private _executeOnceBeforeRender(func: () => void): void {
+            let execFunc = () => {
+                func();
+                setTimeout(() => {
+                    this.unregisterBeforeRender(execFunc);
+                });
+            }
+            this.registerBeforeRender(execFunc);
+        }
+
+        /**
+         * The provided function will run before render once and will be disposed afterwards.
+         * A timeout delay can be provided so that the function will be executed in N ms.
+         * The timeout is using the browser's native setTimeout so time percision cannot be guaranteed.
+         * @param func The function to be executed.
+         * @param timeout optional delay in ms
+         */
+        public executeOnceBeforeRender(func: () => void, timeout?: number): void {
+            if (timeout !== undefined) {
+                setTimeout(() => {
+                    this._executeOnceBeforeRender(func);
+                }, timeout);
+            } else {
+                this._executeOnceBeforeRender(func);
+            }
+        }
+
         public _addPendingData(data: any): void {
         public _addPendingData(data: any): void {
             this._pendingData.push(data);
             this._pendingData.push(data);
         }
         }
@@ -2321,7 +2348,7 @@
 
 
             // Add light to all meshes (To support if the light is removed and then readded)
             // Add light to all meshes (To support if the light is removed and then readded)
             for (var mesh of this.meshes) {
             for (var mesh of this.meshes) {
-                if(mesh._lightSources.indexOf(newLight) === -1){
+                if (mesh._lightSources.indexOf(newLight) === -1) {
                     mesh._lightSources.push(newLight);
                     mesh._lightSources.push(newLight);
                     mesh._resyncLightSources();
                     mesh._resyncLightSources();
                 }
                 }