static void CreateAsset()
    {
        TerrainCompiler item = EditorUtil.CreateAsset <TerrainCompiler>(NMBEditorUtil.AssetLabel);

        EditorUtility.FocusProjectWindow();
        Selection.activeObject = item;
    }
    /// <summary>
    /// Controls behavior of the inspector.
    /// </summary>
    public override void OnInspectorGUI()
    {
        TerrainCompiler targ = (TerrainCompiler)target;

        EditorGUILayout.Separator();

        GUILayout.Label("Priority: " + targ.Priority);

        EditorGUILayout.Separator();

        targ.terrainData = (TerrainData)EditorGUILayout.ObjectField("Terrain Data"
                                                                    , targ.terrainData
                                                                    , typeof(TerrainData)
                                                                    , false);

        targ.includeTrees = EditorGUILayout.Toggle("Include Trees", targ.includeTrees);

        float res = EditorGUILayout.Slider("Resolution (%)", targ.Resolution * 100, 0.01f, 100);

        targ.Resolution = (float)System.Math.Round(Mathf.Max(0.001f, res * 0.01f), 3);

        EditorGUILayout.Separator();

        OnGUIDebug(targ);

        GUI.enabled = true;

        EditorGUILayout.Separator();

        GUILayout.Box("Input Build Processor\n\nLoads and compiles a "
                      + typeof(Terrain).Name + " component if it references the specified "
                      + typeof(TerrainData).Name + " object."
                      , EditorUtil.HelpStyle
                      , GUILayout.ExpandWidth(true));

        EditorGUILayout.Separator();

        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
        }
    }
Exemplo n.º 3
0
    private void OnGUIDebug(TerrainCompiler targ)
    {
        // Getting the terrain requires a full terrain search.  So only check
        // for it occationally.
        if (mDebugEnabled)
        {
            double time = EditorApplication.timeSinceStartup;
            if (targ.terrainData != mLastSource || mLastCheck + CheckDelay > time)
            {
                Terrain terrain = targ.GetTerrain();
                mLastSource = targ.terrainData;
                mLastCheck = time;
                if (mDebugTerrain != terrain)
                {
                    mDebugTerrain = terrain;
                    SceneView.RepaintAll();
                }
            }
        }
        else
        {
            mDebugTerrain = null;
            mLastCheck = 0;
            mLastSource = null;
        }

        EditorGUILayout.Separator();

        GUI.enabled = targ.terrainData;

        bool origChanged = GUI.changed;

        mDebugEnabled = EditorGUILayout.Toggle("Enable Preview", mDebugEnabled);

        if (!mDebugEnabled || !targ.terrainData)
            return;

        GUILayout.Label("Size");
        mDebugZoneSize = (int)GUILayout.HorizontalSlider(mDebugZoneSize, 10, 50);

        GUILayout.Label("Offset");
        mDebugOffset = GUILayout.HorizontalSlider(mDebugOffset, 0, 10);

        EditorGUILayout.Separator();

        string helpText;
        if (mDebugTerrain == null)
            helpText = "There is no enabled terrain in the scene using the source terrain data.";
        else
        {
            int xc;
            int zc;
            Vector3 scale = TerrainUtil.DeriveScale(mDebugTerrain, targ.Resolution, out xc, out zc);
            int triCount = (xc - 1) * (zc - 1) * 2;

            helpText = string.Format("Mouse-over the terrain in the scene to see a "
                + " triangulation preview. Trees are not included in the preview.\n\n"
                + "Total surface triangles: {0:N0}\n"
                + "Sample distance: {1:F1} x {2:F1}"
                , triCount, scale.x, scale.z);
        }

        GUILayout.Box(helpText, EditorUtil.HelpStyle, GUILayout.ExpandWidth(true));

        if (GUI.changed)
            SceneView.RepaintAll();

        GUI.changed = origChanged;

    }
    void OnSceneGUI(SceneView view)
    {
        TerrainCompiler targ = (TerrainCompiler)target;

        if (!mDebugEnabled || mDebugTerrain == null)
        {
            // Nothing to do.
            return;
        }

        Vector3 mousePos = Event.current.mousePosition;
        Camera  cam      = Camera.current;

        Ray ray = cam.ScreenPointToRay(new Vector3(mousePos.x, -mousePos.y + cam.pixelHeight));

        Vector3 point = Vector3.zero;

        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 1000.0f))
        {
            Terrain terrain = hit.collider.gameObject.GetComponent <Terrain>();
            if (terrain == mDebugTerrain)
            {
                point = hit.point;
            }
        }

        if (mDebugPosition != point)
        {
            mDebugPosition = point;
            SceneView.RepaintAll();
        }

        if (mDebugPosition == Vector3.zero)
        {
            return;
        }

        Color c = Color.yellow;

        c.a = 0.25f;

        int     trash;
        Vector3 scale = TerrainUtil.DeriveScale(mDebugTerrain, targ.Resolution, out trash, out trash);

        float xmin = mDebugPosition.x - scale.x * mDebugZoneSize;
        float zmin = mDebugPosition.z - scale.z * mDebugZoneSize;
        float xmax = mDebugPosition.x + scale.x * mDebugZoneSize;
        float zmax = mDebugPosition.z + scale.z * mDebugZoneSize;

        TriangleMesh mesh = TerrainUtil.TriangulateSurface(mDebugTerrain
                                                           , xmin, zmin, xmax, zmax
                                                           , targ.Resolution
                                                           , mDebugOffset);

        if (mesh != null)
        {
            DebugDraw.TriangleMesh(mesh.verts, mesh.tris, mesh.triCount, true, c);
        }
    }
    private void OnGUIDebug(TerrainCompiler targ)
    {
        // Getting the terrain requires a full terrain search.  So only check
        // for it occationally.
        if (mDebugEnabled)
        {
            double time = EditorApplication.timeSinceStartup;
            if (targ.terrainData != mLastSource || mLastCheck + CheckDelay > time)
            {
                Terrain terrain = targ.GetTerrain();
                mLastSource = targ.terrainData;
                mLastCheck  = time;
                if (mDebugTerrain != terrain)
                {
                    mDebugTerrain = terrain;
                    SceneView.RepaintAll();
                }
            }
        }
        else
        {
            mDebugTerrain = null;
            mLastCheck    = 0;
            mLastSource   = null;
        }

        EditorGUILayout.Separator();

        GUI.enabled = targ.terrainData;

        bool origChanged = GUI.changed;

        mDebugEnabled = EditorGUILayout.Toggle("Enable Preview", mDebugEnabled);

        if (!mDebugEnabled || !targ.terrainData)
        {
            return;
        }

        GUILayout.Label("Size");
        mDebugZoneSize = (int)GUILayout.HorizontalSlider(mDebugZoneSize, 10, 50);

        GUILayout.Label("Offset");
        mDebugOffset = GUILayout.HorizontalSlider(mDebugOffset, 0, 10);

        EditorGUILayout.Separator();

        string helpText;

        if (mDebugTerrain == null)
        {
            helpText = "There is no enabled terrain in the scene using the source terrain data.";
        }
        else
        {
            int     xc;
            int     zc;
            Vector3 scale    = TerrainUtil.DeriveScale(mDebugTerrain, targ.Resolution, out xc, out zc);
            int     triCount = (xc - 1) * (zc - 1) * 2;

            helpText = string.Format("Mouse-over the terrain in the scene to see a "
                                     + " triangulation preview. Trees are not included in the preview.\n\n"
                                     + "Total surface triangles: {0:N0}\n"
                                     + "Sample distance: {1:F1} x {2:F1}"
                                     , triCount, scale.x, scale.z);
        }

        GUILayout.Box(helpText, EditorUtil.HelpStyle, GUILayout.ExpandWidth(true));

        if (GUI.changed)
        {
            SceneView.RepaintAll();
        }

        GUI.changed = origChanged;
    }