BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Avatar / Scripts / OvrAvatarMaterialManager.cs

OvrAvatarMaterialManager.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OvrAvatarMaterialManager.cs 16.6 KiB Download Raw
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine;
5using UnityEngine.Rendering;
6
7public class OvrAvatarMaterialManager : MonoBehaviour
8{
9 private Renderer TargetRenderer;
10 private AvatarTextureArrayProperties[] TextureArrays;
11
12 public enum TextureType
13 {
14 DiffuseTextures = 0,
15 NormalMaps,
16 RoughnessMaps,
17
18 Count
19 }
20
21 // Material properties required to render a single component
22 public struct AvatarComponentMaterialProperties
23 {
24 public ovrAvatarBodyPartType TypeIndex;
25 public Color Color;
26 public Texture2D[] Textures;
27 public float DiffuseIntensity;
28 public float RimIntensity;
29 public float ReflectionIntensity;
30 }
31
32 // Texture arrays
33 public struct AvatarTextureArrayProperties
34 {
35 public Texture2D[] Textures;
36 public Texture2DArray TextureArray;
37 }
38
39 // Material property arrays that are pushed to the shader
40 public struct AvatarMaterialPropertyBlock
41 {
42 public Vector4[] Colors;
43 public float[] DiffuseIntensities;
44 public float[] RimIntensities;
45 public float[] ReflectionIntensities;
46 }
47
48 private readonly string[] TextureTypeToShaderProperties =
49 {
50 "_MainTex", // TextureType.DiffuseTextures = 0
51 "_NormalMap", // TextureType.NormalMaps
52 "_RoughnessMap" // TextureType.RoughnessMaps
53 };
54
55 // Container class for all the data relating to an avatar material description
56 [System.Serializable]
57 public class AvatarMaterialConfig
58 {
59 public AvatarComponentMaterialProperties[] ComponentMaterialProperties;
60 public AvatarMaterialPropertyBlock MaterialPropertyBlock;
61 }
62
63 // Local config that this manager instance will render
64 public AvatarMaterialConfig LocalAvatarConfig = new AvatarMaterialConfig();
65
66 public List<ReflectionProbeBlendInfo> ReflectionProbes = new List<ReflectionProbeBlendInfo>();
67
68 // Cache the previous shader when swapping in the loading shader.
69 private Shader CombinedShader;
70 // Shader properties
71 public static string AVATAR_SHADER_LOADER = "OvrAvatar/Avatar_Mobile_Loader";
72 public static string AVATAR_SHADER_MAINTEX = "_MainTex";
73 public static string AVATAR_SHADER_NORMALMAP = "_NormalMap";
74 public static string AVATAR_SHADER_ROUGHNESSMAP = "_RoughnessMap";
75 public static string AVATAR_SHADER_COLOR = "_BaseColor";
76 public static string AVATAR_SHADER_DIFFUSEINTENSITY = "_DiffuseIntensity";
77 public static string AVATAR_SHADER_RIMINTENSITY = "_RimIntensity";
78 public static string AVATAR_SHADER_REFLECTIONINTENSITY = "_ReflectionIntensity";
79 public static string AVATAR_SHADER_CUBEMAP = "_Cubemap";
80 public static string AVATAR_SHADER_ALPHA = "_Alpha";
81 public static string AVATAR_SHADER_LOADING_DIMMER = "_LoadingDimmer";
82
83 public static string AVATAR_SHADER_IRIS_COLOR = "_MaskColorIris";
84 public static string AVATAR_SHADER_LIP_COLOR = "_MaskColorLips";
85 public static string AVATAR_SHADER_BROW_COLOR = "_MaskColorBrows";
86 public static string AVATAR_SHADER_LASH_COLOR = "_MaskColorLashes";
87 public static string AVATAR_SHADER_SCLERA_COLOR = "_MaskColorSclera";
88 public static string AVATAR_SHADER_GUM_COLOR = "_MaskColorGums";
89 public static string AVATAR_SHADER_TEETH_COLOR = "_MaskColorTeeth";
90 public static string AVATAR_SHADER_LIP_SMOOTHNESS = "_LipSmoothness";
91
92 // Diffuse Intensity constants: body, clothes, eyewear, hair, beard
93 public static float[] DiffuseIntensities = new[] {0.3f, 0.1f, 0f, 0.15f, 0.15f};
94 // Rim Intensity constants: body, clothes, eyewear, hair, beard
95 public static float[] RimIntensities = new[] {5f, 2f, 2.84f, 4f, 4f};
96 // Reflection Intensity constants: body, clothes, eyewear, hair, beard
97 public static float[] ReflectionIntensities = new[] {0f, 0.3f, 0.4f, 0f, 0f};
98
99 // Loading animation
100 private const float LOADING_ANIMATION_AMPLITUDE = 0.5f;
101 private const float LOADING_ANIMATION_PERIOD = 0.35f;
102 private const float LOADING_ANIMATION_CURVE_SCALE = 0.25f;
103 private const float LOADING_ANIMATION_DIMMER_MIN = 0.3f;
104
105 public void CreateTextureArrays()
106 {
107 const int componentCount = (int)ovrAvatarBodyPartType.Count;
108 const int textureTypeCount = (int)TextureType.Count;
109
110 LocalAvatarConfig.ComponentMaterialProperties = new AvatarComponentMaterialProperties[componentCount];
111 LocalAvatarConfig.MaterialPropertyBlock.Colors = new Vector4[componentCount];
112 LocalAvatarConfig.MaterialPropertyBlock.DiffuseIntensities = new float[componentCount];
113 LocalAvatarConfig.MaterialPropertyBlock.RimIntensities = new float[componentCount];
114 LocalAvatarConfig.MaterialPropertyBlock.ReflectionIntensities = new float[componentCount];
115
116 for (int i = 0; i < LocalAvatarConfig.ComponentMaterialProperties.Length; ++i)
117 {
118 LocalAvatarConfig.ComponentMaterialProperties[i].Textures = new Texture2D[textureTypeCount];
119 }
120
121 TextureArrays = new AvatarTextureArrayProperties[textureTypeCount];
122 }
123
124 public void SetRenderer(Renderer renderer)
125 {
126 TargetRenderer = renderer;
127 TargetRenderer.GetClosestReflectionProbes(ReflectionProbes);
128 }
129
130 public void OnCombinedMeshReady()
131 {
132 InitTextureArrays();
133 SetMaterialPropertyBlock();
134 // Callback to delete texture set once the avatar is fully loaded
135 StartCoroutine(RunLoadingAnimation(DeleteTextureSet));
136 }
137
138 // Add a texture ID so that it's managed for deletion
139 public void AddTextureIDToTextureManager(ulong assetID, bool isSingleComponent)
140 {
141 OvrAvatarSDKManager.Instance.GetTextureCopyManager().AddTextureIDToTextureSet(
142 GetInstanceID(), assetID, isSingleComponent);
143 }
144
145 // Once avatar loading is completed trigger the texture set for deletion
146 private void DeleteTextureSet()
147 {
148 OvrAvatarSDKManager.Instance.GetTextureCopyManager().DeleteTextureSet(GetInstanceID());
149 }
150
151 // Prepare texture arrays and copy to GPU
152 public void InitTextureArrays()
153 {
154 var localProps = LocalAvatarConfig.ComponentMaterialProperties[0];
155
156 for (int i = 0; i < TextureArrays.Length && i < localProps.Textures.Length; i++)
157 {
158 TextureArrays[i].TextureArray = new Texture2DArray(
159 localProps.Textures[0].height, localProps.Textures[0].width,
160 LocalAvatarConfig.ComponentMaterialProperties.Length,
161 localProps.Textures[0].format,
162 true,
163 QualitySettings.activeColorSpace == ColorSpace.Gamma ? false : true
164 ) { filterMode = FilterMode.Trilinear,
165 //Can probably get away with 4 for roughness maps as well, once we switch
166 //to BC7/ASTC4x4 texture compression.
167 anisoLevel = (TextureType)i == TextureType.RoughnessMaps ? 16 : 4 };
168 //So a name shows up in Renderdoc
169 TextureArrays[i].TextureArray.name = string.Format("Texture Array Type: {0}", (TextureType)i);
170
171 TextureArrays[i].Textures
172 = new Texture2D[LocalAvatarConfig.ComponentMaterialProperties.Length];
173
174 for (int j = 0; j < LocalAvatarConfig.ComponentMaterialProperties.Length; j++)
175 {
176 TextureArrays[i].Textures[j]
177 = LocalAvatarConfig.ComponentMaterialProperties[j].Textures[i];
178 //So a name shows up in Renderdoc
179 TextureArrays[i].Textures[j].name = string.Format("Texture Type: {0} Component: {1}", (TextureType)i, j);
180 }
181
182 ProcessTexturesWithMips(
183 TextureArrays[i].Textures,
184 localProps.Textures[i].height,
185 TextureArrays[i].TextureArray);
186 }
187 }
188
189 private void ProcessTexturesWithMips(
190 Texture2D[] textures,
191 int texArrayResolution,
192 Texture2DArray texArray)
193 {
194 for (int i = 0; i < textures.Length; i++)
195 {
196 int currentMipSize = texArrayResolution;
197 int correctNumberOfMips = textures[i].mipmapCount - 1;
198
199 // Add mips to copyTexture queue in low-high order from correctNumberOfMips..0
200 for (int mipLevel = correctNumberOfMips; mipLevel >= 0; mipLevel--)
201 {
202 int mipSize = texArrayResolution / currentMipSize;
203 OvrAvatarSDKManager.Instance.GetTextureCopyManager().CopyTexture(
204 textures[i],
205 texArray,
206 mipLevel,
207 mipSize,
208 i,
209 false);
210
211 currentMipSize /= 2;
212 }
213 }
214 }
215
216 private void SetMaterialPropertyBlock()
217 {
218 if (TargetRenderer != null)
219 {
220 for (int i = 0; i < LocalAvatarConfig.ComponentMaterialProperties.Length; i++)
221 {
222 LocalAvatarConfig.MaterialPropertyBlock.Colors[i]
223 = LocalAvatarConfig.ComponentMaterialProperties[i].Color;
224 LocalAvatarConfig.MaterialPropertyBlock.DiffuseIntensities[i] = DiffuseIntensities[i];
225 LocalAvatarConfig.MaterialPropertyBlock.RimIntensities[i] = RimIntensities[i];
226 LocalAvatarConfig.MaterialPropertyBlock.ReflectionIntensities[i] = ReflectionIntensities[i];
227 }
228 }
229 }
230
231 private void ApplyMaterialPropertyBlock()
232 {
233 MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock();
234 materialPropertyBlock.SetVectorArray(AVATAR_SHADER_COLOR,
235 LocalAvatarConfig.MaterialPropertyBlock.Colors);
236 materialPropertyBlock.SetFloatArray(AVATAR_SHADER_DIFFUSEINTENSITY,
237 LocalAvatarConfig.MaterialPropertyBlock.DiffuseIntensities);
238 materialPropertyBlock.SetFloatArray(AVATAR_SHADER_RIMINTENSITY,
239 LocalAvatarConfig.MaterialPropertyBlock.RimIntensities);
240 materialPropertyBlock.SetFloatArray(AVATAR_SHADER_REFLECTIONINTENSITY,
241 LocalAvatarConfig.MaterialPropertyBlock.ReflectionIntensities);
242 TargetRenderer.GetClosestReflectionProbes(ReflectionProbes);
243
244 if (ReflectionProbes != null && ReflectionProbes.Count > 0 && ReflectionProbes[0].probe.texture != null)
245 {
246 materialPropertyBlock.SetTexture(AVATAR_SHADER_CUBEMAP, ReflectionProbes[0].probe.texture);
247 }
248
249 for (int i = 0; i < TextureArrays.Length; i++)
250 {
251 materialPropertyBlock.SetTexture(TextureTypeToShaderProperties[i],
252 TextureArrays[(int)(TextureType)i].TextureArray);
253 }
254
255 TargetRenderer.SetPropertyBlock(materialPropertyBlock);
256 }
257
258 // Return a component type based on name
259 public static ovrAvatarBodyPartType GetComponentType(string objectName)
260 {
261 if (objectName.Contains("0"))
262 {
263 return ovrAvatarBodyPartType.Body;
264 }
265 else if (objectName.Contains("1"))
266 {
267 return ovrAvatarBodyPartType.Clothing;
268 }
269 else if (objectName.Contains("2"))
270 {
271 return ovrAvatarBodyPartType.Eyewear;
272 }
273 else if (objectName.Contains("3"))
274 {
275 return ovrAvatarBodyPartType.Hair;
276 }
277 else if (objectName.Contains("4"))
278 {
279 return ovrAvatarBodyPartType.Beard;
280 }
281
282 return ovrAvatarBodyPartType.Count;
283 }
284
285 public void ValidateTextures()
286 {
287 var props = LocalAvatarConfig.ComponentMaterialProperties;
288
289 int[] heights = new int[(int)TextureType.Count];
290 TextureFormat[] formats = new TextureFormat[(int)TextureType.Count];
291
292 for (var propIndex = 0; propIndex < props.Length; propIndex++)
293 {
294 for (var index = 0; index < props[propIndex].Textures.Length; index++)
295 {
296 if (props[propIndex].Textures[index] == null)
297 {
298 throw new System.Exception(
299 props[propIndex].TypeIndex.ToString()
300 + "Invalid: "
301 + ((TextureType)index).ToString());
302 }
303
304 heights[index] = props[propIndex].Textures[index].height;
305 formats[index] = props[propIndex].Textures[index].format;
306 }
307 }
308
309 for (int textureIndex = 0; textureIndex < (int)TextureType.Count; textureIndex++)
310 {
311 for (var propIndex = 1; propIndex < props.Length; propIndex++)
312 {
313 if (props[propIndex - 1].Textures[textureIndex].height
314 != props[propIndex].Textures[textureIndex].height)
315 {
316 throw new System.Exception(
317 props[propIndex].TypeIndex.ToString()
318 + " Mismatching Resolutions: "
319 + ((TextureType)textureIndex).ToString()
320 + " "
321 + props[propIndex - 1].Textures[textureIndex].height
322 + " vs "
323 + props[propIndex].Textures[textureIndex].height
324 + " Ensure you are using ASTC texture compression on Android or turn off CombineMeshes");
325 }
326
327 if (props[propIndex - 1].Textures[textureIndex].format
328 != props[propIndex].Textures[textureIndex].format)
329 {
330 throw new System.Exception(
331 props[propIndex].TypeIndex.ToString()
332 + " Mismatching Formats: "
333 + ((TextureType)textureIndex).ToString()
334 + " Ensure you are using ASTC texture compression on Android or turn off CombineMeshes");
335 }
336 }
337 }
338 }
339
340 // Loading animation on the Dimmer properyt
341 // Smooth sine lerp every 0.3 seconds between 0.25 and 0.5
342 private IEnumerator RunLoadingAnimation(Action callBack)
343 {
344 // Set the material to single component while the avatar loads
345 CombinedShader = TargetRenderer.sharedMaterial.shader;
346
347 // Save shader properties
348 int srcBlend = TargetRenderer.sharedMaterial.GetInt("_SrcBlend");
349 int dstBlend = TargetRenderer.sharedMaterial.GetInt("_DstBlend");
350 string lightModeTag = TargetRenderer.sharedMaterial.GetTag("LightMode", false);
351 string renderTypeTag = TargetRenderer.sharedMaterial.GetTag("RenderType", false);
352 string renderQueueTag = TargetRenderer.sharedMaterial.GetTag("Queue", false);
353 string ignoreProjectorTag = TargetRenderer.sharedMaterial.GetTag("IgnoreProjector", false);
354 int renderQueue = TargetRenderer.sharedMaterial.renderQueue;
355 bool transparentQueue = TargetRenderer.sharedMaterial.IsKeywordEnabled("_ALPHATEST_ON");
356
357 // Swap in loading shader
358 TargetRenderer.sharedMaterial.shader = Shader.Find(AVATAR_SHADER_LOADER);
359 TargetRenderer.sharedMaterial.SetColor(AVATAR_SHADER_COLOR, Color.white);
360
361 while (OvrAvatarSDKManager.Instance.GetTextureCopyManager().GetTextureCount() > 0)
362 {
363 float distance = (LOADING_ANIMATION_AMPLITUDE * Mathf.Sin(Time.timeSinceLevelLoad / LOADING_ANIMATION_PERIOD) +
364 LOADING_ANIMATION_AMPLITUDE) * (LOADING_ANIMATION_CURVE_SCALE) + LOADING_ANIMATION_DIMMER_MIN;
365 TargetRenderer.sharedMaterial.SetFloat(AVATAR_SHADER_LOADING_DIMMER, distance);
366 yield return null;
367 }
368 // Swap back main shader
369 TargetRenderer.sharedMaterial.SetFloat(AVATAR_SHADER_LOADING_DIMMER, 1f);
370 TargetRenderer.sharedMaterial.shader = CombinedShader;
371
372 // Restore shader properties
373 TargetRenderer.sharedMaterial.SetInt("_SrcBlend", srcBlend);
374 TargetRenderer.sharedMaterial.SetInt("_DstBlend", dstBlend);
375 TargetRenderer.sharedMaterial.SetOverrideTag("LightMode", lightModeTag);
376 TargetRenderer.sharedMaterial.SetOverrideTag("RenderType", renderTypeTag);
377 TargetRenderer.sharedMaterial.SetOverrideTag("Queue", renderQueueTag);
378 TargetRenderer.sharedMaterial.SetOverrideTag("IgnoreProjector", ignoreProjectorTag);
379 if (transparentQueue)
380 {
381 TargetRenderer.sharedMaterial.EnableKeyword("_ALPHATEST_ON");
382 TargetRenderer.sharedMaterial.EnableKeyword("_ALPHABLEND_ON");
383 TargetRenderer.sharedMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
384 }
385 else
386 {
387 TargetRenderer.sharedMaterial.DisableKeyword("_ALPHATEST_ON");
388 TargetRenderer.sharedMaterial.DisableKeyword("_ALPHABLEND_ON");
389 TargetRenderer.sharedMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
390 }
391 TargetRenderer.sharedMaterial.renderQueue = renderQueue;
392
393 ApplyMaterialPropertyBlock();
394
395 if (callBack != null)
396 {
397 callBack();
398 }
399 }
400}