Exemple #1
0
    public override void OnInspectorGUI()
    {
        Brush    = new string[] { "Settings", "Raise", "Lower", "Flatten", "Paint", "Trees" };
        myscript = (LowPolyTerrain)target;
        serializedObject.Update();
        if (nResolution == 0)
        {
            nResolution = Mathf.RoundToInt(Mathf.Sqrt(myscript.GetComponent <MeshFilter>().sharedMesh.triangles.Length / 3 / 2));
        }

        index = GUILayout.Toolbar(index, Brush, GUILayout.Height(25));
        if (index == 0)
        {
            if (myscript.GetComponent <MeshFilter>().sharedMesh.triangles != null)
            {
                nResolution = EditorGUILayout.IntField("Resolution:", nResolution);
            }
            if (siz == 0)
            {
                siz = Mathf.RoundToInt(myscript.GetComponent <MeshFilter>().sharedMesh.bounds.size.x);
            }
            siz  = EditorGUILayout.IntField("Size:", siz);
            vert = new Vector3[0];
            tri  = new int[0];

            if (GUILayout.Button("Reset Terrain"))
            {
                Reset();
            }
            if (GUILayout.Button("Smooth edges"))
            {
                Smooth();
            }
            if (GUILayout.Button(new GUIContent("Create LOD", "Creates a 16x16 version of the terrain to use for LOD")))
            {
                Lod();
            }
            if (index == 0)
            {
                myscript.show = false;
            }
            else
            {
                myscript.show = true;
            }
            EditorGUILayout.HelpBox("High resolutions (=> 128) along with a big brush size can cause performance issues, when modifying the terrain! \nYou can, if neccesary, place several terrains alongside eachother. You will then have to remove this script from the terrains you don't edit, but you can always add it again", MessageType.Warning);
            myscript.show = false;
        }
        else
        {
            myscript.show = true;
        }
        if (index != 0 && index != 4)
        {
            myscript.brushSize = EditorGUILayout.IntSlider("Brush Size:", Mathf.CeilToInt(myscript.brushSize), 1, 200);
            myscript.opacity   = EditorGUILayout.IntSlider("Brush Opacity:", Mathf.CeilToInt(myscript.opacity), 1, 500);
        }

        if (index == 3)
        {
            myscript.destHeight = EditorGUILayout.FloatField("Destination height", myscript.destHeight);
        }
        if (index == 4)
        {
            myscript.brushSize = EditorGUILayout.IntSlider("Brush Size:", Mathf.CeilToInt(myscript.brushSize), 0, 500);
            col = EditorGUILayout.ColorField("Brush Color:", col);
        }
        if (index == 5)
        {
            myscript.tree = EditorGUILayout.ObjectField("Tree:", myscript.tree, typeof(GameObject), false) as GameObject;
            treeRot       = EditorGUILayout.Vector3Field("Tree rotation:", new Vector3(-90, 0, 0));
            treeSiz.x     = EditorGUILayout.IntSlider("Random tree x scale:", Mathf.CeilToInt(treeSiz.x), 0, 99);
            treeSiz.y     = EditorGUILayout.IntSlider("Random tree y scale:", Mathf.CeilToInt(treeSiz.y), 0, 99);
            treeSiz.z     = EditorGUILayout.IntSlider("Random tree z scale:", Mathf.CeilToInt(treeSiz.z), 0, 99);
        }
    }
Exemple #2
0
    public void Lod()
    {
        Mesh mesh = new Mesh();

        Vector3[] mVert  = new Vector3[289];
        Vector2[] mUV    = new Vector2[289];
        Vector3[] tNorms = new Vector3[289];
        for (int i = 0; i < 17; i++)
        {
            for (int p = 0; p < 17; p++)
            {
                RaycastHit hit = new RaycastHit();
                Physics.Raycast(new Vector3(i * (siz / 16) + myscript.transform.position.x, myscript.gameObject.GetComponent <Renderer>().bounds.size.y + myscript.transform.position.y, p * (siz / 16) + myscript.transform.position.z), Vector3.down, out hit);
                mVert[i * 17 + p]  = hit.point;
                mUV[i * 17 + p]    = new Vector2(mVert[i * 17 + p].x / siz, mVert[i * 17 + p].z / siz);
                tNorms[i * 17 + p] = new Vector3(64, mVert[i * 17 + p].y, 0);
            }
        }
        mesh.vertices = mVert;
        mesh.uv       = mUV;
        int[] mTri = new int[16 * 16 * 6];
        for (int i = 0; i < 16; i++)
        {
            if (i % 2 == 0)
            {
                for (int p = 0; p < 16; p++)
                {
                    if (p % 2 == 0)
                    {
                        mTri[(i * 16 + p) * 6]     = i * 17 + p;
                        mTri[(i * 16 + p) * 6 + 1] = i * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 2] = (i + 1) * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 3] = (i + 1) * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 4] = (i + 1) * 17 + p;
                        mTri[(i * 16 + p) * 6 + 5] = i * 17 + p;
                    }
                    else
                    {
                        mTri[(i * 16 + p) * 6]     = i * 17 + p;
                        mTri[(i * 16 + p) * 6 + 1] = i * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 2] = (i + 1) * 17 + p;
                        mTri[(i * 16 + p) * 6 + 3] = (i + 1) * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 4] = (i + 1) * 17 + p;
                        mTri[(i * 16 + p) * 6 + 5] = i * 17 + p + 1;
                    }
                }
            }
            else
            {
                for (int p = 0; p < 16; p++)
                {
                    if (p % 2 == 0)
                    {
                        mTri[(i * 16 + p) * 6]     = i * 17 + p;
                        mTri[(i * 16 + p) * 6 + 1] = i * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 2] = (i + 1) * 17 + p;
                        mTri[(i * 16 + p) * 6 + 3] = (i + 1) * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 4] = (i + 1) * 17 + p;
                        mTri[(i * 16 + p) * 6 + 5] = i * 17 + p + 1;
                    }
                    else
                    {
                        mTri[(i * 16 + p) * 6]     = i * 17 + p;
                        mTri[(i * 16 + p) * 6 + 1] = i * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 2] = (i + 1) * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 3] = (i + 1) * 17 + p + 1;
                        mTri[(i * 16 + p) * 6 + 4] = (i + 1) * 17 + p;
                        mTri[(i * 16 + p) * 6 + 5] = i * 17 + p;
                    }
                }
            }
        }
        mesh.triangles = mTri;
        mesh.normals   = tNorms;
        mesh.RecalculateBounds();
        mesh.RecalculateTangents();
        GameObject game = new GameObject();

        game.transform.parent = myscript.transform;
        game.AddComponent <MeshFilter>();
        game.GetComponent <MeshFilter>().sharedMesh = mesh;
        game.AddComponent <MeshRenderer>();
        game.GetComponent <Renderer>().sharedMaterial = myscript.GetComponent <Renderer>().sharedMaterial;
    }