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.

50 lines
2.3 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using Oculus.Avatar;
  5. public class OvrAvatarSkinnedMeshRenderComponent : OvrAvatarRenderComponent
  6. {
  7. Shader surface;
  8. Shader surfaceSelfOccluding;
  9. bool previouslyActive = false;
  10. internal void Initialize(ovrAvatarRenderPart_SkinnedMeshRender skinnedMeshRender, Shader surface, Shader surfaceSelfOccluding, int thirdPersonLayer, int firstPersonLayer)
  11. {
  12. this.surfaceSelfOccluding = surfaceSelfOccluding != null ? surfaceSelfOccluding : Shader.Find("OvrAvatar/AvatarSurfaceShaderSelfOccluding");
  13. this.surface = surface != null ? surface : Shader.Find("OvrAvatar/AvatarSurfaceShader");
  14. this.mesh = CreateSkinnedMesh(skinnedMeshRender.meshAssetID, skinnedMeshRender.visibilityMask, thirdPersonLayer, firstPersonLayer);
  15. bones = mesh.bones;
  16. UpdateMeshMaterial(skinnedMeshRender.visibilityMask, mesh);
  17. }
  18. public void UpdateSkinnedMeshRender(OvrAvatarComponent component, OvrAvatar avatar, IntPtr renderPart)
  19. {
  20. ovrAvatarVisibilityFlags visibilityMask = CAPI.ovrAvatarSkinnedMeshRender_GetVisibilityMask(renderPart);
  21. ovrAvatarTransform localTransform = CAPI.ovrAvatarSkinnedMeshRender_GetTransform(renderPart);
  22. UpdateSkinnedMesh(avatar, bones, localTransform, visibilityMask, renderPart);
  23. UpdateMeshMaterial(visibilityMask, mesh);
  24. bool isActive = this.gameObject.activeSelf;
  25. if( mesh != null )
  26. {
  27. bool changedMaterial = CAPI.ovrAvatarSkinnedMeshRender_MaterialStateChanged(renderPart);
  28. if (changedMaterial || (!previouslyActive && isActive))
  29. {
  30. ovrAvatarMaterialState materialState = CAPI.ovrAvatarSkinnedMeshRender_GetMaterialState(renderPart);
  31. component.UpdateAvatarMaterial(mesh.sharedMaterial, materialState);
  32. }
  33. }
  34. previouslyActive = isActive;
  35. }
  36. private void UpdateMeshMaterial(ovrAvatarVisibilityFlags visibilityMask, SkinnedMeshRenderer rootMesh)
  37. {
  38. Shader shader = (visibilityMask & ovrAvatarVisibilityFlags.SelfOccluding) != 0 ? surfaceSelfOccluding : surface;
  39. if (rootMesh.sharedMaterial == null || rootMesh.sharedMaterial.shader != shader)
  40. {
  41. rootMesh.sharedMaterial = CreateAvatarMaterial(gameObject.name + "_material", shader);
  42. }
  43. }
  44. }