Просмотр исходного кода

Merge pull request #1462 from sebavan/HighlightLayerExcludeList

Fix Rig Cameras
David Catuhe 9 лет назад
Родитель
Сommit
37023233f1
2 измененных файлов с 64 добавлено и 38 удалено
  1. 28 17
      src/Layer/babylon.highlightlayer.ts
  2. 36 21
      src/babylon.scene.ts

+ 28 - 17
src/Layer/babylon.highlightlayer.ts

@@ -26,6 +26,11 @@
         mainTextureRatio?: number;
 
         /**
+         * Enforces a fixed size texture to ensure resize independant blur.
+         */
+        mainTextureFixedSize?: number;
+
+        /**
          * Multiplication factor apply to the main texture size in the first step of the blur to reduce the size 
          * of the picture to blur (the smaller the faster).
          */
@@ -632,10 +637,10 @@
          */
         public addExcludedMesh(mesh: AbstractMesh) {
             var sourceMesh: Mesh;
-            if (mesh instanceof InstancedMesh) {
-              sourceMesh = (<InstancedMesh> mesh).sourceMesh;
+            if (mesh instanceof InstancedMesh) {
+                sourceMesh = (<InstancedMesh>mesh).sourceMesh;
             } else {
-              sourceMesh = <Mesh> mesh;
+                sourceMesh = <Mesh>mesh;
             }
 
             var meshExcluded = this._excludedMeshes[sourceMesh.id];
@@ -658,10 +663,10 @@
           */
         public removeExcludedMesh(mesh: AbstractMesh) {
             var sourceMesh: Mesh;
-            if (mesh instanceof InstancedMesh) {
-              sourceMesh = (<InstancedMesh> mesh).sourceMesh;
+            if (mesh instanceof InstancedMesh) {
+                sourceMesh = (<InstancedMesh>mesh).sourceMesh;
             } else {
-              sourceMesh = <Mesh> mesh;
+                sourceMesh = <Mesh>mesh;
             }
 
             var meshExcluded = this._excludedMeshes[sourceMesh.id];
@@ -681,10 +686,10 @@
          */
         public addMesh(mesh: AbstractMesh, color: Color3, glowEmissiveOnly = false) {
             var sourceMesh: Mesh;
-            if (mesh instanceof InstancedMesh) {
-              sourceMesh = (<InstancedMesh> mesh).sourceMesh;
+            if (mesh instanceof InstancedMesh) {
+                sourceMesh = (<InstancedMesh>mesh).sourceMesh;
             } else {
-              sourceMesh = <Mesh> mesh;
+                sourceMesh = <Mesh>mesh;
             }
 
             var meshHighlight = this._meshes[sourceMesh.id];
@@ -718,12 +723,12 @@
          */
         public removeMesh(mesh: AbstractMesh) {
             var sourceMesh: Mesh;
-            if (mesh instanceof InstancedMesh) {
-              sourceMesh = (<InstancedMesh> mesh).sourceMesh;
+            if (mesh instanceof InstancedMesh) {
+                sourceMesh = (<InstancedMesh>mesh).sourceMesh;
             } else {
-              sourceMesh = <Mesh> mesh;
+                sourceMesh = <Mesh>mesh;
             }
-            
+
             var meshHighlight = this._meshes[sourceMesh.id];
             if (meshHighlight) {
                 sourceMesh.onBeforeRenderObservable.remove(meshHighlight.observerHighlight);
@@ -753,11 +758,17 @@
          * of the engine canvas size.
          */
         private setMainTextureSize(): void {
-            this._mainTextureDesiredSize.width = this._engine.getRenderingCanvas().width * this._options.mainTextureRatio;
-            this._mainTextureDesiredSize.height = this._engine.getRenderingCanvas().height * this._options.mainTextureRatio;
+            if (this._options.mainTextureFixedSize) {
+                this._mainTextureDesiredSize.width = this._options.mainTextureFixedSize;
+                this._mainTextureDesiredSize.height = this._options.mainTextureFixedSize;
+            }
+            else {
+                this._mainTextureDesiredSize.width = this._engine.getRenderingCanvas().width * this._options.mainTextureRatio;
+                this._mainTextureDesiredSize.height = this._engine.getRenderingCanvas().height * this._options.mainTextureRatio;
 
-            this._mainTextureDesiredSize.width = Tools.GetExponentOfTwo(this._mainTextureDesiredSize.width, this._maxSize);
-            this._mainTextureDesiredSize.height = Tools.GetExponentOfTwo(this._mainTextureDesiredSize.height, this._maxSize);
+                this._mainTextureDesiredSize.width = Tools.GetExponentOfTwo(this._mainTextureDesiredSize.width, this._maxSize);
+                this._mainTextureDesiredSize.height = Tools.GetExponentOfTwo(this._mainTextureDesiredSize.height, this._maxSize);
+            }
         }
 
         /**

+ 36 - 21
src/babylon.scene.ts

@@ -2120,6 +2120,8 @@
 
             // Render targets
             this._renderTargetsDuration.beginMonitoring();
+            var needsRestoreFrameBuffer = false;
+
             var beforeRenderTargetDate = Tools.Now;
             if (this.renderTargetsEnabled && this._renderTargets.length > 0) {
                 this._intermediateRendering = true;
@@ -2136,14 +2138,45 @@
 
                 this._intermediateRendering = false;
                 this._renderId++;
-                engine.restoreDefaultFramebuffer(); // Restore back buffer
+
+                needsRestoreFrameBuffer = true; // Restore back buffer
+            }
+
+            // Render HighlightLayer Texture
+            var stencilState = this._engine.getStencilBuffer();
+            var renderhighlights = false;
+            if (this.renderTargetsEnabled && this.highlightLayers && this.highlightLayers.length > 0) {
+                for (let i = 0; i < this.highlightLayers.length; i++) {
+                    let highlightLayer = this.highlightLayers[i];
+
+                    if (highlightLayer.shouldRender() &&
+                        (!highlightLayer.camera ||
+                            (highlightLayer.camera.cameraRigMode === Camera.RIG_MODE_NONE && camera === highlightLayer.camera) ||
+                            (highlightLayer.camera.cameraRigMode !== Camera.RIG_MODE_NONE && highlightLayer.camera._rigCameras.indexOf(camera) > -1))) {
+
+                        renderhighlights = true;
+                        needsRestoreFrameBuffer = true;
+                        this._intermediateRendering = true;
+
+                        var renderTarget = (<RenderTargetTexture>(<any>highlightLayer)._mainTexture);
+                        renderTarget.render(false, false);
+
+                        this._intermediateRendering = false;
+                    }
+                }
+            }
+
+            if (needsRestoreFrameBuffer) {
+                engine.restoreDefaultFramebuffer();
             }
+
             this._renderTargetsDuration.endMonitoring(false);
 
             // Prepare Frame
             this.postProcessManager._prepareFrame();
 
             this._renderDuration.beginMonitoring();
+
             // Backgrounds
             var layerIndex;
             var layer;
@@ -2162,17 +2195,8 @@
             Tools.StartPerformanceCounter("Main render");
 
             // Activate HighlightLayer stencil
-            var stencilState = this._engine.getStencilBuffer();
-            var renderhighlights = false;
-            if (this.highlightLayers && this.highlightLayers.length > 0) {
-                for (let i = 0; i < this.highlightLayers.length; i++) {
-                    let highlightLayer = this.highlightLayers[i];
-                    if ((!highlightLayer.camera || camera == highlightLayer.camera) && highlightLayer.shouldRender()) {
-                        renderhighlights = true;
-                        this._engine.setStencilBuffer(true);
-                        break;
-                    }
-                }
+            if (renderhighlights) {
+                this._engine.setStencilBuffer(true);
             }
 
             this._renderingManager.render(null, null, true, true);
@@ -2412,15 +2436,6 @@
                 this._renderTargets.push(this._depthRenderer.getDepthMap());
             }
 
-            // HighlightLayer
-            if (this.highlightLayers && this.highlightLayers.length > 0) {
-                for (let i = 0; i < this.highlightLayers.length; i++) {
-                    if (this.highlightLayers[i].shouldRender()) {
-                        this._renderTargets.push((<any>this.highlightLayers[i])._mainTexture);
-                    }
-                }
-            }
-
             // RenderPipeline
             this.postProcessRenderPipelineManager.update();