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

Merge pull request #1248 from nockawa/Sprite2DDataMapping

Fix sprite layout construct failure due to texture not ready
David Catuhe 9 лет назад
Родитель
Сommit
1a7ec05362
2 измененных файлов с 36 добавлено и 16 удалено
  1. 4 2
      src/Canvas2d/babylon.renderablePrim2d.ts
  2. 32 14
      src/Canvas2d/babylon.sprite2d.ts

+ 4 - 2
src/Canvas2d/babylon.renderablePrim2d.ts

@@ -560,7 +560,9 @@
                 pd._partJoinedUsedCategories = joinCat;
                 InstanceClassInfo._CurCategories = joinCat;
                 let obj = this.beforeRefreshForLayoutConstruction(dataPart);
-                this.refreshInstanceDataPart(dataPart);
+                if (!this.refreshInstanceDataPart(dataPart)) {
+                    console.log(`Layout construction for ${Tools.getClassName(this._instanceDataParts[0])} failed because refresh returned false`);
+                }
                 this.afterRefreshForLayoutConstruction(dataPart, obj);
                 this.isVisible = curVisible;
 
@@ -664,7 +666,7 @@
             // For each Instance Data part, refresh it to update the data in the DynamicFloatArray
             for (let part of this._instanceDataParts) {
                 // Check if we need to allocate data elements (hidden prim which becomes visible again)
-                if ((visChanged && !part.dataElements) || rmChanged) {
+                if (visChanged || !part.dataElements || rmChanged) {
                     part.allocElements();
                 }
 

+ 32 - 14
src/Canvas2d/babylon.sprite2d.ts

@@ -373,30 +373,48 @@
 
         private static _prop: Vector3 = Vector3.Zero();
 
+        private static layoutConstructMode = false;
+        protected beforeRefreshForLayoutConstruction(part: InstanceDataBase): any {
+            Sprite2D.layoutConstructMode = true;
+        }
+
+        // if obj contains something, we restore the _text property
+        protected afterRefreshForLayoutConstruction(part: InstanceDataBase, obj: any) {
+            Sprite2D.layoutConstructMode = false;
+        }
+
         protected refreshInstanceDataPart(part: InstanceDataBase): boolean {
             if (!super.refreshInstanceDataPart(part)) {
                 return false;
             }
 
-            if (!this.texture.isReady()) {
+            if (!this.texture.isReady() && !Sprite2D.layoutConstructMode) {
                 return false;
             }
 
             if (part.id === Sprite2D.SPRITE2D_MAINPARTID) {
                 let d = <Sprite2DInstanceData>this._instanceDataParts[0];
-                let ts = this.texture.getBaseSize();
-                let sl = this.spriteLocation;
-                let ss = this.actualSize;
-                d.topLeftUV = new Vector2(sl.x / ts.width, sl.y / ts.height);
-                let suv = new Vector2(ss.width / ts.width, ss.height / ts.height);
-                d.sizeUV = suv;
-
-                Sprite2D._prop.x = this.spriteFrame;
-                Sprite2D._prop.y = this.invertY ? 1 : 0;
-                Sprite2D._prop.z = this.alignToPixel ? 1 : 0;
-                d.properties = Sprite2D._prop;
-
-                d.textureSize = new Vector2(ts.width, ts.height);
+
+                if (Sprite2D.layoutConstructMode) {
+                    d.topLeftUV = Vector2.Zero();
+                    d.sizeUV = Vector2.Zero();
+                    d.properties = Vector3.Zero();
+                    d.textureSize = Vector2.Zero();
+                } else {
+                    let ts = this.texture.getBaseSize();
+                    let sl = this.spriteLocation;
+                    let ss = this.actualSize;
+                    d.topLeftUV = new Vector2(sl.x / ts.width, sl.y / ts.height);
+                    let suv = new Vector2(ss.width / ts.width, ss.height / ts.height);
+                    d.sizeUV = suv;
+
+                    Sprite2D._prop.x = this.spriteFrame;
+                    Sprite2D._prop.y = this.invertY ? 1 : 0;
+                    Sprite2D._prop.z = this.alignToPixel ? 1 : 0;
+                    d.properties = Sprite2D._prop;
+
+                    d.textureSize = new Vector2(ts.width, ts.height);
+                }
             }
             return true;
         }