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.

145 lines
8.4 KiB

  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Oculus.Avatar;
  6. using System.Threading;
  7. public class OvrAvatarComponent : MonoBehaviour
  8. {
  9. public static readonly string[] LayerKeywords = new[] { "LAYERS_0", "LAYERS_1", "LAYERS_2", "LAYERS_3", "LAYERS_4", "LAYERS_5", "LAYERS_6", "LAYERS_7", "LAYERS_8", };
  10. public static readonly string[] LayerSampleModeParameters = new[] { "_LayerSampleMode0", "_LayerSampleMode1", "_LayerSampleMode2", "_LayerSampleMode3", "_LayerSampleMode4", "_LayerSampleMode5", "_LayerSampleMode6", "_LayerSampleMode7", };
  11. public static readonly string[] LayerBlendModeParameters = new[] { "_LayerBlendMode0", "_LayerBlendMode1", "_LayerBlendMode2", "_LayerBlendMode3", "_LayerBlendMode4", "_LayerBlendMode5", "_LayerBlendMode6", "_LayerBlendMode7", };
  12. public static readonly string[] LayerMaskTypeParameters = new[] { "_LayerMaskType0", "_LayerMaskType1", "_LayerMaskType2", "_LayerMaskType3", "_LayerMaskType4", "_LayerMaskType5", "_LayerMaskType6", "_LayerMaskType7", };
  13. public static readonly string[] LayerColorParameters = new[] { "_LayerColor0", "_LayerColor1", "_LayerColor2", "_LayerColor3", "_LayerColor4", "_LayerColor5", "_LayerColor6", "_LayerColor7", };
  14. public static readonly string[] LayerSurfaceParameters = new[] { "_LayerSurface0", "_LayerSurface1", "_LayerSurface2", "_LayerSurface3", "_LayerSurface4", "_LayerSurface5", "_LayerSurface6", "_LayerSurface7", };
  15. public static readonly string[] LayerSampleParametersParameters = new[] { "_LayerSampleParameters0", "_LayerSampleParameters1", "_LayerSampleParameters2", "_LayerSampleParameters3", "_LayerSampleParameters4", "_LayerSampleParameters5", "_LayerSampleParameters6", "_LayerSampleParameters7", };
  16. public static readonly string[] LayerMaskParametersParameters = new[] { "_LayerMaskParameters0", "_LayerMaskParameters1", "_LayerMaskParameters2", "_LayerMaskParameters3", "_LayerMaskParameters4", "_LayerMaskParameters5", "_LayerMaskParameters6", "_LayerMaskParameters7", };
  17. public static readonly string[] LayerMaskAxisParameters = new[] { "_LayerMaskAxis0", "_LayerMaskAxis1", "_LayerMaskAxis2", "_LayerMaskAxis3", "_LayerMaskAxis4", "_LayerMaskAxis5", "_LayerMaskAxis6", "_LayerMaskAxis7", };
  18. private Dictionary<Material, ovrAvatarMaterialState> materialStates = new Dictionary<Material, ovrAvatarMaterialState>();
  19. public List<OvrAvatarRenderComponent> RenderParts = new List<OvrAvatarRenderComponent>();
  20. protected OvrAvatar owner = null;
  21. protected ovrAvatarComponent nativeAvatarComponent = new ovrAvatarComponent();
  22. public void SetOvrAvatarOwner(OvrAvatar ovrAvatarOwner)
  23. {
  24. owner = ovrAvatarOwner;
  25. }
  26. public void UpdateAvatar(IntPtr nativeComponent)
  27. {
  28. CAPI.ovrAvatarComponent_Get(nativeComponent, false, ref nativeAvatarComponent);
  29. OvrAvatar.ConvertTransform(nativeAvatarComponent.transform, transform);
  30. for (UInt32 renderPartIndex = 0; renderPartIndex < nativeAvatarComponent.renderPartCount; renderPartIndex++)
  31. {
  32. if (RenderParts.Count <= renderPartIndex)
  33. {
  34. break;
  35. }
  36. OvrAvatarRenderComponent renderComponent = RenderParts[(int)renderPartIndex];
  37. IntPtr renderPart = OvrAvatar.GetRenderPart(nativeAvatarComponent, renderPartIndex);
  38. ovrAvatarRenderPartType type = CAPI.ovrAvatarRenderPart_GetType(renderPart);
  39. switch (type)
  40. {
  41. case ovrAvatarRenderPartType.SkinnedMeshRender:
  42. ((OvrAvatarSkinnedMeshRenderComponent)renderComponent).UpdateSkinnedMeshRender(this, owner, renderPart);
  43. break;
  44. case ovrAvatarRenderPartType.SkinnedMeshRenderPBS:
  45. ((OvrAvatarSkinnedMeshRenderPBSComponent)renderComponent).UpdateSkinnedMeshRenderPBS(owner, renderPart, renderComponent.mesh.sharedMaterial);
  46. break;
  47. case ovrAvatarRenderPartType.SkinnedMeshRenderPBS_V2:
  48. ((OvrAvatarSkinnedMeshPBSV2RenderComponent)renderComponent).UpdateSkinnedMeshRender(this, owner, renderPart);
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. }
  55. protected void UpdateActive(OvrAvatar avatar, ovrAvatarVisibilityFlags mask)
  56. {
  57. bool active = avatar.ShowFirstPerson && (mask & ovrAvatarVisibilityFlags.FirstPerson) != 0;
  58. active |= avatar.ShowThirdPerson && (mask & ovrAvatarVisibilityFlags.ThirdPerson) != 0;
  59. this.gameObject.SetActive(active);
  60. }
  61. public void UpdateAvatarMaterial(Material mat, ovrAvatarMaterialState matState)
  62. {
  63. mat.SetColor("_BaseColor", matState.baseColor);
  64. mat.SetInt("_BaseMaskType", (int)matState.baseMaskType);
  65. mat.SetVector("_BaseMaskParameters", matState.baseMaskParameters);
  66. mat.SetVector("_BaseMaskAxis", matState.baseMaskAxis);
  67. if (matState.alphaMaskTextureID != 0)
  68. {
  69. mat.SetTexture("_AlphaMask", GetLoadedTexture(matState.alphaMaskTextureID));
  70. mat.SetTextureScale("_AlphaMask", new Vector2(matState.alphaMaskScaleOffset.x, matState.alphaMaskScaleOffset.y));
  71. mat.SetTextureOffset("_AlphaMask", new Vector2(matState.alphaMaskScaleOffset.z, matState.alphaMaskScaleOffset.w));
  72. }
  73. if (matState.normalMapTextureID != 0)
  74. {
  75. mat.EnableKeyword("NORMAL_MAP_ON");
  76. mat.SetTexture("_NormalMap", GetLoadedTexture(matState.normalMapTextureID));
  77. mat.SetTextureScale("_NormalMap", new Vector2(matState.normalMapScaleOffset.x, matState.normalMapScaleOffset.y));
  78. mat.SetTextureOffset("_NormalMap", new Vector2(matState.normalMapScaleOffset.z, matState.normalMapScaleOffset.w));
  79. }
  80. if (matState.parallaxMapTextureID != 0)
  81. {
  82. mat.SetTexture("_ParallaxMap", GetLoadedTexture(matState.parallaxMapTextureID));
  83. mat.SetTextureScale("_ParallaxMap", new Vector2(matState.parallaxMapScaleOffset.x, matState.parallaxMapScaleOffset.y));
  84. mat.SetTextureOffset("_ParallaxMap", new Vector2(matState.parallaxMapScaleOffset.z, matState.parallaxMapScaleOffset.w));
  85. }
  86. if (matState.roughnessMapTextureID != 0)
  87. {
  88. mat.EnableKeyword("ROUGHNESS_ON");
  89. mat.SetTexture("_RoughnessMap", GetLoadedTexture(matState.roughnessMapTextureID));
  90. mat.SetTextureScale("_RoughnessMap", new Vector2(matState.roughnessMapScaleOffset.x, matState.roughnessMapScaleOffset.y));
  91. mat.SetTextureOffset("_RoughnessMap", new Vector2(matState.roughnessMapScaleOffset.z, matState.roughnessMapScaleOffset.w));
  92. }
  93. mat.EnableKeyword(LayerKeywords[matState.layerCount]);
  94. for (ulong layerIndex = 0; layerIndex < matState.layerCount; layerIndex++)
  95. {
  96. ovrAvatarMaterialLayerState layer = matState.layers[layerIndex];
  97. mat.SetInt(LayerSampleModeParameters[layerIndex], (int)layer.sampleMode);
  98. mat.SetInt(LayerBlendModeParameters[layerIndex], (int)layer.blendMode);
  99. mat.SetInt(LayerMaskTypeParameters[layerIndex], (int)layer.maskType);
  100. mat.SetColor(LayerColorParameters[layerIndex], layer.layerColor);
  101. if (layer.sampleMode != ovrAvatarMaterialLayerSampleMode.Color)
  102. {
  103. string surfaceProperty = LayerSurfaceParameters[layerIndex];
  104. mat.SetTexture(surfaceProperty, GetLoadedTexture(layer.sampleTexture));
  105. mat.SetTextureScale(surfaceProperty, new Vector2(layer.sampleScaleOffset.x, layer.sampleScaleOffset.y));
  106. mat.SetTextureOffset(surfaceProperty, new Vector2(layer.sampleScaleOffset.z, layer.sampleScaleOffset.w));
  107. }
  108. if (layer.sampleMode == ovrAvatarMaterialLayerSampleMode.Parallax)
  109. {
  110. mat.EnableKeyword("PARALLAX_ON");
  111. }
  112. mat.SetColor(LayerSampleParametersParameters[layerIndex], layer.sampleParameters);
  113. mat.SetColor(LayerMaskParametersParameters[layerIndex], layer.maskParameters);
  114. mat.SetColor(LayerMaskAxisParameters[layerIndex], layer.maskAxis);
  115. }
  116. materialStates[mat] = matState;
  117. }
  118. public static Texture2D GetLoadedTexture(UInt64 assetId)
  119. {
  120. OvrAvatarAssetTexture tex = (OvrAvatarAssetTexture)OvrAvatarSDKManager.Instance.GetAsset(assetId);
  121. if (tex == null)
  122. {
  123. return null;
  124. }
  125. return tex.texture;
  126. }
  127. }