Exemple #1
0
        static public Slice2D Explode(Polygon2D polygon, int explosionSlices = 0)
        {
            if (explosionSlices < 1)
            {
                explosionSlices = Slicer2DSettings.GetExplosionSlices();
            }

            Slice2D result = Slice2D.Create(null, Slice2DType.Explode);

            Rect polyRect = polygon.GetBounds();

            result.AddPolygon(polygon);

            int tries = 0;

            while (result.GetPolygons().Count < explosionSlices)
            {
                foreach (Polygon2D p in new List <Polygon2D>(result.GetPolygons()))
                {
                    Slice2D newResult = SliceFromPoint(p, new Vector2D(polyRect.x + UnityEngine.Random.Range(0, polyRect.width), polyRect.y + UnityEngine.Random.Range(0, polyRect.height)), UnityEngine.Random.Range(0, Mathf.PI * 2));

                    /*
                     * Polygon2D smallest = Polygon2DList.GetSmallest(newResult.GetPolygons());
                     * if (smallest != null) {
                     *      Debug.Log(smallest.GetArea());
                     *
                     *      if (smallest.GetArea() < 1) {
                     *              continue;
                     *      }
                     * }*/

                    if (newResult.GetPolygons().Count > 0)
                    {
                        if (newResult.slices.Count > 0)
                        {
                            foreach (List <Vector2D> i in newResult.slices)
                            {
                                result.AddSlice(i);
                            }
                        }

                        foreach (Polygon2D poly in newResult.GetPolygons())
                        {
                            result.AddPolygon(poly);
                        }
                        result.RemovePolygon(p);
                    }
                }

                tries += 1;
                if (tries > 20)
                {
                    return(result);
                }
            }

            return(result);
        }
        void Preferences()
        {
            Slicer2DSettingsProfile profile = Slicer2DSettings.GetProfile();

            if (profile)
            {
                profile.renderingPipeline = (Slicer2DSettings.RenderingPipeline)EditorGUILayout.EnumPopup("Rendering Pipeline", profile.renderingPipeline);

                profile.garbageCollector = EditorGUILayout.Toggle("Garbage Collector", profile.garbageCollector);
                if (profile.garbageCollector)
                {
                    profile.garbageCollectorSize = EditorGUILayout.FloatField("Garbage Collector Size", profile.garbageCollectorSize);
                }

                profile.explosionPieces = (int)EditorGUILayout.Slider("Explosion Pieces", profile.explosionPieces, 1, 30);

                profile.componentsCopy = (Slicer2DSettings.InstantiationMethod)EditorGUILayout.EnumPopup("Instatiation Method", profile.componentsCopy);

                profile.triangulation = (Slicer2DSettings.Triangulation)EditorGUILayout.EnumPopup("Triangulation", profile.triangulation);

                profile.batching = (Slicer2DSettings.Batching)EditorGUILayout.EnumPopup("Batching", profile.batching);

                profile.centerOfSliceTransform = (Slicer2DSettings.CenterOfSliceTransform)EditorGUILayout.EnumPopup("Center Of Slice", profile.centerOfSliceTransform);

                if (GUILayout.Button("Default Settings"))
                {
                    profile.garbageCollector     = true;
                    profile.garbageCollectorSize = 0.005f;

                    profile.componentsCopy = Slicer2DSettings.InstantiationMethod.Default;
                    profile.triangulation  = Slicer2DSettings.Triangulation.Default;
                    profile.batching       = Slicer2DSettings.Batching.Default;
                }

                if (GUI.changed && EditorApplication.isPlaying == false)
                {
                    EditorUtility.SetDirty(profile);
                }

                EditorGUILayout.HelpBox("Settings marked as 'default' will use local component setting", MessageType.Info);

                EditorGUILayout.HelpBox("Garbage Collector: When enabled, very small unuseful slices are removed", MessageType.None);
                EditorGUILayout.HelpBox("Instatiation Method: Performance mode would increase performance about 25%, however cannot be used in certain cases", MessageType.None);
                EditorGUILayout.HelpBox("Triangulation: The more reliable triangulation method, the slower most likely it performs. Simple shapes could use less complicated triangulation", MessageType.None);
                EditorGUILayout.HelpBox("Batching: when enabled, sliced parts of the object will use same material instance as it's origin (Improves Performance)", MessageType.None);
            }
            else
            {
                EditorGUILayout.HelpBox("Slicer2D Settings Profile Not Found!", MessageType.Error);
            }
        }
 public void SetColor(Color color)
 {
     if (material != null)
     {
         if (Slicer2DSettings.GetRenderingPipeline() == Slicer2DSettings.RenderingPipeline.Universal)
         {
             material.SetColor("_Emission", color);
         }
         else
         {
             material.color = color;
         }
     }
 }
        public void SetTriangulation(Slicer2D script)
        {
            Slicer2DSettingsProfile profile = Slicer2DSettings.GetProfile();

            if (profile == null || profile.triangulation == Slicer2DSettings.Triangulation.Default)
            {
                script.materialSettings.triangulation = (PolygonTriangulator2D.Triangulation)EditorGUILayout.EnumPopup("Triangulation", script.materialSettings.triangulation);
            }
            else
            {
                EditorGUI.BeginDisabledGroup(true);
                EditorGUILayout.EnumPopup("Triangulation", Slicer2DSettings.GetTriangulation(script.materialSettings.triangulation));
                EditorGUI.EndDisabledGroup();
            }
        }
        public void SetBatching(Slicer2D script)
        {
            Slicer2DSettingsProfile profile = Slicer2DSettings.GetProfile();

            if (profile == null || profile.batching == Slicer2DSettings.Batching.Default)
            {
                script.materialSettings.batchMaterial = EditorGUILayout.Toggle("Batch Material", script.materialSettings.batchMaterial);
            }
            else
            {
                EditorGUI.BeginDisabledGroup(true);
                EditorGUILayout.Toggle("Batch Material", Slicer2DSettings.GetBatching(script.materialSettings.batchMaterial));
                EditorGUI.EndDisabledGroup();
            }
        }
        public void SetComponentsCopy(Slicer2D script)
        {
            Slicer2DSettingsProfile profile = Slicer2DSettings.GetProfile();

            if (profile == null || profile.componentsCopy == Slicer2DSettings.InstantiationMethod.Default)
            {
                script.instantiateMethod = (Slicer2D.InstantiationMethod)EditorGUILayout.EnumPopup("Instantiation Method", script.instantiateMethod);
            }
            else
            {
                EditorGUI.BeginDisabledGroup(true);
                EditorGUILayout.EnumPopup("Instantiation", Slicer2DSettings.GetComponentsCopy(script.instantiateMethod));
                EditorGUI.EndDisabledGroup();
            }
        }
Exemple #7
0
        public void CreateMesh(GameObject gameObject, Polygon2D polygon)
        {
            polygon.CreateMesh(gameObject, scale, offset, GetTriangulation());

            if (material)
            {
                MeshRenderer meshRenderer = gameObject.GetComponent <MeshRenderer> ();
                if (Slicer2DSettings.GetBatching(batchMaterial))
                {
                    Slicer2DProfiler.IncBatchingApplied();
                    meshRenderer.sharedMaterial = material;
                }
                else
                {
                    meshRenderer.sharedMaterial = new Material(material);
                }
            }
        }
Exemple #8
0
        static private SmartMaterial GetVertexLit()
        {
            if (vertexLit == null || vertexLit.material == null)
            {
                if (Slicer2DSettings.GetRenderingPipeline() == Slicer2DSettings.RenderingPipeline.Universal)
                {
                    vertexLit = new SmartMaterial("Legacy Shaders/Transparent/VertexLit");
                }
                else
                {
                    vertexLit = new SmartMaterial("Sprites/Default");
                }

                if (vertexLit != null)
                {
                    vertexLit.SetTexture(Resources.Load("Textures/LineTexture16") as Texture);
                }
            }
            return(vertexLit);
        }
Exemple #9
0
 public PolygonTriangulator2D.Triangulation GetTriangulation()
 {
     return(Slicer2DSettings.GetTriangulation(triangulation));
 }
Exemple #10
0
        static public Slice2D ExplodeByPoint(Polygon2D polygon, Vector2D point, int explosionSlices = 0)
        {
            if (explosionSlices < 1)
            {
                explosionSlices = Slicer2DSettings.GetExplosionSlices();
            }

            Slice2D result = Slice2D.Create(null, point);

            if (polygon.PointInPoly(point) == false)
            {
                return(result);
            }

            float sliceRot = UnityEngine.Random.Range(0, 360);

            Polygon2D hole = Polygon2D.Create(Polygon2D.PolygonType.Rectangle, 0.1f);

            hole = hole.ToOffset(point);
            polygon.AddHole(hole);

            result.AddPolygon(polygon);

            int tries = 0;

            while (result.GetPolygons().Count < explosionSlices)
            {
                foreach (Polygon2D p in new List <Polygon2D>(result.GetPolygons()))
                {
                    sliceRot += UnityEngine.Random.Range(15, 45) * Mathf.Deg2Rad;

                    Vector2D v = new Vector2D(point);
                    v.Push(sliceRot, 0.4f);

                    Slice2D newResult = SliceFromPoint(p, v, sliceRot);

                    if (newResult.GetPolygons().Count > 0)
                    {
                        if (newResult.slices.Count > 0)
                        {
                            foreach (List <Vector2D> i in newResult.slices)
                            {
                                result.AddSlice(i);
                            }
                        }

                        foreach (Polygon2D poly in newResult.GetPolygons())
                        {
                            result.AddPolygon(poly);
                        }
                        result.RemovePolygon(p);
                    }
                }

                tries += 1;
                if (tries > 20)
                {
                    return(result);
                }
            }

            return(result);
        }
        public List <GameObject> PerformResult(List <Polygon2D> result, Slice2D slice)
        {
            List <GameObject> resultGameObjects = new List <GameObject> ();

            if (result.Count < 1)
            {
                return(resultGameObjects);
            }

            slice.SetPolygons(result);

            slice.originGameObject = gameObject;

            if (eventHandler.SliceEvent(slice) == false)
            {
                return(resultGameObjects);
            }

            if (Slicer2DEventHandling.GlobalSliceEvent(slice) == false)
            {
                return(resultGameObjects);
            }

            double originArea = 1f;

            if (recalculateMass)
            {
                originArea = shape.GetLocal().GetArea();
            }

            Rigidbody2D originalRigidBody = gameObject.GetComponent <Rigidbody2D>();

            Collider2D        collider2D = gameObject.GetComponent <Collider2D>();
            PhysicsMaterial2D material   = collider2D.sharedMaterial;
            bool isTrigger = collider2D.isTrigger;

            switch (Slicer2DSettings.GetComponentsCopy(instantiateMethod))
            {
            case InstantiationMethod.Performance:

                switch (textureType)
                {
                case TextureType.Sprite:
                case TextureType.Sprite3D:
                    if (spriteRendererComponent)
                    {
                        DestroyImmediate(spriteRendererComponent);
                        spriteRendererComponent = null;
                    }
                    break;
                }

                BoxCollider2D c1 = gameObject.GetComponent <BoxCollider2D>();
                if (c1 != null)
                {
                    DestroyImmediate(c1);
                }

                CircleCollider2D c2 = gameObject.GetComponent <CircleCollider2D>();
                if (c2 != null)
                {
                    DestroyImmediate(c2);
                }

                CapsuleCollider2D c3 = gameObject.GetComponent <CapsuleCollider2D>();
                if (c3 != null)
                {
                    DestroyImmediate(c3);
                }
                break;
            }

            int name_id = 1;

            foreach (Polygon2D id in result)
            {
                GameObject gObject = null;

                switch (Slicer2DSettings.GetComponentsCopy(instantiateMethod))
                {
                case InstantiationMethod.Performance:
                    Slicer2DProfiler.IncSlicesCreatedWithPerformance();

                    gObject = Instantiate(gameObject, transform.position, transform.rotation, transform.parent);

                    break;

                case InstantiationMethod.Quality:
                    Slicer2DProfiler.IncSlicesCreatedWithQuality();

                    gObject = new GameObject();
                    gObject.transform.parent   = transform.parent;
                    gObject.transform.position = transform.position;
                    gObject.transform.rotation = transform.rotation;

                    break;
                }

                gObject.name = name + " (" + name_id + ")";
                gObject.transform.localScale = transform.localScale;

                gObject.layer = gameObject.layer;
                gObject.tag   = gameObject.tag;

                resultGameObjects.Add(gObject);

                switch (Slicer2DSettings.GetComponentsCopy(instantiateMethod))
                {
                case InstantiationMethod.Quality:
                    Slicer2DComponents.Copy(this, gObject);
                    break;
                }

                Slicer2D slicer = gObject.GetComponent <Slicer2D> ();

                //ここがPlay時に情報を書き込んでいるところ
                slicer.limit           = new Slicer2DLimit();
                slicer.limit.counter   = limit.counter + 1;
                slicer.limit.maxSlices = limit.maxSlices;
                slicer.limit.enabled   = limit.enabled;


                slicer.eventHandler = new Slicer2DEventHandling();

                slicer.shape = new Slicer2DShape();
                slicer.shape.SetSlicer2D(slicer);
                slicer.shape.ForceUpdate();

                slicer.materialSettings = materialSettings.Copy();

                slicer.anchor = anchor.Copy();

                Slicer2DComponents.CopyRigidbody2D(originalRigidBody, slicer, id, originArea);

                if (Slicer2DSettings.GetCenterOfSliceTransform(centerOfSlice) == CenterOfSliceTransform.ColliderCenter)
                {
                    Polygon2D localPoly = id.ToLocalSpace(gObject.transform);
                    Rect      bounds    = localPoly.GetBounds();
                    Vector2   center    = bounds.center;

                    Vector2 centerWorld = new Vector2(center.x * transform.lossyScale.x, center.y * transform.lossyScale.y);
                    gObject.transform.Translate(centerWorld);

                    slicer.materialSettings.offset += center;
                }

                Collider2D collider = null;
                switch (colliderType)
                {
                case ColliderType.PolygonCollider2D:
                    collider = (Collider2D)id.ToLocalSpace(gObject.transform).CreatePolygonCollider(gObject);
                    break;

                case ColliderType.EdgeCollider2D:
                    collider = (Collider2D)id.ToLocalSpace(gObject.transform).CreateEdgeCollider(gObject);
                    break;
                }

                collider.sharedMaterial = material;
                collider.isTrigger      = isTrigger;

                Vector2D uvOffset;

                switch (textureType)
                {
                case TextureType.Sprite:
                    slicer.spriteRenderer = spriteRenderer;
                    uvOffset = new Vector2D(slicer.materialSettings.offset);
                    Polygon2D.SpriteToMesh(gObject, spriteRenderer, materialSettings.GetTriangulation(), uvOffset);
                    break;

                case TextureType.Sprite3D:
                    slicer.spriteRenderer = spriteRenderer;
                    uvOffset = new Vector2D(slicer.materialSettings.offset);
                    Polygon2D.SpriteToMesh3D(gObject, spriteRenderer, materialSettings.depth, materialSettings.GetTriangulation(), uvOffset);

                    MeshRenderer meshRenderer = slicer.GetComponent <MeshRenderer>();

                    Material[] sharedMaterials = new Material[2];
                    sharedMaterials[1] = spriteRenderer.material;
                    sharedMaterials[0] = materialSettings.sideMaterial;

                    meshRenderer.sharedMaterials = sharedMaterials;

                    break;

                case TextureType.SpriteAnimation:
                    slicer.textureType = TextureType.Sprite;
                    Polygon2D.SpriteToMesh(gObject, spriteRenderer, materialSettings.GetTriangulation());
                    break;

                default:
                    break;
                }

                name_id += 1;
            }

            if (afterSliceRemoveOrigin)
            {
                Destroy(gameObject);
            }

            if (resultGameObjects.Count > 0)
            {
                slice.originGameObject = gameObject;

                slice.SetGameObjects(resultGameObjects);

                if (supportJoints == true)
                {
                    SliceJointEvent(slice);
                }

                eventHandler.Result(slice);
            }

            return(resultGameObjects);
        }
        // Check Before Each Function - Then This Could Be Private
        public void Initialize()
        {
            shape.ForceUpdate();

            List <Polygon2D> result = Polygon2DList.CreateFromGameObject(gameObject);

            // Split collider if there are more polygons than 1
            if (result.Count > 1)
            {
                PerformResult(result, new Slice2D());
            }

            switch (textureType)
            {
            case TextureType.Mesh2D:
                if (Slicer2DSettings.GetBatching(materialSettings.batchMaterial) && spriteRenderer != null)
                {
                    materialSettings.material = spriteRenderer.material;
                }

                // Needs Mesh UV Options
                materialSettings.CreateMesh(gameObject, shape.GetLocal());

                meshRenderer = GetComponent <MeshRenderer> ();

                meshFilter = GetComponent <MeshFilter>();

                break;

            case TextureType.Mesh3D:
                shape.GetLocal().CreateMesh3D(gameObject, materialSettings.depth, materialSettings.scale, materialSettings.offset, 0, materialSettings.GetTriangulation());

                meshRenderer = GetComponent <MeshRenderer> ();
                meshFilter   = GetComponent <MeshFilter>();

                Material[] sharedMaterials = new Material[2];
                sharedMaterials[1] = materialSettings.material;
                sharedMaterials[0] = materialSettings.sideMaterial;

                meshRenderer.sharedMaterials = sharedMaterials;

                break;

            case TextureType.Sprite:
                if (spriteRenderer == null)
                {
                    spriteRendererComponent = GetComponent <SpriteRenderer>();

                    spriteRenderer = new VirtualSpriteRenderer(spriteRendererComponent);
                }
                else
                {
                    if (Slicer2DSettings.GetBatching(materialSettings.batchMaterial) == false)
                    {
                        spriteRenderer.material = new Material(spriteRenderer.material);
                    }
                }
                break;

            case TextureType.Sprite3D:
                if (spriteRenderer == null)
                {
                    spriteRendererComponent = GetComponent <SpriteRenderer>();

                    spriteRenderer = new VirtualSpriteRenderer(spriteRendererComponent);

                    reinitialize = true;

                    Destroy(spriteRendererComponent);
                    spriteRendererComponent = null;
                }
                else
                {
                    if (Slicer2DSettings.GetBatching(materialSettings.batchMaterial) == false)
                    {
                        spriteRenderer.material = new Material(spriteRenderer.material);
                    }
                }
                break;

            case TextureType.SpriteAnimation:
                if (spriteRenderer == null)
                {
                    spriteRenderer = new VirtualSpriteRenderer(GetComponent <SpriteRenderer>());
                }
                break;

            default:
                break;
            }
        }