Ver código fonte

Text2DModelRenderCache now handles correctly the lifetime of the shared FontTexture, making things working after a dispose called on Text2d.

FontTexture has now two new methods: inc/decCachedFontTextureCounter to extend/reduce the lifetime of the shared FontTexture retrieved with GetCachedFontTexture.
nockawa 9 anos atrás
pai
commit
4f369de31c

+ 2 - 1
src/Canvas2d/babylon.text2d.ts

@@ -72,7 +72,7 @@
             }
 
             if (this.fontTexture) {
-                this.fontTexture.dispose();
+                this.fontTexture.decCachedFontTextureCounter();
                 this.fontTexture = null;
             }
 
@@ -354,6 +354,7 @@
             let engine = this.owner.engine;
 
             renderCache.fontTexture = this.fontTexture;
+            renderCache.fontTexture.incCachedFontTextureCounter();
 
             let vb = new Float32Array(4);
             for (let i = 0; i < 4; i++) {

+ 26 - 0
src/Materials/Textures/babylon.fontTexture.ts

@@ -35,6 +35,7 @@
         private _spaceWidthSuper;
         private _usedCounter = 1;
         private _superSample: boolean;
+        private _cachedFontId: string;
 
         public get isSuperSampled(): boolean {
             return this._superSample;
@@ -64,6 +65,7 @@
             }
 
             ft = new FontTexture(null, fontName, scene, supersample ? 100 : 200, Texture.BILINEAR_SAMPLINGMODE, supersample);
+            ft._cachedFontId = lfn;
             dic.add(lfn, ft);
 
             return ft;
@@ -115,6 +117,7 @@
             this._context = this._canvas.getContext("2d");
             this._context.font = font;
             this._context.fillStyle = "white";
+            this._cachedFontId = null;
 
             var res = this.getFontHeight(font);
             this._lineHeightSuper = res.height;
@@ -323,5 +326,28 @@
         public clone(): FontTexture {
             return null;
         }
+
+        /**
+         * For FontTexture retrieved using GetCachedFontTexture, use this method when you transfer this object's lifetime to another party in order to share this resource.
+         * When the other party is done with this object, decCachedFontTextureCounter must be called.
+         */
+        public incCachedFontTextureCounter() {
+            ++this._usedCounter;
+        }
+
+        /**
+         * Use this method only in conjunction with incCachedFontTextureCounter, call it when you no longer need to use this shared resource.
+         */
+        public decCachedFontTextureCounter() {
+            let s = <any>this.getScene();
+            let dic = <StringDictionary<FontTexture>>s.__fontTextureCache__;
+            if (!dic) {
+                return;
+            }
+            if (--this._usedCounter === 0) {
+                dic.remove(this._cachedFontId);
+                this.dispose();
+            }         
+        }
     }
 }