Ejemplo n.º 1
0
        private void ConvertCoposite(CompositeCollider2D collider, List <GameObject> createdObjects)
        {
            if (collider.edgeRadius < Mathf.Epsilon)
            {
                for (int i = 0; i < collider.pathCount; i++)
                {
                    var path = new Vector2[collider.GetPathPointCount(i)];
                    collider.GetPath(i, path);

                    Mesh mesh         = Collider2DTo3DUtils.GetMeshFromPath(path, true);
                    var  obj          = GetObjectCopy(collider.gameObject, createdObjects);
                    var  meshCollider = obj.AddComponent <MeshCollider>();
                    meshCollider.sharedMesh = mesh;
                }
            }
            else
            {
                for (int i = 0; i < collider.pathCount; i++)
                {
                    var path = new Vector2[collider.GetPathPointCount(i)];
                    collider.GetPath(i, path);
                    ConvertPath(collider.gameObject, collider.edgeRadius, createdObjects, path, true);
                }
            }
        }
        private static void ReplaceWithPolygonCollider2D(CompositeCollider2D composite)
        {
            var go = composite.gameObject;

            // The convex shape parts are put into our specialized collider component for debug drawing
            var super = go.AddComponent <SuperColliderComponent>();

            var combined = go.AddComponent <PolygonCollider2D>();

            combined.pathCount = composite.pathCount;
            combined.isTrigger = composite.isTrigger;

            // Copy composite paths to combined PolygonCollider2D
            List <Vector2> points = new List <Vector2>(1024 * 8);

            for (int p = 0; p < composite.pathCount; p++)
            {
                composite.GetPath(p, points);

                if (points.Count > 0)
                {
                    combined.SetPath(p, points);
                }
            }

            // Switch the composite collider to outlines in order to get the outline of our convex with holes polygon
            if (composite.geometryType != CompositeCollider2D.GeometryType.Outlines)
            {
                composite.geometryType = CompositeCollider2D.GeometryType.Outlines;
                composite.GenerateGeometry();
            }

            // Add outline shapes
            for (int p = 0; p < composite.pathCount; p++)
            {
                composite.GetPath(p, points);

                if (points.Count != 0)
                {
                    super.AddOutline(points);
                }
            }

            // Add shapes from polygon children (which are guaranteed to be convex)
            var childPolygons = go.GetComponentsInChildren <PolygonCollider2D>();

            foreach (var poly in childPolygons)
            {
                if (poly.usedByComposite == true)
                {
                    super.AddPolygonShape(poly.GetPath(0));
                }
            }
        }
    private void Reset()
    {
        toDelete = new List <GameObject>(shadowCasters);
        shadowCasters.Clear();
        shadowPolygons.Clear();
        shadowCasterComponents.Clear();

        for (int i = 0; i < tilemapCollider.pathCount; i++)
        {
            Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)];
            tilemapCollider.GetPath(i, pathVertices);
            GameObject shadowCaster = new GameObject("shadow_caster_" + i);
            shadowCasters.Add(shadowCaster);
            PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D));
            shadowPolygons.Add(shadowPolygon);
            Vector3 lP = shadowCaster.transform.localPosition;
            shadowCaster.transform.parent        = shadowCasterContainer.transform;
            shadowCaster.transform.localPosition = lP;
            shadowPolygon.points  = pathVertices;
            shadowPolygon.enabled = false;
            //if (shadowCaster.GetComponent<ShadowCaster2D>() != null) // remove existing caster?
            //    Destroy(shadowCaster.GetComponent<ShadowCaster2D>());
            ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>();
            shadowCasterComponents.Add(shadowCasterComponent);
            shadowCasterComponent.selfShadows = selfShadows;
        }
        doCleanup = true;
    }
    public void Start()
    {
        Instance        = this;
        tilemapCollider = GetComponent <CompositeCollider2D>();

        shadowCasterContainer = new GameObject("Shadow Casters");
        Vector3 lP = shadowCasterContainer.transform.localPosition;

        shadowCasterContainer.transform.parent        = gameObject.transform;
        shadowCasterContainer.transform.localPosition = lP;
        for (int i = 0; i < tilemapCollider.pathCount; i++)
        {
            Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)];
            tilemapCollider.GetPath(i, pathVertices);
            GameObject shadowCaster = new GameObject("shadow_caster_" + i);
            shadowCasters.Add(shadowCaster);
            PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D));
            shadowPolygons.Add(shadowPolygon);
            lP = shadowCaster.transform.localPosition;
            shadowCaster.transform.parent        = shadowCasterContainer.transform;
            shadowCaster.transform.localPosition = lP;
            shadowPolygon.points  = pathVertices;
            shadowPolygon.enabled = false;
            //if (shadowCaster.GetComponent<ShadowCaster2D>() != null) // remove existing caster?
            //    Destroy(shadowCaster.GetComponent<ShadowCaster2D>());
            ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>();
            shadowCasterComponents.Add(shadowCasterComponent);
            shadowCasterComponent.selfShadows = selfShadows;
        }
    }
Ejemplo n.º 5
0
        //el metodo
        void ExtractColliders()
        {
            int colscount = LevelCollider.pathCount;

            if (colscount > 0)
            {
                for (int i = 0; i < colscount; i++)
                {
                    int m = LevelCollider.GetPathPointCount(i);
                    ColPoints = new Vector2[m];
                    int n = LevelCollider.GetPath(i, ColPoints);

                    //detect which collider shape is better
                    if (ColPoints.Length == 4)
                    {
                        CreateBoxCollider();
                    }
                    if (ColPoints.Length > 4)
                    {
                        CreatePolyCollider();
                    }
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
        }
Ejemplo n.º 6
0
    public static List <Polygon2D> CreateFromCompositeCollider(CompositeCollider2D compositeCollider)
    {
        List <Polygon2D> list = new List <Polygon2D>();

        if (compositeCollider != null)
        {
            int pathCount = compositeCollider.pathCount;


            for (int i = 0; i < pathCount; i++)
            {
                int pointCount = compositeCollider.GetPathPointCount(i);

                Vector2[] pointsInPath = new Vector2[pointCount];

                compositeCollider.GetPath(i, pointsInPath);

                Polygon2D polygon = new Polygon2D();
                for (int j = 0; j < pointsInPath.Length; j++)
                {
                    polygon.AddPoint(pointsInPath[j]);
                }

                polygon.Normalize();

                list.Add(polygon);
            }
        }

        return(list);
    }
        private static void ReplaceWithEdgeCollider2Ds(CompositeCollider2D composite)
        {
            // Add an edge collider child for every path in our composite
            var go = composite.gameObject;

            List <Vector2> points = new List <Vector2>(1024 * 8);

            for (int p = 0; p < composite.pathCount; p++)
            {
                composite.GetPath(p, points);

                // Close the loop
                points.Add(points[0]);

                // Add the edge child
                var goEdge = new GameObject("Edge");
                go.AddChildWithUniqueName(goEdge);

                var edge = goEdge.AddComponent <EdgeCollider2D>();
                edge.isTrigger = composite.isTrigger;
                edge.points    = points.ToArray();

                goEdge.AddComponent <SuperColliderComponent>();
            }
        }
Ejemplo n.º 8
0
    public void Start()
    {
        eventController.OnEnvChangeShapeEvent.AddListener(ResetAndCleanUp);

        Instance              = this;
        tilemapCollider       = GetComponent <CompositeCollider2D>();
        shadowCasterContainer = shadow_caster_go;
        //shadowCasterContainer = GetComponent<ShadowCaster2D>().gameObject;
        for (int i = 0; i < tilemapCollider.pathCount; i++)
        {
            Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)];
            tilemapCollider.GetPath(i, pathVertices);

            GameObject shadowCaster = new GameObject("shadow_caster_^" + i);
            shadowCasters.Add(shadowCaster);
            PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D));
            shadowPolygons.Add(shadowPolygon);
            shadowCaster.transform.parent = shadowCasterContainer.transform;
            shadowPolygon.points          = pathVertices;
            shadowPolygon.enabled         = true;
            shadowPolygon.isTrigger       = true;
            //if (shadowCaster.GetComponent<ShadowCaster2D>() != null) // remove existing caster?
            //    Destroy(shadowCaster.GetComponent<ShadowCaster2D>());
            ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>();
            shadowCasterComponents.Add(shadowCasterComponent);
            shadowCasterComponent.selfShadows = true;
        }
    }
Ejemplo n.º 9
0
    public void Generate()
    {
        DestroyAllChildren();

        tilemapCollider = GetComponent <CompositeCollider2D>();

        for (int i = 0; i < tilemapCollider.pathCount; i++)
        {
            Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)];
            tilemapCollider.GetPath(i, pathVertices);
            GameObject shadowCaster = new GameObject("shadow_caster_" + i);
            shadowCaster.transform.parent = gameObject.transform;
            ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>();
            shadowCasterComponent.selfShadows = this.selfShadows;

            Vector3[] testPath = new Vector3[pathVertices.Length];
            for (int j = 0; j < pathVertices.Length; j++)
            {
                testPath[j] = pathVertices[j];
            }

            shapePathField.SetValue(shadowCasterComponent, testPath);
            meshField.SetValue(shadowCasterComponent, new Mesh());
            generateShadowMeshMethod.Invoke(shadowCasterComponent, new object[] { meshField.GetValue(shadowCasterComponent), shapePathField.GetValue(shadowCasterComponent) });
        }

        // Debug.Log("Generate");
    }
Ejemplo n.º 10
0
    //
    public void GenerateBlockingMesh()
    {
        for (int i = 0; i < CompositeCollider.pathCount; i++)
        {
            Vector2[] pathPoints = new Vector2[CompositeCollider.GetPathPointCount(i)];
            CompositeCollider.GetPath(i, pathPoints);

            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Quad);
            go.transform.parent = transform;
            Mesh      mesh  = new Mesh();
            Vector3[] verts = new Vector3[pathPoints.Length];
            for (int j = 0; j < verts.Length; j++)
            {
                verts[j] = pathPoints[j];
            }
            mesh.vertices  = verts;
            mesh.triangles = new int[6] {
                0, 1, 2, 0, 2, 3
            };
            go.GetComponent <MeshFilter>().mesh = mesh;

            NavMeshModifier mod = go.AddComponent <NavMeshModifier>();
            mod.area         = 1;
            mod.overrideArea = true;
        }

        //MeshFilter.gameObject.transform.position = MeshFilter.gameObject.transform.position + Walls.gameObject.transform.position;
        transform.position = transform.position + Walls.gameObject.transform.position;

        GetComponent <NavMeshSurface>().BuildNavMesh();
    }
Ejemplo n.º 11
0
        bool ValidatePathCache()
        {
#if CINEMACHINE_PHYSICS_2D
            if (m_BoundingShape2DCache != m_BoundingShape2D)
            {
                InvalidatePathCache();
                m_BoundingShape2DCache = m_BoundingShape2D;
            }

            Type colliderType = m_BoundingShape2D == null ? null:  m_BoundingShape2D.GetType();
            if (colliderType == typeof(PolygonCollider2D))
            {
                PolygonCollider2D poly = m_BoundingShape2D as PolygonCollider2D;
                if (m_pathCache == null || m_pathCache.Count != poly.pathCount || m_pathTotalPointCount != poly.GetTotalPointCount())
                {
                    m_pathCache = new List <List <Vector2> >();
                    for (int i = 0; i < poly.pathCount; ++i)
                    {
                        Vector2[]      path = poly.GetPath(i);
                        List <Vector2> dst  = new List <Vector2>();
                        for (int j = 0; j < path.Length; ++j)
                        {
                            dst.Add(path[j]);
                        }
                        m_pathCache.Add(dst);
                    }
                    m_pathTotalPointCount = poly.GetTotalPointCount();
                }
                return(true);
            }
            else if (colliderType == typeof(CompositeCollider2D))
            {
                CompositeCollider2D poly = m_BoundingShape2D as CompositeCollider2D;
                if (m_pathCache == null || m_pathCache.Count != poly.pathCount || m_pathTotalPointCount != poly.pointCount)
                {
                    m_pathCache = new List <List <Vector2> >();
                    Vector2[] path       = new Vector2[poly.pointCount];
                    var       lossyScale = m_BoundingShape2D.transform.lossyScale;
                    Vector2   revertCompositeColliderScale = new Vector2(
                        1f / lossyScale.x,
                        1f / lossyScale.y);
                    for (int i = 0; i < poly.pathCount; ++i)
                    {
                        int            numPoints = poly.GetPath(i, path);
                        List <Vector2> dst       = new List <Vector2>();
                        for (int j = 0; j < numPoints; ++j)
                        {
                            dst.Add(path[j] * revertCompositeColliderScale);
                        }
                        m_pathCache.Add(dst);
                    }
                    m_pathTotalPointCount = poly.pointCount;
                }
                return(true);
            }
#endif
            InvalidatePathCache();
            return(false);
        }
        public static Vector2[] GetPathPoints(this CompositeCollider2D composite, int index)
        {
            int count = composite.GetPathPointCount(index);

            Vector2[] points = new Vector2[count];
            composite.GetPath(index, points);
            return(points);
        }
Ejemplo n.º 13
0
    private void Awake()
    {
        compositeCollider = GetComponent <CompositeCollider2D>();

        for (int i = 0; i < compositeCollider.pathCount; i++)
        {
            paths.Add(new Vector2[compositeCollider.GetPathPointCount(i)]);
            compositeCollider.GetPath(i, paths[i]);
        }
    }
    public override void Extrude()
    {
        for (int i = 0; i < m_compositeCollider2D.pathCount; i++)
        {
            Vector2[] pathVertices = new Vector2[m_compositeCollider2D.GetPathPointCount(i)];
            m_compositeCollider2D.GetPath(i, pathVertices);

            Create3DMeshObject(pathVertices, transform, gameObject.name + "Extrusion");
        }
    }
Ejemplo n.º 15
0
    // 모서리를 구함
    public void GetEdges(List <Vector2> edges)
    {
        if (gameObject.isStatic && isCalc)
        {
            edges = calcEdges;

            return;
        }

        for (int i = 0; i < compositeCollider.pathCount; i++)
        {
            int       pathPointCount = compositeCollider.GetPathPointCount(i);
            Vector2[] pathVerts      = new Vector2[pathPointCount];

            compositeCollider.GetPath(i, pathVerts);
            edges.AddRange(pathVerts);

            for (int k = 0; k < pathPointCount - 1; k++)
            {
                edges.Add(pathVerts[k]);
                edges.Add(pathVerts[k + 1]);
            }

            edges.Add(pathVerts[pathPointCount - 1]);
            edges.Add(pathVerts[0]);
        }

        calcEdges = edges;

        //// 사각형 기준으로 4방향
        //Vector3 v1 = new Vector3(-0.5f, -0.5f, 0.0f);
        //      Vector3 v2 = new Vector3(+0.5f, -0.5f, 0.0f);
        //      Vector3 v3 = new Vector3(+0.5f, +0.5f, 0.0f);
        //      Vector3 v4 = new Vector3(-0.5f, +0.5f, 0.0f);

        //// 꼭지점 구함 ( 잘모르겠음 )
        //      v1 = transform.localToWorldMatrix.MultiplyPoint(v1);
        //      v2 = transform.localToWorldMatrix.MultiplyPoint(v2);
        //      v3 = transform.localToWorldMatrix.MultiplyPoint(v3);
        //      v4 = transform.localToWorldMatrix.MultiplyPoint(v4);

        //// 모서리를 넣음
        //      edges.Add(new Vector2(v1.x, v1.y));
        //      edges.Add(new Vector2(v2.x, v2.y));

        //      edges.Add(new Vector2(v2.x, v2.y));
        //      edges.Add(new Vector2(v3.x, v3.y));

        //      edges.Add(new Vector2(v3.x, v3.y));
        //      edges.Add(new Vector2(v4.x, v4.y));

        //      edges.Add(new Vector2(v4.x, v4.y));
        //      edges.Add(new Vector2(v1.x, v1.y));
    }
    void getPoly()
    {
        List <Vector2> verts = new List <Vector2>();

        for (int i = 0; i < _comp.pathCount; i++)
        {
            Vector2[] pathVerts = new Vector2[_comp.GetPathPointCount(i)];
            _comp.GetPath(i, pathVerts);
            verts.AddRange(pathVerts);
        }
    }
    private void ExtractPointsFromCollider(CompositeCollider2D collider, List <Vector2> outputPoints)
    {
        int pathCount = collider.pathCount;

        for (int i = 0; i < pathCount; ++i)
        {
            List <Vector2> pathPoints = new List <Vector2>();
            collider.GetPath(i, pathPoints);
            outputPoints.AddRange(pathPoints);
        }
    }
Ejemplo n.º 18
0
    ObstacleDatas GetCompositeDatas(CompositeCollider2D composite)
    {
        Vector2[] points = new Vector2[composite.pointCount];

        for (int i = 0; i < composite.pathCount; i++)
        {
            Vector2[] path = new Vector2[composite.GetPathPointCount(i)];
            composite.GetPath(i, path);
            points = ConcatenateArrays(points, path);
        }

        return(new ObstacleDatas(points, boxPrecision));
    }
Ejemplo n.º 19
0
    private static List <Vector2> GetVertsFromCompositeCollider2D(CompositeCollider2D platformsCollider)
    {
        List <Vector2> verts = new List <Vector2>();

        for (int i = 0; i < platformsCollider.pathCount; i++)
        {
            Vector2[] pathVerts = new Vector2[platformsCollider.GetPathPointCount(i)];
            platformsCollider.GetPath(i, pathVerts);
            verts.AddRange(pathVerts);
        }

        return(verts);
    }
Ejemplo n.º 20
0
    /// <summary>
    /// Load the vertices coordinates of the Composite Collider
    /// </summary>
    private void LoadPolygonVertices()
    {
        //Get Composite Collider vertices
        compositePoints = new Vector2[coll.GetPathPointCount(0)];
        coll.GetPath(0, compositePoints);

        //Put them back to world scale for accurate World Position
        for (int i = 0; i < compositePoints.Length; i++)
        {
            compositePoints[i].x /= transform.lossyScale.x;
            compositePoints[i].y /= transform.lossyScale.y;
        }
    }
Ejemplo n.º 21
0
        private void Awake()
        {
            if (m_ApplyToSortingLayers == null)
            {
                m_ApplyToSortingLayers = SetDefaultSortingLayers();
            }

            Bounds bounds = new Bounds(transform.position, Vector3.one);

            Renderer   renderer = GetComponent <Renderer>();
            Collider2D collider = GetComponent <Collider2D>();

            if (renderer != null)
            {
                bounds = renderer.bounds;
            }
            else if (collider != null)
            {
                bounds = collider.bounds;
            }

            Vector3 relOffset = bounds.center - transform.position;

            if (m_ShapePath == null || m_ShapePath.Length == 0)
            {
                m_ShapePath = new Vector3[]
                {
                    relOffset + new Vector3(-bounds.extents.x, -bounds.extents.y),
                    relOffset + new Vector3(bounds.extents.x, -bounds.extents.y),
                    relOffset + new Vector3(bounds.extents.x, bounds.extents.y),
                    relOffset + new Vector3(-bounds.extents.x, bounds.extents.y)
                };
            }

            PolygonCollider2D polygonCollider = GetComponent <PolygonCollider2D>();

            if (polygonCollider != null && polygonCollider.pathCount >= 0)
            {
                m_ShapePath = Array.ConvertAll <Vector2, Vector3>(polygonCollider.GetPath(0), vec2To3);
            }

            CompositeCollider2D compositeCollider = GetComponent <CompositeCollider2D>();

            if (compositeCollider != null && compositeCollider.pathCount >= 0)
            {
                Vector2[] pathVertices = new Vector2[compositeCollider.GetPathPointCount(0)];
                compositeCollider.GetPath(0, pathVertices);
                m_ShapePath = Array.ConvertAll <Vector2, Vector3>(pathVertices, vec2To3);
            }
        }
Ejemplo n.º 22
0
        bool ValidatePathCache()
        {
            Type colliderType = m_BoundingShape2D == null ? null:  m_BoundingShape2D.GetType();

            if (colliderType == typeof(PolygonCollider2D))
            {
                PolygonCollider2D poly = m_BoundingShape2D as PolygonCollider2D;
                if (m_pathCache == null || m_pathCache.Count != poly.pathCount || m_pathTotalPointCount != poly.GetTotalPointCount())
                {
                    m_pathCache = new List <List <Vector2> >();
                    for (int i = 0; i < poly.pathCount; ++i)
                    {
                        Vector2[]      path = poly.GetPath(i);
                        List <Vector2> dst  = new List <Vector2>();
                        for (int j = 0; j < path.Length; ++j)
                        {
                            dst.Add(path[j]);
                        }
                        m_pathCache.Add(dst);
                    }
                    m_pathTotalPointCount = poly.GetTotalPointCount();
                }
                return(true);
            }
            else if (colliderType == typeof(CompositeCollider2D))
            {
                CompositeCollider2D poly = m_BoundingShape2D as CompositeCollider2D;
                if (m_pathCache == null || m_pathCache.Count != poly.pathCount || m_pathTotalPointCount != poly.pointCount)
                {
                    m_pathCache = new List <List <Vector2> >();
                    Vector2[] path = new Vector2[poly.pointCount];
                    for (int i = 0; i < poly.pathCount; ++i)
                    {
                        int            numPoints = poly.GetPath(i, path);
                        List <Vector2> dst       = new List <Vector2>();
                        for (int j = 0; j < numPoints; ++j)
                        {
                            dst.Add(path[j]);
                        }
                        m_pathCache.Add(dst);
                    }
                    m_pathTotalPointCount = poly.pointCount;
                }
                return(true);
            }
            InvalidatePathCache();
            return(false);
        }
Ejemplo n.º 23
0
    void Start()
    {
        red.setColor(Color.red);
        blue.setColor(Color.blue);

        brush.GetPhysicsShape(0, Spoints);

        for (int i = 0; i < filter.pathCount; i++)
        {
            Vector2[] points = new Vector2[filter.GetPathPointCount(i)];
            filter.GetPath(i, points);

            redCollider.addFilter(points);
            blueCollider.addFilter(points);
        }
    }
Ejemplo n.º 24
0
        private void ReinitializeShapes(CompositeCollider2D collider)
        {
            RemoveCurrentShadows();

            for (int i = 0; i < collider.pathCount; i++)
            {
                List <Vector2> points = new List <Vector2>();
                collider.GetPath(i, points);

                GameObject go = new GameObject("AutogeneratedShadowPath", typeof(MeshRenderer));
                go.transform.parent = transform;
                PathShadow path = go.AddComponent <PathShadow>();
                path.useRendererSilhouette = false;
                path.selfShadows           = m_SelfShadows;
                path.SetShape(points);
            }
        }
Ejemplo n.º 25
0
        private bool ValidatePathCache()
        {
            Type type = (!(this.m_BoundingShape2D == null)) ? this.m_BoundingShape2D.GetType() : null;

            if (type == typeof(PolygonCollider2D))
            {
                PolygonCollider2D polygonCollider2D = this.m_BoundingShape2D as PolygonCollider2D;
                if (this.m_pathCache == null || this.m_pathCache.Count != polygonCollider2D.pathCount)
                {
                    this.m_pathCache = new List <List <Vector2> >();
                    for (int i = 0; i < polygonCollider2D.pathCount; i++)
                    {
                        Vector2[]      path = polygonCollider2D.GetPath(i);
                        List <Vector2> list = new List <Vector2>();
                        for (int j = 0; j < path.Length; j++)
                        {
                            list.Add(path[j]);
                        }
                        this.m_pathCache.Add(list);
                    }
                }
                return(true);
            }
            if (type == typeof(CompositeCollider2D))
            {
                CompositeCollider2D compositeCollider2D = this.m_BoundingShape2D as CompositeCollider2D;
                if (this.m_pathCache == null || this.m_pathCache.Count != compositeCollider2D.pathCount)
                {
                    this.m_pathCache = new List <List <Vector2> >();
                    Vector2[] array = new Vector2[compositeCollider2D.pointCount];
                    for (int k = 0; k < compositeCollider2D.pathCount; k++)
                    {
                        int            path2 = compositeCollider2D.GetPath(k, array);
                        List <Vector2> list2 = new List <Vector2>();
                        for (int l = 0; l < path2; l++)
                        {
                            list2.Add(array[l]);
                        }
                        this.m_pathCache.Add(list2);
                    }
                }
                return(true);
            }
            this.InvalidatePathCache();
            return(false);
        }
Ejemplo n.º 26
0
    public void Start()
    {
        tilemapCollider = GetComponent <CompositeCollider2D>();
        GameObject shadowCasterContainer = GameObject.Find("shadow_casters");

        for (int i = 0; i < tilemapCollider.pathCount; i++)
        {
            Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)];
            tilemapCollider.GetPath(i, pathVertices);
            GameObject        shadowCaster  = new GameObject("shadow_caster_" + i);
            PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D));
            shadowCaster.transform.parent = shadowCasterContainer.transform;
            shadowPolygon.points          = pathVertices;
            shadowPolygon.enabled         = false;
            UnityEngine.Experimental.Rendering.Universal.ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <UnityEngine.Experimental.Rendering.Universal.ShadowCaster2D>();
            shadowCasterComponent.selfShadows = true;
        }
    }
Ejemplo n.º 27
0
    /// <summary>
    /// Returns a list of the edges of the composite collider.
    /// </summary>
    /// <param name="edges">A list of the edges of the collider. Output param, will have edges added.</param>
    private void GetCompositeEdges(ref List <Vector2> edges)
    {
        if (my_composite_collider == null)
        {
            return;
        }

        for (int i = 0; i < my_composite_collider.pathCount; i++)
        {
            Vector2[] points      = new Vector2[my_composite_collider.GetPathPointCount(i)];
            int       point_count = my_composite_collider.GetPath(i, points);
            for (int j = 0; j < point_count; j++)
            {
                edges.Add(points[j]);
                edges.Add(points[(j + 1) % point_count]);
            }
        }
    }
Ejemplo n.º 28
0
    /// <summary>
    /// Given a Composite Collider 2D, it replaces existing Shadow Caster 2Ds (children) with new Shadow Caster 2D objects whose
    /// shapes coincide with the paths of the collider.
    /// </summary>
    /// <remarks>
    /// It is recommended that the object that contains the collider component has a Composite Shadow Caster 2D too.
    /// It is recommended to call this method in editor only.
    /// </remarks>
    /// <param name="collider">The collider which will be the parent of the new shadow casters.</param>
    /// <param name="selfShadows">Whether the shadow casters will have the Self Shadows option enabled..</param>
    public static void GenerateTilemapShadowCasters(CompositeCollider2D collider, bool selfShadows)
    {
        // First, it destroys the existing shadow casters
        ShadowCaster2D[] existingShadowCasters = collider.GetComponentsInChildren <ShadowCaster2D>();

        for (int i = 0; i < existingShadowCasters.Length; ++i)
        {
            if (existingShadowCasters[i].transform.parent != collider.transform)
            {
                continue;
            }

            GameObject.DestroyImmediate(existingShadowCasters[i].gameObject);
        }

        // Then it creates the new shadow casters, based on the paths of the composite collider
        int            pathCount      = collider.pathCount;
        List <Vector2> pointsInPath   = new List <Vector2>();
        List <Vector3> pointsInPath3D = new List <Vector3>();

        for (int i = 0; i < pathCount; ++i)
        {
            collider.GetPath(i, pointsInPath);

            GameObject newShadowCaster = new GameObject("ShadowCaster2D");
            newShadowCaster.isStatic = true;
            newShadowCaster.transform.SetParent(collider.transform, false);

            for (int j = 0; j < pointsInPath.Count; ++j)
            {
                pointsInPath3D.Add(pointsInPath[j]);
            }

            ShadowCaster2D component = newShadowCaster.AddComponent <ShadowCaster2D>();
            component.SetPath(pointsInPath3D.ToArray());
            component.SetPathHash(Random.Range(int.MinValue, int.MaxValue)); // The hashing function GetShapePathHash could be copied from the LightUtility class
            component.selfShadows = selfShadows;
            component.Update();

            pointsInPath.Clear();
            pointsInPath3D.Clear();
        }
    }
Ejemplo n.º 29
0
    public static float2 GetClosestPointFromPoint(float2 point, CompositeCollider2D composite)
    {
        var points = new List <Vector2>();

        for (int i = 0; i < composite.pathCount; i++)
        {
            Vector2[] currentPoints = new Vector2[composite.GetPathPointCount(i)];
            composite.GetPath(i, currentPoints);
            points.AddRange(currentPoints);
        }

        var distanceDictionary = points.ToDictionary <Vector2, float, Vector2>(
            position => Vector2.Distance(point, composite.transform.TransformPoint(position)),
            position => composite.transform.TransformPoint(position));

        var orderedDictionary = distanceDictionary.OrderBy(e => e.Key);

        return(orderedDictionary.Any() ? orderedDictionary.First().Value : Vector2.zero);
    }
    /// <summary>
    /// Gets all the end points out of the composite collider, and casts rays against them.
    /// </summary>
    /// <param name="collider">The collider to process</param>
    /// <param name="list">Outputs the resulting raycast results</param>
    private void ProcessCompositeCollider(Collider2D collider, ref List <CastData> list)
    {
        // Composite collider 2D geometry
        CompositeCollider2D composite = collider.gameObject.GetComponent <CompositeCollider2D>();

        if (composite == null)
        {
            return;
        }

        for (int i = 0; i < composite.pathCount; i++)
        {
            Vector2[] points      = new Vector2[composite.GetPathPointCount(i)];
            int       point_count = composite.GetPath(i, points);
            for (int j = 0; j < point_count; j++)
            {
                TriRayCast(points[j], ref list);
            }
        }
    }