Example #1
0
        public static SpriteCutterGameObject CreateAs(GameObject origin)
        {
            SpriteCutterGameObject result = new SpriteCutterGameObject();

            result.gameObject = origin;
            return(result);
        }
 public static SpriteCutterGameObject CreateAsInstantiatedCopyOf( GameObject origin, bool secondSide )
 {
     SpriteCutterGameObject result = new SpriteCutterGameObject();
     result.gameObject = GameObject.Instantiate( origin );
     result.gameObject.name = origin.name + ( !secondSide ? "_firstSide" : "_secondSide" );
     return result;
 }
Example #3
0
        public static SpriteCutterGameObject CreateAsInstantiatedCopyOf(GameObject origin, bool secondSide)
        {
            SpriteCutterGameObject result = new SpriteCutterGameObject();

            result.gameObject      = GameObject.Instantiate(origin);
            result.gameObject.name = origin.name + (!secondSide ? "_firstSide" : "_secondSide");
            return(result);
        }
 public static SpriteCutterGameObject CreateNew( GameObject origin, bool secondSide )
 {
     SpriteCutterGameObject result = new SpriteCutterGameObject();
     result.gameObject = new GameObject( origin.name + ( !secondSide ? "_firstSide" : "_secondSide" ) );
     result.CopyGameObjectParametersFrom( origin );
     result.CopyTransformFrom( origin.transform );
     return result;
 }
Example #5
0
        public static SpriteCutterGameObject CreateAsCopyOf(GameObject origin, bool secondSide)
        {
            SpriteCutterGameObject result = new SpriteCutterGameObject();

            result.gameObject = new GameObject(origin.name + (!secondSide ? "_firstSide" : "_secondSide"));
            result.CopyGameObjectParametersFrom(origin);
            result.CopyTransformFrom(origin.transform);
            return(result);
        }
Example #6
0
        static void PrepareResultGameObject(SpriteCutterGameObject resultGameObject, RendererParametersRepresentation tempParameters,
                                            SideCutData cutData)
        {
            resultGameObject.AssignMeshFilter(cutData.cuttedMesh);
            resultGameObject.AssignMeshRendererFrom(tempParameters);

            if (cutData.cuttedCollidersRepresentations != null)
            {
                resultGameObject.BuildCollidersFrom(cutData.cuttedCollidersRepresentations);
            }
        }
Example #7
0
        static void PrepareResultGameObject(SpriteCutterGameObject resultGameObject, RendererParametersRepresentation tempParameters,
                                            Mesh mesh, List <PolygonColliderParametersRepresentation> colliderRepresentations)
        {
            resultGameObject.AssignMeshFilter(mesh);
            resultGameObject.AssignMeshRendererFrom(tempParameters);

            if (colliderRepresentations != null)
            {
                resultGameObject.BuildCollidersFrom(colliderRepresentations);
            }
        }
Example #8
0
        static void PrepareResultGameObject(SpriteCutterGameObject resultGameObject, SpriteRenderer spriteRenderer,
                                            MeshRenderer meshRenderer, SideCutData cutData)
        {
            resultGameObject.AssignMeshFilter(cutData.cuttedMesh);
            if (spriteRenderer != null)
            {
                resultGameObject.AssignMeshRendererFrom(spriteRenderer);
            }
            else
            {
                resultGameObject.AssignMeshRendererFrom(meshRenderer);
            }

            if (cutData.cuttedCollidersRepresentations != null)
            {
                resultGameObject.BuildCollidersFrom(cutData.cuttedCollidersRepresentations);
            }
        }
        static void PrepareResultGameObject( SpriteCutterGameObject resultGameObject, RendererParametersRepresentation tempParameters,
		                                     SideCutData cutData )
        {
            resultGameObject.AssignMeshFilter( cutData.cuttedMesh );
            resultGameObject.AssignMeshRendererFrom( tempParameters );

            if ( cutData.cuttedCollidersRepresentations != null ) {
                resultGameObject.BuildCollidersFrom( cutData.cuttedCollidersRepresentations );
            }
        }
        static void PrepareResultGameObject( SpriteCutterGameObject resultGameObject, SpriteRenderer spriteRenderer,
		                                     MeshRenderer meshRenderer, SideCutData cutData )
        {
            resultGameObject.AssignMeshFilter( cutData.cuttedMesh );
            if ( spriteRenderer != null ) {
                resultGameObject.AssignMeshRendererFrom( spriteRenderer );
            } else {
                resultGameObject.AssignMeshRendererFrom( meshRenderer );
            }

            if ( cutData.cuttedCollidersRepresentations != null ) {
                resultGameObject.BuildCollidersFrom( cutData.cuttedCollidersRepresentations );
            }
        }
Example #11
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 #12
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,
            });
        }
 public static SpriteCutterGameObject CreateAs( GameObject origin )
 {
     SpriteCutterGameObject result = new SpriteCutterGameObject();
     result.gameObject = origin;
     return result;
 }