Repository navigation
Expand file tree
/
Copy pathObjectSurfaceObserver.cs
More file actions
83 lines (72 loc) · 2.93 KB
/
Copy pathObjectSurfaceObserver.cs
File metadata and controls
83 lines (72 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
namespace HoloToolkit.Unity
{
public class ObjectSurfaceObserver : SpatialMappingSource
{
[Tooltip("The room model to use when loading meshes in Unity.")]
public GameObject roomModel;
// Use this for initialization.
private void Start()
{
#if UNITY_EDITOR
// When in the Unity editor, try loading saved meshes from a model.
Load(roomModel);
if (GetMeshFilters().Count > 0)
{
SpatialMappingManager.Instance.SetSpatialMappingSource(this);
}
#endif
}
/// <summary>
/// Loads the SpatialMapping mesh from the specified room object.
/// </summary>
/// <param name="roomModel">The room model to load meshes from.</param>
public void Load(GameObject roomModel)
{
if (roomModel == null)
{
Debug.Log("No room model specified.");
return;
}
GameObject roomObject = GameObject.Instantiate(roomModel);
Cleanup();
try
{
MeshFilter[] roomFilters = roomObject.GetComponentsInChildren<MeshFilter>();
foreach (MeshFilter filter in roomFilters)
{
GameObject surface = AddSurfaceObject(filter.sharedMesh, "roomMesh-" + surfaceObjects.Count, transform);
Renderer renderer = surface.GetComponent<MeshRenderer>();
if (SpatialMappingManager.Instance.DrawVisualMeshes == false)
{
renderer.enabled = false;
}
if (SpatialMappingManager.Instance.CastShadows == false)
{
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
}
// Reset the surface mesh collider to fit the updated mesh.
// Unity tribal knowledge indicates that to change the mesh assigned to a
// mesh collider, the mesh must first be set to null. Presumably there
// is a side effect in the setter when setting the shared mesh to null.
MeshCollider collider = surface.GetComponent<MeshCollider>();
collider.sharedMesh = null;
collider.sharedMesh = surface.GetComponent<MeshFilter>().sharedMesh;
}
}
catch
{
Debug.Log("Failed to load object " + roomModel.name);
}
finally
{
if (roomModel != null && roomObject != null)
{
GameObject.DestroyImmediate(roomObject);
}
}
}
}
}