OvrAvatarRenderComponent.cs
5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using Oculus.Avatar;
public class OvrAvatarRenderComponent : MonoBehaviour {
private bool firstSkinnedUpdate = true;
public SkinnedMeshRenderer mesh;
public Transform[] bones;
bool isBodyComponent = false;
protected void UpdateActive(OvrAvatar avatar, ovrAvatarVisibilityFlags mask)
{
bool doActiveHack = isBodyComponent && avatar.EnableExpressive && avatar.ShowFirstPerson && !avatar.ShowThirdPerson;
if (doActiveHack)
{
bool showFirstPerson = (mask & ovrAvatarVisibilityFlags.FirstPerson) != 0;
bool showThirdPerson = (mask & ovrAvatarVisibilityFlags.ThirdPerson) != 0;
gameObject.SetActive(showThirdPerson || showThirdPerson);
if (!showFirstPerson)
{
mesh.enabled = false;
}
}
else
{
bool active = avatar.ShowFirstPerson && (mask & ovrAvatarVisibilityFlags.FirstPerson) != 0;
active |= avatar.ShowThirdPerson && (mask & ovrAvatarVisibilityFlags.ThirdPerson) != 0;
this.gameObject.SetActive(active);
mesh.enabled = active;
}
}
protected SkinnedMeshRenderer CreateSkinnedMesh(ulong assetID, ovrAvatarVisibilityFlags visibilityMask, int thirdPersonLayer, int firstPersonLayer)
{
isBodyComponent = name.Contains("body");
OvrAvatarAssetMesh meshAsset = (OvrAvatarAssetMesh)OvrAvatarSDKManager.Instance.GetAsset(assetID);
if (meshAsset == null)
{
throw new Exception("Couldn't find mesh for asset " + assetID);
}
if ((visibilityMask & ovrAvatarVisibilityFlags.ThirdPerson) != 0)
{
this.gameObject.layer = thirdPersonLayer;
}
else
{
this.gameObject.layer = firstPersonLayer;
}
SkinnedMeshRenderer renderer = meshAsset.CreateSkinnedMeshRendererOnObject(gameObject);
#if UNITY_ANDROID
renderer.quality = SkinQuality.Bone2;
#else
renderer.quality = SkinQuality.Bone4;
#endif
renderer.updateWhenOffscreen = true;
if ((visibilityMask & ovrAvatarVisibilityFlags.SelfOccluding) == 0)
{
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
}
gameObject.SetActive(false);
return renderer;
}
protected void UpdateSkinnedMesh(OvrAvatar avatar, Transform[] bones, ovrAvatarTransform localTransform, ovrAvatarVisibilityFlags visibilityMask, IntPtr renderPart)
{
UpdateActive(avatar, visibilityMask);
OvrAvatar.ConvertTransform(localTransform, this.transform);
ovrAvatarRenderPartType type = CAPI.ovrAvatarRenderPart_GetType(renderPart);
UInt64 dirtyJoints;
switch (type)
{
case ovrAvatarRenderPartType.SkinnedMeshRender:
dirtyJoints = CAPI.ovrAvatarSkinnedMeshRender_GetDirtyJoints(renderPart);
break;
case ovrAvatarRenderPartType.SkinnedMeshRenderPBS:
dirtyJoints = CAPI.ovrAvatarSkinnedMeshRenderPBS_GetDirtyJoints(renderPart);
break;
case ovrAvatarRenderPartType.SkinnedMeshRenderPBS_V2:
dirtyJoints = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetDirtyJoints(renderPart);
break;
default:
throw new Exception("Unhandled render part type: " + type);
}
for (UInt32 i = 0; i < 64; i++)
{
UInt64 dirtyMask = (ulong)1 << (int)i;
// We need to make sure that we fully update the initial position of
// Skinned mesh renderers, then, thereafter, we can only update dirty joints
if ((firstSkinnedUpdate && i < bones.Length) ||
(dirtyMask & dirtyJoints) != 0)
{
//This joint is dirty and needs to be updated
Transform targetBone = bones[i];
ovrAvatarTransform transform;
switch (type)
{
case ovrAvatarRenderPartType.SkinnedMeshRender:
transform = CAPI.ovrAvatarSkinnedMeshRender_GetJointTransform(renderPart, i);
break;
case ovrAvatarRenderPartType.SkinnedMeshRenderPBS:
transform = CAPI.ovrAvatarSkinnedMeshRenderPBS_GetJointTransform(renderPart, i);
break;
case ovrAvatarRenderPartType.SkinnedMeshRenderPBS_V2:
transform = CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetJointTransform(renderPart, i);
break;
default:
throw new Exception("Unhandled render part type: " + type);
}
OvrAvatar.ConvertTransform(transform, targetBone);
}
}
firstSkinnedUpdate = false;
}
protected Material CreateAvatarMaterial(string name, Shader shader)
{
if (shader == null)
{
throw new Exception("No shader provided for avatar material.");
}
Material mat = new Material(shader);
mat.name = name;
return mat;
}
}