Skip to main content

How to Create a Mesh and Add Physics

This how-to covers:

  • Adding dynamic meshing to a scene with the default manager;
  • How prefabs work;
  • Adding physics collision to the mesh;
  • Making the mesh invisible;
  • Interacting with mesh physics.

Prerequisites

You will need a Unity project with Lightship AR enabled. For more information, see Installing ARDK 3.0.

Creating the Mesh

To create a mesh prefab:

  1. If the main scene is not AR-ready, set it up:

    1. Remove the Main Camera.

    2. Add an ARSession and XROrigin to the Hierarchy.

    3. Create an Empty GameObject under XROrigin and name it OcclusionManager. Add an AR Occlusion Manager component to it.

      Add occlusion manager
  2. Add an AR Mesh Manager to the XROrigin:

    1. Add an Empty GameObject to the XROrigin. Name it MeshManager.

    2. Select MeshManager and add an ARMeshManager Component to it.

    3. Click Add Component. In the search box, type "Lightship Meshing Extension" and select it.

      Add mesh manager
  3. Prepare the mesh for conversion to prefab:

    1. Create an Empty GameObject in the main scene and name it MeshChunk.

    2. Add three Components to MeshChunk; a Mesh Filter to hold geographic information, a Mesh Renderer to display the mesh (if desired), and a Mesh Collider to provide physics.

    3. Select the Mesh Renderer and add a Default Material to it.

      Create a MeshChunk game object like so
  4. Convert the mesh into a prefab and assign it:

    1. Drag MeshChunk into the Assets window, converting it into a prefab.

    2. Delete the MeshChunk GameObject from the scene.

    3. Open the MeshManager and drag the MeshChunk prefab to its Mesh Prefab slot in the Inspector.

      Create a MeshChunk prefab and add it to the ARMeshManager
  5. Test it out:

    1. Build to device or press play in the editor. A mesh overlay should appear on screen.

      Mesh overlay with playback

Adding Physics to the Mesh

Because the mesh has a Mesh Collider, the mesh will respond to any object using standard Unity physics collision. For example, the mesh can provide collision for creatures walking on meshed surfaces or inform game entities when they have bumped into walls. This exercise demonstrates this by allowing the player to shoot spheres into the scene and see them bounce off of the mesh.

To add physics to the mesh:

  1. Create a physics prefab and add rigid body collision to it:
  2. Create a Sphere and give it a name.
  3. Add a RigidBody Component to it.
  4. Drag the Sphere to the Assets window and delete it from the scene.
  5. Create a script to launch spheres:
  6. Create a new Script in Assets and name it Launcher.
  7. Copy the code from Code Block 1: Launcher, found later in this article.
  8. Create an Empty named LauncherScript in the scene, then add Launcher to it.
  9. Build to device and test! You should be able to tap anywhere on screen and shoot a sphere that bounces off of surfaces.

Making the Mesh Invisible

The simplest way to make the mesh invisible is to tell Unity not to render it. Delete the Mesh Renderer from the prefab and Unity will no longer be able to draw it, but this also means the mesh cannot provide occlusion and shadows in the scene. To keep this functionality, Lightship provides a shader called InvisibleWithShadows that will receive light and draw shadows without rendering the mesh directly.

To add an invisibility shader to the mesh:

  1. Create a new Material in the Assets window and name it InvisibleShader.
  2. Add the InvisibleWithShadows shader to InvisibleShader.
  3. Add InvisibleShader to the Mesh Renderer in the prefab.
Invisible mesh with shadows

Extra Credit: Custom Mesh Texturing

While this how-to will not cover them, there are other ways to leverage mesh textures for AR development. Two examples follow:

  • A custom mesh renderer (as used in the Meshing sample scene) could display normals to see triangle facing directions while debugging.
Custom shader to visualize normals
  • By default, the mesh renderer does not create a UV surface map, but using tri-planar projection when generating the mesh can create a UV map of the world space.

Launcher.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Launcher : MonoBehaviour
{
public GameObject _prefab;
void Update()
{
#if UNITY_EDITOR
if(Input.GetMouseButtonDown(0))
#else
if (Input.touchCount > 0)
#endif
{
//spawn in front of at the camera
var pos = Camera.main.transform.position;
var forw = Camera.main.transform.forward;
var thing = Instantiate(_prefab, pos+(forw*0.1f), Quaternion.identity);

//if it has physics fire it!
if (thing.TryGetComponent(out Rigidbody rb))
{
rb.AddForce(forw * 200.0f);
}
}
}
}

More Information

  • For more details on how this works, see Meshing.