private void LegacyAddFill(bool aSkirt, bool aFullBuild)
    {
        IFerr2DTMaterial mat       = TerrainMaterial;
        float            texWidth  = mat.edgeMaterial ? 256 : mat.edgeMaterial.mainTexture.width;
        float            fillDist  = (mat.ToUV(mat.GetBody((Ferr2DT_TerrainDirection)0, 0)).width *(texWidth / pixelsPerUnit)) / (Mathf.Max(1, splitCount)) * splitDist;
        List <Vector2>   fillVerts = GetSegmentsCombined(fillDist);
        Vector2          scale     = Vector2.one;

        // scale is different for the fill texture
        if (mat.fillMaterial != null && mat.fillMaterial.mainTexture != null)
        {
            scale = new Vector2(
                mat.fillMaterial.mainTexture.width / pixelsPerUnit,
                mat.fillMaterial.mainTexture.height / pixelsPerUnit);
        }

        if (aSkirt)
        {
            Vector2 start = fillVerts[0];
            Vector2 end   = fillVerts[fillVerts.Count - 1];

            fillVerts.Add(new Vector2(end.x, fillY));
            fillVerts.Add(new Vector2(Mathf.Lerp(end.x, start.x, 0.33f), fillY));
            fillVerts.Add(new Vector2(Mathf.Lerp(end.x, start.x, 0.66f), fillY));
            fillVerts.Add(new Vector2(start.x, fillY));
        }

        int        offset  = DMesh.VertCount;
        List <int> indices = Ferr2D_Triangulator.GetIndices(ref fillVerts, true, fill == Ferr2DT_FillMode.InvertedClosed, invertFillBorder, fillSplit && aFullBuild ? fillSplitDistance : 0);

        for (int i = 0; i < fillVerts.Count; i++)
        {
            DMesh.AddVertex(fillVerts[i].x, fillVerts[i].y, fillZ, (fillVerts[i].x + uvOffset.x + transform.position.x) / scale.x, (fillVerts[i].y + uvOffset.y + transform.position.y) / scale.y);
        }
        for (int i = 0; i < indices.Count; i += 3)
        {
            try {
                DMesh.AddFace(indices[i] + offset,
                              indices[i + 1] + offset,
                              indices[i + 2] + offset);
            } catch {
            }
        }
    }
示例#2
0
    public DMesh GetDMeshByName(string dmeshName)
    {
        // Adjust the given dmesh name to handle subfolders
        dmeshName = dmeshName.Replace('\\', '/');

        // Has it already been loaded?
        DMesh res = null;

        if (!this.m_dmeshMap.TryGetValue(dmeshName, out res))
        {
            // This is a new DMesh, get the full path to the DMesh
            string dmeshFullPath = Path.ChangeExtension(Path.Combine(Path.Combine(this.m_editorRootFolder, kFolderDecals), dmeshName), ".dmesh");
            if (!File.Exists(dmeshFullPath))
            {
                Debug.LogError(string.Format("Unable to find DMesh '{0}'", dmeshName));
                this.m_dmeshMap.Add(dmeshName, null);                 // prevent future errors
                return(null);
            }

            // Attempt to load the DMesh
            try {
                string  dmeshFileData = System.IO.File.ReadAllText(dmeshFullPath);
                JObject root          = JObject.Parse(dmeshFileData);

                res = new DMesh(dmeshName);
                res.Deserialize(root);
            }
            catch (Exception ex) {
                Debug.LogError(string.Format("Error loading DMesh '{0}': {1}", dmeshName, ex.Message));
                res = null;
            }

            // Update cache
            this.m_dmeshMap.Add(dmeshName, res);
        }

        return(res);
    }
    private void LegacyBuild(bool aFullBuild)
    {
        if (TerrainMaterial == null)
        {
            Debug.LogWarning("Cannot create terrain without a Terrain Material!");
            return;
        }
        if (Path.Count < 2)
        {
            GetComponent <MeshFilter>().sharedMesh = null;
            return;
        }

        MarkColorSave();
        MatchOverrides();
        ForceMaterial(TerrainMaterial, true, false);

        DMesh.Clear();

        if (fill != Ferr2DT_FillMode.FillOnlyClosed && fill != Ferr2DT_FillMode.FillOnlySkirt)
        {
            LegacyAddEdge();
        }
        int[] submesh1 = DMesh.GetCurrentTriangleList();

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

        // compile the mesh!
        Mesh m = GetComponent <MeshFilter>().sharedMesh = GetMesh();

        DMesh.Build(ref m, createTangents && aFullBuild);

        LegacyCreateVertColors();

        // set up submeshes and submaterials
        if (submesh1.Length > 0 && submesh2.Length > 0)
        {
            m.subMeshCount = 2;
            m.SetTriangles(submesh1, 1);
            m.SetTriangles(submesh2, 0);
        }
        else if (submesh1.Length > 0)
        {
            m.subMeshCount = 1;
            m.SetTriangles(submesh1, 0);
        }
        else if (submesh2.Length > 0)
        {
            m.subMeshCount = 1;
            m.SetTriangles(submesh2, 0);
        }

        bool hasCollider = GetComponent <MeshCollider>() != null || GetComponent <PolygonCollider2D>() != null || GetComponent <EdgeCollider2D>() != null;

        if (createCollider && hasCollider)
        {
            RecreateCollider();
        }
#if UNITY_EDITOR
        if (aFullBuild && gameObject.isStatic)
        {
            UnityEditor.Unwrapping.GenerateSecondaryUVSet(m);
        }
#endif

        if (aFullBuild)
        {
            ClearColorSave();
        }
    }