MeshObjGenerator.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using System;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. namespace NRKernal.NRExamples
  13. {
  14. /// <summary> A mesh info processor to generate unity object. </summary>
  15. public class MeshObjGenerator : MonoBehaviour, IMeshInfoProcessor
  16. {
  17. /// <summary> Parent of every mesh object. </summary>
  18. [SerializeField]
  19. private Transform m_MeshObjRoot;
  20. /// <summary>
  21. /// GameObject for mesh generation, requires a MeshFilter component.
  22. /// MeshCollider and MeshRenderer components are optional.
  23. /// </summary>
  24. [SerializeField]
  25. private GameObject m_MeshObjPrefab;
  26. /// <summary> If set true, mesh objects are rendered using OverrideMaterial. </summary>
  27. [SerializeField]
  28. private bool m_UseOverrideMaterial;
  29. /// <summary> (Optional) Used as a override material. </summary>
  30. [SerializeField]
  31. private Material m_OverrideMaterial;
  32. private Dictionary<ulong, GameObject> m_MeshObjDict = new Dictionary<ulong, GameObject>();
  33. void Awake()
  34. {
  35. if (m_MeshObjPrefab == null)
  36. {
  37. NRDebugger.Warning("[MeshObjGenerator] MeshObjPrefab Null!");
  38. }
  39. else
  40. {
  41. Renderer renderer = m_MeshObjPrefab.GetComponent<Renderer>();
  42. if (renderer != null && m_UseOverrideMaterial)
  43. {
  44. renderer.sharedMaterial = m_OverrideMaterial;
  45. }
  46. }
  47. }
  48. void IMeshInfoProcessor.UpdateMeshInfo(ulong identifier, NRMeshingBlockState meshingBlockState, Mesh mesh)
  49. {
  50. if (m_MeshObjPrefab != null)
  51. {
  52. NRDebugger.Debug("[MeshObjGenerator] UpdateMeshInfo identifier: {0} meshingBlockState: {1}", identifier, meshingBlockState);
  53. GameObject go = null;
  54. MeshFilter meshFilter = null;
  55. MeshCollider meshCollider = null;
  56. switch (meshingBlockState)
  57. {
  58. case NRMeshingBlockState.NR_MESHING_BLOCK_STATE_NEW:
  59. case NRMeshingBlockState.NR_MESHING_BLOCK_STATE_UPDATED:
  60. if (m_MeshObjDict.ContainsKey(identifier))
  61. {
  62. go = m_MeshObjDict[identifier];
  63. if (go == null)
  64. m_MeshObjDict.Remove(identifier);
  65. }
  66. if (go == null)
  67. {
  68. go = Instantiate(m_MeshObjPrefab, m_MeshObjRoot);
  69. go.name = identifier.ToString();
  70. }
  71. meshFilter = go.GetComponent<MeshFilter>();
  72. meshFilter.sharedMesh = mesh;
  73. meshCollider = go.GetComponent<MeshCollider>();
  74. if (meshCollider != null)
  75. meshCollider.sharedMesh = mesh;
  76. if (!m_MeshObjDict.ContainsKey(identifier))
  77. m_MeshObjDict.Add(identifier, go);
  78. break;
  79. case NRMeshingBlockState.NR_MESHING_BLOCK_STATE_DELETED:
  80. if (m_MeshObjDict.ContainsKey(identifier))
  81. {
  82. go = m_MeshObjDict[identifier];
  83. Destroy(go);
  84. m_MeshObjDict.Remove(identifier);
  85. }
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. }
  92. void IMeshInfoProcessor.ClearMeshInfo()
  93. {
  94. NRDebugger.Debug("[MeshObjGenerator] ClearMeshInfo.");
  95. foreach (var identifier in m_MeshObjDict.Keys)
  96. {
  97. Destroy(m_MeshObjDict[identifier]);
  98. }
  99. m_MeshObjDict.Clear();
  100. }
  101. }
  102. }