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;
    }