Beispiel #1
0
    private void Compile(InputBuildContext context)
    {
        context.info.compilerCount++;

        InputGeometryCompiler compiler = context.geomCompiler;
        List <Component>      items    = context.components;
        List <byte>           areas    = context.areas;

        for (int i = 0; i < items.Count; i++)
        {
            Component item = items[i];

            if (item is Terrain)
            {
                Terrain terrain = (Terrain)item;

                if (terrain.terrainData != terrainData)
                {
                    continue;
                }

                TriangleMesh mesh   = TerrainUtil.TriangulateSurface(terrain, mResolution);
                byte[]       lareas = NMGen.CreateAreaBuffer(mesh.triCount, areas[i]);

                if (compiler.AddTriangles(mesh, lareas))
                {
                    string msg = string.Format("Compiled the {0} terrain surface. Triangles: {1}"
                                               , terrain.name, mesh.triCount);

                    context.Log(msg, this);
                }
                else
                {
                    string msg =
                        string.Format("Compiler rejected mesh for the {0} terrain.", terrain.name);

                    context.LogError(msg, this);

                    return;
                }

                if (includeTrees)
                {
                    int before = compiler.TriCount;

                    TerrainUtil.TriangluateTrees(terrain, areas[i], compiler);

                    string msg = string.Format("Compiled the {0} terrain trees. Triangles: {1}"
                                               , terrain.name, compiler.TriCount - before);

                    context.Log(msg, this);
                }

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