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.

347 lines
16 KiB

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. using Oculus.Avatar;
  5. public class OvrAvatarSkinnedMeshPBSV2RenderComponent : OvrAvatarRenderComponent
  6. {
  7. private OvrAvatarMaterialManager avatarMaterialManager;
  8. private bool previouslyActive = false;
  9. private bool isCombinedMaterial = false;
  10. private ovrAvatarExpressiveParameters ExpressiveParameters;
  11. private bool EnableExpressive = false;
  12. private int blendShapeCount = 0;
  13. private ovrAvatarBlendShapeParams blendShapeParams;
  14. private const string MAIN_MATERIAL_NAME = "main_material";
  15. private const string EYE_MATERIAL_NAME = "eye_material";
  16. private const string DEFAULT_MATERIAL_NAME = "_material";
  17. internal void Initialize(
  18. IntPtr renderPart,
  19. ovrAvatarRenderPart_SkinnedMeshRenderPBS_V2 skinnedMeshRender,
  20. OvrAvatarMaterialManager materialManager,
  21. int thirdPersonLayer,
  22. int firstPersonLayer,
  23. bool combinedMesh,
  24. ovrAvatarAssetLevelOfDetail lod,
  25. bool assignExpressiveParams,
  26. OvrAvatar avatar,
  27. bool isControllerModel)
  28. {
  29. avatarMaterialManager = materialManager;
  30. isCombinedMaterial = combinedMesh;
  31. mesh = CreateSkinnedMesh(
  32. skinnedMeshRender.meshAssetID,
  33. skinnedMeshRender.visibilityMask,
  34. thirdPersonLayer,
  35. firstPersonLayer);
  36. EnableExpressive = assignExpressiveParams;
  37. #if UNITY_ANDROID
  38. var singleComponentShader = EnableExpressive
  39. ? avatar.Skinshaded_Expressive_VertFrag_SingleComponent
  40. : avatar.Skinshaded_VertFrag_SingleComponent;
  41. #else
  42. var singleComponentShader = EnableExpressive
  43. ? avatar.Skinshaded_Expressive_SurfaceShader_SingleComponent
  44. : avatar.Skinshaded_SurfaceShader_SingleComponent;
  45. #endif
  46. var combinedComponentShader = EnableExpressive
  47. ? avatar.Skinshaded_Expressive_VertFrag_CombinedMesh
  48. : avatar.Skinshaded_VertFrag_CombinedMesh;
  49. var mainShader = isCombinedMaterial ? combinedComponentShader : singleComponentShader;
  50. if (isControllerModel)
  51. {
  52. mainShader = avatar.ControllerShader;
  53. }
  54. AvatarLogger.Log("OvrAvatarSkinnedMeshPBSV2RenderComponent Shader is: " + mainShader != null
  55. ? mainShader.name : "null");
  56. if (EnableExpressive)
  57. {
  58. ExpressiveParameters = CAPI.ovrAvatar_GetExpressiveParameters(avatar.sdkAvatar);
  59. var eyeShader = avatar.EyeLens;
  60. Material[] matArray = new Material[2];
  61. matArray[0] = CreateAvatarMaterial(gameObject.name + MAIN_MATERIAL_NAME, mainShader);
  62. matArray[1] = CreateAvatarMaterial(gameObject.name + EYE_MATERIAL_NAME, eyeShader);
  63. if (avatar.UseTransparentRenderQueue)
  64. {
  65. SetMaterialTransparent(matArray[0]);
  66. }
  67. else
  68. {
  69. SetMaterialOpaque(matArray[0]);
  70. }
  71. // Eye lens shader queue is transparent and set from shader
  72. matArray[1].renderQueue = -1;
  73. mesh.materials = matArray;
  74. }
  75. else
  76. {
  77. mesh.sharedMaterial = CreateAvatarMaterial(gameObject.name + DEFAULT_MATERIAL_NAME, mainShader);
  78. if (avatar.UseTransparentRenderQueue && !isControllerModel)
  79. {
  80. SetMaterialTransparent(mesh.sharedMaterial);
  81. }
  82. else
  83. {
  84. SetMaterialOpaque(mesh.sharedMaterial);
  85. }
  86. }
  87. bones = mesh.bones;
  88. if (isCombinedMaterial)
  89. {
  90. avatarMaterialManager.SetRenderer(mesh);
  91. InitializeCombinedMaterial(renderPart, (int)lod);
  92. avatarMaterialManager.OnCombinedMeshReady();
  93. }
  94. blendShapeParams = new ovrAvatarBlendShapeParams();
  95. blendShapeParams.blendShapeParamCount = 0;
  96. blendShapeParams.blendShapeParams = new float[64];
  97. blendShapeCount = mesh.sharedMesh.blendShapeCount;
  98. }
  99. public void UpdateSkinnedMeshRender(
  100. OvrAvatarComponent component,
  101. OvrAvatar avatar,
  102. IntPtr renderPart)
  103. {
  104. ovrAvatarVisibilityFlags visibilityMask
  105. = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetVisibilityMask(renderPart);
  106. ovrAvatarTransform localTransform
  107. = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetTransform(renderPart);
  108. UpdateSkinnedMesh(avatar, bones, localTransform, visibilityMask, renderPart);
  109. bool isActive = gameObject.activeSelf;
  110. if (mesh != null && !previouslyActive && isActive)
  111. {
  112. if (!isCombinedMaterial)
  113. {
  114. InitializeSingleComponentMaterial(renderPart, (int)avatar.LevelOfDetail - 1);
  115. }
  116. }
  117. if (blendShapeCount > 0)
  118. {
  119. const float BLEND_MULTIPLIER = 100.0f;
  120. CAPI.ovrAvatarSkinnedMeshRender_GetBlendShapeParams(renderPart, ref blendShapeParams);
  121. for (uint i = 0; i < blendShapeParams.blendShapeParamCount && i < blendShapeCount; i++)
  122. {
  123. float value = blendShapeParams.blendShapeParams[i];
  124. mesh.SetBlendShapeWeight((int)i, value * BLEND_MULTIPLIER);
  125. }
  126. }
  127. previouslyActive = isActive;
  128. }
  129. private void InitializeSingleComponentMaterial(IntPtr renderPart, int lodIndex)
  130. {
  131. ovrAvatarPBSMaterialState materialState =
  132. CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetPBSMaterialState(renderPart);
  133. int componentType = (int)OvrAvatarMaterialManager.GetComponentType(gameObject.name);
  134. Texture2D diffuseTexture = OvrAvatarComponent.GetLoadedTexture(materialState.albedoTextureID);
  135. Texture2D normalTexture = OvrAvatarComponent.GetLoadedTexture(materialState.normalTextureID);
  136. Texture2D metallicTexture = OvrAvatarComponent.GetLoadedTexture(materialState.metallicnessTextureID);
  137. if (diffuseTexture != null)
  138. {
  139. avatarMaterialManager.AddTextureIDToTextureManager(materialState.albedoTextureID, true);
  140. }
  141. else
  142. {
  143. diffuseTexture = OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
  144. }
  145. diffuseTexture.anisoLevel = 4;
  146. if (normalTexture != null)
  147. {
  148. avatarMaterialManager.AddTextureIDToTextureManager(materialState.normalTextureID, true);
  149. }
  150. else
  151. {
  152. normalTexture = OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].Normal;
  153. }
  154. normalTexture.anisoLevel = 4;
  155. if (metallicTexture != null)
  156. {
  157. avatarMaterialManager.AddTextureIDToTextureManager(materialState.metallicnessTextureID, true);
  158. }
  159. else
  160. {
  161. metallicTexture = OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
  162. }
  163. metallicTexture.anisoLevel = 16;
  164. mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_MAINTEX, diffuseTexture);
  165. mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_NORMALMAP, normalTexture);
  166. mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_ROUGHNESSMAP, metallicTexture);
  167. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_COLOR, materialState.albedoMultiplier);
  168. mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_DIFFUSEINTENSITY,
  169. OvrAvatarMaterialManager.DiffuseIntensities[componentType]);
  170. mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_RIMINTENSITY,
  171. OvrAvatarMaterialManager.RimIntensities[componentType]);
  172. mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_REFLECTIONINTENSITY,
  173. OvrAvatarMaterialManager.ReflectionIntensities[componentType]);
  174. mesh.GetClosestReflectionProbes(avatarMaterialManager.ReflectionProbes);
  175. if (avatarMaterialManager.ReflectionProbes != null &&
  176. avatarMaterialManager.ReflectionProbes.Count > 0)
  177. {
  178. mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_CUBEMAP,
  179. avatarMaterialManager.ReflectionProbes[0].probe.texture);
  180. }
  181. if (EnableExpressive)
  182. {
  183. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_IRIS_COLOR,
  184. ExpressiveParameters.irisColor);
  185. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_COLOR,
  186. ExpressiveParameters.lipColor);
  187. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_BROW_COLOR,
  188. ExpressiveParameters.browColor);
  189. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LASH_COLOR,
  190. ExpressiveParameters.lashColor);
  191. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_SCLERA_COLOR,
  192. ExpressiveParameters.scleraColor);
  193. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_GUM_COLOR,
  194. ExpressiveParameters.gumColor);
  195. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_TEETH_COLOR,
  196. ExpressiveParameters.teethColor);
  197. mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_SMOOTHNESS,
  198. ExpressiveParameters.lipSmoothness);
  199. }
  200. }
  201. private void InitializeCombinedMaterial(IntPtr renderPart, int lodIndex)
  202. {
  203. ovrAvatarPBSMaterialState[] materialStates = CAPI.ovrAvatar_GetBodyPBSMaterialStates(renderPart);
  204. if (materialStates.Length == (int)ovrAvatarBodyPartType.Count)
  205. {
  206. avatarMaterialManager.CreateTextureArrays();
  207. var localProperties = avatarMaterialManager.LocalAvatarConfig.ComponentMaterialProperties;
  208. AvatarLogger.Log("InitializeCombinedMaterial - Loading Material States");
  209. for (int i = 0; i < materialStates.Length; i++)
  210. {
  211. localProperties[i].TypeIndex = (ovrAvatarBodyPartType)i;
  212. localProperties[i].Color = materialStates[i].albedoMultiplier;
  213. localProperties[i].DiffuseIntensity = OvrAvatarMaterialManager.DiffuseIntensities[i];
  214. localProperties[i].RimIntensity = OvrAvatarMaterialManager.RimIntensities[i];
  215. localProperties[i].ReflectionIntensity = OvrAvatarMaterialManager.ReflectionIntensities[i];
  216. var diffuse = OvrAvatarComponent.GetLoadedTexture(materialStates[i].albedoTextureID);
  217. var normal = OvrAvatarComponent.GetLoadedTexture(materialStates[i].normalTextureID);
  218. var roughness = OvrAvatarComponent.GetLoadedTexture(materialStates[i].metallicnessTextureID);
  219. if (diffuse != null)
  220. {
  221. localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.DiffuseTextures] = diffuse;
  222. avatarMaterialManager.AddTextureIDToTextureManager(materialStates[i].albedoTextureID, false);
  223. }
  224. else
  225. {
  226. localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.DiffuseTextures] =
  227. OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
  228. }
  229. localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.DiffuseTextures].anisoLevel = 4;
  230. if (normal != null)
  231. {
  232. localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.NormalMaps] = normal;
  233. avatarMaterialManager.AddTextureIDToTextureManager(materialStates[i].normalTextureID, false);
  234. }
  235. else
  236. {
  237. localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.NormalMaps] =
  238. OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].Normal;
  239. }
  240. localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.NormalMaps].anisoLevel = 4;
  241. if (roughness != null)
  242. {
  243. localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.RoughnessMaps] = roughness;
  244. avatarMaterialManager.AddTextureIDToTextureManager(materialStates[i].metallicnessTextureID, false);
  245. }
  246. else
  247. {
  248. localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.RoughnessMaps] =
  249. OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
  250. }
  251. localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.RoughnessMaps].anisoLevel = 16;
  252. AvatarLogger.Log(localProperties[i].TypeIndex.ToString());
  253. AvatarLogger.Log(AvatarLogger.Tab + "Diffuse: " + materialStates[i].albedoTextureID);
  254. AvatarLogger.Log(AvatarLogger.Tab + "Normal: " + materialStates[i].normalTextureID);
  255. AvatarLogger.Log(AvatarLogger.Tab + "Metallic: " + materialStates[i].metallicnessTextureID);
  256. }
  257. if (EnableExpressive)
  258. {
  259. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_IRIS_COLOR,
  260. ExpressiveParameters.irisColor);
  261. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_COLOR,
  262. ExpressiveParameters.lipColor);
  263. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_BROW_COLOR,
  264. ExpressiveParameters.browColor);
  265. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LASH_COLOR,
  266. ExpressiveParameters.lashColor);
  267. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_SCLERA_COLOR,
  268. ExpressiveParameters.scleraColor);
  269. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_GUM_COLOR,
  270. ExpressiveParameters.gumColor);
  271. mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_TEETH_COLOR,
  272. ExpressiveParameters.teethColor);
  273. mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_SMOOTHNESS,
  274. ExpressiveParameters.lipSmoothness);
  275. }
  276. avatarMaterialManager.ValidateTextures(materialStates);
  277. }
  278. }
  279. private void SetMaterialTransparent(Material mat)
  280. {
  281. // Initialize shader to use transparent render queue with alpha blending
  282. mat.SetOverrideTag("Queue", "Transparent");
  283. mat.SetOverrideTag("RenderType", "Transparent");
  284. mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  285. mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  286. mat.EnableKeyword("_ALPHATEST_ON");
  287. mat.EnableKeyword("_ALPHABLEND_ON");
  288. mat.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  289. mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  290. }
  291. private void SetMaterialOpaque(Material mat)
  292. {
  293. // Initialize shader to use geometry render queue with no blending
  294. mat.SetOverrideTag("Queue", "Geometry");
  295. mat.SetOverrideTag("RenderType", "Opaque");
  296. mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  297. mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  298. mat.DisableKeyword("_ALPHATEST_ON");
  299. mat.DisableKeyword("_ALPHABLEND_ON");
  300. mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  301. mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;
  302. }
  303. }