OvrAvatarSkinnedMeshRenderComponent.cs
2.33 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
using UnityEngine;
using System.Collections;
using System;
using Oculus.Avatar;
public class OvrAvatarSkinnedMeshRenderComponent : OvrAvatarRenderComponent
{
Shader surface;
Shader surfaceSelfOccluding;
bool previouslyActive = false;
internal void Initialize(ovrAvatarRenderPart_SkinnedMeshRender skinnedMeshRender, Shader surface, Shader surfaceSelfOccluding, int thirdPersonLayer, int firstPersonLayer)
{
this.surfaceSelfOccluding = surfaceSelfOccluding != null ? surfaceSelfOccluding : Shader.Find("OvrAvatar/AvatarSurfaceShaderSelfOccluding");
this.surface = surface != null ? surface : Shader.Find("OvrAvatar/AvatarSurfaceShader");
this.mesh = CreateSkinnedMesh(skinnedMeshRender.meshAssetID, skinnedMeshRender.visibilityMask, thirdPersonLayer, firstPersonLayer);
bones = mesh.bones;
UpdateMeshMaterial(skinnedMeshRender.visibilityMask, mesh);
}
public void UpdateSkinnedMeshRender(OvrAvatarComponent component, OvrAvatar avatar, IntPtr renderPart)
{
ovrAvatarVisibilityFlags visibilityMask = CAPI.ovrAvatarSkinnedMeshRender_GetVisibilityMask(renderPart);
ovrAvatarTransform localTransform = CAPI.ovrAvatarSkinnedMeshRender_GetTransform(renderPart);
UpdateSkinnedMesh(avatar, bones, localTransform, visibilityMask, renderPart);
UpdateMeshMaterial(visibilityMask, mesh);
bool isActive = this.gameObject.activeSelf;
if( mesh != null )
{
bool changedMaterial = CAPI.ovrAvatarSkinnedMeshRender_MaterialStateChanged(renderPart);
if (changedMaterial || (!previouslyActive && isActive))
{
ovrAvatarMaterialState materialState = CAPI.ovrAvatarSkinnedMeshRender_GetMaterialState(renderPart);
component.UpdateAvatarMaterial(mesh.sharedMaterial, materialState);
}
}
previouslyActive = isActive;
}
private void UpdateMeshMaterial(ovrAvatarVisibilityFlags visibilityMask, SkinnedMeshRenderer rootMesh)
{
Shader shader = (visibilityMask & ovrAvatarVisibilityFlags.SelfOccluding) != 0 ? surfaceSelfOccluding : surface;
if (rootMesh.sharedMaterial == null || rootMesh.sharedMaterial.shader != shader)
{
rootMesh.sharedMaterial = CreateAvatarMaterial(gameObject.name + "_material", shader);
}
}
}