NativeMeshing.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. namespace NRKernal
  10. {
  11. using System;
  12. using System.Runtime.InteropServices;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using UnityEngine;
  16. using System.Collections;
  17. /// <summary> Meshing Native API. </summary>
  18. public class NativeMeshing
  19. {
  20. /// <summary> Handle of meshing. </summary>
  21. private UInt64 m_MeshingHandle = 0;
  22. /// <summary> Handle of mesh info request. </summary>
  23. private UInt64 m_RequestMeshInfoHandle = 0;
  24. /// <summary> Handle of mesh info. </summary>
  25. private UInt64 m_MeshInfoHandle = 0;
  26. /// <summary> Handle of mesh detail request. </summary>
  27. private UInt64 m_RequestMeshDetailHandle = 0;
  28. /// <summary> Handle of mesh detail. </summary>
  29. private UInt64 m_MeshDetailHandle = 0;
  30. /// <summary> Struct contains information of a block. </summary>
  31. public struct BlockInfo
  32. {
  33. public ulong timestamp;
  34. public NRMeshingBlockState blockState;
  35. public NRMeshingFlags meshingFlag;
  36. }
  37. /// <summary> Dictionary contains the result of GetBlockInfoData. </summary>
  38. private Dictionary<ulong, BlockInfo> m_BlockInfos = new Dictionary<ulong, BlockInfo>();
  39. /// <summary>
  40. /// Create the Meshing system object.
  41. /// </summary>
  42. /// <returns> True if it succeeds, false if it fails. </returns>
  43. public bool Create()
  44. {
  45. var result = NativeApi.NRMeshingCreate(ref m_MeshingHandle);
  46. NRDebugger.Info("[NativeMeshing] NRMeshingCreate result: {0} MeshingHandle: {1}.", result, m_MeshingHandle);
  47. return result == NativeResult.Success;
  48. }
  49. /// <summary>
  50. /// Start the Meshing system object.
  51. /// </summary>
  52. /// <returns> True if it succeeds, false if it fails. </returns>
  53. public bool Start()
  54. {
  55. if (m_MeshingHandle == 0)
  56. {
  57. NRDebugger.Warning("[NativeMeshing] NRMeshingStart Zero MeshingHandle.");
  58. return false;
  59. }
  60. var result = NativeApi.NRMeshingStart(m_MeshingHandle);
  61. NRDebugger.Info("[NativeMeshing] NRMeshingStart result: {0}.", result);
  62. return result == NativeResult.Success;
  63. }
  64. /// <summary>
  65. /// Pause the Meshing system object.
  66. /// </summary>
  67. /// <returns> True if it succeeds, false if it fails. </returns>
  68. public bool Pause()
  69. {
  70. if (m_MeshingHandle == 0)
  71. {
  72. NRDebugger.Warning("[NativeMeshing] NRMeshingPause Zero MeshingHandle.");
  73. return false;
  74. }
  75. var result = NativeApi.NRMeshingPause(m_MeshingHandle);
  76. NRDebugger.Info("[NativeMeshing] NRMeshingPause result: {0}.", result);
  77. return result == NativeResult.Success;
  78. }
  79. /// <summary>
  80. /// Resume the Meshing system object.
  81. /// </summary>
  82. /// <returns> True if it succeeds, false if it fails. </returns>
  83. public bool Resume()
  84. {
  85. if (m_MeshingHandle == 0)
  86. {
  87. NRDebugger.Warning("[NativeMeshing] NRMeshingResume Zero MeshingHandle.");
  88. return false;
  89. }
  90. var result = NativeApi.NRMeshingResume(m_MeshingHandle);
  91. NRDebugger.Info("[NativeMeshing] NRMeshingResume result: {0}.", result);
  92. return result == NativeResult.Success;
  93. }
  94. /// <summary>
  95. /// Stop the Meshing system object.
  96. /// </summary>
  97. /// <returns> True if it succeeds, false if it fails. </returns>
  98. public bool Stop()
  99. {
  100. if (m_MeshingHandle == 0)
  101. {
  102. NRDebugger.Warning("[NativeMeshing] NRMeshingStop Zero MeshingHandle.");
  103. return false;
  104. }
  105. NativeResult result = NativeApi.NRMeshingStop(m_MeshingHandle);
  106. NRDebugger.Info("[NativeMeshing] NRMeshingStop result: {0}.", result);
  107. return result == NativeResult.Success;
  108. }
  109. /// <summary>
  110. /// Release memory used by the Meshing system object.
  111. /// </summary>
  112. /// <returns> True if it succeeds, false if it fails. </returns>
  113. public bool Destroy()
  114. {
  115. if (m_MeshingHandle == 0)
  116. {
  117. NRDebugger.Warning("[NativeMeshing] NRMeshingDestroy Zero MeshingHandle.");
  118. return true;
  119. }
  120. NativeResult result = NativeApi.NRMeshingDestroy(m_MeshingHandle);
  121. NRDebugger.Info("[NativeMeshing] NRMeshingDestroy result: {0}.", result);
  122. m_MeshingHandle = 0;
  123. return result == NativeResult.Success;
  124. }
  125. /// <summary>
  126. /// Set flags which mesh runtime will use.
  127. /// </summary>
  128. /// <param name="flags"> Request flag that are a combination of NRMeshingFlags. </param>
  129. /// <returns> True if it succeeds, false if it fails. </returns>
  130. public bool SetMeshingFlags(NRMeshingFlags flags)
  131. {
  132. if (m_MeshingHandle == 0)
  133. {
  134. NRDebugger.Warning("[NativeMeshing] NRMeshingSetFlags Zero MeshingHandle.");
  135. return false;
  136. }
  137. var result = NativeApi.NRMeshingSetFlags(m_MeshingHandle, flags);
  138. NRDebugger.Debug("[NativeMeshing] NRMeshingSetFlags result: {0} flag: {1}.", result, flags);
  139. return result == NativeResult.Success;
  140. }
  141. /// <summary>
  142. /// Request mesh info which includes state and bounding extents of the block.
  143. /// </summary>
  144. /// <param name="boundingBoxSize"> The size of interest region for meshing. </param>
  145. /// <param name="pose"> The pose of interest region for meshing. </param>
  146. /// <returns> True if it succeeds, false if it fails. </returns>
  147. public bool RequestMeshInfo(Vector3 boundingBoxSize, Pose pose)
  148. {
  149. if (m_MeshingHandle == 0)
  150. {
  151. NRDebugger.Warning("[NativeMeshing] NRMeshingRequestMeshInfo Zero MeshingHandle.");
  152. return false;
  153. }
  154. if (m_MeshInfoHandle != 0)
  155. {
  156. DestroyMeshInfo();
  157. }
  158. if (m_RequestMeshInfoHandle != 0)
  159. {
  160. DestroyMeshInfoRequest();
  161. }
  162. NRExtents extents = new NRExtents
  163. {
  164. transform = new NRTransform
  165. {
  166. position = new NativeVector3f(pose.position),
  167. rotation = new NativeVector4f(pose.rotation.x, pose.rotation.y, pose.rotation.z, pose.rotation.w)
  168. },
  169. extents = new NativeVector3f(boundingBoxSize)
  170. };
  171. var result = NativeApi.NRMeshingRequestMeshInfo(m_MeshingHandle, ref extents, ref m_RequestMeshInfoHandle);
  172. NRDebugger.Debug("[NativeMeshing] NRMeshingRequestMeshInfo result: {0} Handle: {1}.", result, m_RequestMeshInfoHandle);
  173. return result == NativeResult.Success;
  174. }
  175. /// <summary>
  176. /// Get the Result of a earlier request.
  177. /// </summary>
  178. /// <returns> True if it succeeds, false if it fails. </returns>
  179. public bool GetMeshInfoResult()
  180. {
  181. if (m_MeshingHandle == 0)
  182. {
  183. NRDebugger.Warning("[NativeMeshing] NRMeshingGetMeshInfoResult Zero MeshingHandle.");
  184. return false;
  185. }
  186. if (m_RequestMeshInfoHandle == 0)
  187. {
  188. NRDebugger.Warning("[NativeMeshing] NRMeshingGetMeshInfoResult Zero RequestMeshInfoHandle.");
  189. return false;
  190. }
  191. if (m_MeshInfoHandle != 0)
  192. {
  193. NRDebugger.Warning("[NativeMeshing] NRMeshingGetMeshInfoResult Nonzero MeshInfoHandle.");
  194. return true;
  195. }
  196. NativeResult result = NativeApi.NRMeshingGetMeshInfoResult(m_MeshingHandle, m_RequestMeshInfoHandle, ref m_MeshInfoHandle);
  197. NRDebugger.Debug("[NativeMeshing] NRMeshingGetMeshInfoResult result: {0} Handle: {1}.", result, m_MeshInfoHandle);
  198. return result == NativeResult.Success;
  199. }
  200. /// <summary>
  201. /// Get response timestamp (in nano seconds) to a earlier request.
  202. /// </summary>
  203. /// <returns> The timestamp in nano seconds. </returns>
  204. public ulong GetMeshInfoTimestamp()
  205. {
  206. if (m_MeshingHandle == 0)
  207. {
  208. NRDebugger.Warning("[NativeMeshing] NRMeshInfoGetTimestamp Zero MeshingHandle.");
  209. return 0;
  210. }
  211. if (m_MeshInfoHandle == 0)
  212. {
  213. NRDebugger.Warning("[NativeMeshing] NRMeshInfoGetTimestamp Zero MeshInfoHandle.");
  214. return 0;
  215. }
  216. ulong timeStamp = 0;
  217. NativeResult result = NativeApi.NRMeshInfoGetTimestamp(m_MeshingHandle, m_MeshInfoHandle, ref timeStamp);
  218. NRDebugger.Debug("[NativeMeshing] NRMeshInfoGetTimestamp result: {0} timestamp: {1}.", result, timeStamp);
  219. return timeStamp;
  220. }
  221. /// <summary>
  222. /// Get block information of interest region.
  223. /// </summary>
  224. /// <returns> True if it succeeds, false if it fails. </returns>
  225. public bool GetBlockInfoData()
  226. {
  227. if (m_MeshingHandle == 0)
  228. {
  229. NRDebugger.Warning("[NativeMeshing] NRMeshInfoGetBlockInfoCount Zero MeshingHandle.");
  230. return false;
  231. }
  232. if (m_MeshInfoHandle == 0)
  233. {
  234. NRDebugger.Warning("[NativeMeshing] NRMeshInfoGetBlockInfoCount Zero MeshInfoHandle.");
  235. return false;
  236. }
  237. uint blockCount = 0;
  238. m_BlockInfos.Clear();
  239. NativeResult retResult = NativeApi.NRMeshInfoGetBlockInfoCount(m_MeshingHandle, m_MeshInfoHandle, ref blockCount);
  240. NRDebugger.Debug("[NativeMeshing] NRMeshInfoGetBlockInfoCount result: {0} blockCount: {1}.", retResult, blockCount);
  241. for (uint i = 0; i < blockCount; i++)
  242. {
  243. UInt64 blockInfoHandle = 0;
  244. NativeResult result = NativeApi.NRMeshInfoGetBlockInfoData(m_MeshingHandle, m_MeshInfoHandle, i, ref blockInfoHandle);
  245. NRDebugger.Debug("[NativeMeshing] NRMeshInfoGetBlockInfoData result: {0} Handle: {1}.", result, blockInfoHandle);
  246. BlockInfo blockInfo = new BlockInfo();
  247. ulong identifier = 0;
  248. result = NativeApi.NRBlockInfoGetBlockIdentifier(m_MeshingHandle, blockInfoHandle, ref identifier);
  249. NRDebugger.Debug("[NativeMeshing] NRBlockInfoGetBlockIdentifier result: {0} identifier: {1}.", result, identifier);
  250. result = NativeApi.NRBlockInfoGetTimestamp(m_MeshingHandle, blockInfoHandle, ref blockInfo.timestamp);
  251. NRDebugger.Debug("[NativeMeshing] NRBlockInfoGetTimestamp result: {0} timestamp: {1}.", result, blockInfo.timestamp);
  252. result = NativeApi.NRBlockInfoGetBlockState(m_MeshingHandle, blockInfoHandle, ref blockInfo.blockState);
  253. NRDebugger.Debug("[NativeMeshing] NRBlockInfoGetBlockState result: {0} blockState: {1}.", result, blockInfo.blockState);
  254. result = NativeApi.NRBlockInfoDestroy(m_MeshingHandle, blockInfoHandle);
  255. NRDebugger.Debug("[NativeMeshing] NRBlockInfoDestroy: {0}.", result);
  256. m_BlockInfos.Add(identifier, blockInfo);
  257. }
  258. return retResult == NativeResult.Success;
  259. }
  260. /// <summary>
  261. /// Destroy the mesh info handle.
  262. /// </summary>
  263. /// <returns> True if it succeeds, false if it fails. </returns>
  264. public bool DestroyMeshInfo()
  265. {
  266. if (m_MeshingHandle == 0)
  267. {
  268. NRDebugger.Warning("[NativeMeshing] NRMeshInfoDestroy Zero MeshingHandle.");
  269. return false;
  270. }
  271. if (m_MeshInfoHandle == 0)
  272. {
  273. NRDebugger.Warning("[NativeMeshing] NRMeshInfoDestroy Zero MeshInfoHandle.");
  274. return true;
  275. }
  276. NativeResult result = NativeApi.NRMeshInfoDestroy(m_MeshingHandle, m_MeshInfoHandle);
  277. NRDebugger.Debug("[NativeMeshing] NRMeshInfoDestroy result: {0}.", result);
  278. m_MeshInfoHandle = 0;
  279. return result == NativeResult.Success;
  280. }
  281. /// <summary>
  282. /// Destroy the request handle.
  283. /// </summary>
  284. /// <returns> True if it succeeds, false if it fails. </returns>
  285. public bool DestroyMeshInfoRequest()
  286. {
  287. if (m_MeshingHandle == 0)
  288. {
  289. NRDebugger.Warning("[NativeMeshing] NRMeshingMeshInfoRequestDestroy Zero MeshingHandle.");
  290. return false;
  291. }
  292. if (m_RequestMeshInfoHandle == 0)
  293. {
  294. NRDebugger.Warning("[NativeMeshing] NRMeshingMeshInfoRequestDestroy Zero RequestMeshInfoHandle.");
  295. return true;
  296. }
  297. NativeResult result = NativeApi.NRMeshingMeshInfoRequestDestroy(m_MeshingHandle, m_RequestMeshInfoHandle);
  298. NRDebugger.Debug("[NativeMeshing] NRMeshingMeshInfoRequestDestroy result: {0}.", result);
  299. m_RequestMeshInfoHandle = 0;
  300. return result == NativeResult.Success;
  301. }
  302. /// <summary>
  303. /// Request mesh detail for all blocks in request.
  304. /// </summary>
  305. /// <param name="predicate"> The search function of block infos. </param>
  306. /// <returns> True if it succeeds, false if it fails. </returns>
  307. public bool RequestMeshDetail(Func<KeyValuePair<ulong, BlockInfo>, bool> predicate = null)
  308. {
  309. if (m_MeshingHandle == 0)
  310. {
  311. NRDebugger.Warning("[NativeMeshing] NRMeshingRequestMeshDetail Zero MeshingHandle.");
  312. return false;
  313. }
  314. if (m_MeshDetailHandle != 0)
  315. {
  316. DestroyMeshDetail();
  317. }
  318. if (m_RequestMeshDetailHandle != 0)
  319. {
  320. DestroyMeshDetailRequest();
  321. }
  322. ulong[] blockIdentifiers = m_BlockInfos.Where(predicate ?? (p => true)).Select(p => p.Key).ToArray();
  323. if (blockIdentifiers.Length == 0)
  324. {
  325. NRDebugger.Warning("[NativeMeshing] NRMeshingRequestMeshDetail Zero blockIdentifier.");
  326. return false;
  327. }
  328. var result = NativeApi.NRMeshingRequestMeshDetail(m_MeshingHandle, (uint)blockIdentifiers.Length, blockIdentifiers, ref m_RequestMeshDetailHandle);
  329. NRDebugger.Debug("[NativeMeshing] NRMeshingRequestMeshDetail result: {0} Handle: {1}.", result, m_RequestMeshDetailHandle);
  330. return result == NativeResult.Success;
  331. }
  332. /// <summary>
  333. /// Get the Result of a earlier request.
  334. /// </summary>
  335. /// <returns> True if it succeeds, false if it fails. </returns>
  336. public bool GetMeshDetailResult()
  337. {
  338. if (m_MeshingHandle == 0)
  339. {
  340. NRDebugger.Warning("[NativeMeshing] NRMeshingGetMeshDetailResult Zero MeshingHandle.");
  341. return false;
  342. }
  343. if (m_RequestMeshDetailHandle == 0)
  344. {
  345. NRDebugger.Warning("[NativeMeshing] NRMeshingGetMeshDetailResult Zero RequestMeshDetailHandle.");
  346. return false;
  347. }
  348. if (m_MeshDetailHandle != 0)
  349. {
  350. NRDebugger.Warning("[NativeMeshing] NRMeshingGetMeshDetailResult Nonzero MeshDetailHandle.");
  351. return true;
  352. }
  353. NativeResult result = NativeApi.NRMeshingGetMeshDetailResult(m_MeshingHandle, m_RequestMeshDetailHandle, ref m_MeshDetailHandle);
  354. NRDebugger.Debug("[NativeMeshing] NRMeshingGetMeshDetailResult result: {0} Handle: {1}.", result, m_MeshDetailHandle);
  355. return result == NativeResult.Success;
  356. }
  357. /// <summary>
  358. /// Get response timestamp (in nano seconds) to a earlier request.
  359. /// </summary>
  360. /// <returns> The timestamp in nano seconds. </returns>
  361. public ulong GetMeshDetailTimestamp()
  362. {
  363. if (m_MeshingHandle == 0)
  364. {
  365. NRDebugger.Warning("[NativeMeshing] NRMeshDetailGetTimestamp Zero MeshingHandle.");
  366. return 0;
  367. }
  368. if (m_MeshDetailHandle == 0)
  369. {
  370. NRDebugger.Warning("[NativeMeshing] NRMeshDetailGetTimestamp Zero MeshDetailHandle.");
  371. return 0;
  372. }
  373. ulong timeStamp = 0;
  374. NativeResult result = NativeApi.NRMeshDetailGetTimestamp(m_MeshingHandle, m_MeshDetailHandle, ref timeStamp);
  375. NRDebugger.Debug("[NativeMeshing] NRMeshDetailGetTimestamp: {0} {1}.", result, timeStamp);
  376. return timeStamp;
  377. }
  378. /// <summary>
  379. /// Get block detail data in request
  380. /// </summary>
  381. /// <returns> True if it succeeds, false if it fails. </returns>
  382. public IEnumerator GetMeshDetailData(Action<ulong, NRMeshingBlockState, Mesh> action)
  383. {
  384. if (m_MeshingHandle == 0)
  385. {
  386. NRDebugger.Warning("[NativeMeshing] NRMeshDetailGetBlockDetailCount Zero MeshingHandle.");
  387. yield break;
  388. }
  389. if (m_MeshDetailHandle == 0)
  390. {
  391. NRDebugger.Warning("[NativeMeshing] NRMeshDetailGetBlockDetailCount Zero MeshDetailHandle.");
  392. yield break;
  393. }
  394. ulong blockDetailCount = 0;
  395. NativeResult retResult = NativeApi.NRMeshDetailGetBlockDetailCount(m_MeshingHandle, m_MeshDetailHandle, ref blockDetailCount);
  396. NRDebugger.Debug("[NativeMeshing] NRMeshDetailGetBlockDetailCount result: {0} blockDetailCount: {1}.", retResult, blockDetailCount);
  397. for (uint i = 0; i < blockDetailCount; i++)
  398. {
  399. UInt64 blockDetailHandle = 0;
  400. NativeResult result = NativeApi.NRMeshDetailGetBlockDetailData(m_MeshingHandle, m_MeshDetailHandle, i, ref blockDetailHandle);
  401. NRDebugger.Debug("[NativeMeshing] NRMeshDetailGetBlockDetailData result: {0} Handle: {1}.", result, blockDetailHandle);
  402. ulong identifier = 0;
  403. result = NativeApi.NRBlockDetailGetBlockIdentifier(m_MeshingHandle, blockDetailHandle, ref identifier);
  404. NRDebugger.Debug("[NativeMeshing] NRBlockDetailGetBlockIdentifier result: {0} identifier: {1}.", result, identifier);
  405. NRMeshingFlags meshingFlag = NRMeshingFlags.NR_MESHING_FLAGS_NULL;
  406. result = NativeApi.NRBlockDetailGetFlags(m_MeshingHandle, blockDetailHandle, ref meshingFlag);
  407. NRDebugger.Debug("[NativeMeshing] NRBlockDetailGetFlags result: {0} meshingFlag: {1}.", result, meshingFlag);
  408. uint vertexCount = 0;
  409. result = NativeApi.NRBlockDetailGetVertexCount(m_MeshingHandle, blockDetailHandle, ref vertexCount);
  410. NRDebugger.Debug("[NativeMeshing] NRBlockDetailGetVertexCount result: {0} vertexCount: {1}.", result, vertexCount);
  411. if (vertexCount != 0)
  412. {
  413. NativeVector3f[] outVertices = new NativeVector3f[vertexCount];
  414. result = NativeApi.NRBlockDetailGetVertices(m_MeshingHandle, blockDetailHandle, outVertices);
  415. NRDebugger.Debug("[NativeMeshing] NRBlockDetailGetVertices result: {0}.", result);
  416. NativeVector3f[] outNormals = new NativeVector3f[vertexCount];
  417. result = NativeApi.NRBlockDetailGetNormals(m_MeshingHandle, blockDetailHandle, outNormals);
  418. NRDebugger.Debug("[NativeMeshing] NRBlockDetailGetNormals result: {0}.", result);
  419. uint indexCount = 0;
  420. result = NativeApi.NRBlockDetailGetIndexCount(m_MeshingHandle, blockDetailHandle, ref indexCount);
  421. NRDebugger.Debug("[NativeMeshing] NRBlockDetailGetIndexCount result: {0} indexCount: {1}.", result, indexCount);
  422. ushort[] outIndex = new ushort[indexCount];
  423. result = NativeApi.NRBlockDetailGetIndeices(m_MeshingHandle, blockDetailHandle, outIndex);
  424. NRDebugger.Debug("[NativeMeshing] NRBlockDetailGetIndeices result: {0}.", result);
  425. Vector3[] vertices = new Vector3[vertexCount];
  426. Vector3[] normals = new Vector3[vertexCount];
  427. for (int j = 0; j < vertexCount; j++)
  428. {
  429. vertices[j] = outVertices[j].ToUnityVector3();
  430. normals[j] = outNormals[j].ToUnityVector3();
  431. }
  432. int[] triangles = new int[indexCount];
  433. for (int j = 0; j < indexCount; j++)
  434. {
  435. triangles[j] = outIndex[j];
  436. }
  437. Mesh mesh = new Mesh
  438. {
  439. vertices = vertices,
  440. normals = normals,
  441. triangles = triangles
  442. };
  443. mesh.RecalculateBounds();
  444. action?.Invoke(identifier, m_BlockInfos[identifier].blockState, mesh);
  445. NRDebugger.Debug("[NativeMeshing] GetMeshDetailData Invoke: {0} {1} {2}.", identifier, m_BlockInfos[identifier].blockState, mesh.vertexCount);
  446. }
  447. result = NativeApi.NRBlockDetailDestroy(m_MeshingHandle, blockDetailHandle);
  448. NRDebugger.Debug("[NativeMeshing] NRBlockDetailDestroy result: {0}.", result);
  449. yield return null;
  450. }
  451. }
  452. /// <summary>
  453. /// Destroy the mesh detail handle.
  454. /// </summary>
  455. /// <returns> True if it succeeds, false if it fails. </returns>
  456. public bool DestroyMeshDetail()
  457. {
  458. if (m_MeshingHandle == 0)
  459. {
  460. NRDebugger.Warning("[NativeMeshing] NRMeshDetailDestroy Zero MeshingHandle.");
  461. return false;
  462. }
  463. if (m_MeshDetailHandle == 0)
  464. {
  465. NRDebugger.Warning("[NativeMeshing] NRMeshDetailDestroy Zero MeshDetailHandle.");
  466. return true;
  467. }
  468. NativeResult result = NativeApi.NRMeshDetailDestroy(m_MeshingHandle, m_MeshDetailHandle);
  469. NRDebugger.Debug("[NativeMeshing] NRMeshDetailDestroy.");
  470. m_MeshDetailHandle = 0;
  471. return result == NativeResult.Success;
  472. }
  473. /// <summary>
  474. /// Destroy the request handle.
  475. /// </summary>
  476. /// <returns> True if it succeeds, false if it fails. </returns>
  477. public bool DestroyMeshDetailRequest()
  478. {
  479. if (m_MeshingHandle == 0)
  480. {
  481. NRDebugger.Warning("[NativeMeshing] NRMeshingMeshDetailRequestDestroy Zero MeshingHandle.");
  482. return false;
  483. }
  484. if (m_RequestMeshDetailHandle == 0)
  485. {
  486. NRDebugger.Warning("[NativeMeshing] NRMeshingMeshDetailRequestDestroy Zero RequestMeshDetailHandle.");
  487. return true;
  488. }
  489. NativeResult result = NativeApi.NRMeshingMeshDetailRequestDestroy(m_MeshingHandle, m_RequestMeshDetailHandle);
  490. NRDebugger.Debug("[NativeMeshing] NRMeshingMeshDetailRequestDestroy.");
  491. m_RequestMeshDetailHandle = 0;
  492. return result == NativeResult.Success;
  493. }
  494. private partial struct NativeApi
  495. {
  496. #region LifeCycle
  497. /// <summary> Create the Meshing system object. </summary>
  498. /// <param name="out_meshing_handle"> The handle of Meshing. </param>
  499. /// <returns> The result of operation. </returns>
  500. [DllImport(NativeConstants.NRNativeLibrary)]
  501. public static extern NativeResult NRMeshingCreate(ref UInt64 out_meshing_handle);
  502. /// <summary> Start the Meshing system object. </summary>
  503. /// <param name="meshing_handle"> The handle of Meshing. </param>
  504. /// <returns> The result of operation. </returns>
  505. [DllImport(NativeConstants.NRNativeLibrary)]
  506. public static extern NativeResult NRMeshingStart(UInt64 meshing_handle);
  507. /// <summary> Pause the Meshing system object. </summary>
  508. /// <param name="meshing_handle"> The handle of Meshing. </param>
  509. /// <returns> The result of operation. </returns>
  510. [DllImport(NativeConstants.NRNativeLibrary)]
  511. public static extern NativeResult NRMeshingPause(UInt64 meshing_handle);
  512. /// <summary> Resume the Meshing system object. </summary>
  513. /// <param name="meshing_handle"> The handle of Meshing. </param>
  514. /// <returns> The result of operation. </returns>
  515. [DllImport(NativeConstants.NRNativeLibrary)]
  516. public static extern NativeResult NRMeshingResume(UInt64 meshing_handle);
  517. /// <summary> Stop the Meshing system object. </summary>
  518. /// <param name="meshing_handle"> The handle of Meshing. </param>
  519. /// <returns> The result of operation. </returns>
  520. [DllImport(NativeConstants.NRNativeLibrary)]
  521. public static extern NativeResult NRMeshingStop(UInt64 meshing_handle);
  522. /// <summary> Release memory used by the Meshing system object. </summary>
  523. /// <param name="meshing_handle"> The handle of Meshing. </param>
  524. /// <returns> The result of operation. </returns>
  525. [DllImport(NativeConstants.NRNativeLibrary)]
  526. public static extern NativeResult NRMeshingDestroy(UInt64 meshing_handle);
  527. /// <summary> Set flags which mesh runtime will use. </summary>
  528. /// <param name="meshing_handle"> The handle of Meshing. </param>
  529. /// <param name="flags"> Request flags that are a combination of NRMeshingFlags. </param>
  530. /// <returns> The result of operation. </returns>
  531. [DllImport(NativeConstants.NRNativeLibrary)]
  532. public static extern NativeResult NRMeshingSetFlags(UInt64 meshing_handle, NRMeshingFlags flags);
  533. #endregion
  534. #region MeshInfo
  535. /// <summary> Request mesh info which includes state and bounding extents of the block. </summary>
  536. /// <param name="meshing_handle"> The handle of Meshing. </param>
  537. /// <param name="extents"> The region of interest for meshing. </param>
  538. /// <param name="out_request_mesh_info_handle"> The handle of request for mesh info, which identifies the request. </param>
  539. /// <returns> The result of operation. If the meshing is computing, the result will be NR_RESULT_BUSY, until the NRMeshingGetMeshInfoResult return success. </returns>
  540. [DllImport(NativeConstants.NRNativeLibrary)]
  541. public static extern NativeResult NRMeshingRequestMeshInfo(UInt64 meshing_handle, ref NRExtents extents, ref UInt64 out_request_mesh_info_handle);
  542. /// <summary> Get the Result of a earlier request. </summary>
  543. /// <param name="meshing_handle"> The handle of Meshing. </param>
  544. /// <param name="request_mesh_info_handle"> The handle of request for mesh info, which identifies the request. </param>
  545. /// <param name="out_mesh_info_handle"> The handle of mesh info. </param>
  546. /// <returns> The result of operation. </returns>
  547. [DllImport(NativeConstants.NRNativeLibrary)]
  548. public static extern NativeResult NRMeshingGetMeshInfoResult(UInt64 meshing_handle, UInt64 request_mesh_info_handle, ref UInt64 out_mesh_info_handle);
  549. /// <summary> Get response timestamp (in nano seconds) to a earlier request. </summary>
  550. /// <param name="meshing_handle"> The handle of Meshing. </param>
  551. /// <param name="mesh_info_handle"> The handle of mesh info. </param>
  552. /// <param name="out_hmd_time_nanos"> The timestamp in nano seconds. </param>
  553. /// <returns> The result of operation. </returns>
  554. [DllImport(NativeConstants.NRNativeLibrary)]
  555. public static extern NativeResult NRMeshInfoGetTimestamp(UInt64 meshing_handle, UInt64 mesh_info_handle, ref ulong out_hmd_time_nanos);
  556. /// <summary> Get the number of elements in block info buffer. </summary>
  557. /// <param name="meshing_handle"> The handle of Meshing. </param>
  558. /// <param name="mesh_info_handle"> The handle of mesh info. </param>
  559. /// <param name="out_block_info_count"> The count of block info. </param>
  560. /// <returns> The result of operation. </returns>
  561. [DllImport(NativeConstants.NRNativeLibrary)]
  562. public static extern NativeResult NRMeshInfoGetBlockInfoCount(UInt64 meshing_handle, UInt64 mesh_info_handle, ref uint out_block_info_count);
  563. /// <summary> Get block info data reference to specific index. </summary>
  564. /// <param name="meshing_handle"> The handle of Meshing. </param>
  565. /// <param name="mesh_info_handle"> The handle of mesh info. </param>
  566. /// <param name="index"> The index of block in mesh, which should be less then block_info_count. </param>
  567. /// <param name="out_block_info_handle"> The handle of block info. </param>
  568. /// <returns> The result of operation. </returns>
  569. [DllImport(NativeConstants.NRNativeLibrary)]
  570. public static extern NativeResult NRMeshInfoGetBlockInfoData(UInt64 meshing_handle, UInt64 mesh_info_handle, uint index, ref UInt64 out_block_info_handle);
  571. /// <summary> Get block identifier. </summary>
  572. /// <param name="meshing_handle"> The handle of Meshing. </param>
  573. /// <param name="block_info_handle"> The handle of block info. </param>
  574. /// <param name="out_block_identifier"> The identifier of block. </param>
  575. /// <returns> The result of operation. </returns>
  576. [DllImport(NativeConstants.NRNativeLibrary)]
  577. public static extern NativeResult NRBlockInfoGetBlockIdentifier(UInt64 meshing_handle, UInt64 block_info_handle, ref ulong out_block_identifier);
  578. /// <summary> Get timestamp (in nano seconds) when block was updated. </summary>
  579. /// <param name="meshing_handle"> The handle of Meshing. </param>
  580. /// <param name="block_info_handle"> The handle of block info. </param>
  581. /// <param name="out_hmd_time_nanos"> The timestamp in nano seconds. </param>
  582. /// <returns> The result of operation. </returns>
  583. [DllImport(NativeConstants.NRNativeLibrary)]
  584. public static extern NativeResult NRBlockInfoGetTimestamp(UInt64 meshing_handle, UInt64 block_info_handle, ref ulong out_hmd_time_nanos);
  585. /// <summary> Get the state of block. </summary>
  586. /// <param name="meshing_handle"> The handle of Meshing. </param>
  587. /// <param name="block_info_handle"> The handle of block info. </param>
  588. /// <param name="out_block_state"> The state of block. </param>
  589. /// <returns> The result of operation. </returns>
  590. [DllImport(NativeConstants.NRNativeLibrary)]
  591. public static extern NativeResult NRBlockInfoGetBlockState(UInt64 meshing_handle, UInt64 block_info_handle, ref NRMeshingBlockState out_block_state);
  592. /// <summary> Destroy the block info handle. </summary>
  593. /// <param name="meshing_handle"> The handle of Meshing. </param>
  594. /// <param name="block_info_handle"> The handle of block info. </param>
  595. /// <returns> The result of operation. </returns>
  596. [DllImport(NativeConstants.NRNativeLibrary)]
  597. public static extern NativeResult NRBlockInfoDestroy(UInt64 meshing_handle, UInt64 block_info_handle);
  598. /// <summary> Destroy the mesh info handle. </summary>
  599. /// <param name="meshing_handle"> The handle of Meshing. </param>
  600. /// <param name="mesh_info_handle"> The handle of mesh info. </param>
  601. /// <returns> The result of operation. </returns>
  602. [DllImport(NativeConstants.NRNativeLibrary)]
  603. public static extern NativeResult NRMeshInfoDestroy(UInt64 meshing_handle, UInt64 mesh_info_handle);
  604. /// <summary> Destroy the request handle. </summary>
  605. /// <param name="meshing_handle"> The handle of Meshing. </param>
  606. /// <param name="request_mesh_info_handle"> The handle of request for mesh info, which identifies the request. </param>
  607. /// <returns> The result of operation. </returns>
  608. [DllImport(NativeConstants.NRNativeLibrary)]
  609. public static extern NativeResult NRMeshingMeshInfoRequestDestroy(UInt64 meshing_handle, UInt64 request_mesh_info_handle);
  610. #endregion
  611. #region MeshDetail
  612. /// <summary> Request mesh detail for all blocks in request. </summary>
  613. /// <param name="meshing_handle"> The handle of Meshing. </param>
  614. /// <param name="block_identifier_count"> The numbers of block identifiers. </param>
  615. /// <param name="block_identifiers"> All blocks identifies to request. </param>
  616. /// <param name="out_request_mesh_detail_handle"> The handle of request for mesh detail, which identifies the request. </param>
  617. /// <returns> The result of operation. If the meshing is computing, the result will be NR_RESULT_BUSY, until the NRMeshingGetMeshDetailResult return success. </returns>
  618. [DllImport(NativeConstants.NRNativeLibrary)]
  619. public static extern NativeResult NRMeshingRequestMeshDetail(UInt64 meshing_handle, uint block_identifier_count, ulong[] block_identifiers, ref UInt64 out_request_mesh_detail_handle);
  620. /// <summary> Get the Result of a earlier request. </summary>
  621. /// <param name="meshing_handle"> The handle of Meshing. </param>
  622. /// <param name="request_mesh_detail_handle"> The handle of request for mesh detail, which identifies the request. </param>
  623. /// <param name="out_mesh_detail_handle"> The handle of mesh detail. </param>
  624. /// <returns> The result of operation. </returns>
  625. [DllImport(NativeConstants.NRNativeLibrary)]
  626. public static extern NativeResult NRMeshingGetMeshDetailResult(UInt64 meshing_handle, UInt64 request_mesh_detail_handle, ref UInt64 out_mesh_detail_handle);
  627. /// <summary> Get the timestamp (in nano seconds) when data was generated. </summary>
  628. /// <param name="meshing_handle"> The handle of Meshing. </param>
  629. /// <param name="mesh_detail_handle"> The handle of mesh detail. </param>
  630. /// <param name="out_hmd_time_nanos"> The timestamp in nano seconds. </param>
  631. /// <returns> The result of operation. </returns>
  632. [DllImport(NativeConstants.NRNativeLibrary)]
  633. public static extern NativeResult NRMeshDetailGetTimestamp(UInt64 meshing_handle, UInt64 mesh_detail_handle, ref ulong out_hmd_time_nanos);
  634. /// <summary> Get the number of element in block detail buffer. </summary>
  635. /// <param name="meshing_handle"> The handle of Meshing. </param>
  636. /// <param name="mesh_detail_handle"> The handle of mesh detail. </param>
  637. /// <param name="out_block_detail_count"> The count of block detail. </param>
  638. /// <returns> The result of operation. </returns>
  639. [DllImport(NativeConstants.NRNativeLibrary)]
  640. public static extern NativeResult NRMeshDetailGetBlockDetailCount(UInt64 meshing_handle, UInt64 mesh_detail_handle, ref ulong out_block_detail_count);
  641. /// <summary> Get block detail data reference to specific index. </summary>
  642. /// <param name="meshing_handle"> The handle of Meshing. </param>
  643. /// <param name="mesh_detail_handle"> The handle of mesh detail. </param>
  644. /// <param name="index"> The index of block in mesh, which should be less then block_detail_count. </param>
  645. /// <param name="out_block_detail_handle"> The handle of block detail. </param>
  646. /// <returns> The result of operation. </returns>
  647. [DllImport(NativeConstants.NRNativeLibrary)]
  648. public static extern NativeResult NRMeshDetailGetBlockDetailData(UInt64 meshing_handle, UInt64 mesh_detail_handle, uint index, ref UInt64 out_block_detail_handle);
  649. /// <summary> Get block identifier. </summary>
  650. /// <param name="meshing_handle"> The handle of Meshing. </param>
  651. /// <param name="block_detail_handle"> The handle of block detail. </param>
  652. /// <param name="out_block_identifier"> The identifier of block. </param>
  653. /// <returns> The result of operation. </returns>
  654. [DllImport(NativeConstants.NRNativeLibrary)]
  655. public static extern NativeResult NRBlockDetailGetBlockIdentifier(UInt64 meshing_handle, UInt64 block_detail_handle, ref ulong out_block_identifier);
  656. /// <summary> Get block flags which mesh block took place. </summary>
  657. /// <param name="meshing_handle"> The handle of Meshing. </param>
  658. /// <param name="block_detail_handle"> The handle of block detail. </param>
  659. /// <param name="out_flags"> Flags that are a combination of NRMeshingFlags. </param>
  660. /// <returns> The result of operation. </returns>
  661. [DllImport(NativeConstants.NRNativeLibrary)]
  662. public static extern NativeResult NRBlockDetailGetFlags(UInt64 meshing_handle, UInt64 block_detail_handle, ref NRMeshingFlags out_flags);
  663. /// <summary> Get the number of vertices in vertex/normal buffer. </summary>
  664. /// <param name="meshing_handle"> The handle of Meshing. </param>
  665. /// <param name="block_detail_handle"> The handle of block detail. </param>
  666. /// <param name="out_vertex_count"> Number of elements in buffer. </param>
  667. /// <returns> The result of operation. </returns>
  668. [DllImport(NativeConstants.NRNativeLibrary)]
  669. public static extern NativeResult NRBlockDetailGetVertexCount(UInt64 meshing_handle, UInt64 block_detail_handle, ref uint out_vertex_count);
  670. /// <summary> Get the pointer to vertex buffer. </summary>
  671. /// <param name="meshing_handle"> The handle of Meshing. </param>
  672. /// <param name="block_detail_handle"> The handle of block detail. </param>
  673. /// <param name="out_vertices"> Pointer to vertex buffer. </param>
  674. /// <returns> The result of operation. </returns>
  675. [DllImport(NativeConstants.NRNativeLibrary)]
  676. public static extern NativeResult NRBlockDetailGetVertices(UInt64 meshing_handle, UInt64 block_detail_handle, NativeVector3f[] out_vertices);
  677. /// <summary> Get the pointer to normal buffer. </summary>
  678. /// <param name="meshing_handle"> The handle of Meshing. </param>
  679. /// <param name="block_detail_handle"> The handle of block detail. </param>
  680. /// <param name="out_normals"> Pointer to normal buffer. </param>
  681. /// <returns> The result of operation. </returns>
  682. [DllImport(NativeConstants.NRNativeLibrary)]
  683. public static extern NativeResult NRBlockDetailGetNormals(UInt64 meshing_handle, UInt64 block_detail_handle, NativeVector3f[] out_normals);
  684. /// <summary> Get the number of elements in face-vertex-index buffer. </summary>
  685. /// <param name="meshing_handle"> The handle of Meshing. </param>
  686. /// <param name="block_detail_handle"> The handle of block detail. </param>
  687. /// <param name="out_face_vertex_index_count"> Number of elements in buffer. </param>
  688. /// <returns> The result of operation. </returns>
  689. [DllImport(NativeConstants.NRNativeLibrary)]
  690. public static extern NativeResult NRBlockDetailGetIndexCount(UInt64 meshing_handle, UInt64 block_detail_handle, ref uint out_face_vertex_index_count);
  691. /// <summary>
  692. /// Get the pointer to face-vertex-index buffer.
  693. /// In the buffer, each element is a index to vertex buffer.
  694. /// Three index elements will define one triangle.
  695. /// For example: the first triangle is: vertex[index[0]], vertex[index[1]], vertex[index[2]].
  696. /// The second triangle is: vertex[index[3]], vertex[index[4]], vertex[index[5]].
  697. /// All faces are listed back-to-back in counter-clockwise vertex order.
  698. /// </summary>
  699. /// <param name="meshing_handle"> The handle of Meshing. </param>
  700. /// <param name="block_detail_handle"> The handle of block detail. </param>
  701. /// <param name="out_face_vertices_index"> Pointer of face-vertex-index buffer. </param>
  702. /// <returns> The result of operation. </returns>
  703. [DllImport(NativeConstants.NRNativeLibrary)]
  704. public static extern NativeResult NRBlockDetailGetIndeices(UInt64 meshing_handle, UInt64 block_detail_handle, ushort[] out_face_vertices_index);
  705. /// <summary> Destroy the block detail handle. </summary>
  706. /// <param name="meshing_handle"> The handle of Meshing. </param>
  707. /// <param name="block_detail_handle"> The handle of block detail. </param>
  708. /// <returns> The result of operation. </returns>
  709. [DllImport(NativeConstants.NRNativeLibrary)]
  710. public static extern NativeResult NRBlockDetailDestroy(UInt64 meshing_handle, UInt64 block_detail_handle);
  711. /// <summary> Destroy the mesh detail handle. </summary>
  712. /// <param name="meshing_handle"> The handle of Meshing. </param>
  713. /// <param name="mesh_detail_handle"> The handle of mesh detail. </param>
  714. /// <returns> The result of operation. </returns>
  715. [DllImport(NativeConstants.NRNativeLibrary)]
  716. public static extern NativeResult NRMeshDetailDestroy(UInt64 meshing_handle, UInt64 mesh_detail_handle);
  717. /// <summary> Destroy the request handle. </summary>
  718. /// <param name="meshing_handle"> The handle of Meshing. </param>
  719. /// <param name="request_mesh_detail_handle"> The handle of request for mesh detail, which identifies the request. </param>
  720. /// <returns> The result of operation. </returns>
  721. [DllImport(NativeConstants.NRNativeLibrary)]
  722. public static extern NativeResult NRMeshingMeshDetailRequestDestroy(UInt64 meshing_handle, UInt64 request_mesh_detail_handle);
  723. #endregion
  724. };
  725. }
  726. }