Selaa lähdekoodia

Merge pull request #1318 from nockawa/master

Intersection/Interaction supported in Cached modes
Loïc Baumann 9 vuotta sitten
vanhempi
commit
5f9e291be9

+ 6 - 0
src/Canvas2d/babylon.canvas2d.ts

@@ -1352,6 +1352,12 @@
                 else {
                     let sprite = new Sprite2D(map, { parent: parent, id: `__cachedSpriteOfGroup__${group.id}`, x: group.actualPosition.x, y: group.actualPosition.y, spriteSize: node.contentSize, spriteLocation: node.pos });
                     sprite.origin = group.origin.clone();
+                    sprite.addExternalData("__cachedGroup__", group);
+                    sprite.pointerEventObservable.add((e, s) => {
+                        if (group.pointerEventObservable !== null) {
+                            group.pointerEventObservable.notifyObservers(e, s.mask);
+                        }
+                    });
                     res.sprite = sprite;
                 }
             }

+ 14 - 7
src/Canvas2d/babylon.group2d.ts

@@ -119,7 +119,7 @@
         }
 
         static _createCachedCanvasGroup(owner: Canvas2D): Group2D {
-            var g = new Group2D({ parent: owner, id: "__cachedCanvasGroup__", position: Vector2.Zero(), origin: Vector2.Zero(), size: null, isVisible: true });
+            var g = new Group2D({ parent: owner, id: "__cachedCanvasGroup__", position: Vector2.Zero(), origin: Vector2.Zero(), size: null, isVisible: true, isPickable: false });
             return g;
 
         }
@@ -837,23 +837,30 @@
         protected handleGroupChanged(prop: Prim2DPropInfo) {
             // This method is only for cachedGroup
             let rd = this._renderableData;
+            if (!rd) {
+                return;
+            }
 
-            if (!this.isCachedGroup || !rd._cacheRenderSprite) {
+            let cachedSprite = rd._cacheRenderSprite;
+            if (!this.isCachedGroup || !cachedSprite) {
                 return;
             }
 
             // For now we only support these property changes
             // TODO: add more! :)
             if (prop.id === Prim2DBase.actualPositionProperty.id) {
-                rd._cacheRenderSprite.actualPosition = this.actualPosition.clone();
+                cachedSprite.actualPosition = this.actualPosition.clone();
+                if (cachedSprite.position != null) {
+                    cachedSprite.position = cachedSprite.actualPosition.clone();
+                }
             } else if (prop.id === Prim2DBase.rotationProperty.id) {
-                rd._cacheRenderSprite.rotation = this.rotation;
+                cachedSprite.rotation = this.rotation;
             } else if (prop.id === Prim2DBase.scaleProperty.id) {
-                rd._cacheRenderSprite.scale = this.scale;
+                cachedSprite.scale = this.scale;
             } else if (prop.id === Prim2DBase.originProperty.id) {
-                rd._cacheRenderSprite.origin = this.origin.clone();
+                cachedSprite.origin = this.origin.clone();
             } else if (prop.id === Group2D.actualSizeProperty.id) {
-                rd._cacheRenderSprite.size = this.actualSize.clone();
+                cachedSprite.size = this.actualSize.clone();
                 //console.log(`[${this._globalTransformProcessStep}] Sync Sprite ${this.id}, width: ${this.actualSize.width}, height: ${this.actualSize.height}`);
             }
         }

+ 20 - 0
src/Canvas2d/babylon.prim2dBase.ts

@@ -2237,6 +2237,7 @@
          * Get the global transformation matrix of the primitive
          */
         public get globalTransform(): Matrix {
+            this._updateLocalTransform();
             return this._globalTransform;
         }
 
@@ -2262,6 +2263,7 @@
          * Get invert of the global transformation matrix of the primitive
          */
         public get invGlobalTransform(): Matrix {
+            this._updateLocalTransform();
             return this._invGlobalTransform;
         }
 
@@ -2409,6 +2411,24 @@
                 return false;
             }
 
+            let id = this.id;
+            if (id!=null && id.indexOf("__cachedSpriteOfGroup__") === 0) {
+                let ownerGroup = this.getExternalData<Group2D>("__cachedGroup__");
+                return ownerGroup.intersect(intersectInfo);
+            }
+
+            // If we're testing a cachedGroup, we must reject pointer outside its levelBoundingInfo because children primitives could be partially clipped outside so we must not accept them as intersected when it's the case (because they're not visually visible).
+            let isIntersectionTest = false;
+            if (this instanceof Group2D) {
+                let g = <Group2D><any>this;
+                isIntersectionTest = g.isCachedGroup;
+            }
+            if (isIntersectionTest && !this.levelBoundingInfo.doesIntersect(intersectInfo._localPickPosition)) {
+                // Important to call this before each return to allow a good recursion next time this intersectInfo is reused
+                intersectInfo._exit(firstLevel);
+                return false;
+            }
+
             // Fast rejection test with boundingInfo
             if (this.isPickable && !this.boundingInfo.doesIntersect(intersectInfo._localPickPosition)) {
                 // Important to call this before each return to allow a good recursion next time this intersectInfo is reused

+ 1 - 1
src/Canvas2d/babylon.renderablePrim2d.ts

@@ -434,7 +434,7 @@
                     usedCount += td._partData.usedElementCount;
                 }
 
-                if (usedCount === 0) {
+                if (usedCount === 0 && gii.modelRenderCache!=null) {
                     this.renderGroup._renderableData._renderGroupInstancesInfo.remove(gii.modelRenderCache.modelKey);
                     gii.dispose();
                 }