Esempio n. 1
0
        public override List <MeshObject> GetMeshes()
        {
            if (Meshes == null)
            {
                List <Polygon2> polygons = GetPolygonsLocal();

                if (polygons.Count > 0)
                {
                    Meshes = new List <MeshObject>();

                    foreach (Polygon2 poly in polygons)
                    {
                        if (poly.points.Length < 3)
                        {
                            continue;
                        }

                        Mesh mesh = PolygonTriangulator2.Triangulate(poly, Vector2.zero, Vector2.zero, PolygonTriangulator2.Triangulation.Advanced);

                        if (mesh)
                        {
                            MeshObject meshObject = MeshObject.Get(mesh);

                            if (meshObject != null)
                            {
                                Meshes.Add(meshObject);
                            }
                        }
                    }
                }
            }
            return(Meshes);
        }
        public void GenerateFill(Transform transform, DayLightColliderShape shape, float height)
        {
            List <Polygon2> polys     = shape.GetPolygonsLocal();
            float           direction = Lighting2D.DayLightingSettings.direction * Mathf.Deg2Rad;

            foreach (Polygon2 polygon in polys)
            {
                if (polygon.points.Length < 2)
                {
                    continue;
                }

                Polygon2 worldPolygon = polygon.Copy();

                worldPolygon.ToScaleSelf(transform.localScale);
                worldPolygon.ToRotationSelf(transform.rotation.eulerAngles.z * Mathf.Deg2Rad);

                Polygon2           polygonShadow = GenerateShadow(worldPolygon, direction, height);
                List <DoublePair2> polygonPairs  = DoublePair2.GetList(polygonShadow.points);

                Polygon2 polygonShadowFill = polygonShadow.Copy();

                int pointsCount = polygonShadowFill.points.Length;

                for (int i = 0; i < pointsCount; i++)
                {
                    Vector2 a = polygonShadowFill.points[(i - 1 + pointsCount) % pointsCount];
                    Vector2 b = polygonShadowFill.points[i];
                    Vector2 c = polygonShadowFill.points[(i + 1) % pointsCount];

                    float angle_a = (float)System.Math.Atan2(a.y - b.y, a.x - b.x) + pi2;
                    float angle_c = (float)System.Math.Atan2(b.y - c.y, b.x - c.x) + pi2;

                    b.x += Mathf.Cos(angle_a) * 0.001f;
                    b.y += Mathf.Sin(angle_a) * 0.001f;

                    b.x += Mathf.Cos(angle_c) * 0.001f;
                    b.y += Mathf.Sin(angle_c) * 0.001f;

                    polygonShadowFill.points[i] = b;
                }

                polygonsPairs.Add(polygonPairs);

                Mesh mesh = PolygonTriangulator2.TriangulateSimple(polygonShadowFill, Vector2.zero, Vector2.zero);

                MeshObject meshObject = MeshObject.Get(mesh);

                if (meshObject != null)
                {
                    meshes.Add(meshObject);
                }
            }
        }
Esempio n. 3
0
 public MeshObject GetMesh()
 {
     if (shapeMeshObject == null)
     {
         if (polygons.Count > 0)
         {
             shapeMeshObject = MeshObject.Get(polygons[0].CreateMesh(Vector2.zero, Vector2.zero));
         }
     }
     return(shapeMeshObject);
 }
Esempio n. 4
0
    public MeshObject GetMesh(SpriteRenderer spriteRenderer)
    {
        if (cacheSize.Equals(spriteRenderer.size) == false || cacheSprite.Equals(spriteRenderer.sprite) == false)
        {
            cacheMesh       = Generate(spriteRenderer);
            cacheMeshObject = MeshObject.Get(cacheMesh);

            cacheSize   = spriteRenderer.size;
            cacheSprite = spriteRenderer.sprite;
        }

        return(cacheMeshObject);
    }
Esempio n. 5
0
        public static MeshObject GetStaticMesh()
        {
            if (meshObject == null)
            {
                Mesh mesh = new Mesh();

                float x = 0.5f;
                float y = 0.5f;

                mesh.vertices  = new Vector3[] { new Vector2(0, y), new Vector2(x, y / 2), new Vector2(0, 0), new Vector2(-x, y / 2) };
                mesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 };
                mesh.uv        = new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) };

                meshObject = MeshObject.Get(mesh);
            }
            return(meshObject);
        }
Esempio n. 6
0
        public static MeshObject GetStaticMesh()
        {
            if (meshObject == null)
            {
                Mesh mesh = new Mesh();

                float x = 0.5f;
                float y = 0.5f;

                float yOffset = -0.25f;
                mesh.vertices  = new Vector3[] { new Vector2(0, y * 1.5f + yOffset), new Vector2(x, y + yOffset), new Vector2(0, -y * 0.5f + yOffset), new Vector2(-x, y + yOffset), new Vector2(-x, 0 + yOffset), new Vector2(x, 0 + yOffset) };
                mesh.triangles = new int[] { 0, 1, 5, 4, 3, 0, 0, 5, 2, 0, 2, 4 };
                mesh.uv        = new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 1), new Vector2(0, 1) };


                meshObject = MeshObject.Get(mesh);
            }
            return(meshObject);
        }
        public override List <MeshObject> GetMeshes()
        {
            if (Meshes == null)
            {
                if (GetSkinnedMeshRenderer() != null)
                {
                    Mesh mesh = GetSkinnedMeshRenderer().sharedMesh;

                    if (mesh != null)
                    {
                        Meshes = new List <MeshObject>();

                        MeshObject meshObject = MeshObject.Get(mesh);

                        if (meshObject != null)
                        {
                            Meshes.Add(meshObject);
                        }
                    }
                }
            }
            return(Meshes);
        }
Esempio n. 8
0
        public void GenerateSoftness()
        {
            List <DoublePair2> polygonPairs;
            DoublePair2        p;
            int   polygonCount   = polygonsPairs.Count;
            int   trianglesCount = 0;
            float intensity      = Lighting2D.DayLightingSettings.softness.intensity;

            for (int i = 0; i < polygonCount; i++)
            {
                polygonPairs = polygonsPairs[i];

                int polygonPairsCount = polygonPairs.Count;

                for (int x = 0; x < polygonPairsCount; x++)
                {
                    p = polygonPairs[x];

                    zA.x = (float)p.A.x;
                    zA.y = (float)p.A.y;

                    zB.x = (float)p.B.x;
                    zB.y = (float)p.B.y;

                    zC.x = zB.x;
                    zC.y = zB.y;

                    pA.x = zA.x;
                    pA.y = zA.y;

                    pB.x = zB.x;
                    pB.y = zB.y;

                    float angleA = (float)System.Math.Atan2(p.A.y - p.B.y, p.A.x - p.B.x) + pi2;
                    float angleB = (float)System.Math.Atan2(p.A.y - p.B.y, p.A.x - p.B.x) + pi2;
                    float angleC = (float)System.Math.Atan2(p.B.y - p.C.y, p.B.x - p.C.x) + pi2;

                    zA.x += (float)System.Math.Cos(angleA) * intensity;
                    zA.y += (float)System.Math.Sin(angleA) * intensity;

                    zB.x += (float)System.Math.Cos(angleB) * intensity;
                    zB.y += (float)System.Math.Sin(angleB) * intensity;

                    zC.x += (float)System.Math.Cos(angleC) * intensity;
                    zC.y += (float)System.Math.Sin(angleC) * intensity;

                    meshBrush.uv.Add(new Vector2(0, 0));
                    meshBrush.vertices.Add(new Vector3((float)pB.x, (float)pB.y, 0));

                    meshBrush.uv.Add(new Vector2(0.5f - 0, 0));
                    meshBrush.vertices.Add(new Vector3((float)pA.x, (float)pA.y, 0));


                    meshBrush.uv.Add(new Vector2(0.5f - 0, 1));
                    meshBrush.vertices.Add(new Vector3((float)zA.x, (float)zA.y, 0));



                    meshBrush.uv.Add(new Vector2(0, 1));
                    meshBrush.vertices.Add(new Vector3((float)zA.x, (float)zA.y, 0));

                    meshBrush.uv.Add(new Vector2(0.5f - 0, 1));
                    meshBrush.vertices.Add(new Vector3((float)zB.x, (float)zB.y, 0));

                    meshBrush.uv.Add(new Vector2(0.5f - 0, 0));
                    meshBrush.vertices.Add(new Vector3((float)pB.x, (float)pB.y, 0));



                    meshBrush.uv.Add(new Vector2(0, 1));
                    meshBrush.vertices.Add(new Vector3((float)zB.x, (float)zB.y, 0));

                    meshBrush.uv.Add(new Vector2(0.5f - 0, 0));
                    meshBrush.vertices.Add(new Vector3((float)pB.x, (float)pB.y, 0));

                    meshBrush.uv.Add(new Vector2(0.5f - 0, 1));
                    meshBrush.vertices.Add(new Vector3((float)zC.x, (float)zC.y, 0));

                    meshBrush.triangles.Add(trianglesCount + 0);
                    meshBrush.triangles.Add(trianglesCount + 1);
                    meshBrush.triangles.Add(trianglesCount + 2);

                    meshBrush.triangles.Add(trianglesCount + 3);
                    meshBrush.triangles.Add(trianglesCount + 4);
                    meshBrush.triangles.Add(trianglesCount + 5);

                    meshBrush.triangles.Add(trianglesCount + 6);
                    meshBrush.triangles.Add(trianglesCount + 7);
                    meshBrush.triangles.Add(trianglesCount + 8);

                    trianglesCount += 9;
                }
            }

            if (trianglesCount > 0)
            {
                Mesh mesh = meshBrush.Export();

                MeshObject meshObject = MeshObject.Get(mesh);

                if (meshObject != null)
                {
                    softMeshes.Add(meshObject);
                }
            }
        }
Esempio n. 9
0
        private static MeshObject GenerateMesh()
        {
            UnityEngine.Mesh mesh = new UnityEngine.Mesh();

            List <Vector3> vertices  = new List <Vector3>();
            List <int>     triangles = new List <int>();

            int count = 0;

            vertices.Add(right0);
            vertices.Add(right1);
            vertices.Add(right3);
            vertices.Add(right2);

            triangles.Add(count + 0);
            triangles.Add(count + 1);
            triangles.Add(count + 2);

            triangles.Add(count + 2);
            triangles.Add(count + 3);
            triangles.Add(count + 0);

            count += 4;

            vertices.Add(left0);
            vertices.Add(left1);
            vertices.Add(left3);
            vertices.Add(left2);

            triangles.Add(count + 0);
            triangles.Add(count + 1);
            triangles.Add(count + 2);

            triangles.Add(count + 2);
            triangles.Add(count + 3);
            triangles.Add(count + 0);

            count += 4;


            vertices.Add(down0);
            vertices.Add(down1);
            vertices.Add(down3);
            vertices.Add(down2);

            triangles.Add(count + 0);
            triangles.Add(count + 1);
            triangles.Add(count + 2);

            triangles.Add(count + 2);
            triangles.Add(count + 3);
            triangles.Add(count + 0);


            count += 4;

            vertices.Add(up0);
            vertices.Add(up1);
            vertices.Add(up3);
            vertices.Add(up2);

            triangles.Add(count + 0);
            triangles.Add(count + 1);
            triangles.Add(count + 2);

            triangles.Add(count + 2);
            triangles.Add(count + 3);
            triangles.Add(count + 0);

            mesh.vertices  = vertices.ToArray();
            mesh.triangles = triangles.ToArray();

            return(MeshObject.Get(mesh));
        }