DHART
|
Previous Tutorial: Reading a Mesh From Unity
In this tutorial we'll be using the project created in the first tutorial: Project Setup, and we'll be using concepts and code covered in the previous guides.
In this guide we will cover:
The majority of this tutorial is specific to Unity, and is mostly about converting the mesh data from Unity into a format understandable by DHART.
In the previous tutorial we showed how to use a mesh from within Unity. Due to the wide-ranging file imports and settings for Unity, there are a few important requirements to understand when trying to use a prefab or object that was imported from Unity.
The first requirement is to enable the Read/Write
attribute of the imported file by selecting the model in the Assets browser and going to the Inspector
as shown in the following image:
For a similar reason (accessing vertices in Unity scripts), under the inspector for the model, the Batching Static
must not be selected. The object does not need to be static, but if it is, this particular attribute cannot be used.
There are a few functions we have written to help parse Unity-specific data that are not included in DHART (since they are specific to Unity).
MeshUtils.cs
In summary there are two main functions that we use, FlattenVertArray
and GetMeshesFromParent
. The first function has an option to convert the coordinates from Unity to DHART defaults, which we will use in the next section.
Now you will create another script called DHART_BVH.cs
.
This script will have a public facing variable than can be set in the Unity Editor. With the included references, the script starts with:
Next, within Start()
, we will set the mesh to active, use our helper function for getting meshes under this parent:
Note that this function is mostly used for nested objects, meaning that instead of a single mesh game object, we have a list of objects under a game object and we want to include all of them.
Now that we have a list of all the meshes, we iterate through each mesh, extract the triangles (vertex indices), get a vertex array with converted coordinates (using our helper function), and finally make a MeshInfo
object of that data.
The line that checks if (vert_array.Length > 0)
is IMPORTANT!. We must remove any meshes that do not have vertices. A simple way to do this at the end of the mesh conversion process is to remove the null values of the array (the indices we skipped).
Passing an invalid mesh to the BVH is a likely cause of crashes.
The only real function that we need is this.BVH = new EmbreeBVH(walkableMesh);
. However, we also show some nice helper functions to structure your project. For example, in this script we demonstrate how to check for a specific string name in the meshes (for example, if you had a naming convention), and remove the meshes that are not of interest.
Finally, we run a quick test to make sure our BVH was properly loaded. This example uses (0,0,1) and (0,0,-1) as the coordinates, but you should use any values that you know will be hit by a ray.
and of course, don't forget to close the Class and Start()
functions.
The final step is to save the file, go back into the editor, click and drag the DHART_BVH
script onto the Main Camera (you could also make an empty game object, but this is easier).
Once the script is attached to the game object, click the game object (Main Camera in this case). In the inspector you should see the Mesh Parent
that was created in our script. Without changing the selection, click and drag the mesh from the scene hierarchy to the Mesh Parent, as shown in the image.
And, thats its. You should be able to press play and the distance and mesh id should be displayed on the console.