Exemple #1
0
    private List <Vector2> GetColliderVerts()
    {
        Ferr2D_Path    path  = GetComponent <Ferr2D_Path> ();
        List <Vector2> verts = path.GetVerts(smoothPath, splitDist, splitCorners);

        if (path.closed && smoothPath == false)
        {
            verts.Add(verts [0]);
        }
        List <Vector2> raw = new List <Vector2> (verts);

        MatchOverrides();

        // consider the offset values for the collider. Also, if it's not filled, make sure we add in
        // extra path verts for the underside of the path
        int count = raw.Count;

        for (int i = count - 1; i >= 0; i--)
        {
            Vector2 norm = Ferr2D_Path.GetNormal(raw, i, path.closed);
            if (fill == Ferr2DT_FillMode.None)
            {
                verts.Add(raw [i] + new Vector2(norm.x * surfaceOffset [(int)Ferr2DT_TerrainDirection.Top], norm.y * surfaceOffset [(int)Ferr2DT_TerrainDirection.Top]));
                verts [i] += new Vector2(norm.x * -surfaceOffset [(int)Ferr2DT_TerrainDirection.Bottom], norm.y * -surfaceOffset [(int)Ferr2DT_TerrainDirection.Bottom]);
            }
            else
            {
                Vector2 offset = GetOffset(raw, i, path.closed);
                verts [i] += new Vector2(norm.x * -offset.x, norm.y * -offset.y);
            }
        }

        if (fill == Ferr2DT_FillMode.Skirt || fill == Ferr2DT_FillMode.FillOnlySkirt)
        {
            Vector2 start = verts [0];
            Vector2 end   = verts [verts.Count - 1];
            verts.Add(new Vector2(end.x, fillY));
            verts.Add(new Vector2(Mathf.Lerp(end.x, start.x, 0.33f), fillY));
            verts.Add(new Vector2(Mathf.Lerp(end.x, start.x, 0.66f), fillY));
            verts.Add(new Vector2(start.x, fillY));
        }
        else if (fill == Ferr2DT_FillMode.None)
        {
        }
        return(verts);
    }
Exemple #2
0
    private void DoPath(Ferr2D_Path path)
    {
        Handles.color = Color.white;
        Ferr2DT_PathTerrain terrain = path.gameObject.GetComponent <Ferr2DT_PathTerrain>();
        List <Vector2>      verts   = terrain == null?path.GetVertsRaw() : path.GetVerts(terrain.smoothPath, terrain.splitDist, terrain.splitCorners);

        for (int i = 0; i < verts.Count - 1; i++)
        {
            Vector3 pos  = path.transform.position + Vector3.Scale(new Vector3(verts[i].x, verts[i].y, 0), path.transform.localScale);
            Vector3 pos2 = path.transform.position + Vector3.Scale(new Vector3(verts[i + 1].x, verts[i + 1].y, 0), path.transform.localScale);
            Handles.DrawLine(pos + offset, pos2 + offset);
        }
        if (path.closed)
        {
            Vector3 pos  = path.transform.position + Vector3.Scale(new Vector3(verts[0].x, verts[0].y, 0), path.transform.localScale);
            Vector3 pos2 = path.transform.position + Vector3.Scale(new Vector3(verts[verts.Count - 1].x, verts[verts.Count - 1].y, 0), path.transform.localScale);
            Handles.DrawLine(pos + offset, pos2 + offset);
        }
    }
Exemple #3
0
    /// <summary>
    /// The Ferr2DT_IPath method, gets called automatically whenever the Ferr2DT path gets updated in the
    /// editor. This will completely recreate the the visual mesh (only) for the terrain. If you want
    /// To recreate the collider as well, that's a separate call to RecreateCollider.
    /// </summary>
    public void RecreatePath()
    {
        //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        //sw.Start();

        if (path == null)
        {
            path = GetComponent <Ferr2D_Path> ();
        }
        if (dMesh == null)
        {
            dMesh = new Ferr2DT_DynamicMesh();
        }
        if (terrainMaterial == null)
        {
            Debug.LogWarning("Cannot create terrain without a Terrain Material!");
            return;
        }

        MatchOverrides();

        // double check the materials!
        ForceMaterial(terrainMaterial, true, false);

        dMesh.Clear();
        dMesh.color = vertexColor;

        if (path.Count < 2)
        {
            GetComponent <MeshFilter>().sharedMesh = null;
            return;
        }

        // make sure we can keep a consistent scale for the texture
        if (terrainMaterial.edgeMaterial.mainTexture != null && terrainMaterial.edgeMaterial.mainTexture != null)
        {
            unitsPerUV.x = terrainMaterial.edgeMaterial.mainTexture.width / pixelsPerUnit;
            unitsPerUV.y = terrainMaterial.edgeMaterial.mainTexture.height / pixelsPerUnit;
        }

        if (fill != Ferr2DT_FillMode.FillOnlyClosed && fill != Ferr2DT_FillMode.FillOnlySkirt)
        {
            // split the path into segments based on the split angle
            List <List <Vector2> >          segments = new List <List <Vector2> >           ();
            List <Ferr2DT_TerrainDirection> dirs     = new List <Ferr2DT_TerrainDirection>();

            segments = GetSegments(path.GetVerts(smoothPath, splitDist, splitCorners), out dirs);
            if (dirs.Count < segments.Count)
            {
                dirs.Add(directionOverrides[directionOverrides.Count - 1]);
            }
            List <int> order = new List <int>();
            for (int i = 0; i < segments.Count; i++)
            {
                order.Add(i);
            }

            order.Sort(
                new Ferr2DT_Comparer <int>(
                    (x, y) => GetDescription(segments[y]).zOffset.CompareTo(GetDescription(segments[x]).zOffset)
                    ));

            // process the segments into meshes
            for (int i = 0; i < order.Count; i++)
            {
                AddSegment(segments[order[i]], order.Count <= 1 && path.closed, dirs[order[i]]);
            }
        }
        int[] submesh1 = dMesh.GetCurrentTriangleList();

        // add a fill if the user desires
        if ((fill == Ferr2DT_FillMode.Skirt || fill == Ferr2DT_FillMode.FillOnlySkirt) && terrainMaterial.fillMaterial != null)
        {
            AddFill(true);
        }
        else if ((fill == Ferr2DT_FillMode.Closed || fill == Ferr2DT_FillMode.InvertedClosed || fill == Ferr2DT_FillMode.FillOnlyClosed) && terrainMaterial.fillMaterial != null)
        {
            AddFill(false);
        }
        else if (fill == Ferr2DT_FillMode.None)
        {
        }
        int[] submesh2 = dMesh.GetCurrentTriangleList(submesh1.Length);

        // compile the mesh!
        Mesh   m    = GetComponent <MeshFilter>().sharedMesh;
        string name = "Ferr2DT_PathMesh_" + gameObject.name + gameObject.GetInstanceID();

        if (m == null || m.name != name)
        {
            GetComponent <MeshFilter>().sharedMesh = m = new Mesh();
            m.name = name;
        }
        dMesh.Build(ref m);

        // set up submeshes and submaterials
        m.subMeshCount = 2;
        if (renderer.sharedMaterials.Length < 2)
        {
            Material[] old = renderer.sharedMaterials;
            renderer.sharedMaterials = new Material[2];
            if (old.Length > 0)
            {
                renderer.sharedMaterials[0] = old[0];
            }
        }
        m.SetTriangles(submesh1, 1);
        m.SetTriangles(submesh2, 0);
        //sw.Stop();
        //Debug.Log("Creating mesh took: " + sw.Elapsed.TotalMilliseconds + "ms");
    }
Exemple #4
0
 private void DoPath    (Ferr2D_Path path)
 {
     Handles.color = Color.white;
     Ferr2DT_PathTerrain terrain = path.gameObject.GetComponent<Ferr2DT_PathTerrain>();
     List<Vector2> verts = terrain == null ? path.GetVertsRaw() : path.GetVerts(false, 2, terrain.splitCorners);
     for (int i = 0; i < verts.Count - 1; i++)
     {
         Vector3 pos  = path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(verts[i    ].x, verts[i    ].y, 0), path.transform.localScale);
         Vector3 pos2 = path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(verts[i + 1].x, verts[i + 1].y, 0), path.transform.localScale);
         Handles.DrawLine(pos + offset, pos2 + offset);
     }
     if (path.closed)
     {
         Vector3 pos  = path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(verts[0              ].x, verts[0              ].y, 0), path.transform.localScale);
         Vector3 pos2 = path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(verts[verts.Count - 1].x, verts[verts.Count - 1].y, 0), path.transform.localScale);
         Handles.DrawLine(pos + offset, pos2 + offset);
     }
 }
    void OnSceneGUI()
    {
        Ferr2DT_PathTerrain collider = (Ferr2DT_PathTerrain)target;
        Ferr2D_Path         path     = collider.gameObject.GetComponent <Ferr2D_Path>();

        EditorUtility.SetSelectedWireframeHidden(collider.gameObject.renderer, Ferr_Menu.HideMeshes);

        if (collider.enabled == false || path == null || path.pathVerts.Count <= 1 || !collider.createCollider)
        {
            return;
        }

        Handles.color = new Color(0, 1, 0, 1);

        List <Vector2> verts = path.GetVerts(collider.smoothPath, collider.splitDist, collider.splitCorners);

        if (path.closed && collider.smoothPath == false)
        {
            verts.Add(verts[0]);
        }

        Vector2 norm      = Ferr2D_Path.GetNormal(verts, 0, path.closed);
        Vector2 tmp       = Vector2.zero;
        Vector2 tmpBottom = Vector2.zero;

        if (collider.fill == Ferr2DT_FillMode.None)
        {
            tmp       = verts[0] + new Vector2(norm.x * -collider.surfaceOffset[(int)Ferr2DT_TerrainDirection.Top], norm.y * -collider.surfaceOffset[(int)Ferr2DT_TerrainDirection.Top]);
            tmpBottom = verts[0] + new Vector2(norm.x * collider.surfaceOffset[(int)Ferr2DT_TerrainDirection.Bottom], norm.y * collider.surfaceOffset[(int)Ferr2DT_TerrainDirection.Bottom]);
        }
        else
        {
            Vector2 offset = collider.GetOffset(path.pathVerts, 0, path.closed);
            tmp = verts[0] + new Vector2(norm.x * -offset.x, norm.y * -offset.y);
        }
        Vector3 startPos   = path.gameObject.transform.position + Ferr2D_PathEditor.offset + Vector3.Scale(new Vector3(tmp.x, tmp.y, 0), path.transform.localScale);
        Vector3 currBottom = path.gameObject.transform.position + Ferr2D_PathEditor.offset + Vector3.Scale(new Vector3(tmpBottom.x, tmpBottom.y, 0), path.transform.localScale);
        Vector3 curr       = startPos;

        if (collider.fill == Ferr2DT_FillMode.None)
        {
            Handles.DrawLine(curr, currBottom);
        }
        for (int i = 0; i < verts.Count - 1; i++)
        {
            norm = Ferr2D_Path.GetNormal(verts, i + 1, path.closed);
            if (collider.fill == Ferr2DT_FillMode.None)
            {
                tmp       = verts[i + 1] + new Vector2(norm.x * -collider.surfaceOffset[(int)Ferr2DT_TerrainDirection.Top], norm.y * -collider.surfaceOffset[(int)Ferr2DT_TerrainDirection.Top]);
                tmpBottom = verts[i + 1] + new Vector2(norm.x * collider.surfaceOffset[(int)Ferr2DT_TerrainDirection.Bottom], norm.y * collider.surfaceOffset[(int)Ferr2DT_TerrainDirection.Bottom]);
                Vector3 pos       = path.gameObject.transform.position + Ferr2D_PathEditor.offset + Vector3.Scale(new Vector3(tmp.x, tmp.y, 0), path.transform.localScale);
                Vector3 posBottom = path.gameObject.transform.position + Ferr2D_PathEditor.offset + Vector3.Scale(new Vector3(tmpBottom.x, tmpBottom.y, 0), path.transform.localScale);

                if (ColliderNormValid(collider, verts[i], verts[i + 1]))
                {
                    Handles.DrawLine(curr, pos);
                }
                if (ColliderNormValid(collider, verts[i + 1], verts[i]))
                {
                    Handles.DrawLine(currBottom, posBottom);
                }
                curr       = pos;
                currBottom = posBottom;
            }
            else
            {
                Vector2 offset = collider.GetOffset(verts, i + 1, path.closed);
                tmp = verts[i + 1] + new Vector2(norm.x * -offset.x, norm.y * -offset.y);
                Vector3 pos = path.gameObject.transform.position + Ferr2D_PathEditor.offset + Vector3.Scale(new Vector3(tmp.x, tmp.y, 0), path.transform.localScale);
                if (i + 1 == verts.Count - 1 && path.closed)
                {
                    pos = startPos;
                }
                if (ColliderNormValid(collider, verts[i], verts[i + 1]))
                {
                    Handles.DrawLine(curr, pos);
                }
                curr = pos;
            }
        }
        if (collider.fill == Ferr2DT_FillMode.None)
        {
            Handles.DrawLine(curr, currBottom);
        }

        if (collider.fill == Ferr2DT_FillMode.Skirt || collider.fill == Ferr2DT_FillMode.FillOnlySkirt)
        {
            Vector3 start      = startPos;
            Vector3 end        = curr;
            Vector3 skirtStart = new Vector3(start.x, collider.transform.position.y + collider.fillY, start.z);
            Vector3 skirtEnd   = new Vector3(end.x, collider.transform.position.y + collider.fillY, end.z);
            Handles.DrawLine(start, skirtStart);
            Handles.DrawLine(skirtStart, skirtEnd);
            Handles.DrawLine(skirtEnd, end);
        }
    }