Example #1
0
        /// <summary>
        /// Returns null, if cutting didn't took place.
        /// </summary>
        public static SpriteCutterOutput Cut(SpriteCutterInput input)
        {
            if (input.gameObject == null)
            {
                Debug.LogWarning("SpriteCutter.Cut exceuted with null gameObject!");
                return(null);
            }

            /* Cutting  mesh and collider */

            Vector3 localLineStart = input.gameObject.transform.InverseTransformPoint(input.lineStart);
            Vector3 localLineEnd   = input.gameObject.transform.InverseTransformPoint(input.lineEnd);

            SpriteRenderer spriteRenderer = input.gameObject.GetComponent <SpriteRenderer>();
            MeshRenderer   meshRenderer   = input.gameObject.GetComponent <MeshRenderer>();

            FlatConvexPolygonMeshCutter.CutResult meshCutResult =
                CutSpriteOrMeshRenderer(localLineStart, localLineEnd, spriteRenderer, meshRenderer);
            if (meshCutResult.DidNotCut())
            {
                return(null);
            }

            FlatConvexCollidersCutter.CutResult collidersCutResults;
            if (input.dontCutColliders)
            {
                collidersCutResults = new FlatConvexCollidersCutter.CutResult();
            }
            else
            {
                collidersCutResults =
                    CutColliders(localLineStart, localLineEnd, input.gameObject.GetComponents <Collider2D>());

                if (collidersCutResults.DidNotCut())
                {
                    return(null);
                }
            }

            SideCutData firstCutData = new SideCutData()
            {
                cuttedMesh = meshCutResult.firstSideMesh,
                cuttedCollidersRepresentations = collidersCutResults.firstSideColliderRepresentations
            };
            SideCutData secondCutData = new SideCutData()
            {
                cuttedMesh = meshCutResult.secondSideMesh,
                cuttedCollidersRepresentations = collidersCutResults.secondSideColliderRepresentations
            };

            /* Second side result - created as new GameObject or instantiated copy of the original GameObject. */

            SpriteCutterGameObject secondSideResult = null;

            switch (input.gameObjectCreationMode)
            {
            case GameObjectCreationMode.CUT_OFF_NEW:
            case GameObjectCreationMode.CUT_INTO_TWO:
                secondSideResult = SpriteCutterGameObject.CreateNew(input.gameObject, true);
                PrepareResultGameObject(secondSideResult, spriteRenderer, meshRenderer, secondCutData);
                break;

            case GameObjectCreationMode.CUT_OFF_COPY:
                secondSideResult = SpriteCutterGameObject.CreateAsInstantiatedCopyOf(input.gameObject, true);
                GameObject.Destroy(secondSideResult.gameObject, .5f);
                SpriteRenderer copiedSpriteRenderer = secondSideResult.gameObject.GetComponent <SpriteRenderer>();
                MeshRenderer   copiedMeshRenderer   = secondSideResult.gameObject.GetComponent <MeshRenderer>();
                if (copiedSpriteRenderer != null)
                {
                    RendererParametersRepresentation tempParameters = new RendererParametersRepresentation();
                    tempParameters.CopyFrom(copiedSpriteRenderer);
                    SafeSpriteRendererRemoverBehaviour.get.RemoveAndWaitOneFrame(copiedSpriteRenderer, onFinish: () => {
                        PrepareResultGameObject(secondSideResult, tempParameters, secondCutData);
                    });
                }
                else
                {
                    PrepareResultGameObject(secondSideResult, copiedSpriteRenderer, copiedMeshRenderer, secondCutData);
                }
                break;
            }


            /* First side result */

            SpriteCutterGameObject firstSideResult = null;

            switch (input.gameObjectCreationMode)
            {
            case GameObjectCreationMode.CUT_OFF_NEW:
            case GameObjectCreationMode.CUT_OFF_COPY:
                firstSideResult = SpriteCutterGameObject.CreateAs(input.gameObject);
                if (spriteRenderer != null)
                {
                    RendererParametersRepresentation tempParameters = new RendererParametersRepresentation();
                    tempParameters.CopyFrom(spriteRenderer);
                    SafeSpriteRendererRemoverBehaviour.get.RemoveAndWaitOneFrame(spriteRenderer, onFinish: () => {
                        PrepareResultGameObject(firstSideResult, tempParameters, firstCutData);
                    });
                }
                else
                {
                    PrepareResultGameObject(firstSideResult, spriteRenderer, meshRenderer, firstCutData);
                }
                break;

            case GameObjectCreationMode.CUT_INTO_TWO:
                firstSideResult = SpriteCutterGameObject.CreateNew(input.gameObject, true);
                PrepareResultGameObject(firstSideResult, spriteRenderer, meshRenderer, firstCutData);
                break;
            }

            return(new SpriteCutterOutput()
            {
                firstSideGameObject = firstSideResult.gameObject,
                secondSideGameObject = secondSideResult.gameObject,
            });
        }
Example #2
0
        /// <summary>
        /// Returns null, if cutting didn't took place.
        /// </summary>
        public static SpriteCutterOutput Cut(SpriteCutterInput input)
        {
            if (input.gameObject == null)
            {
                Debug.LogWarning("SpriteCutter.Cut exceuted with null gameObject!");
                return(null);
            }

            Vector3 localLineStart = input.gameObject.transform.InverseTransformPoint(input.lineStart);
            Vector3 localLineEnd   = input.gameObject.transform.InverseTransformPoint(input.lineEnd);

            SpriteRenderer spriteRenderer = input.gameObject.GetComponent <SpriteRenderer>();
            MeshRenderer   meshRenderer   = input.gameObject.GetComponent <MeshRenderer>();

            FlatConvexPolygonMeshCutter.CutResult meshCutResult =
                CutSpriteOrMeshRenderer(localLineStart, localLineEnd, spriteRenderer, meshRenderer);
            if (meshCutResult.DidNotCut())
            {
                return(null);
            }

            FlatConvexCollidersCutter.CutResult collidersCutResults;
            if (input.dontCutColliders)
            {
                collidersCutResults = new FlatConvexCollidersCutter.CutResult();
            }
            else
            {
                collidersCutResults =
                    CutColliders(localLineStart, localLineEnd, input.gameObject.GetComponents <Collider2D>());

                if (collidersCutResults.DidNotCut())
                {
                    return(null);
                }
            }

            SpriteCutterGameObject secondSideResult = SpriteCutterGameObject.CreateAsCopyOf(input.gameObject, true);

            PrepareResultGameObject(secondSideResult, spriteRenderer, meshRenderer,
                                    meshCutResult.secondSideMesh, collidersCutResults.secondSideColliderRepresentations);

            SpriteCutterGameObject firstSideResult = null;

            //if ( input.gameObjectCreationMode == SpriteCutterInput.GameObjectCreationMode.CUT_INTO_TWO ) {
            //	firstSideResult = SpriteCutterGameObject.CreateAsCopyOf( input.gameObject, true );
            //	PrepareResultGameObject( firstSideResult, spriteRenderer, meshRenderer,
            //	                         meshCutResult.firstSideMesh, collidersCutResults.firstSideColliderRepresentations );

            //} else
            if (input.gameObjectCreationMode == SpriteCutterInput.GameObjectCreationMode.CUT_OFF_ONE)
            {
                firstSideResult = SpriteCutterGameObject.CreateAs(input.gameObject);
                if (spriteRenderer != null)
                {
                    RendererParametersRepresentation tempParameters = new RendererParametersRepresentation();
                    tempParameters.CopyFrom(spriteRenderer);
                    SafeSpriteRendererRemoverBehaviour.get.RemoveAndWaitOneFrame(spriteRenderer, onFinish: () => {
                        PrepareResultGameObject(firstSideResult, tempParameters,
                                                meshCutResult.firstSideMesh, collidersCutResults.firstSideColliderRepresentations);
                    });
                }
                else
                {
                    PrepareResultGameObject(firstSideResult, spriteRenderer, meshRenderer,
                                            meshCutResult.firstSideMesh, collidersCutResults.firstSideColliderRepresentations);
                }
            }

            return(new SpriteCutterOutput()
            {
                firstSideGameObject = firstSideResult.gameObject,
                secondSideGameObject = secondSideResult.gameObject,
            });
        }