public static Mesh Triangulate(Polygon2D polygon, Vector2 UVScale, Vector2 UVOffset, Triangulation triangulation) { polygon.Normalize(); Mesh result = null; switch (triangulation) { case Triangulation.Advanced: Slicer2DProfiler.IncAdvancedTriangulation(); float GC = Slicer2DSettings.GetGarbageCollector(); if (GC > 0 & polygon.GetArea() < GC) { Debug.LogWarning("SmartUtilities2D: Garbage Collector Removed Object Because it was too small"); return(null); } Polygon2D newPolygon = new Polygon2D(PreparePolygon.Get(polygon)); if (newPolygon.pointsList.Count < 3) { Debug.LogWarning("SmartUtilities2D: Mesh is too small for advanced triangulation, using simplified triangulations instead (size: " + polygon.GetArea() + ")"); result = PerformTriangulation(polygon, UVScale, UVOffset); return(result); } foreach (Polygon2D hole in polygon.holesList) { newPolygon.AddHole(new Polygon2D(PreparePolygon.Get(hole, -1))); } result = PerformTriangulation(newPolygon, UVScale, UVOffset); break; case Triangulation.Legacy: Slicer2DProfiler.IncLegacyTriangulation(); List <Vector2> list = new List <Vector2>(); foreach (Vector2D p in polygon.pointsList) { list.Add(p.ToVector2()); } result = Triangulator.Create(list.ToArray(), UVScale, UVOffset); return(result); } return(result); }
public void SetColor(Color color) { if (material != null) { if (Slicer2DSettings.GetRenderingPipeline() == Slicer2DSettings.RenderingPipeline.Universal) { material.SetColor("_Emission", color); } else { material.color = color; } } }
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(); } }
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(); } }
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)); if (newResult.slices.Count > 0) { foreach (List <Vector2D> i in newResult.slices) { result.AddSlice(i); } } if (newResult.GetPolygons().Count > 0) { foreach (Polygon2D poly in newResult.GetPolygons()) { result.AddPolygon(poly); } result.RemovePolygon(p); } } tries += 1; if (tries > 20) { return(result); } } return(result); }
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); } } }
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); 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; } 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); } }
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); }
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.slices.Count > 0) { foreach (List <Vector2D> i in newResult.slices) { result.AddSlice(i); } } if (newResult.GetPolygons().Count > 0) { foreach (Polygon2D poly in newResult.GetPolygons()) { result.AddPolygon(poly); } result.RemovePolygon(p); } } tries += 1; if (tries > 20) { return(result); } } return(result); }
public PolygonTriangulator2D.Triangulation GetTriangulation() { return(Slicer2DSettings.GetTriangulation(triangulation)); }
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; int name_id = 1; foreach (Polygon2D id in result) { GameObject gObject = null; switch (Slicer2DSettings.GetComponentsCopy(instantiateMethod)) { case InstantiationMethod.Performance: Slicer2DProfiler.IncSlicesCreatedWithPerformance(); switch (textureType) { case TextureType.Sprite: 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); } gObject = Instantiate(gameObject); break; case InstantiationMethod.Quality: Slicer2DProfiler.IncSlicesCreatedWithQuality(); gObject = new GameObject(); Slicer2DComponents.Copy(this, gObject); break; } resultGameObjects.Add(gObject); Slicer2D slicer = gObject.GetComponent <Slicer2D> (); slicer.limit.counter += 1; slicer.limit.maxSlices = limit.maxSlices; slicer.eventHandler = new Slicer2DEventHandling(); slicer.shape = new Slicer2DShape(); slicer.shape.SetSlicer2D(slicer); slicer.shape.ForceUpdate(); gObject.name = name + " (" + name_id + ")"; gObject.transform.parent = transform.parent; gObject.transform.position = transform.position; gObject.transform.rotation = transform.rotation; gObject.transform.localScale = transform.localScale; gObject.layer = gameObject.layer; gObject.tag = gameObject.tag; Slicer2DComponents.CopyRigidbody2D(originalRigidBody, slicer, id, originArea); if (centerOfMass == CenterOfMass.TransformOnly) { Polygon2D localPoly = id.ToLocalSpace(gObject.transform); Rect bounds = localPoly.GetBounds(); Vector2 centerOffset = new Vector2(bounds.center.x * transform.lossyScale.x, bounds.center.y * transform.lossyScale.y); gObject.transform.Translate(centerOffset, 0); } 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; switch (textureType) { case TextureType.Sprite: slicer.spriteRenderer = spriteRenderer; Polygon2D.SpriteToMesh(gObject, spriteRenderer, materialSettings.GetTriangulation()); 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, 1, new Vector2(1, 1), Vector2.zero, materialSettings.GetTriangulation()); meshRenderer = GetComponent <MeshRenderer> (); meshRenderer.sharedMaterial = materialSettings.material; meshFilter = GetComponent <MeshFilter>(); 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.SpriteAnimation: if (spriteRenderer == null) { spriteRenderer = new VirtualSpriteRenderer(GetComponent <SpriteRenderer>()); } break; default: break; } }