OvrAvatarSkinnedMeshRenderPBSV2Component.cs
15.7 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using UnityEngine;
using System.Collections.Generic;
using System;
using Oculus.Avatar;
public class OvrAvatarSkinnedMeshPBSV2RenderComponent : OvrAvatarRenderComponent
{
private OvrAvatarMaterialManager avatarMaterialManager;
private bool previouslyActive = false;
private bool isCombinedMaterial = false;
private ovrAvatarExpressiveParameters ExpressiveParameters;
private bool EnableExpressive = false;
private int blendShapeCount = 0;
private ovrAvatarBlendShapeParams blendShapeParams;
private const string MAIN_MATERIAL_NAME = "main_material";
private const string EYE_MATERIAL_NAME = "eye_material";
private const string DEFAULT_MATERIAL_NAME = "_material";
internal void Initialize(
IntPtr renderPart,
ovrAvatarRenderPart_SkinnedMeshRenderPBS_V2 skinnedMeshRender,
OvrAvatarMaterialManager materialManager,
int thirdPersonLayer,
int firstPersonLayer,
bool combinedMesh,
ovrAvatarAssetLevelOfDetail lod,
bool assignExpressiveParams,
OvrAvatar avatar,
bool isControllerModel)
{
avatarMaterialManager = materialManager;
isCombinedMaterial = combinedMesh;
mesh = CreateSkinnedMesh(
skinnedMeshRender.meshAssetID,
skinnedMeshRender.visibilityMask,
thirdPersonLayer,
firstPersonLayer);
EnableExpressive = assignExpressiveParams;
#if UNITY_ANDROID
var singleComponentShader = EnableExpressive
? avatar.Skinshaded_Expressive_VertFrag_SingleComponent
: avatar.Skinshaded_VertFrag_SingleComponent;
#else
var singleComponentShader = EnableExpressive
? avatar.Skinshaded_Expressive_SurfaceShader_SingleComponent
: avatar.Skinshaded_SurfaceShader_SingleComponent;
#endif
var combinedComponentShader = EnableExpressive
? avatar.Skinshaded_Expressive_VertFrag_CombinedMesh
: avatar.Skinshaded_VertFrag_CombinedMesh;
var mainShader = isCombinedMaterial ? combinedComponentShader : singleComponentShader;
if (isControllerModel)
{
mainShader = avatar.ControllerShader;
}
AvatarLogger.Log("OvrAvatarSkinnedMeshPBSV2RenderComponent Shader is: " + mainShader != null
? mainShader.name : "null");
if (EnableExpressive)
{
ExpressiveParameters = CAPI.ovrAvatar_GetExpressiveParameters(avatar.sdkAvatar);
var eyeShader = avatar.EyeLens;
Material[] matArray = new Material[2];
matArray[0] = CreateAvatarMaterial(gameObject.name + MAIN_MATERIAL_NAME, mainShader);
matArray[1] = CreateAvatarMaterial(gameObject.name + EYE_MATERIAL_NAME, eyeShader);
if (avatar.UseTransparentRenderQueue)
{
SetMaterialTransparent(matArray[0]);
}
else
{
SetMaterialOpaque(matArray[0]);
}
// Eye lens shader queue is transparent and set from shader
matArray[1].renderQueue = -1;
mesh.materials = matArray;
}
else
{
mesh.sharedMaterial = CreateAvatarMaterial(gameObject.name + DEFAULT_MATERIAL_NAME, mainShader);
if (avatar.UseTransparentRenderQueue && !isControllerModel)
{
SetMaterialTransparent(mesh.sharedMaterial);
}
else
{
SetMaterialOpaque(mesh.sharedMaterial);
}
}
bones = mesh.bones;
if (isCombinedMaterial)
{
avatarMaterialManager.SetRenderer(mesh);
InitializeCombinedMaterial(renderPart, (int)lod);
avatarMaterialManager.OnCombinedMeshReady();
}
blendShapeParams = new ovrAvatarBlendShapeParams();
blendShapeParams.blendShapeParamCount = 0;
blendShapeParams.blendShapeParams = new float[64];
blendShapeCount = mesh.sharedMesh.blendShapeCount;
}
public void UpdateSkinnedMeshRender(
OvrAvatarComponent component,
OvrAvatar avatar,
IntPtr renderPart)
{
ovrAvatarVisibilityFlags visibilityMask
= CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetVisibilityMask(renderPart);
ovrAvatarTransform localTransform
= CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetTransform(renderPart);
UpdateSkinnedMesh(avatar, bones, localTransform, visibilityMask, renderPart);
bool isActive = gameObject.activeSelf;
if (mesh != null && !previouslyActive && isActive)
{
if (!isCombinedMaterial)
{
InitializeSingleComponentMaterial(renderPart, (int)avatar.LevelOfDetail - 1);
}
}
if (blendShapeCount > 0)
{
const float BLEND_MULTIPLIER = 100.0f;
CAPI.ovrAvatarSkinnedMeshRender_GetBlendShapeParams(renderPart, ref blendShapeParams);
for (uint i = 0; i < blendShapeParams.blendShapeParamCount && i < blendShapeCount; i++)
{
float value = blendShapeParams.blendShapeParams[i];
mesh.SetBlendShapeWeight((int)i, value * BLEND_MULTIPLIER);
}
}
previouslyActive = isActive;
}
private void InitializeSingleComponentMaterial(IntPtr renderPart, int lodIndex)
{
ovrAvatarPBSMaterialState materialState =
CAPI.ovrAvatarSkinnedMeshRenderPBSV2_GetPBSMaterialState(renderPart);
int componentType = (int)OvrAvatarMaterialManager.GetComponentType(gameObject.name);
Texture2D diffuseTexture = OvrAvatarComponent.GetLoadedTexture(materialState.albedoTextureID);
Texture2D normalTexture = OvrAvatarComponent.GetLoadedTexture(materialState.normalTextureID);
Texture2D metallicTexture = OvrAvatarComponent.GetLoadedTexture(materialState.metallicnessTextureID);
if (diffuseTexture != null)
{
avatarMaterialManager.AddTextureIDToTextureManager(materialState.albedoTextureID, true);
}
else
{
diffuseTexture = OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
}
diffuseTexture.anisoLevel = 4;
if (normalTexture != null)
{
avatarMaterialManager.AddTextureIDToTextureManager(materialState.normalTextureID, true);
}
else
{
normalTexture = OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].Normal;
}
normalTexture.anisoLevel = 4;
if (metallicTexture != null)
{
avatarMaterialManager.AddTextureIDToTextureManager(materialState.metallicnessTextureID, true);
}
else
{
metallicTexture = OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
}
metallicTexture.anisoLevel = 16;
mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_MAINTEX, diffuseTexture);
mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_NORMALMAP, normalTexture);
mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_ROUGHNESSMAP, metallicTexture);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_COLOR, materialState.albedoMultiplier);
mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_DIFFUSEINTENSITY,
OvrAvatarMaterialManager.DiffuseIntensities[componentType]);
mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_RIMINTENSITY,
OvrAvatarMaterialManager.RimIntensities[componentType]);
mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_REFLECTIONINTENSITY,
OvrAvatarMaterialManager.ReflectionIntensities[componentType]);
mesh.GetClosestReflectionProbes(avatarMaterialManager.ReflectionProbes);
if (avatarMaterialManager.ReflectionProbes != null &&
avatarMaterialManager.ReflectionProbes.Count > 0)
{
mesh.materials[0].SetTexture(OvrAvatarMaterialManager.AVATAR_SHADER_CUBEMAP,
avatarMaterialManager.ReflectionProbes[0].probe.texture);
}
if (EnableExpressive)
{
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_IRIS_COLOR,
ExpressiveParameters.irisColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_COLOR,
ExpressiveParameters.lipColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_BROW_COLOR,
ExpressiveParameters.browColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LASH_COLOR,
ExpressiveParameters.lashColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_SCLERA_COLOR,
ExpressiveParameters.scleraColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_GUM_COLOR,
ExpressiveParameters.gumColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_TEETH_COLOR,
ExpressiveParameters.teethColor);
mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_SMOOTHNESS,
ExpressiveParameters.lipSmoothness);
}
}
private void InitializeCombinedMaterial(IntPtr renderPart, int lodIndex)
{
ovrAvatarPBSMaterialState[] materialStates = CAPI.ovrAvatar_GetBodyPBSMaterialStates(renderPart);
if (materialStates.Length == (int)ovrAvatarBodyPartType.Count)
{
avatarMaterialManager.CreateTextureArrays();
var localProperties = avatarMaterialManager.LocalAvatarConfig.ComponentMaterialProperties;
AvatarLogger.Log("InitializeCombinedMaterial - Loading Material States");
for (int i = 0; i < materialStates.Length; i++)
{
localProperties[i].TypeIndex = (ovrAvatarBodyPartType)i;
localProperties[i].Color = materialStates[i].albedoMultiplier;
localProperties[i].DiffuseIntensity = OvrAvatarMaterialManager.DiffuseIntensities[i];
localProperties[i].RimIntensity = OvrAvatarMaterialManager.RimIntensities[i];
localProperties[i].ReflectionIntensity = OvrAvatarMaterialManager.ReflectionIntensities[i];
var diffuse = OvrAvatarComponent.GetLoadedTexture(materialStates[i].albedoTextureID);
var normal = OvrAvatarComponent.GetLoadedTexture(materialStates[i].normalTextureID);
var roughness = OvrAvatarComponent.GetLoadedTexture(materialStates[i].metallicnessTextureID);
if (diffuse != null)
{
localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.DiffuseTextures] = diffuse;
avatarMaterialManager.AddTextureIDToTextureManager(materialStates[i].albedoTextureID, false);
}
else
{
localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.DiffuseTextures] =
OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
}
localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.DiffuseTextures].anisoLevel = 4;
if (normal != null)
{
localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.NormalMaps] = normal;
avatarMaterialManager.AddTextureIDToTextureManager(materialStates[i].normalTextureID, false);
}
else
{
localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.NormalMaps] =
OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].Normal;
}
localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.NormalMaps].anisoLevel = 4;
if (roughness != null)
{
localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.RoughnessMaps] = roughness;
avatarMaterialManager.AddTextureIDToTextureManager(materialStates[i].metallicnessTextureID, false);
}
else
{
localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.RoughnessMaps] =
OvrAvatarSDKManager.Instance.GetTextureCopyManager().FallbackTextureSets[lodIndex].DiffuseRoughness;
}
localProperties[i].Textures[(int)OvrAvatarMaterialManager.TextureType.RoughnessMaps].anisoLevel = 16;
AvatarLogger.Log(localProperties[i].TypeIndex.ToString());
AvatarLogger.Log(AvatarLogger.Tab + "Diffuse: " + materialStates[i].albedoTextureID);
AvatarLogger.Log(AvatarLogger.Tab + "Normal: " + materialStates[i].normalTextureID);
AvatarLogger.Log(AvatarLogger.Tab + "Metallic: " + materialStates[i].metallicnessTextureID);
}
if (EnableExpressive)
{
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_IRIS_COLOR,
ExpressiveParameters.irisColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_COLOR,
ExpressiveParameters.lipColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_BROW_COLOR,
ExpressiveParameters.browColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_LASH_COLOR,
ExpressiveParameters.lashColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_SCLERA_COLOR,
ExpressiveParameters.scleraColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_GUM_COLOR,
ExpressiveParameters.gumColor);
mesh.materials[0].SetVector(OvrAvatarMaterialManager.AVATAR_SHADER_TEETH_COLOR,
ExpressiveParameters.teethColor);
mesh.materials[0].SetFloat(OvrAvatarMaterialManager.AVATAR_SHADER_LIP_SMOOTHNESS,
ExpressiveParameters.lipSmoothness);
}
avatarMaterialManager.ValidateTextures(materialStates);
}
}
private void SetMaterialTransparent(Material mat)
{
// Initialize shader to use transparent render queue with alpha blending
mat.SetOverrideTag("Queue", "Transparent");
mat.SetOverrideTag("RenderType", "Transparent");
mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
mat.EnableKeyword("_ALPHATEST_ON");
mat.EnableKeyword("_ALPHABLEND_ON");
mat.EnableKeyword("_ALPHAPREMULTIPLY_ON");
mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
}
private void SetMaterialOpaque(Material mat)
{
// Initialize shader to use geometry render queue with no blending
mat.SetOverrideTag("Queue", "Geometry");
mat.SetOverrideTag("RenderType", "Opaque");
mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
mat.DisableKeyword("_ALPHATEST_ON");
mat.DisableKeyword("_ALPHABLEND_ON");
mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;
}
}