Возможно ли создание безэкранного дисплея? / Хабр

TeleportArea generation using NavMesh

While developing for VR on Unity, I had a problem with TeleportArea, namely, by default it can only be created rectangular.

Rectangular TeleportArea

But this is quite inconvenient if there is a static object on TeleportArea and the player can get stuck in it.

This problem can be solved like this, but the solution will look a little unsatisfactory (well, for me personally).

The solution, but we already have 4 TeleportAreaThe solution, but we already have 4 TeleportArea

It seems that the problem has been solved, but

  1. Instead of one TeleportArea, we have as many as 4 of them
  2. This solution is difficult to implement for a large room with a large number of items
  3. This is not shit for Terrain

Incorrect work with TerainIncorrect work with Terain

Ready-made solution

there is a ready-made solution to this problem, but unfortunately it is for SteamVR 1.x https://github.com/Flafla2/Vive-Teleporter,

Therefore, I decided to solve this problem, because I was not the only one who suffered with her!

The solution to this problem

Unity has an excellent NavMesh character passability generator, it generates a terrain map for AI.

Baked NavMeshBaked NavMesh

Why not use it to generate TeleportArea. So I made a sketch of a script to convert NavMesh to Mesh.

NavMeshTriangulation triangles = NavMesh.CalculateTriangulation();
Mesh mesh = new Mesh();
mesh.vertices = triangles.vertices;
mesh.triangles = triangles.indices;

Knowing that TeleportArea uses MeshRender and MeshFilter for the movement zone, it was necessary to save it to the project folder for further manipulations.

public static void SaveMesh(Mesh mesh, string name, bool makeNewInstance, bool optimizeMesh)
    {
        Mesh meshToSave = (makeNewInstance) ? Instantiate(mesh) : mesh;

        if (optimizeMesh)
            MeshUtility.Optimize(meshToSave);

        AssetDatabase.CreateAsset(meshToSave, "Assets/GeneratedNavMeshes/" + name + ".asset");
        AssetDatabase.SaveAssets();
    }

I designed the tool in the form of an EditorWindow

[CustomEditor(typeof(NaveMeshToMeshGenerator))]
public class NavMeshToMeshEditor : Editor
{
    public override void OnInspectorGUI()
    {
        NaveMeshToMeshGenerator now = (NaveMeshToMeshGenerator) target;
        GUILayout.Label("Created by LivelyPuer https://github.com/LivelyPuer");
        now.BakeNaveMesh = GUILayout.Toggle(now.BakeNaveMesh, "Bake NavMesh", EditorStyles.toggle);
        if (GUILayout.Button("GenerateMesh"))
        {
            string curScene = EditorSceneManager.GetActiveScene().name;
            if (now.BakeNaveMesh)
            {
                NavMeshBuilder.BuildNavMesh();
            }
            NavMeshTriangulation triangles = NavMesh.CalculateTriangulation();
            Mesh mesh = new Mesh();
            mesh.vertices = triangles.vertices;
            mesh.triangles = triangles.indices;
            SaveMesh(mesh, "Mesh" + curScene, true, false);
            MeshCollider now_Mesh = now.GetComponent<MeshCollider>();
            now_Mesh.sharedMesh = mesh;
            MeshFilter nowMeshFilter = now.GetComponent<MeshFilter>();
            nowMeshFilter.mesh = mesh;
            Debug.Log("TeleportArea was created");
        }
    }
    public static void SaveMesh(Mesh mesh, string name, bool makeNewInstance, bool optimizeMesh)
    {
        Mesh meshToSave = (makeNewInstance) ? Instantiate(mesh) : mesh;

        if (optimizeMesh)
            MeshUtility.Optimize(meshToSave);

        AssetDatabase.CreateAsset(meshToSave, "Assets/GeneratedNavMeshes/" + name + ".asset");
        AssetDatabase.SaveAssets();
    }
}

How to use it

  1. Download the latest release from github https://github.com/LivelyPuer/NavMeshToTeleportArea
  2. Import it into Unity by pre-installing the SteamVR 2.x package
  3. Creating a room

Room decorationRoom decoration

4. Add Collider to all static objects and tick Static

StaticStatic

5. Add the SteamVR player’s prefab to the stage

6. Open Window/NavMesh to Mesh in the window

7. Select settings for TeleportArea

8. Click the “Generate” button in the window that opens

9. 2 objects will appear on the stage:

1. Teleporting(Movement system for SteamVR)

2. Our generated TeleportArea

10. Everything is ready!

Our finished result:

Thank you for reading the article, all suggestions for future improvement of the system are welcome in the comments.

The finished project can be found on github

VR APP development company | VR Company

Go to our cases Get a free quote