瀏覽代碼

new TextureTools.CreateResizedCopy

David Catuhe 8 年之前
父節點
當前提交
4c256f895c

File diff suppressed because it is too large
+ 4878 - 4868
dist/preview release/babylon.d.ts


File diff suppressed because it is too large
+ 35 - 35
dist/preview release/babylon.js


+ 67 - 6
dist/preview release/babylon.max.js

@@ -9972,20 +9972,28 @@ var BABYLON;
             }
             return rgbaData;
         };
-        Engine.prototype._releaseTexture = function (texture) {
+        Engine.prototype._releaseFramebufferObjects = function (texture) {
             var gl = this._gl;
             if (texture._framebuffer) {
                 gl.deleteFramebuffer(texture._framebuffer);
+                texture._framebuffer = null;
             }
             if (texture._depthStencilBuffer) {
                 gl.deleteRenderbuffer(texture._depthStencilBuffer);
+                texture._depthStencilBuffer = null;
             }
             if (texture._MSAAFramebuffer) {
                 gl.deleteFramebuffer(texture._MSAAFramebuffer);
+                texture._MSAAFramebuffer = null;
             }
             if (texture._MSAARenderBuffer) {
                 gl.deleteRenderbuffer(texture._MSAARenderBuffer);
+                texture._MSAARenderBuffer = null;
             }
+        };
+        Engine.prototype._releaseTexture = function (texture) {
+            var gl = this._gl;
+            this._releaseFramebufferObjects(texture);
             gl.deleteTexture(texture);
             // Unbind channels
             this.unbindAllTextures();
@@ -42879,6 +42887,7 @@ var BABYLON;
             _this._currentRefreshId = -1;
             _this._refreshRate = 1;
             _this._samples = 1;
+            scene = _this.getScene();
             _this.name = name;
             _this.isRenderTarget = true;
             _this._size = size;
@@ -43290,12 +43299,29 @@ var BABYLON;
             }
             return serializationObject;
         };
+        // This will remove the attached framebuffer objects. The texture will not be able to be used as render target anymore
+        RenderTargetTexture.prototype.disposeFramebufferObjects = function () {
+            this.getScene().getEngine()._releaseFramebufferObjects(this.getInternalTexture());
+        };
         RenderTargetTexture.prototype.dispose = function () {
             if (this._postProcessManager) {
                 this._postProcessManager.dispose();
                 this._postProcessManager = null;
             }
             this.clearPostProcesses(true);
+            // Remove from custom render targets
+            var scene = this.getScene();
+            var index = scene.customRenderTargets.indexOf(this);
+            if (index >= 0) {
+                scene.customRenderTargets.splice(index, 1);
+            }
+            for (var _i = 0, _a = scene.cameras; _i < _a.length; _i++) {
+                var camera = _a[_i];
+                index = camera.customRenderTargets.indexOf(this);
+                if (index >= 0) {
+                    camera.customRenderTargets.splice(index, 1);
+                }
+            }
             _super.prototype.dispose.call(this);
         };
         return RenderTargetTexture;
@@ -43523,7 +43549,7 @@ var BABYLON;
                 var textureType = engine.getCaps().textureFloatRender ? BABYLON.Engine.TEXTURETYPE_FLOAT : BABYLON.Engine.TEXTURETYPE_HALF_FLOAT;
                 this._blurX = new BABYLON.BlurPostProcess("horizontal blur", new BABYLON.Vector2(1.0, 0), this._blurKernelX, this._blurRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, textureType);
                 this._blurX.autoClear = false;
-                if (this._blurRatio === 1) {
+                if (this._blurRatio === 1 && this.samples < 2) {
                     this._blurX.outputTexture = this._texture;
                 }
                 else {
@@ -44070,8 +44096,8 @@ var BABYLON;
             this._shareOutputWithPostProcess = postProcess;
             return this;
         };
-        PostProcess.prototype.updateEffect = function (defines, uniforms, samplers, indexParameters) {
-            this._effect = this._engine.createEffect({ vertex: this._vertexUrl, fragment: this._fragmentUrl }, ["position"], uniforms || this._parameters, samplers || this._samplers, defines !== undefined ? defines : "", null, null, null, indexParameters || this._indexParameters);
+        PostProcess.prototype.updateEffect = function (defines, uniforms, samplers, indexParameters, onCompiled, onError) {
+            this._effect = this._engine.createEffect({ vertex: this._vertexUrl, fragment: this._fragmentUrl }, ["position"], uniforms || this._parameters, samplers || this._samplers, defines !== undefined ? defines : "", null, onCompiled, onError, indexParameters || this._indexParameters);
         };
         PostProcess.prototype.isReusable = function () {
             return this._reusable;
@@ -44248,9 +44274,10 @@ var BABYLON;
 (function (BABYLON) {
     var PassPostProcess = (function (_super) {
         __extends(PassPostProcess, _super);
-        function PassPostProcess(name, options, camera, samplingMode, engine, reusable, textureType) {
+        function PassPostProcess(name, options, camera, samplingMode, engine, reusable, textureType, blockCompilation) {
             if (textureType === void 0) { textureType = BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT; }
-            return _super.call(this, name, "pass", null, null, options, camera, samplingMode, engine, reusable, null, textureType) || this;
+            if (blockCompilation === void 0) { blockCompilation = false; }
+            return _super.call(this, name, "pass", null, null, options, camera, samplingMode, engine, reusable, null, textureType, "postprocess", null, blockCompilation) || this;
         }
         return PassPostProcess;
     }(BABYLON.PostProcess));
@@ -66815,6 +66842,40 @@ var BABYLON;
     var TextureTools = (function () {
         function TextureTools() {
         }
+        /**
+         * Uses the GPU to create a copy texture rescaled at a given size
+         * @param texture Texture to copy from
+         * @param width Desired width
+         * @param height Desired height
+         * @return Generated texture
+         */
+        TextureTools.CreateResizedCopy = function (texture, width, height) {
+            var rtt = new BABYLON.RenderTargetTexture('resized' + texture.name, { width: width, height: height }, scene, !texture.noMipmap, true, texture._texture.type, false, texture._samplingMode, false);
+            var scene = texture.getScene();
+            rtt.anisotropicFilteringLevel = texture.anisotropicFilteringLevel;
+            rtt.wrapU = texture.wrapU;
+            rtt.wrapV = texture.wrapV;
+            rtt.uOffset = texture.uOffset;
+            rtt.vOffset = texture.vOffset;
+            rtt.uScale = texture.uScale;
+            rtt.vScale = texture.vScale;
+            rtt.uAng = texture.uAng;
+            rtt.vAng = texture.vAng;
+            rtt.wAng = texture.wAng;
+            rtt.coordinatesIndex = texture.coordinatesIndex;
+            rtt.level = texture.level;
+            rtt.anisotropicFilteringLevel = texture.anisotropicFilteringLevel;
+            var passPostProcess = new BABYLON.PassPostProcess("pass", 1, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT, true);
+            passPostProcess.updateEffect(null, null, null, null, function () {
+                passPostProcess.onApply = function (effect) {
+                    effect.setTexture("textureSampler", texture);
+                };
+                scene.postProcessManager.directRender([passPostProcess], rtt.getInternalTexture());
+                scene.getEngine().restoreDefaultFramebuffer();
+                rtt.disposeFramebufferObjects();
+            });
+            return rtt;
+        };
         return TextureTools;
     }());
     BABYLON.TextureTools = TextureTools;

File diff suppressed because it is too large
+ 4878 - 4868
dist/preview release/babylon.module.d.ts


File diff suppressed because it is too large
+ 35 - 35
dist/preview release/babylon.worker.js


File diff suppressed because it is too large
+ 6551 - 6541
dist/preview release/customConfigurations/minimalViewer/babylon.d.ts


File diff suppressed because it is too large
+ 24 - 24
dist/preview release/customConfigurations/minimalViewer/babylon.js


+ 67 - 6
dist/preview release/customConfigurations/minimalViewer/babylon.max.js

@@ -9972,20 +9972,28 @@ var BABYLON;
             }
             return rgbaData;
         };
-        Engine.prototype._releaseTexture = function (texture) {
+        Engine.prototype._releaseFramebufferObjects = function (texture) {
             var gl = this._gl;
             if (texture._framebuffer) {
                 gl.deleteFramebuffer(texture._framebuffer);
+                texture._framebuffer = null;
             }
             if (texture._depthStencilBuffer) {
                 gl.deleteRenderbuffer(texture._depthStencilBuffer);
+                texture._depthStencilBuffer = null;
             }
             if (texture._MSAAFramebuffer) {
                 gl.deleteFramebuffer(texture._MSAAFramebuffer);
+                texture._MSAAFramebuffer = null;
             }
             if (texture._MSAARenderBuffer) {
                 gl.deleteRenderbuffer(texture._MSAARenderBuffer);
+                texture._MSAARenderBuffer = null;
             }
+        };
+        Engine.prototype._releaseTexture = function (texture) {
+            var gl = this._gl;
+            this._releaseFramebufferObjects(texture);
             gl.deleteTexture(texture);
             // Unbind channels
             this.unbindAllTextures();
@@ -31055,6 +31063,7 @@ var BABYLON;
             _this._currentRefreshId = -1;
             _this._refreshRate = 1;
             _this._samples = 1;
+            scene = _this.getScene();
             _this.name = name;
             _this.isRenderTarget = true;
             _this._size = size;
@@ -31466,12 +31475,29 @@ var BABYLON;
             }
             return serializationObject;
         };
+        // This will remove the attached framebuffer objects. The texture will not be able to be used as render target anymore
+        RenderTargetTexture.prototype.disposeFramebufferObjects = function () {
+            this.getScene().getEngine()._releaseFramebufferObjects(this.getInternalTexture());
+        };
         RenderTargetTexture.prototype.dispose = function () {
             if (this._postProcessManager) {
                 this._postProcessManager.dispose();
                 this._postProcessManager = null;
             }
             this.clearPostProcesses(true);
+            // Remove from custom render targets
+            var scene = this.getScene();
+            var index = scene.customRenderTargets.indexOf(this);
+            if (index >= 0) {
+                scene.customRenderTargets.splice(index, 1);
+            }
+            for (var _i = 0, _a = scene.cameras; _i < _a.length; _i++) {
+                var camera = _a[_i];
+                index = camera.customRenderTargets.indexOf(this);
+                if (index >= 0) {
+                    camera.customRenderTargets.splice(index, 1);
+                }
+            }
             _super.prototype.dispose.call(this);
         };
         return RenderTargetTexture;
@@ -31699,7 +31725,7 @@ var BABYLON;
                 var textureType = engine.getCaps().textureFloatRender ? BABYLON.Engine.TEXTURETYPE_FLOAT : BABYLON.Engine.TEXTURETYPE_HALF_FLOAT;
                 this._blurX = new BABYLON.BlurPostProcess("horizontal blur", new BABYLON.Vector2(1.0, 0), this._blurKernelX, this._blurRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, textureType);
                 this._blurX.autoClear = false;
-                if (this._blurRatio === 1) {
+                if (this._blurRatio === 1 && this.samples < 2) {
                     this._blurX.outputTexture = this._texture;
                 }
                 else {
@@ -32822,8 +32848,8 @@ var BABYLON;
             this._shareOutputWithPostProcess = postProcess;
             return this;
         };
-        PostProcess.prototype.updateEffect = function (defines, uniforms, samplers, indexParameters) {
-            this._effect = this._engine.createEffect({ vertex: this._vertexUrl, fragment: this._fragmentUrl }, ["position"], uniforms || this._parameters, samplers || this._samplers, defines !== undefined ? defines : "", null, null, null, indexParameters || this._indexParameters);
+        PostProcess.prototype.updateEffect = function (defines, uniforms, samplers, indexParameters, onCompiled, onError) {
+            this._effect = this._engine.createEffect({ vertex: this._vertexUrl, fragment: this._fragmentUrl }, ["position"], uniforms || this._parameters, samplers || this._samplers, defines !== undefined ? defines : "", null, onCompiled, onError, indexParameters || this._indexParameters);
         };
         PostProcess.prototype.isReusable = function () {
             return this._reusable;
@@ -33000,9 +33026,10 @@ var BABYLON;
 (function (BABYLON) {
     var PassPostProcess = (function (_super) {
         __extends(PassPostProcess, _super);
-        function PassPostProcess(name, options, camera, samplingMode, engine, reusable, textureType) {
+        function PassPostProcess(name, options, camera, samplingMode, engine, reusable, textureType, blockCompilation) {
             if (textureType === void 0) { textureType = BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT; }
-            return _super.call(this, name, "pass", null, null, options, camera, samplingMode, engine, reusable, null, textureType) || this;
+            if (blockCompilation === void 0) { blockCompilation = false; }
+            return _super.call(this, name, "pass", null, null, options, camera, samplingMode, engine, reusable, null, textureType, "postprocess", null, blockCompilation) || this;
         }
         return PassPostProcess;
     }(BABYLON.PostProcess));
@@ -44241,6 +44268,40 @@ var BABYLON;
     var TextureTools = (function () {
         function TextureTools() {
         }
+        /**
+         * Uses the GPU to create a copy texture rescaled at a given size
+         * @param texture Texture to copy from
+         * @param width Desired width
+         * @param height Desired height
+         * @return Generated texture
+         */
+        TextureTools.CreateResizedCopy = function (texture, width, height) {
+            var rtt = new BABYLON.RenderTargetTexture('resized' + texture.name, { width: width, height: height }, scene, !texture.noMipmap, true, texture._texture.type, false, texture._samplingMode, false);
+            var scene = texture.getScene();
+            rtt.anisotropicFilteringLevel = texture.anisotropicFilteringLevel;
+            rtt.wrapU = texture.wrapU;
+            rtt.wrapV = texture.wrapV;
+            rtt.uOffset = texture.uOffset;
+            rtt.vOffset = texture.vOffset;
+            rtt.uScale = texture.uScale;
+            rtt.vScale = texture.vScale;
+            rtt.uAng = texture.uAng;
+            rtt.vAng = texture.vAng;
+            rtt.wAng = texture.wAng;
+            rtt.coordinatesIndex = texture.coordinatesIndex;
+            rtt.level = texture.level;
+            rtt.anisotropicFilteringLevel = texture.anisotropicFilteringLevel;
+            var passPostProcess = new BABYLON.PassPostProcess("pass", 1, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT, true);
+            passPostProcess.updateEffect(null, null, null, null, function () {
+                passPostProcess.onApply = function (effect) {
+                    effect.setTexture("textureSampler", texture);
+                };
+                scene.postProcessManager.directRender([passPostProcess], rtt.getInternalTexture());
+                scene.getEngine().restoreDefaultFramebuffer();
+                rtt.disposeFramebufferObjects();
+            });
+            return rtt;
+        };
         return TextureTools;
     }());
     BABYLON.TextureTools = TextureTools;

File diff suppressed because it is too large
+ 6551 - 6541
dist/preview release/customConfigurations/minimalViewer/babylon.module.d.ts


+ 1 - 0
dist/preview release/what's new.md

@@ -65,6 +65,7 @@
 - Added glTF 2.0 loader with versioning support ([bghgary](https://github.com/bghgary), thanks to [BeardedGnome](https://github.com/BeardedGnome) for animation updates)
 - New `Motion Blur` effect added into `StandardRenderingPipeline` [Demo](http://www.babylonjs.com/Demos/MotionBlur/) ([Julien Moreau-Mathis](https://github.com/julien-moreau))
 - Allow the BlackAndWhite post process to adjust the degree in subsequent frames, for `Welcome to Wonderland`	types of animation ([jcpalmer](https://github.com/Palmer-JC))
+- New `BABYLON.TextureTools.CreateResizedCopy` function to create a copy of a texture and chage its size ([deltakosh](https://github.com/deltakosh)) 
  
 ### Bug fixes
 - Fixed a bug with spotlight direction ([deltakosh](https://github.com/deltakosh)) 

+ 1 - 1
src/Materials/Textures/babylon.mirrorTexture.ts

@@ -96,7 +96,7 @@ module BABYLON {
                 this._blurX = new BABYLON.BlurPostProcess("horizontal blur", new BABYLON.Vector2(1.0, 0), this._blurKernelX, this._blurRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, textureType);
                 this._blurX.autoClear = false;
 
-                if (this._blurRatio === 1) {
+                if (this._blurRatio === 1 && this.samples < 2) {
                     this._blurX.outputTexture = this._texture;
                 } else {
                     this._blurX.alwaysForcePOT = true;

+ 23 - 1
src/Materials/Textures/babylon.renderTargetTexture.ts

@@ -118,6 +118,7 @@
 
         constructor(name: string, size: any, scene: Scene, generateMipMaps?: boolean, doNotChangeAspectRatio: boolean = true, type: number = Engine.TEXTURETYPE_UNSIGNED_INT, public isCube = false, samplingMode = Texture.TRILINEAR_SAMPLINGMODE, generateDepthBuffer = true, generateStencilBuffer = false, isMulti = false) {
             super(null, scene, !generateMipMaps);
+            scene = this.getScene();
 
             this.name = name;
             this.isRenderTarget = true;
@@ -399,7 +400,7 @@
             scene.resetCachedMaterial();
         }
 
-        renderToTarget(faceIndex: number, currentRenderList: AbstractMesh[], currentRenderListLength:number, useCameraPostProcess: boolean, dumpForDebug: boolean): void {
+        private renderToTarget(faceIndex: number, currentRenderList: AbstractMesh[], currentRenderListLength:number, useCameraPostProcess: boolean, dumpForDebug: boolean): void {
             var scene = this.getScene();
             var engine = scene.getEngine();
 
@@ -537,6 +538,11 @@
             return serializationObject;
         }
 
+        // This will remove the attached framebuffer objects. The texture will not be able to be used as render target anymore
+        public disposeFramebufferObjects(): void {
+            this.getScene().getEngine()._releaseFramebufferObjects(this.getInternalTexture());
+        }
+
         public dispose(): void {
             if (this._postProcessManager) {
                 this._postProcessManager.dispose();
@@ -545,6 +551,22 @@
 
             this.clearPostProcesses(true);
 
+            // Remove from custom render targets
+            var scene = this.getScene();
+            var index = scene.customRenderTargets.indexOf(this);
+
+            if (index >= 0) {
+                scene.customRenderTargets.splice(index, 1);
+            }
+
+            for (var camera of scene.cameras) {
+                index = camera.customRenderTargets.indexOf(this);
+
+                if (index >= 0) {
+                    camera.customRenderTargets.splice(index, 1);
+                }
+            }
+
             super.dispose();
         }
     }

+ 2 - 2
src/PostProcess/babylon.passPostProcess.ts

@@ -1,7 +1,7 @@
 module BABYLON {
     export class PassPostProcess extends PostProcess {    
-        constructor(name: string, options: number | PostProcessOptions, camera: Camera, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT) {
-            super(name, "pass", null, null, options, camera, samplingMode, engine, reusable, null, textureType);
+        constructor(name: string, options: number | PostProcessOptions, camera: Camera, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false) {
+            super(name, "pass", null, null, options, camera, samplingMode, engine, reusable, null, textureType, "postprocess", null, blockCompilation);
         }
     }
 } 

+ 5 - 4
src/PostProcess/babylon.postProcess.ts

@@ -39,7 +39,7 @@
         private _shareOutputWithPostProcess: PostProcess;
         private _texelSize = Vector2.Zero();
         private _forcedOutputTexture: WebGLTexture;
-        
+       
         // Events
 
         /**
@@ -180,15 +180,16 @@
             return this;
         }
         
-        public updateEffect(defines?: string, uniforms?: string[], samplers?: string[], indexParameters?: any) {
+        public updateEffect(defines?: string, uniforms?: string[], samplers?: string[], indexParameters?: any,
+                            onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void) {
             this._effect = this._engine.createEffect({ vertex: this._vertexUrl, fragment: this._fragmentUrl },
                 ["position"],
                 uniforms || this._parameters,
                 samplers || this._samplers, 
                 defines !== undefined ? defines : "",
                 null,
-                null,
-                null,
+                onCompiled,
+                onError,
                 indexParameters || this._indexParameters
                 );
         }

+ 50 - 0
src/Tools/babylon.textureTools.ts

@@ -1,4 +1,54 @@
 module BABYLON {
     export class TextureTools {
+		/**
+		 * Uses the GPU to create a copy texture rescaled at a given size
+		 * @param texture Texture to copy from
+		 * @param width Desired width
+		 * @param height Desired height
+		 * @return Generated texture
+		 */
+		public static CreateResizedCopy(texture: BABYLON.Texture, width: number, height: number): BABYLON.Texture {
+			let rtt = new BABYLON.RenderTargetTexture(
+				'resized' + texture.name,
+				{ width: width, height: height },
+				scene,
+				!texture.noMipmap,
+				true,
+				texture._texture.type,
+				false,
+				texture._samplingMode,
+				false
+			);
+
+            var scene = texture.getScene();
+
+			rtt.anisotropicFilteringLevel = texture.anisotropicFilteringLevel;
+			rtt.wrapU = texture.wrapU;
+			rtt.wrapV = texture.wrapV;
+            rtt.uOffset = texture.uOffset;
+            rtt.vOffset = texture.vOffset;
+            rtt.uScale = texture.uScale;
+            rtt.vScale = texture.vScale;            
+            rtt.uAng = texture.uAng;
+            rtt.vAng = texture.vAng;
+            rtt.wAng = texture.wAng;
+            rtt.coordinatesIndex = texture.coordinatesIndex;
+            rtt.level = texture.level;
+            rtt.anisotropicFilteringLevel = texture.anisotropicFilteringLevel;
+
+            let passPostProcess = new BABYLON.PassPostProcess("pass", 1, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, Engine.TEXTURETYPE_UNSIGNED_INT, true);
+            passPostProcess.updateEffect(null, null, null, null, () => {
+                passPostProcess.onApply = function (effect) {
+                    effect.setTexture("textureSampler", texture);
+                }
+
+                scene.postProcessManager.directRender([passPostProcess], rtt.getInternalTexture());
+
+                scene.getEngine().restoreDefaultFramebuffer();
+                rtt.disposeFramebufferObjects();
+            });
+
+			return rtt;
+		}        
     }
 } 

+ 13 - 3
src/babylon.engine.ts

@@ -3474,24 +3474,34 @@
             return rgbaData;
         }
 
-        public _releaseTexture(texture: WebGLTexture): void {
+        public _releaseFramebufferObjects(texture: WebGLTexture): void {
             var gl = this._gl;
 
             if (texture._framebuffer) {
                 gl.deleteFramebuffer(texture._framebuffer);
-            }
+                texture._framebuffer = null;
+            }            
 
             if (texture._depthStencilBuffer) {
                 gl.deleteRenderbuffer(texture._depthStencilBuffer);
+                texture._depthStencilBuffer = null;
             }
 
             if (texture._MSAAFramebuffer) {
                 gl.deleteFramebuffer(texture._MSAAFramebuffer);
+                texture._MSAAFramebuffer = null;
             }
 
             if (texture._MSAARenderBuffer) {
                 gl.deleteRenderbuffer(texture._MSAARenderBuffer);
-            }
+                texture._MSAARenderBuffer = null;
+            }            
+        }
+
+        public _releaseTexture(texture: WebGLTexture): void {
+            var gl = this._gl;
+
+            this._releaseFramebufferObjects(texture);
 
             gl.deleteTexture(texture);