Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1663 lines
71 KiB

  1. using UnityEngine;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using AOT;
  5. using Oculus.Avatar;
  6. //This needs to be the csharp equivalent of ovrAvatarCapabilities in OVR_Avatar.h
  7. [Flags]
  8. public enum ovrAvatarCapabilities
  9. {
  10. Body = 1 << 0,
  11. Hands = 1 << 1,
  12. Base = 1 << 2,
  13. BodyTilt = 1 << 4,
  14. Expressive = 1 << 5,
  15. All = -1
  16. };
  17. // This needs to be the csharp equivalent of ovrAvatarMessageType in OVR_Avatar.h
  18. public enum ovrAvatarMessageType {
  19. AvatarSpecification,
  20. AssetLoaded,
  21. Count
  22. };
  23. // This needs to be the csharp equivalent of ovrAvatarMessage_AvatarSpecification in OVR_Avatar.h
  24. public struct ovrAvatarMessage_AvatarSpecification {
  25. public IntPtr avatarSpec; //ovrAvatarSpecification*, opaque pointer
  26. public UInt64 oculusUserID;
  27. };
  28. // This needs to be the csharp equivalent of ovrAvatarMessage_AssetLoaded in OVR_Avatar.h
  29. public struct ovrAvatarMessage_AssetLoaded {
  30. public UInt64 assetID;
  31. public IntPtr asset; //ovrAvatarAsset*, opaque pointer
  32. };
  33. // This needs to be the csharp equivalent of ovrAvatarAssetType in OVR_Avatar.h
  34. public enum ovrAvatarAssetType {
  35. Mesh,
  36. Texture,
  37. Pose,
  38. Material,
  39. CombinedMesh,
  40. PBSMaterial,
  41. FailedLoad,
  42. Count
  43. };
  44. // This needs to be the csharp equivalent of ovrAvatarMeshVertex in OVR_Avatar.h
  45. public struct ovrAvatarMeshVertex
  46. {
  47. public float x;
  48. public float y;
  49. public float z;
  50. public float nx;
  51. public float ny;
  52. public float nz;
  53. public float tx;
  54. public float ty;
  55. public float tz;
  56. public float tw;
  57. public float u;
  58. public float v;
  59. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  60. public byte[] blendIndices; ///< Indices into the bind pose
  61. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  62. public float[] blendWeights; ///< Blend weights for each component in the bind pose
  63. };
  64. // This needs to be the csharp equivalent of ovrAvatarMeshVertex in OVR_Avatar.h
  65. public struct ovrAvatarMeshVertexV2
  66. {
  67. public float x;
  68. public float y;
  69. public float z;
  70. public float nx;
  71. public float ny;
  72. public float nz;
  73. public float tx;
  74. public float ty;
  75. public float tz;
  76. public float tw;
  77. public float u;
  78. public float v;
  79. public float r;
  80. public float g;
  81. public float b;
  82. public float a;
  83. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  84. public byte[] blendIndices; ///< Indices into the bind pose
  85. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  86. public float[] blendWeights; ///< Blend weights for each component in the bind pose
  87. };
  88. // This needs to be the csharp equivalent of ovrAvatarMeshVertex in OVR_Avatar.h
  89. public struct ovrAvatarBlendVertex
  90. {
  91. public float x;
  92. public float y;
  93. public float z;
  94. public float nx;
  95. public float ny;
  96. public float nz;
  97. public float tx;
  98. public float ty;
  99. public float tz;
  100. };
  101. // This needs to be the csharp equivalent of ovrAvatarMeshAssetData in OVR_Avatar.h
  102. public struct ovrAvatarMeshAssetData
  103. {
  104. public UInt32 vertexCount;
  105. public IntPtr vertexBuffer; //const ovrAvatarMeshVertex*
  106. public UInt32 indexCount;
  107. public IntPtr indexBuffer; //const uint16t*
  108. public ovrAvatarSkinnedMeshPose skinnedBindPose;
  109. };
  110. /// Mesh Asset Data V2
  111. ///
  112. public struct ovrAvatarMeshAssetDataV2
  113. {
  114. public UInt32 vertexCount;
  115. public IntPtr vertexBuffer; //const ovrAvatarMeshVertexV2*
  116. public UInt32 indexCount;
  117. public IntPtr indexBuffer; //const uint16t*
  118. public ovrAvatarSkinnedMeshPose skinnedBindPose;
  119. };
  120. // This needs to be the csharp equivalent of ovrAvatarTextureFormat in OVR_Avatar.h
  121. public enum ovrAvatarTextureFormat {
  122. RGB24,
  123. DXT1,
  124. DXT5,
  125. ASTC_RGB_6x6,
  126. ASTC_RGB_6x6_MIPMAPS,
  127. Count
  128. };
  129. // This needs to be the csharp equivalent of ovrAvatarTextureAssetData in OVR_Avatar.h
  130. public struct ovrAvatarTextureAssetData {
  131. public ovrAvatarTextureFormat format;
  132. public UInt32 sizeX;
  133. public UInt32 sizeY;
  134. public UInt32 mipCount;
  135. public UInt64 textureDataSize;
  136. public IntPtr textureData; // const uint8_t*
  137. };
  138. // This needs to be the csharp equivalent of ovrAvatarRenderPartType in OVR_Avatar.h
  139. public enum ovrAvatarRenderPartType
  140. {
  141. SkinnedMeshRender,
  142. SkinnedMeshRenderPBS,
  143. ProjectorRender,
  144. SkinnedMeshRenderPBS_V2,
  145. Count
  146. };
  147. /// Avatar Logging Level
  148. /// Matches the Android Log Levels
  149. public enum ovrAvatarLogLevel
  150. {
  151. Unknown,
  152. Default,
  153. Verbose,
  154. Debug,
  155. Info,
  156. Warn,
  157. Error,
  158. Fatal,
  159. Silent
  160. };
  161. // This needs to be the csharp equivalent of ovrAvatarTransform in OVR_Avatar.h
  162. public struct ovrAvatarTransform
  163. {
  164. public Vector3 position;
  165. public Quaternion orientation;
  166. public Vector3 scale;
  167. };
  168. // This needs to be the csharp equivalent of ovrAvatarButton in OVR_Avatar.h
  169. [Flags]
  170. public enum ovrAvatarButton
  171. {
  172. One = 0x0001,
  173. Two = 0x0002,
  174. Three = 0x0004,
  175. Joystick = 0x0008,
  176. }
  177. // This needs to be the csharp equivalent of ovrAvatarTouch in OVR_Avatar.h
  178. [Flags]
  179. public enum ovrAvatarTouch
  180. {
  181. One = 0x0001,
  182. Two = 0x0002,
  183. Joystick = 0x0004,
  184. ThumbRest = 0x0008,
  185. Index = 0x0010,
  186. Pointing = 0x0040,
  187. ThumbUp = 0x0080,
  188. }
  189. // This needs to be the csharp equivalent of ovrAvatarHandInputState in OVR_Avatar.h
  190. public struct ovrAvatarHandInputState
  191. {
  192. public ovrAvatarTransform transform;
  193. public ovrAvatarButton buttonMask;
  194. public ovrAvatarTouch touchMask;
  195. public float joystickX;
  196. public float joystickY;
  197. public float indexTrigger;
  198. public float handTrigger;
  199. [MarshalAs(UnmanagedType.I1)]
  200. public bool isActive;
  201. };
  202. // This needs to be the csharp equivalent of ovrAvatarComponent in OVR_Avatar.h
  203. public struct ovrAvatarComponent
  204. {
  205. public ovrAvatarTransform transform;
  206. public UInt32 renderPartCount;
  207. public IntPtr renderParts; //const ovrAvatarRenderPart* const*
  208. [MarshalAs(UnmanagedType.LPStr)]
  209. public string name;
  210. };
  211. struct ovrAvatarComponent_Offsets
  212. {
  213. public static long transform = Marshal.OffsetOf(typeof(ovrAvatarComponent), "transform").ToInt64();
  214. public static Int32 renderPartCount = Marshal.OffsetOf(typeof(ovrAvatarComponent), "renderPartCount").ToInt32();
  215. public static Int32 renderParts = Marshal.OffsetOf(typeof(ovrAvatarComponent), "renderParts").ToInt32();
  216. public static Int32 name = Marshal.OffsetOf(typeof(ovrAvatarComponent), "name").ToInt32();
  217. };
  218. // This needs to be the csharp equivalent of ovrAvatarBodyComponent in OVR_Avatar.h
  219. public struct ovrAvatarBaseComponent
  220. {
  221. public Vector3 basePosition;
  222. public IntPtr renderComponent; //const ovrAvatarComponent*
  223. };
  224. // This needs to be the csharp equivalent of ovrAvatarBodyComponent in OVR_Avatar.h
  225. public struct ovrAvatarBodyComponent {
  226. public ovrAvatarTransform leftEyeTransform;
  227. public ovrAvatarTransform rightEyeTransform;
  228. public ovrAvatarTransform centerEyeTransform;
  229. public IntPtr renderComponent; //const ovrAvatarComponent*
  230. };
  231. public struct ovrAvatarBodyComponent_Offsets
  232. {
  233. public static long leftEyeTransform = Marshal.OffsetOf(typeof(ovrAvatarBodyComponent), "leftEyeTransform").ToInt64();
  234. public static long rightEyeTransform = Marshal.OffsetOf(typeof(ovrAvatarBodyComponent), "rightEyeTransform").ToInt64();
  235. public static long centerEyeTransform = Marshal.OffsetOf(typeof(ovrAvatarBodyComponent), "centerEyeTransform").ToInt64();
  236. public static long renderComponent = Marshal.OffsetOf(typeof(ovrAvatarBodyComponent), "renderComponent").ToInt64();
  237. };
  238. // This needs to be the csharp equivalent of ovrAvatarControllerComponent in OVR_Avatar.h
  239. public struct ovrAvatarControllerComponent
  240. {
  241. public ovrAvatarHandInputState inputState;
  242. public IntPtr renderComponent; //const ovrAvatarComponent*
  243. };
  244. // This needs to be the csharp equivalent of ovrAvatarHandComponent in OVR_Avatar.h
  245. public struct ovrAvatarHandComponent {
  246. public ovrAvatarHandInputState inputState;
  247. public IntPtr renderComponent; //const ovrAvatarComponent*
  248. };
  249. // This needs to be the csharp equivalent of ovrAvatarMaterialLayerBlendMode in OVR_Avatar.h
  250. public enum ovrAvatarMaterialLayerBlendMode{
  251. Add,
  252. Multiply,
  253. Count
  254. };
  255. // This needs to be the csharp equivalent of ovrAvatarMaterialLayerSampleMode in OVR_Avatar.h
  256. public enum ovrAvatarMaterialLayerSampleMode{
  257. Color,
  258. Texture,
  259. TextureSingleChannel,
  260. Parallax,
  261. Count
  262. };
  263. // This needs to be the csharp equivalent of ovrAvatarMaterialLayerMaskType in OVR_Avatar.h
  264. public enum ovrAvatarMaterialMaskType{
  265. None,
  266. Positional,
  267. ViewReflection,
  268. Fresnel,
  269. Pulse,
  270. Count
  271. };
  272. // This needs to be the csharp equivalent of Controller Types from OVR_Avatar.h
  273. public enum ovrAvatarControllerType
  274. {
  275. Touch,
  276. Malibu,
  277. Go,
  278. Quest,
  279. Count,
  280. };
  281. public enum ovrAvatarAssetLevelOfDetail
  282. {
  283. Lowest = 1,
  284. Medium = 3,
  285. Highest = 5,
  286. };
  287. public enum ovrAvatarLookAndFeelVersion
  288. {
  289. Unknown = -1,
  290. One = 0,
  291. Two = 1,
  292. };
  293. // This needs to be the csharp equivalent of ovrAvatarMaterialLayerState in OVR_Avatar.h
  294. public struct ovrAvatarMaterialLayerState{
  295. public ovrAvatarMaterialLayerBlendMode blendMode;
  296. public ovrAvatarMaterialLayerSampleMode sampleMode;
  297. public ovrAvatarMaterialMaskType maskType;
  298. public Vector4 layerColor;
  299. public Vector4 sampleParameters;
  300. public UInt64 sampleTexture;
  301. public Vector4 sampleScaleOffset;
  302. public Vector4 maskParameters;
  303. public Vector4 maskAxis;
  304. static bool VectorEquals(Vector4 a, Vector4 b)
  305. {
  306. return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
  307. }
  308. public override bool Equals(object obj)
  309. {
  310. if (!(obj is ovrAvatarMaterialLayerState))
  311. {
  312. return false;
  313. }
  314. ovrAvatarMaterialLayerState other = (ovrAvatarMaterialLayerState)obj;
  315. if (blendMode != other.blendMode) return false;
  316. if (sampleMode != other.sampleMode) return false;
  317. if (maskType != other.maskType) return false;
  318. if (!VectorEquals(layerColor, other.layerColor)) return false;
  319. if (!VectorEquals(sampleParameters, other.sampleParameters)) return false;
  320. if (sampleTexture != other.sampleTexture) return false;
  321. if (!VectorEquals(sampleScaleOffset, other.sampleScaleOffset)) return false;
  322. if (!VectorEquals(maskParameters, other.maskParameters)) return false;
  323. if (!VectorEquals(maskAxis, other.maskAxis)) return false;
  324. return true;
  325. }
  326. public override int GetHashCode()
  327. {
  328. return blendMode.GetHashCode() ^
  329. sampleMode.GetHashCode() ^
  330. maskType.GetHashCode() ^
  331. layerColor.GetHashCode() ^
  332. sampleParameters.GetHashCode() ^
  333. sampleTexture.GetHashCode() ^
  334. sampleScaleOffset.GetHashCode() ^
  335. maskParameters.GetHashCode() ^
  336. maskAxis.GetHashCode();
  337. }
  338. };
  339. // This needs to be the csharp equivalent of ovrAvatarMaterialState in OVR_Avatar.h
  340. public struct ovrAvatarMaterialState
  341. {
  342. public Vector4 baseColor;
  343. public ovrAvatarMaterialMaskType baseMaskType;
  344. public Vector4 baseMaskParameters;
  345. public Vector4 baseMaskAxis;
  346. public ovrAvatarMaterialLayerSampleMode sampleMode;
  347. public UInt64 alphaMaskTextureID;
  348. public Vector4 alphaMaskScaleOffset;
  349. public UInt64 normalMapTextureID;
  350. public Vector4 normalMapScaleOffset;
  351. public UInt64 parallaxMapTextureID;
  352. public Vector4 parallaxMapScaleOffset;
  353. public UInt64 roughnessMapTextureID;
  354. public Vector4 roughnessMapScaleOffset;
  355. public UInt32 layerCount;
  356. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  357. public ovrAvatarMaterialLayerState[] layers;
  358. static bool VectorEquals(Vector4 a, Vector4 b)
  359. {
  360. return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
  361. }
  362. public override bool Equals(object obj)
  363. {
  364. if (!(obj is ovrAvatarMaterialState))
  365. {
  366. return false;
  367. }
  368. ovrAvatarMaterialState other = (ovrAvatarMaterialState)obj;
  369. if (!VectorEquals(baseColor, other.baseColor)) return false;
  370. if (baseMaskType != other.baseMaskType) return false;
  371. if (!VectorEquals(baseMaskParameters, other.baseMaskParameters)) return false;
  372. if (!VectorEquals(baseMaskAxis, other.baseMaskAxis)) return false;
  373. if (sampleMode != other.sampleMode) return false;
  374. if (alphaMaskTextureID != other.alphaMaskTextureID) return false;
  375. if (!VectorEquals(alphaMaskScaleOffset, other.alphaMaskScaleOffset)) return false;
  376. if (normalMapTextureID != other.normalMapTextureID) return false;
  377. if (!VectorEquals(normalMapScaleOffset, other.normalMapScaleOffset)) return false;
  378. if (parallaxMapTextureID != other.parallaxMapTextureID) return false;
  379. if (!VectorEquals(parallaxMapScaleOffset, other.parallaxMapScaleOffset)) return false;
  380. if (roughnessMapTextureID != other.roughnessMapTextureID) return false;
  381. if (!VectorEquals(roughnessMapScaleOffset, other.roughnessMapScaleOffset)) return false;
  382. if (layerCount != other.layerCount) return false;
  383. for (int i = 0; i < layerCount; ++i)
  384. {
  385. if (!layers[i].Equals(other.layers[i])) return false;
  386. }
  387. return true;
  388. }
  389. public override int GetHashCode()
  390. {
  391. int hash = 0;
  392. hash ^= baseColor.GetHashCode();
  393. hash ^= baseMaskType.GetHashCode();
  394. hash ^= baseMaskParameters.GetHashCode();
  395. hash ^= baseMaskAxis.GetHashCode();
  396. hash ^= sampleMode.GetHashCode();
  397. hash ^= alphaMaskTextureID.GetHashCode();
  398. hash ^= alphaMaskScaleOffset.GetHashCode();
  399. hash ^= normalMapTextureID.GetHashCode();
  400. hash ^= normalMapScaleOffset.GetHashCode();
  401. hash ^= parallaxMapTextureID.GetHashCode();
  402. hash ^= parallaxMapScaleOffset.GetHashCode();
  403. hash ^= roughnessMapTextureID.GetHashCode();
  404. hash ^= roughnessMapScaleOffset.GetHashCode();
  405. hash ^= layerCount.GetHashCode();
  406. for (int i = 0; i < layerCount; ++i)
  407. {
  408. hash ^= layers[i].GetHashCode();
  409. }
  410. return hash;
  411. }
  412. };
  413. public struct ovrAvatarExpressiveParameters
  414. {
  415. public Vector4 irisColor;
  416. public Vector4 scleraColor;
  417. public Vector4 lashColor;
  418. public Vector4 browColor;
  419. public Vector4 lipColor;
  420. public Vector4 teethColor;
  421. public Vector4 gumColor;
  422. public float browLashIntensity;
  423. public float lipSmoothness;
  424. static bool VectorEquals(Vector4 a, Vector4 b)
  425. {
  426. return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
  427. }
  428. public override bool Equals(object obj)
  429. {
  430. if (!(obj is ovrAvatarExpressiveParameters))
  431. {
  432. return false;
  433. }
  434. ovrAvatarExpressiveParameters other = (ovrAvatarExpressiveParameters)obj;
  435. if (!VectorEquals(irisColor, other.irisColor)) return false;
  436. if (!VectorEquals(scleraColor, other.scleraColor)) return false;
  437. if (!VectorEquals(lashColor, other.lashColor)) return false;
  438. if (!VectorEquals(browColor, other.browColor)) return false;
  439. if (!VectorEquals(lipColor, other.lipColor)) return false;
  440. if (!VectorEquals(teethColor, other.teethColor)) return false;
  441. if (!VectorEquals(gumColor, other.gumColor)) return false;
  442. if (browLashIntensity != other.browLashIntensity) return false;
  443. if (lipSmoothness != other.lipSmoothness) return false;
  444. return true;
  445. }
  446. public override int GetHashCode()
  447. {
  448. return irisColor.GetHashCode() ^
  449. scleraColor.GetHashCode() ^
  450. lashColor.GetHashCode() ^
  451. browColor.GetHashCode() ^
  452. lipColor.GetHashCode() ^
  453. teethColor.GetHashCode() ^
  454. gumColor.GetHashCode() ^
  455. browLashIntensity.GetHashCode() ^
  456. lipSmoothness.GetHashCode();
  457. }
  458. }
  459. public struct ovrAvatarPBSMaterialState
  460. {
  461. public Vector4 baseColor; ///< Underlying base color for the material
  462. public UInt64 albedoTextureID; ///< Texture id for the albedo map
  463. public Vector4 albedoMultiplier; ///< Multiplier for albedo texture sample
  464. public UInt64 metallicnessTextureID; ///< Texture id for the metal map
  465. public float glossinessScale; ///< Glossiness factor
  466. public UInt64 normalTextureID; ///< Texture id for the normal map
  467. public UInt64 heightTextureID; ///< Texture id for the height map
  468. public UInt64 occlusionTextureID; ///< Texture id for the occlusion map
  469. public UInt64 emissionTextureID; ///< Texture id for the emission map
  470. public Vector4 emissionMultiplier; ///< Multiplier for emission texture sample
  471. public UInt64 detailMaskTextureID; ///< Texture id for the detail mask map
  472. public UInt64 detailAlbedoTextureID; ///< Texture id for the detail albedo map
  473. public UInt64 detailNormalTextureID; ///< Texture id for the detail normal map
  474. static bool VectorEquals(Vector4 a, Vector4 b)
  475. {
  476. return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
  477. }
  478. public override bool Equals(object obj)
  479. {
  480. if (!(obj is ovrAvatarPBSMaterialState))
  481. {
  482. return false;
  483. }
  484. ovrAvatarPBSMaterialState other = (ovrAvatarPBSMaterialState)obj;
  485. if (!VectorEquals(baseColor, other.baseColor)) return false;
  486. if (albedoTextureID != other.albedoTextureID) return false;
  487. if (!VectorEquals(albedoMultiplier, other.albedoMultiplier)) return false;
  488. if (metallicnessTextureID != other.metallicnessTextureID) return false;
  489. if (glossinessScale != other.glossinessScale) return false;
  490. if (normalTextureID != other.normalTextureID) return false;
  491. if (heightTextureID != other.heightTextureID) return false;
  492. if (occlusionTextureID != other.occlusionTextureID) return false;
  493. if (emissionTextureID != other.emissionTextureID) return false;
  494. if (!VectorEquals(emissionMultiplier, other.emissionMultiplier)) return false;
  495. if (detailMaskTextureID != other.detailMaskTextureID) return false;
  496. if (detailAlbedoTextureID != other.detailAlbedoTextureID) return false;
  497. if (detailNormalTextureID != other.detailNormalTextureID) return false;
  498. return true;
  499. }
  500. public override int GetHashCode()
  501. {
  502. return baseColor.GetHashCode() ^
  503. albedoTextureID.GetHashCode() ^
  504. albedoMultiplier.GetHashCode() ^
  505. metallicnessTextureID.GetHashCode() ^
  506. glossinessScale.GetHashCode() ^
  507. normalTextureID.GetHashCode() ^
  508. heightTextureID.GetHashCode() ^
  509. occlusionTextureID.GetHashCode() ^
  510. emissionTextureID.GetHashCode() ^
  511. emissionMultiplier.GetHashCode() ^
  512. detailMaskTextureID.GetHashCode() ^
  513. detailAlbedoTextureID.GetHashCode() ^
  514. detailNormalTextureID.GetHashCode();
  515. }
  516. };
  517. public class OvrAvatarAssetMaterial : OvrAvatarAsset
  518. {
  519. public OvrAvatarAssetMaterial(UInt64 id, IntPtr mat)
  520. {
  521. assetID = id;
  522. material = CAPI.ovrAvatarAsset_GetMaterialState(mat);
  523. }
  524. public ovrAvatarMaterialState material;
  525. }
  526. // This needs to be the csharp equivalent of ovrAvatarSkinnedMeshPose in OVR_Avatar.h
  527. public struct ovrAvatarSkinnedMeshPose
  528. {
  529. public UInt32 jointCount;
  530. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
  531. public ovrAvatarTransform[] jointTransform;
  532. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
  533. public int[] jointParents;
  534. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
  535. public IntPtr[] jointNames; //const char * jointNames[64];
  536. };
  537. [Flags]
  538. public enum ovrAvatarVisibilityFlags
  539. {
  540. FirstPerson = 1 << 0,
  541. ThirdPerson = 1 << 1,
  542. SelfOccluding = 1 << 2,
  543. };
  544. // This needs to be the csharp equivalent of ovrAvatarRenderPart_SkinnedMeshRender in OVR_Avatar.h
  545. public struct ovrAvatarRenderPart_SkinnedMeshRender
  546. {
  547. public ovrAvatarTransform localTransform;
  548. public ovrAvatarVisibilityFlags visibilityMask;
  549. public UInt64 meshAssetID;
  550. public ovrAvatarMaterialState materialState;
  551. public ovrAvatarSkinnedMeshPose skinnedPose;
  552. };
  553. // This needs to be the csharp equivalent of ovrAvatarRenderPart_SkinnedMeshRenderPBS in OVR_Avatar.h
  554. public struct ovrAvatarRenderPart_SkinnedMeshRenderPBS
  555. {
  556. public ovrAvatarTransform localTransform;
  557. public ovrAvatarVisibilityFlags visibilityMask;
  558. public UInt64 meshAssetID;
  559. public UInt64 albedoTextureAssetID;
  560. public UInt64 surfaceTextureAssetID;
  561. public ovrAvatarSkinnedMeshPose skinnedPose;
  562. };
  563. // This needs to be the csharp equivalent of ovrAvatarRenderPart_ProjectorRender in OVR_Avatar.h
  564. public struct ovrAvatarRenderPart_ProjectorRender
  565. {
  566. public ovrAvatarTransform localTransform;
  567. public UInt32 componentIndex;
  568. public UInt32 renderPartIndex;
  569. public ovrAvatarMaterialState materialState;
  570. };
  571. // This needs to be the csharp equivalent of ovrAvatarRenderPart_SkinnedMeshRenderPBS_V2 in OVR_Avatar.h
  572. public struct ovrAvatarRenderPart_SkinnedMeshRenderPBS_V2
  573. {
  574. public ovrAvatarTransform localTransform;
  575. public ovrAvatarVisibilityFlags visibilityMask;
  576. public UInt64 meshAssetID;
  577. public ovrAvatarPBSMaterialState materialState;
  578. public ovrAvatarSkinnedMeshPose skinnedPose;
  579. };
  580. // This needs to be the csharp equivalent of ovrAvatarHandGesture in OVR_Avatar.h
  581. public enum ovrAvatarHandGesture {
  582. Default,
  583. GripSphere,
  584. GripCube,
  585. Count
  586. };
  587. public enum ovrAvatarBodyPartType
  588. {
  589. Body,
  590. Clothing,
  591. Eyewear,
  592. Hair,
  593. Beard,
  594. Count
  595. };
  596. // This needs to be the csharp equivalent of ovrAvatarBlendShapeParams in OVR_Avatar.h
  597. public struct ovrAvatarBlendShapeParams
  598. {
  599. public UInt32 blendShapeParamCount;
  600. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
  601. public float[] blendShapeParams;
  602. };
  603. struct ovrAvatarBlendShapeParams_Offsets
  604. {
  605. public static Int32 blendShapeParamCount = Marshal.OffsetOf(typeof(ovrAvatarBlendShapeParams), "blendShapeParamCount").ToInt32();
  606. // Bug with Marshal.OffsetOf is returning an incorrect offset, causing an off by 1 float issue in the blendShapeParams
  607. //public static long blendShapeParams = Marshal.OffsetOf(typeof(ovrAvatarBlendShapeParams), "blendShapeParams").ToInt64();
  608. public static long blendShapeParams = Marshal.SizeOf(typeof(UInt32));
  609. };
  610. // This needs to be the csharp equivalent of ovrAvatarVisemes in OVR_Avatar.h
  611. public struct ovrAvatarVisemes
  612. {
  613. public UInt32 visemeParamCount;
  614. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
  615. public float[] visemeParams;
  616. };
  617. struct ovrAvatarVisemes_Offsets
  618. {
  619. public static Int32 visemeParamCount = Marshal.OffsetOf(typeof(ovrAvatarVisemes), "visemeParamCount").ToInt32();
  620. // Bug with Marshal.OffsetOf is returning an incorrect offset, causing an off by 1 float issue in the visemeParams
  621. //public static long visemeParams = Marshal.OffsetOf(typeof(ovrAvatarVisemes), "visemeParams").ToInt64();
  622. public static long visemeParams = Marshal.SizeOf(typeof(UInt32));
  623. };
  624. // This needs to be the csharp equivalent of ovrAvatarGazeTargetType in OVR_AvatarInternal.h
  625. public enum ovrAvatarGazeTargetType {
  626. AvatarHead = 0,
  627. AvatarHand,
  628. Object,
  629. ObjectStatic,
  630. Count,
  631. };
  632. // This needs to be the csharp equivalent of ovrAvatarGazeTarget in OVR_AvatarInternal.h
  633. public struct ovrAvatarGazeTarget
  634. {
  635. public UInt32 id;
  636. public Vector3 worldPosition;
  637. public ovrAvatarGazeTargetType type;
  638. };
  639. struct ovrAvatarGazeTarget_Offsets
  640. {
  641. public static Int32 id = 0;
  642. public static Int32 worldPosition = Marshal.SizeOf(typeof(UInt32));
  643. public static Int32 type = worldPosition + Marshal.SizeOf(typeof(Vector3));
  644. };
  645. public struct ovrAvatarGazeTargets
  646. {
  647. public UInt32 targetCount;
  648. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
  649. public ovrAvatarGazeTarget[] targets;
  650. };
  651. struct ovrAvatarGazeTargets_Offsets
  652. {
  653. public static Int32 targetCount = Marshal.OffsetOf(typeof(ovrAvatarGazeTargets), "targetCount").ToInt32();
  654. // Bug with Marshal.OffsetOf is returning an incorrect offset, causing an off by 1 float issue in the targets
  655. //public static long targets = Marshal.OffsetOf(typeof(ovrAvatarGazeTargets), "targets").ToInt64();
  656. public static long targets = Marshal.SizeOf(typeof(UInt32));
  657. };
  658. // This needs to be the csharp equivalent of ovrAvatarLightType in OVR_AvatarInternal.h
  659. public enum ovrAvatarLightType {
  660. Point = 0,
  661. Direction,
  662. Spot,
  663. Count,
  664. };
  665. // This needs to be the csharp equivalent of ovrAvatarLight in OVR_AvatarInternal.h
  666. public struct ovrAvatarLight
  667. {
  668. public UInt32 id;
  669. public ovrAvatarLightType type;
  670. public float intensity;
  671. public Vector3 worldDirection;
  672. public Vector3 worldPosition;
  673. public float range;
  674. public float spotAngleDeg;
  675. };
  676. struct ovrAvatarLight_Offsets
  677. {
  678. public static long id = Marshal.OffsetOf(typeof(ovrAvatarLight), "id").ToInt64();
  679. public static long type = Marshal.OffsetOf(typeof(ovrAvatarLight), "type").ToInt64();
  680. public static long intensity = Marshal.OffsetOf(typeof(ovrAvatarLight), "intensity").ToInt64();
  681. public static long worldDirection = Marshal.OffsetOf(typeof(ovrAvatarLight), "worldDirection").ToInt64();
  682. public static long worldPosition = Marshal.OffsetOf(typeof(ovrAvatarLight), "worldPosition").ToInt64();
  683. public static long range = Marshal.OffsetOf(typeof(ovrAvatarLight), "range").ToInt64();
  684. public static long spotAngleDeg = Marshal.OffsetOf(typeof(ovrAvatarLight), "spotAngleDeg").ToInt64();
  685. };
  686. public struct ovrAvatarLights
  687. {
  688. public float ambientIntensity;
  689. public UInt32 lightCount;
  690. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  691. public ovrAvatarLight[] lights;
  692. };
  693. struct ovrAvatarLights_Offsets
  694. {
  695. public static long ambientIntensity = Marshal.OffsetOf(typeof(ovrAvatarLights), "ambientIntensity").ToInt64();
  696. public static long lightCount = Marshal.OffsetOf(typeof(ovrAvatarLights), "lightCount").ToInt64();
  697. public static long lights = Marshal.OffsetOf(typeof(ovrAvatarLights), "lights").ToInt64();
  698. };
  699. // Debug Render
  700. [Flags]
  701. public enum ovrAvatarDebugContext : uint
  702. {
  703. None = 0,
  704. GazeTarget = 0x01,
  705. Any = 0xffffffff
  706. };
  707. public struct ovrAvatarDebugLine
  708. {
  709. public Vector3 startPoint;
  710. public Vector3 endPoint;
  711. public Vector3 color;
  712. public ovrAvatarDebugContext context;
  713. public IntPtr text;
  714. };
  715. public struct ovrAvatarDebugTransform
  716. {
  717. public ovrAvatarTransform transform;
  718. public ovrAvatarDebugContext context;
  719. public IntPtr text;
  720. };
  721. namespace Oculus.Avatar
  722. {
  723. public class CAPI
  724. {
  725. #if UNITY_ANDROID && !UNITY_EDITOR
  726. #if AVATAR_XPLAT
  727. private const string LibFile = "ovravatar";
  728. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  729. public static extern void ovrAvatar_Initialize(string appID);
  730. #else
  731. private const string LibFile = "ovravatarloader";
  732. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  733. public static extern void ovrAvatar_InitializeAndroidUnity(string appID);
  734. #endif
  735. #else
  736. private const string LibFile = "libovravatar";
  737. public static readonly System.Version AvatarSDKVersion = new System.Version(1, 36, 0);
  738. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  739. public static extern void ovrAvatar_Initialize(string appID);
  740. #endif
  741. static IntPtr nativeVisemeData = IntPtr.Zero;
  742. static IntPtr nativeGazeTargetsData = IntPtr.Zero;
  743. static IntPtr nativeAvatarLightsData = IntPtr.Zero;
  744. static IntPtr DebugLineCountData = IntPtr.Zero;
  745. static float[] scratchBufferFloat = new float[16];
  746. static GameObject debugLineGo;
  747. public static void Initialize()
  748. {
  749. nativeVisemeData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ovrAvatarVisemes)));
  750. nativeGazeTargetsData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ovrAvatarGazeTargets)));
  751. nativeAvatarLightsData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ovrAvatarLights)));
  752. DebugLineCountData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(uint)));
  753. debugLineGo = new GameObject();
  754. debugLineGo.name = "AvatarSDKDebugDrawHelper";
  755. }
  756. public static void Shutdown()
  757. {
  758. Marshal.FreeHGlobal(nativeVisemeData);
  759. Marshal.FreeHGlobal(nativeGazeTargetsData);
  760. Marshal.FreeHGlobal(nativeAvatarLightsData);
  761. Marshal.FreeHGlobal(DebugLineCountData);
  762. debugLineGo = null;
  763. }
  764. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  765. public static extern void ovrAvatar_Shutdown();
  766. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  767. public static extern IntPtr ovrAvatarMessage_Pop();
  768. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  769. public static extern ovrAvatarMessageType ovrAvatarMessage_GetType(IntPtr msg);
  770. public static ovrAvatarMessage_AvatarSpecification ovrAvatarMessage_GetAvatarSpecification(
  771. IntPtr msg)
  772. {
  773. IntPtr ptr = ovrAvatarMessage_GetAvatarSpecification_Native(msg);
  774. return (ovrAvatarMessage_AvatarSpecification)Marshal.PtrToStructure(
  775. ptr, typeof(ovrAvatarMessage_AvatarSpecification));
  776. }
  777. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  778. "ovrAvatarMessage_GetAvatarSpecification")]
  779. private static extern IntPtr ovrAvatarMessage_GetAvatarSpecification_Native(IntPtr msg);
  780. public static ovrAvatarMessage_AssetLoaded ovrAvatarMessage_GetAssetLoaded(
  781. IntPtr msg)
  782. {
  783. IntPtr ptr = ovrAvatarMessage_GetAssetLoaded_Native(msg);
  784. return (ovrAvatarMessage_AssetLoaded)Marshal.PtrToStructure(
  785. ptr, typeof(ovrAvatarMessage_AssetLoaded));
  786. }
  787. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  788. "ovrAvatarMessage_GetAssetLoaded")]
  789. private static extern IntPtr ovrAvatarMessage_GetAssetLoaded_Native(IntPtr msg);
  790. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  791. public static extern void ovrAvatarMessage_Free(IntPtr msg);
  792. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  793. public static extern IntPtr ovrAvatarSpecificationRequest_Create(UInt64 userID);
  794. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  795. public static extern void ovrAvatarSpecificationRequest_Destroy(IntPtr specificationRequest);
  796. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  797. public static extern void ovrAvatarSpecificationRequest_SetCombineMeshes(IntPtr specificationRequest, bool useCombinedMesh);
  798. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  799. public static extern void ovrAvatarSpecificationRequest_SetLookAndFeelVersion(IntPtr specificationRequest, ovrAvatarLookAndFeelVersion version);
  800. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  801. public static extern void ovrAvatarSpecificationRequest_SetLevelOfDetail(IntPtr specificationRequest, ovrAvatarAssetLevelOfDetail lod);
  802. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  803. public static extern void ovrAvatar_RequestAvatarSpecification(UInt64 userID);
  804. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  805. public static extern void ovrAvatar_RequestAvatarSpecificationFromSpecRequest(IntPtr specificationRequest);
  806. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  807. public static extern void ovrAvatarSpecificationRequest_SetFallbackLookAndFeelVersion(IntPtr specificationRequest, ovrAvatarLookAndFeelVersion version);
  808. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  809. public static extern void ovrAvatarSpecificationRequest_SetExpressiveFlag(IntPtr specificationRequest, bool enable);
  810. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  811. public static extern IntPtr ovrAvatar_Create(IntPtr avatarSpecification,
  812. ovrAvatarCapabilities capabilities);
  813. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  814. public static extern void ovrAvatar_Destroy(IntPtr avatar);
  815. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  816. public static extern void ovrAvatarPose_UpdateBody(
  817. IntPtr avatar, ovrAvatarTransform headPose);
  818. public static void ovrAvatarPose_UpdateVoiceVisualization(
  819. IntPtr avatar, float[] pcmData)
  820. {
  821. ovrAvatarPose_UpdateVoiceVisualization_Native(
  822. avatar, (UInt32)pcmData.Length, pcmData);
  823. }
  824. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  825. "ovrAvatarPose_UpdateVoiceVisualization")]
  826. private static extern void ovrAvatarPose_UpdateVoiceVisualization_Native(
  827. IntPtr avatar, UInt32 pcmDataSize, [In] float[] pcmData);
  828. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  829. public static extern void ovrAvatarPose_UpdateHands(
  830. IntPtr avatar,
  831. ovrAvatarHandInputState inputStateLeft,
  832. ovrAvatarHandInputState inputStateRight);
  833. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  834. public static extern void ovrAvatarPose_UpdateHandsWithType(
  835. IntPtr avatar,
  836. ovrAvatarHandInputState inputStateLeft,
  837. ovrAvatarHandInputState inputStateRight,
  838. ovrAvatarControllerType type);
  839. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  840. public static extern void ovrAvatarPose_Finalize(IntPtr avatar, float elapsedSeconds);
  841. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  842. public static extern void ovrAvatar_SetLeftControllerVisibility(IntPtr avatar, bool show);
  843. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  844. public static extern void ovrAvatar_SetRightControllerVisibility(IntPtr avatar, bool show);
  845. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  846. public static extern void ovrAvatar_SetLeftHandVisibility(IntPtr avatar, bool show);
  847. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  848. public static extern void ovrAvatar_SetRightHandVisibility(IntPtr avatar, bool show);
  849. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  850. public static extern UInt32 ovrAvatarComponent_Count(IntPtr avatar);
  851. public static void ovrAvatarComponent_Get(IntPtr avatar, UInt32 index, bool includeName, ref ovrAvatarComponent component)
  852. {
  853. IntPtr ptr = ovrAvatarComponent_Get_Native(avatar, index);
  854. ovrAvatarComponent_Get(ptr, includeName, ref component);
  855. }
  856. public static void ovrAvatarComponent_Get(IntPtr componentPtr, bool includeName, ref ovrAvatarComponent component)
  857. {
  858. Marshal.Copy(new IntPtr(componentPtr.ToInt64() + ovrAvatarComponent_Offsets.transform), scratchBufferFloat, 0, 10);
  859. OvrAvatar.ConvertTransform(scratchBufferFloat, ref component.transform);
  860. component.renderPartCount = (UInt32)Marshal.ReadInt32(componentPtr, ovrAvatarComponent_Offsets.renderPartCount);
  861. component.renderParts = Marshal.ReadIntPtr(componentPtr, ovrAvatarComponent_Offsets.renderParts);
  862. if (includeName)
  863. {
  864. IntPtr namePtr = Marshal.ReadIntPtr(componentPtr, ovrAvatarComponent_Offsets.name);
  865. component.name = Marshal.PtrToStringAnsi(namePtr);
  866. }
  867. }
  868. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  869. "ovrAvatarComponent_Get")]
  870. public static extern IntPtr ovrAvatarComponent_Get_Native(IntPtr avatar, UInt32 index);
  871. public static bool ovrAvatarPose_GetBaseComponent(IntPtr avatar, ref ovrAvatarBaseComponent component)
  872. {
  873. IntPtr ptr = ovrAvatarPose_GetBaseComponent_Native(avatar);
  874. if (ptr == IntPtr.Zero)
  875. {
  876. return false;
  877. }
  878. int renderComponentOffset = Marshal.SizeOf(typeof(ovrAvatarBaseComponent)) - Marshal.SizeOf(typeof(IntPtr));
  879. component.renderComponent = Marshal.ReadIntPtr(ptr, renderComponentOffset);
  880. return true;
  881. }
  882. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint ="ovrAvatarPose_GetBaseComponent")]
  883. private static extern IntPtr ovrAvatarPose_GetBaseComponent_Native(IntPtr avatar);
  884. public static IntPtr MarshalRenderComponent<T>(IntPtr ptr) where T : struct
  885. {
  886. return Marshal.ReadIntPtr(new IntPtr(ptr.ToInt64() + Marshal.OffsetOf(typeof(T), "renderComponent").ToInt64()));
  887. }
  888. public static bool ovrAvatarPose_GetBodyComponent(IntPtr avatar, ref ovrAvatarBodyComponent component)
  889. {
  890. IntPtr ptr = ovrAvatarPose_GetBodyComponent_Native(avatar);
  891. if (ptr == IntPtr.Zero)
  892. {
  893. return false;
  894. }
  895. Marshal.Copy(new IntPtr(ptr.ToInt64() + ovrAvatarBodyComponent_Offsets.leftEyeTransform), scratchBufferFloat, 0, 10);
  896. OvrAvatar.ConvertTransform(scratchBufferFloat, ref component.leftEyeTransform);
  897. Marshal.Copy(new IntPtr(ptr.ToInt64() + ovrAvatarBodyComponent_Offsets.rightEyeTransform), scratchBufferFloat, 0, 10);
  898. OvrAvatar.ConvertTransform(scratchBufferFloat, ref component.rightEyeTransform);
  899. Marshal.Copy(new IntPtr(ptr.ToInt64() + ovrAvatarBodyComponent_Offsets.centerEyeTransform), scratchBufferFloat, 0, 10);
  900. OvrAvatar.ConvertTransform(scratchBufferFloat, ref component.centerEyeTransform);
  901. component.renderComponent = MarshalRenderComponent<ovrAvatarBodyComponent>(ptr);
  902. return true;
  903. }
  904. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint ="ovrAvatarPose_GetBodyComponent")]
  905. private static extern IntPtr ovrAvatarPose_GetBodyComponent_Native(IntPtr avatar);
  906. public static bool ovrAvatarPose_GetLeftControllerComponent(IntPtr avatar, ref ovrAvatarControllerComponent component)
  907. {
  908. IntPtr ptr = ovrAvatarPose_GetLeftControllerComponent_Native(avatar);
  909. if (ptr == IntPtr.Zero)
  910. {
  911. return false;
  912. }
  913. int renderComponentOffset = Marshal.SizeOf(typeof(ovrAvatarControllerComponent)) - Marshal.SizeOf(typeof(IntPtr));
  914. component.renderComponent = Marshal.ReadIntPtr(ptr, renderComponentOffset);
  915. return true;
  916. }
  917. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  918. "ovrAvatarPose_GetLeftControllerComponent")]
  919. private static extern IntPtr ovrAvatarPose_GetLeftControllerComponent_Native(IntPtr avatar);
  920. public static bool ovrAvatarPose_GetRightControllerComponent(IntPtr avatar, ref ovrAvatarControllerComponent component)
  921. {
  922. IntPtr ptr = ovrAvatarPose_GetRightControllerComponent_Native(avatar);
  923. if (ptr == IntPtr.Zero)
  924. {
  925. return false;
  926. }
  927. int renderComponentOffset = Marshal.SizeOf(typeof(ovrAvatarControllerComponent)) - Marshal.SizeOf(typeof(IntPtr));
  928. component.renderComponent = Marshal.ReadIntPtr(ptr, renderComponentOffset);
  929. return true;
  930. }
  931. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  932. "ovrAvatarPose_GetRightControllerComponent")]
  933. private static extern IntPtr ovrAvatarPose_GetRightControllerComponent_Native(IntPtr avatar);
  934. public static bool ovrAvatarPose_GetLeftHandComponent(IntPtr avatar, ref ovrAvatarHandComponent component)
  935. {
  936. IntPtr ptr = ovrAvatarPose_GetLeftHandComponent_Native(avatar);
  937. if (ptr == IntPtr.Zero)
  938. {
  939. return false;
  940. }
  941. int renderComponentOffset = Marshal.SizeOf(typeof(ovrAvatarHandComponent)) - Marshal.SizeOf(typeof(IntPtr));
  942. component.renderComponent = Marshal.ReadIntPtr(ptr, renderComponentOffset);
  943. return true;
  944. }
  945. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  946. "ovrAvatarPose_GetLeftHandComponent")]
  947. private static extern IntPtr ovrAvatarPose_GetLeftHandComponent_Native(IntPtr avatar);
  948. public static bool ovrAvatarPose_GetRightHandComponent(IntPtr avatar, ref ovrAvatarHandComponent component)
  949. {
  950. IntPtr ptr = ovrAvatarPose_GetRightHandComponent_Native(avatar);
  951. if (ptr == IntPtr.Zero)
  952. {
  953. return false;
  954. }
  955. int renderComponentOffset = Marshal.SizeOf(typeof(ovrAvatarHandComponent)) - Marshal.SizeOf(typeof(IntPtr));
  956. component.renderComponent = Marshal.ReadIntPtr(ptr, renderComponentOffset);
  957. return true;
  958. }
  959. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  960. "ovrAvatarPose_GetRightHandComponent")]
  961. private static extern IntPtr ovrAvatarPose_GetRightHandComponent_Native(IntPtr avatar);
  962. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  963. public static extern void ovrAvatarAsset_BeginLoading(UInt64 assetID);
  964. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  965. public static extern bool ovrAvatarAsset_BeginLoadingLOD(UInt64 assetId, ovrAvatarAssetLevelOfDetail lod);
  966. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  967. public static extern ovrAvatarAssetType ovrAvatarAsset_GetType(IntPtr assetHandle);
  968. public static ovrAvatarMeshAssetData ovrAvatarAsset_GetMeshData(
  969. IntPtr assetPtr)
  970. {
  971. IntPtr ptr = ovrAvatarAsset_GetMeshData_Native(assetPtr);
  972. return (ovrAvatarMeshAssetData)Marshal.PtrToStructure(
  973. ptr, typeof(ovrAvatarMeshAssetData));
  974. }
  975. public static ovrAvatarMeshAssetDataV2 ovrAvatarAsset_GetCombinedMeshData(
  976. IntPtr assetPtr)
  977. {
  978. IntPtr ptr = ovrAvatarAsset_GetCombinedMeshData_Native(assetPtr);
  979. return (ovrAvatarMeshAssetDataV2)Marshal.PtrToStructure(
  980. ptr, typeof(ovrAvatarMeshAssetDataV2));
  981. }
  982. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatarAsset_GetCombinedMeshData")]
  983. private static extern IntPtr ovrAvatarAsset_GetCombinedMeshData_Native(IntPtr assetPtr);
  984. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatarAsset_GetMeshData")]
  985. private static extern IntPtr ovrAvatarAsset_GetMeshData_Native(IntPtr assetPtr);
  986. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  987. public static extern UInt32 ovrAvatarAsset_GetMeshBlendShapeCount(IntPtr assetPtr);
  988. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  989. public static extern IntPtr ovrAvatarAsset_GetMeshBlendShapeName(IntPtr assetPtr, UInt32 index);
  990. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  991. public static extern UInt32 ovrAvatarAsset_GetSubmeshCount(IntPtr assetPtr);
  992. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  993. public static extern UInt32 ovrAvatarAsset_GetSubmeshLastIndex(IntPtr assetPtr, UInt32 index);
  994. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  995. public static extern IntPtr ovrAvatarAsset_GetMeshBlendShapeVertices(IntPtr assetPtr);
  996. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  997. public static extern IntPtr ovrAvatarAsset_GetAvatar(IntPtr assetHandle);
  998. public static UInt64[] ovrAvatarAsset_GetCombinedMeshIDs(IntPtr assetHandle)
  999. {
  1000. UInt32 count = 0;
  1001. System.IntPtr countPtr = Marshal.AllocHGlobal(Marshal.SizeOf(count));
  1002. IntPtr idBuffer = ovrAvatarAsset_GetCombinedMeshIDs_Native(assetHandle, countPtr);
  1003. count = (UInt32)Marshal.PtrToStructure(countPtr, typeof(UInt32));
  1004. UInt64[] meshIDs = new UInt64[count];
  1005. for (int i = 0; i < count; i++)
  1006. {
  1007. meshIDs[i] = (UInt64)Marshal.ReadInt64(idBuffer, i * Marshal.SizeOf(typeof(UInt64)));
  1008. }
  1009. Marshal.FreeHGlobal(countPtr);
  1010. return meshIDs;
  1011. }
  1012. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatarAsset_GetCombinedMeshIDs")]
  1013. public static extern IntPtr ovrAvatarAsset_GetCombinedMeshIDs_Native(IntPtr assetHandle, IntPtr count);
  1014. public static void ovrAvatar_GetCombinedMeshAlphaData(IntPtr avatar, ref UInt64 textureID, ref Vector4 offset)
  1015. {
  1016. System.IntPtr textureIDPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UInt64)));
  1017. System.IntPtr offsetPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Vector4)));
  1018. ovrAvatar_GetCombinedMeshAlphaData_Native(avatar, textureIDPtr, offsetPtr);
  1019. textureID = (UInt64)Marshal.PtrToStructure(textureIDPtr, typeof(UInt64));
  1020. offset = (Vector4)Marshal.PtrToStructure(offsetPtr, typeof(Vector4));
  1021. Marshal.FreeHGlobal(textureIDPtr);
  1022. Marshal.FreeHGlobal(offsetPtr);
  1023. }
  1024. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatar_GetCombinedMeshAlphaData")]
  1025. public static extern IntPtr ovrAvatar_GetCombinedMeshAlphaData_Native(IntPtr avatar, IntPtr textureIDPtr, IntPtr offsetPtr);
  1026. public static ovrAvatarTextureAssetData ovrAvatarAsset_GetTextureData(
  1027. IntPtr assetPtr)
  1028. {
  1029. IntPtr ptr = ovrAvatarAsset_GetTextureData_Native(assetPtr);
  1030. return (ovrAvatarTextureAssetData)Marshal.PtrToStructure(
  1031. ptr, typeof(ovrAvatarTextureAssetData));
  1032. }
  1033. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  1034. "ovrAvatarAsset_GetTextureData")]
  1035. private static extern IntPtr ovrAvatarAsset_GetTextureData_Native(IntPtr assetPtr);
  1036. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint =
  1037. "ovrAvatarAsset_GetMaterialData")]
  1038. private static extern IntPtr ovrAvatarAsset_GetMaterialData_Native(IntPtr assetPtr);
  1039. public static ovrAvatarMaterialState ovrAvatarAsset_GetMaterialState(IntPtr assetPtr)
  1040. {
  1041. IntPtr ptr = ovrAvatarAsset_GetMaterialData_Native(assetPtr);
  1042. return (ovrAvatarMaterialState)Marshal.PtrToStructure(ptr, typeof(ovrAvatarMaterialState));
  1043. }
  1044. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1045. public static extern ovrAvatarRenderPartType ovrAvatarRenderPart_GetType(IntPtr renderPart);
  1046. public static ovrAvatarRenderPart_SkinnedMeshRender ovrAvatarRenderPart_GetSkinnedMeshRender(IntPtr renderPart)
  1047. {
  1048. IntPtr ptr = ovrAvatarRenderPart_GetSkinnedMeshRender_Native(renderPart);
  1049. return (ovrAvatarRenderPart_SkinnedMeshRender)Marshal.PtrToStructure(
  1050. ptr, typeof(ovrAvatarRenderPart_SkinnedMeshRender));
  1051. }
  1052. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatarRenderPart_GetSkinnedMeshRender")]
  1053. private static extern IntPtr ovrAvatarRenderPart_GetSkinnedMeshRender_Native(IntPtr renderPart);
  1054. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1055. public static extern ovrAvatarTransform ovrAvatarSkinnedMeshRender_GetTransform(IntPtr renderPart);
  1056. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1057. public static extern ovrAvatarTransform ovrAvatarSkinnedMeshRenderPBS_GetTransform(IntPtr renderPart);
  1058. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1059. public static extern ovrAvatarTransform ovrAvatarSkinnedMeshRenderPBSV2_GetTransform(IntPtr renderPart);
  1060. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1061. public static extern ovrAvatarVisibilityFlags ovrAvatarSkinnedMeshRender_GetVisibilityMask(IntPtr renderPart);
  1062. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1063. public static extern bool ovrAvatarSkinnedMeshRender_MaterialStateChanged(IntPtr renderPart);
  1064. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1065. public static extern bool ovrAvatarSkinnedMeshRenderPBSV2_MaterialStateChanged(IntPtr renderPart);
  1066. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1067. public static extern ovrAvatarVisibilityFlags ovrAvatarSkinnedMeshRenderPBS_GetVisibilityMask(IntPtr renderPart);
  1068. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1069. public static extern ovrAvatarVisibilityFlags ovrAvatarSkinnedMeshRenderPBSV2_GetVisibilityMask(IntPtr renderPart);
  1070. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1071. public static extern ovrAvatarMaterialState ovrAvatarSkinnedMeshRender_GetMaterialState(IntPtr renderPart);
  1072. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1073. public static extern ovrAvatarPBSMaterialState ovrAvatarSkinnedMeshRenderPBSV2_GetPBSMaterialState(IntPtr renderPart);
  1074. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1075. public static extern ovrAvatarExpressiveParameters ovrAvatar_GetExpressiveParameters(IntPtr avatar);
  1076. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1077. public static extern UInt64 ovrAvatarSkinnedMeshRender_GetDirtyJoints(IntPtr renderPart);
  1078. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1079. public static extern UInt64 ovrAvatarSkinnedMeshRenderPBS_GetDirtyJoints(IntPtr renderPart);
  1080. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1081. public static extern UInt64 ovrAvatarSkinnedMeshRenderPBSV2_GetDirtyJoints(IntPtr renderPart);
  1082. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1083. public static extern ovrAvatarTransform ovrAvatarSkinnedMeshRender_GetJointTransform(IntPtr renderPart, UInt32 jointIndex);
  1084. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1085. public static extern void ovrAvatar_SetActionUnitOnsetSpeed(IntPtr avatar, float onsetSpeed);
  1086. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1087. public static extern void ovrAvatar_SetActionUnitFalloffSpeed(IntPtr avatar, float falloffSpeed);
  1088. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1089. public static extern void ovrAvatar_SetVisemeMultiplier(IntPtr avatar, float visemeMultiplier);
  1090. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1091. public static extern ovrAvatarTransform ovrAvatarSkinnedMeshRenderPBS_GetJointTransform(IntPtr renderPart, UInt32 jointIndex);
  1092. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1093. public static extern ovrAvatarTransform ovrAvatarSkinnedMeshRenderPBSV2_GetJointTransform(IntPtr renderPart, UInt32 jointIndex);
  1094. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1095. public static extern UInt64 ovrAvatarSkinnedMeshRenderPBS_GetAlbedoTextureAssetID(IntPtr renderPart);
  1096. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1097. public static extern UInt64 ovrAvatarSkinnedMeshRenderPBS_GetSurfaceTextureAssetID(IntPtr renderPart);
  1098. public static ovrAvatarRenderPart_SkinnedMeshRenderPBS ovrAvatarRenderPart_GetSkinnedMeshRenderPBS(IntPtr renderPart)
  1099. {
  1100. IntPtr ptr = ovrAvatarRenderPart_GetSkinnedMeshRenderPBS_Native(renderPart);
  1101. return (ovrAvatarRenderPart_SkinnedMeshRenderPBS)Marshal.PtrToStructure(
  1102. ptr, typeof(ovrAvatarRenderPart_SkinnedMeshRenderPBS));
  1103. }
  1104. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatarRenderPart_GetSkinnedMeshRenderPBS")]
  1105. private static extern IntPtr ovrAvatarRenderPart_GetSkinnedMeshRenderPBS_Native(IntPtr renderPart);
  1106. public static ovrAvatarRenderPart_SkinnedMeshRenderPBS_V2 ovrAvatarRenderPart_GetSkinnedMeshRenderPBSV2(IntPtr renderPart)
  1107. {
  1108. IntPtr ptr = ovrAvatarRenderPart_GetSkinnedMeshRenderPBSV2_Native(renderPart);
  1109. return (ovrAvatarRenderPart_SkinnedMeshRenderPBS_V2)Marshal.PtrToStructure(
  1110. ptr, typeof(ovrAvatarRenderPart_SkinnedMeshRenderPBS_V2));
  1111. }
  1112. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatarRenderPart_GetSkinnedMeshRenderPBSV2")]
  1113. private static extern IntPtr ovrAvatarRenderPart_GetSkinnedMeshRenderPBSV2_Native(IntPtr renderPart);
  1114. public static void ovrAvatarSkinnedMeshRender_GetBlendShapeParams(IntPtr renderPart, ref ovrAvatarBlendShapeParams blendParams)
  1115. {
  1116. IntPtr ptr = ovrAvatarSkinnedMeshRender_GetBlendShapeParams_Native(renderPart);
  1117. blendParams.blendShapeParamCount = (UInt32)Marshal.ReadInt32(ptr);
  1118. Marshal.Copy(new IntPtr(ptr.ToInt64() + ovrAvatarBlendShapeParams_Offsets.blendShapeParams), blendParams.blendShapeParams, 0, (int)blendParams.blendShapeParamCount);
  1119. }
  1120. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatarSkinnedMeshRender_GetBlendShapeParams")]
  1121. private static extern IntPtr ovrAvatarSkinnedMeshRender_GetBlendShapeParams_Native(IntPtr renderPart);
  1122. public static ovrAvatarRenderPart_ProjectorRender ovrAvatarRenderPart_GetProjectorRender(IntPtr renderPart)
  1123. {
  1124. IntPtr ptr = ovrAvatarRenderPart_GetProjectorRender_Native(renderPart);
  1125. return (ovrAvatarRenderPart_ProjectorRender)Marshal.PtrToStructure(
  1126. ptr, typeof(ovrAvatarRenderPart_ProjectorRender));
  1127. }
  1128. public static ovrAvatarPBSMaterialState[] ovrAvatar_GetBodyPBSMaterialStates(IntPtr renderPart)
  1129. {
  1130. System.IntPtr countPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UInt32)));
  1131. IntPtr ptrState = ovrAvatar_GetBodyPBSMaterialStates_Native(renderPart, countPtr);
  1132. UInt32 count = (UInt32)Marshal.ReadInt32(countPtr);
  1133. ovrAvatarPBSMaterialState[] states = new ovrAvatarPBSMaterialState[count];
  1134. for (int i = 0; i < states.Length; i++)
  1135. {
  1136. IntPtr nextItem = new IntPtr(ptrState.ToInt64() + i * Marshal.SizeOf(typeof(ovrAvatarPBSMaterialState)));
  1137. states[i] = (ovrAvatarPBSMaterialState)Marshal.PtrToStructure(nextItem, typeof(ovrAvatarPBSMaterialState));
  1138. }
  1139. Marshal.FreeHGlobal(countPtr);
  1140. return states;
  1141. }
  1142. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatar_GetBodyPBSMaterialStates")]
  1143. private static extern IntPtr ovrAvatar_GetBodyPBSMaterialStates_Native(IntPtr avatar, IntPtr count);
  1144. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatarRenderPart_GetProjectorRender")]
  1145. private static extern IntPtr ovrAvatarRenderPart_GetProjectorRender_Native(IntPtr renderPart);
  1146. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1147. public static extern UInt32 ovrAvatar_GetReferencedAssetCount(IntPtr avatar);
  1148. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1149. public static extern UInt64 ovrAvatar_GetReferencedAsset(IntPtr avatar, UInt32 index);
  1150. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1151. public static extern void ovrAvatar_SetLeftHandGesture(IntPtr avatar, ovrAvatarHandGesture gesture);
  1152. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1153. public static extern void ovrAvatar_SetRightHandGesture(IntPtr avatar, ovrAvatarHandGesture gesture);
  1154. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1155. public static extern void ovrAvatar_SetLeftHandCustomGesture(IntPtr avatar, UInt32 jointCount, [In] ovrAvatarTransform[] customJointTransforms);
  1156. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1157. public static extern void ovrAvatar_SetRightHandCustomGesture(IntPtr avatar, UInt32 jointCount, [In] ovrAvatarTransform[] customJointTransforms);
  1158. //Native calls for efficient packet updates
  1159. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1160. public static extern void ovrAvatar_UpdatePoseFromPacket(IntPtr avatar, IntPtr packet, float secondsFromStart);
  1161. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1162. public static extern void ovrAvatarPacket_BeginRecording(IntPtr avatar);
  1163. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1164. public static extern IntPtr ovrAvatarPacket_EndRecording(IntPtr avatar);
  1165. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1166. public static extern UInt32 ovrAvatarPacket_GetSize(IntPtr packet);
  1167. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1168. public static extern float ovrAvatarPacket_GetDurationSeconds(IntPtr packet);
  1169. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1170. public static extern void ovrAvatarPacket_Free(IntPtr packet);
  1171. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1172. public static extern bool ovrAvatarPacket_Write(IntPtr packet, UInt32 bufferSize, [Out] byte[] buffer);
  1173. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1174. public static extern IntPtr ovrAvatarPacket_Read(UInt32 bufferSize, [In] byte[] buffer);
  1175. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1176. private static extern void ovrAvatar_SetInternalForceASTCTextures(bool value);
  1177. // Renaming the outward facing method to remove Internal from name
  1178. public static void ovrAvatar_SetForceASTCTextures(bool value)
  1179. {
  1180. ovrAvatar_SetInternalForceASTCTextures(value);
  1181. }
  1182. public static void ovrAvatar_OverrideExpressiveLogic(IntPtr avatar, ovrAvatarBlendShapeParams blendParams)
  1183. {
  1184. IntPtr statePtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ovrAvatarBlendShapeParams)));
  1185. Marshal.StructureToPtr(blendParams, statePtr, false);
  1186. ovrAvatar_OverrideExpressiveLogic_Native(avatar, statePtr);
  1187. Marshal.FreeHGlobal(statePtr);
  1188. }
  1189. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatar_OverrideExpressiveLogic")]
  1190. private static extern void ovrAvatar_OverrideExpressiveLogic_Native(IntPtr avatar, IntPtr state);
  1191. public static void ovrAvatar_SetVisemes(IntPtr avatar, ovrAvatarVisemes visemes)
  1192. {
  1193. Marshal.WriteInt32(nativeVisemeData, (Int32)visemes.visemeParamCount);
  1194. Marshal.Copy(visemes.visemeParams, 0, new IntPtr(nativeVisemeData.ToInt64() + ovrAvatarVisemes_Offsets.visemeParams), (int)visemes.visemeParamCount);
  1195. ovrAvatar_SetVisemes_Native(avatar, nativeVisemeData);
  1196. }
  1197. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatar_SetVisemes")]
  1198. private static extern void ovrAvatar_SetVisemes_Native(IntPtr avatar, IntPtr visemes);
  1199. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1200. public static extern void ovrAvatar_UpdateWorldTransform(IntPtr avatar, ovrAvatarTransform transform);
  1201. public static void ovrAvatar_UpdateGazeTargets(ovrAvatarGazeTargets targets)
  1202. {
  1203. Marshal.WriteInt32(nativeGazeTargetsData, (Int32)targets.targetCount);
  1204. var targetOffset = ovrAvatarGazeTargets_Offsets.targets;
  1205. for (uint index = 0; index < targets.targetCount; index++)
  1206. {
  1207. var baseOffset = targetOffset + index * Marshal.SizeOf(typeof(ovrAvatarGazeTarget));
  1208. Marshal.WriteInt32(new IntPtr(nativeGazeTargetsData.ToInt64() + baseOffset + ovrAvatarGazeTarget_Offsets.id), (int)targets.targets[index].id);
  1209. scratchBufferFloat[0] = targets.targets[index].worldPosition.x;
  1210. scratchBufferFloat[1] = targets.targets[index].worldPosition.y;
  1211. scratchBufferFloat[2] = targets.targets[index].worldPosition.z;
  1212. Marshal.Copy(scratchBufferFloat, 0, new IntPtr(nativeGazeTargetsData.ToInt64() + baseOffset + ovrAvatarGazeTarget_Offsets.worldPosition), 3);
  1213. Marshal.WriteInt32(new IntPtr(nativeGazeTargetsData.ToInt64() + baseOffset + ovrAvatarGazeTarget_Offsets.type), (int)targets.targets[index].type);
  1214. }
  1215. ovrAvatar_UpdateGazeTargets_Native(nativeGazeTargetsData);
  1216. }
  1217. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatar_UpdateGazeTargets")]
  1218. private static extern void ovrAvatar_UpdateGazeTargets_Native(IntPtr targets);
  1219. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1220. public static extern void ovrAvatar_RemoveGazeTargets(UInt32 targetCount, UInt32[] ids);
  1221. public static void ovrAvatar_UpdateLights(ovrAvatarLights lights)
  1222. {
  1223. scratchBufferFloat[0] = lights.ambientIntensity;
  1224. Marshal.Copy(scratchBufferFloat, 0, nativeAvatarLightsData, 1);
  1225. Marshal.WriteInt32(new IntPtr(nativeAvatarLightsData.ToInt64() + Marshal.OffsetOf(typeof(ovrAvatarLights), "lightCount").ToInt64()), (int)lights.lightCount);
  1226. var lightsOffset = Marshal.OffsetOf(typeof(ovrAvatarLights), "lights").ToInt64();
  1227. for (uint index = 0; index < lights.lightCount; index++)
  1228. {
  1229. var baseOffset = lightsOffset + index * Marshal.SizeOf(typeof(ovrAvatarLight));
  1230. Marshal.WriteInt32(new IntPtr(nativeAvatarLightsData.ToInt64() + baseOffset + Marshal.OffsetOf(typeof(ovrAvatarLight), "id").ToInt64()), (int)lights.lights[index].id);
  1231. Marshal.WriteInt32(new IntPtr(nativeAvatarLightsData.ToInt64() + baseOffset + Marshal.OffsetOf(typeof(ovrAvatarLight), "type").ToInt64()), (int)lights.lights[index].type);
  1232. scratchBufferFloat[0] = lights.lights[index].intensity;
  1233. Marshal.Copy(scratchBufferFloat, 0, new IntPtr(nativeAvatarLightsData.ToInt64() + baseOffset + Marshal.OffsetOf(typeof(ovrAvatarLight), "intensity").ToInt64()), 1);
  1234. scratchBufferFloat[0] = lights.lights[index].worldDirection.x;
  1235. scratchBufferFloat[1] = lights.lights[index].worldDirection.y;
  1236. scratchBufferFloat[2] = lights.lights[index].worldDirection.z;
  1237. Marshal.Copy(scratchBufferFloat, 0, new IntPtr(nativeAvatarLightsData.ToInt64() + baseOffset + Marshal.OffsetOf(typeof(ovrAvatarLight), "worldDirection").ToInt64()), 3);
  1238. scratchBufferFloat[0] = lights.lights[index].worldPosition.x;
  1239. scratchBufferFloat[1] = lights.lights[index].worldPosition.y;
  1240. scratchBufferFloat[2] = lights.lights[index].worldPosition.z;
  1241. Marshal.Copy(scratchBufferFloat, 0, new IntPtr(nativeAvatarLightsData.ToInt64() + baseOffset + Marshal.OffsetOf(typeof(ovrAvatarLight), "worldPosition").ToInt64()), 3);
  1242. scratchBufferFloat[0] = lights.lights[index].range;
  1243. Marshal.Copy(scratchBufferFloat, 0, new IntPtr(nativeAvatarLightsData.ToInt64() + baseOffset + Marshal.OffsetOf(typeof(ovrAvatarLight), "range").ToInt64()), 1);
  1244. scratchBufferFloat[0] = lights.lights[index].spotAngleDeg;
  1245. Marshal.Copy(scratchBufferFloat, 0, new IntPtr(nativeAvatarLightsData.ToInt64() + baseOffset + Marshal.OffsetOf(typeof(ovrAvatarLight), "spotAngleDeg").ToInt64()), 1);
  1246. }
  1247. ovrAvatar_UpdateLights_Native(nativeAvatarLightsData);
  1248. }
  1249. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatar_UpdateLights")]
  1250. private static extern void ovrAvatar_UpdateLights_Native(IntPtr lights);
  1251. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1252. public static extern void ovrAvatar_RemoveLights(UInt32 lightCount, UInt32[] ids);
  1253. private static string SDKRuntimePrefix = "[RUNTIME] - ";
  1254. public delegate void LoggingDelegate(IntPtr str);
  1255. [MonoPInvokeCallback(typeof(LoggingDelegate))]
  1256. public static void LoggingCallback(IntPtr str)
  1257. {
  1258. string csharpStr = Marshal.PtrToStringAnsi(str);
  1259. AvatarLogger.Log(SDKRuntimePrefix + csharpStr);
  1260. }
  1261. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1262. public static extern void ovrAvatar_RegisterLoggingCallback(LoggingDelegate callback);
  1263. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1264. public static extern void ovrAvatar_SetLoggingLevel(ovrAvatarLogLevel level);
  1265. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatar_GetDebugTransforms")]
  1266. public static extern IntPtr ovrAvatar_GetDebugTransforms_Native(IntPtr count);
  1267. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrAvatar_GetDebugLines")]
  1268. public static extern IntPtr ovrAvatar_GetDebugLines_Native(IntPtr count);
  1269. public static void ovrAvatar_DrawDebugLines()
  1270. {
  1271. IntPtr debugLinePtr = ovrAvatar_GetDebugLines_Native(DebugLineCountData);
  1272. int lineCount = Marshal.ReadInt32(DebugLineCountData);
  1273. ovrAvatarDebugLine tempLine = new ovrAvatarDebugLine();
  1274. for (int i = 0; i < lineCount; i++)
  1275. {
  1276. var offset = i * Marshal.SizeOf(typeof(ovrAvatarDebugLine));
  1277. Marshal.Copy(new IntPtr(debugLinePtr.ToInt64() + offset), scratchBufferFloat, 0, 9);
  1278. tempLine.startPoint.x = scratchBufferFloat[0];
  1279. tempLine.startPoint.y = scratchBufferFloat[1];
  1280. tempLine.startPoint.z = -scratchBufferFloat[2];
  1281. tempLine.endPoint.x = scratchBufferFloat[3];
  1282. tempLine.endPoint.y = scratchBufferFloat[4];
  1283. tempLine.endPoint.z = -scratchBufferFloat[5];
  1284. tempLine.color.x = scratchBufferFloat[6];
  1285. tempLine.color.y = scratchBufferFloat[7];
  1286. tempLine.color.z = scratchBufferFloat[8];
  1287. tempLine.context = (ovrAvatarDebugContext)Marshal.ReadInt32(new IntPtr(debugLinePtr.ToInt64() + offset + Marshal.OffsetOf(typeof(ovrAvatarDebugLine), "context").ToInt64()));
  1288. tempLine.text = Marshal.ReadIntPtr(new IntPtr(debugLinePtr.ToInt64() + offset + Marshal.OffsetOf(typeof(ovrAvatarDebugLine), "text").ToInt64()));
  1289. Debug.DrawLine(tempLine.startPoint, tempLine.endPoint, new Color(tempLine.color.x, tempLine.color.y, tempLine.color.z));
  1290. // TODO: Decide what to do with the text. Can only debug render in OnGUI()
  1291. //if (tempLine.text != IntPtr.Zero)
  1292. //{
  1293. // string text = Marshal.PtrToStringAnsi(tempLine.text);
  1294. // AvatarLogger.Log(text);
  1295. //}
  1296. }
  1297. debugLinePtr = ovrAvatar_GetDebugTransforms_Native(DebugLineCountData);
  1298. lineCount = Marshal.ReadInt32(DebugLineCountData);
  1299. ovrAvatarDebugTransform tempTrans = new ovrAvatarDebugTransform();
  1300. for (int i = 0; i < lineCount; i++)
  1301. {
  1302. var offset = i * Marshal.SizeOf(typeof(ovrAvatarDebugTransform));
  1303. Marshal.Copy(new IntPtr(debugLinePtr.ToInt64() + offset), scratchBufferFloat, 0, 10);
  1304. OvrAvatar.ConvertTransform(scratchBufferFloat, ref tempTrans.transform);
  1305. OvrAvatar.ConvertTransform(tempTrans.transform, debugLineGo.transform);
  1306. tempTrans.context = (ovrAvatarDebugContext)Marshal.ReadInt32(new IntPtr(debugLinePtr.ToInt64() + offset + Marshal.OffsetOf(typeof(ovrAvatarDebugTransform), "context").ToInt64()));
  1307. tempTrans.text = Marshal.ReadIntPtr(new IntPtr(debugLinePtr.ToInt64() + offset + Marshal.OffsetOf(typeof(ovrAvatarDebugTransform), "text").ToInt64()));
  1308. const float SCALE_FACTOR = 0.1f;
  1309. Vector3 transUp = SCALE_FACTOR * debugLineGo.transform.TransformVector(Vector3.up);
  1310. Vector3 transRight = SCALE_FACTOR * debugLineGo.transform.TransformVector(Vector3.right);
  1311. Vector3 transFwd = SCALE_FACTOR * debugLineGo.transform.TransformVector(Vector3.forward);
  1312. Debug.DrawLine(debugLineGo.transform.position, debugLineGo.transform.position + transUp, Color.green);
  1313. Debug.DrawLine(debugLineGo.transform.position, debugLineGo.transform.position + transRight, Color.red);
  1314. Debug.DrawLine(debugLineGo.transform.position, debugLineGo.transform.position + transFwd, Color.blue);
  1315. // TODO: Decide what to do with the text. Can only debug render in OnGUI()
  1316. //if (tempTrans.text != IntPtr.Zero)
  1317. //{
  1318. // string text = Marshal.PtrToStringAnsi(tempTrans.text);
  1319. // AvatarLogger.Log(text);
  1320. //}
  1321. }
  1322. }
  1323. [DllImport(LibFile, CallingConvention = CallingConvention.Cdecl)]
  1324. public static extern void ovrAvatar_SetDebugDrawContext(UInt32 context);
  1325. //OvrPlugin Hooks
  1326. private const string ovrPluginDLL = "OVRPlugin";
  1327. private static System.Version ovrPluginVersion;
  1328. public enum Result
  1329. {
  1330. /// Success
  1331. Success = 0,
  1332. /// Failure
  1333. Failure = -1000,
  1334. Failure_InvalidParameter = -1001,
  1335. Failure_NotInitialized = -1002,
  1336. Failure_InvalidOperation = -1003,
  1337. Failure_Unsupported = -1004,
  1338. Failure_NotYetImplemented = -1005,
  1339. Failure_OperationFailed = -1006,
  1340. Failure_InsufficientSize = -1007,
  1341. }
  1342. public static bool SendEvent(string name, string param = "", string source = "")
  1343. {
  1344. try
  1345. {
  1346. if (ovrPluginVersion == null)
  1347. {
  1348. string version = ovrp_GetVersion();
  1349. if (!String.IsNullOrEmpty(version))
  1350. {
  1351. ovrPluginVersion = new System.Version(version.Split('-')[0]);
  1352. }
  1353. else
  1354. {
  1355. ovrPluginVersion = new System.Version(0, 0, 0);
  1356. }
  1357. }
  1358. if (ovrPluginVersion >= OVRP_1_30_0.version)
  1359. {
  1360. return OVRP_1_30_0.ovrp_SendEvent2(name, param, source.Length == 0 ? "avatar_sdk" : source) == Result.Success;
  1361. }
  1362. else
  1363. {
  1364. return false;
  1365. }
  1366. }
  1367. catch (Exception)
  1368. {
  1369. return false;
  1370. }
  1371. }
  1372. [DllImport(ovrPluginDLL, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ovrp_GetVersion")]
  1373. private static extern IntPtr _ovrp_GetVersion();
  1374. public static string ovrp_GetVersion() { return Marshal.PtrToStringAnsi(_ovrp_GetVersion()); }
  1375. private static class OVRP_1_30_0
  1376. {
  1377. public static readonly System.Version version = new System.Version(1, 30, 0);
  1378. [DllImport(ovrPluginDLL, CallingConvention = CallingConvention.Cdecl)]
  1379. public static extern Result ovrp_SendEvent2(string name, string param, string source);
  1380. }
  1381. }
  1382. }