| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Shaders; |
| | 5 | | using MainScripts.DCL.Helpers.Utils; |
| | 6 | | using Unity.Collections; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Pool; |
| | 9 | | using UnityEngine.Rendering; |
| | 10 | |
|
| | 11 | | namespace DCL |
| | 12 | | { |
| | 13 | | public static class AvatarMeshCombinerUtils |
| | 14 | | { |
| | 15 | | internal const string AVATAR_MAP_PROPERTY_NAME = "_AvatarMap"; |
| | 16 | |
|
| 1 | 17 | | internal static readonly int[] AVATAR_MAP_ID_PROPERTIES = |
| | 18 | | { |
| | 19 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "1"), |
| | 20 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "2"), |
| | 21 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "3"), |
| | 22 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "4"), |
| | 23 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "5"), |
| | 24 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "6"), |
| | 25 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "7"), |
| | 26 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "8"), |
| | 27 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "9"), |
| | 28 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "10"), |
| | 29 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "11"), |
| | 30 | | Shader.PropertyToID(AVATAR_MAP_PROPERTY_NAME + "12") |
| | 31 | | }; |
| | 32 | |
|
| 1 | 33 | | private static bool VERBOSE = false; |
| 1 | 34 | | private static ILogger logger = new Logger(Debug.unityLogger.logHandler) { filterLogType = VERBOSE ? LogType.Log |
| | 35 | |
|
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// This method iterates over all the renderers contained in the given CombineLayer list, and |
| | 39 | | /// outputs an array of all the bones per vertexes and an array of all the bone weights |
| | 40 | | /// |
| | 41 | | /// This is needed because Mesh.CombineMeshes don't calculate boneWeights correctly. |
| | 42 | | /// When using Mesh.CombineMeshes, the boneWeights returned correspond to indexes of skeleton copies, |
| | 43 | | /// not the same skeleton. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="layers">A CombineLayer list. You can generate this array using CombineLayerUtils.Slice().</para |
| | 46 | | /// <returns> |
| | 47 | | /// A list of Bones per vertex that share the same skeleton. |
| | 48 | | /// A list of bone weights that share the same skeleton. |
| | 49 | | /// </returns> |
| | 50 | | public static (NativeArray<byte> bonesPerVertex, NativeArray<BoneWeight1> boneWeights) CombineBones(CombineLayer |
| | 51 | | { |
| 14 | 52 | | int layersCount = layers.Count; |
| 14 | 53 | | int totalVertexes = 0; |
| 14 | 54 | | int totalBones = 0; |
| | 55 | |
|
| 14 | 56 | | List<NativeArray<byte>> bonesPerVertexList = ListPool<NativeArray<byte>>.Get(); |
| 14 | 57 | | List<NativeArray<BoneWeight1>> boneWeightArrays = ListPool<NativeArray<BoneWeight1>>.Get(); |
| | 58 | |
|
| 80 | 59 | | for (int layerIndex = 0; layerIndex < layersCount; layerIndex++) |
| | 60 | | { |
| 26 | 61 | | CombineLayer layer = layers[layerIndex]; |
| 26 | 62 | | var layerRenderers = layer.Renderers; |
| 26 | 63 | | int layerRenderersCount = layerRenderers.Count; |
| | 64 | |
|
| 216 | 65 | | for (int i = 0; i < layerRenderersCount; i++) |
| | 66 | | { |
| 82 | 67 | | var bonesPerVertex = layerRenderers[i].sharedMesh.GetBonesPerVertex(); |
| 82 | 68 | | bonesPerVertexList.Add(bonesPerVertex); |
| | 69 | |
|
| 82 | 70 | | var boneWeights = layerRenderers[i].sharedMesh.GetAllBoneWeights(); |
| 82 | 71 | | boneWeightArrays.Add(boneWeights); |
| | 72 | |
|
| 82 | 73 | | totalVertexes += bonesPerVertex.Length; |
| 82 | 74 | | totalBones += boneWeights.Length; |
| | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| 14 | 78 | | NativeArray<byte> finalBpV = new NativeArray<byte>(totalVertexes, Allocator.Temp, NativeArrayOptions.Uniniti |
| 14 | 79 | | NativeArray<BoneWeight1> finalBones = new NativeArray<BoneWeight1>(totalBones, Allocator.Temp, NativeArrayOp |
| | 80 | |
|
| 14 | 81 | | int indexOffset = 0; |
| 14 | 82 | | int bonesPerVertexListCount = bonesPerVertexList.Count; |
| | 83 | |
|
| 192 | 84 | | for (int i = 0; i < bonesPerVertexListCount; i++) |
| | 85 | | { |
| 82 | 86 | | var narray = bonesPerVertexList[i]; |
| 82 | 87 | | int narrayLength = narray.Length; |
| 82 | 88 | | NativeArray<byte>.Copy(narray, 0, finalBpV, indexOffset, narrayLength); |
| 82 | 89 | | indexOffset += narrayLength; |
| 82 | 90 | | narray.Dispose(); |
| | 91 | | } |
| | 92 | |
|
| 14 | 93 | | indexOffset = 0; |
| 14 | 94 | | var finalBonesCount = boneWeightArrays.Count; |
| | 95 | |
|
| 192 | 96 | | for (int i = 0; i < finalBonesCount; i++) |
| | 97 | | { |
| 82 | 98 | | var narray = boneWeightArrays[i]; |
| 82 | 99 | | int narrayLength = narray.Length; |
| 82 | 100 | | NativeArray<BoneWeight1>.Copy(narray, 0, finalBones, indexOffset, narrayLength); |
| 82 | 101 | | indexOffset += narrayLength; |
| 82 | 102 | | narray.Dispose(); |
| | 103 | | } |
| | 104 | |
|
| 14 | 105 | | ListPool<NativeArray<byte>>.Release(bonesPerVertexList); |
| 14 | 106 | | ListPool<NativeArray<BoneWeight1>>.Release(boneWeightArrays); |
| | 107 | |
|
| 14 | 108 | | return (finalBpV, finalBones); |
| | 109 | | } |
| | 110 | |
|
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// FlattenMaterials take a CombineLayer list and returns a FlattenedMaterialsData object. |
| | 114 | | /// |
| | 115 | | /// This object can be used to construct a combined mesh that has uniform data encoded in uv attributes. |
| | 116 | | /// This type of encoding can be used to greatly reduce draw calls for seemingly unrelated objects. |
| | 117 | | /// |
| | 118 | | /// The returned object also contains a single material per CombineLayer. |
| | 119 | | /// </summary> |
| | 120 | | /// <param name="layers">A CombineLayer list. You can generate this array using CombineLayerUtils.Slice().</para |
| | 121 | | /// <param name="materialAsset">Material asset that will be cloned to generate the returned materials.</param> |
| | 122 | | /// <returns>A FlattenedMaterialsData object. This object can be used to construct a combined mesh that has unif |
| | 123 | | public static FlattenedMaterialsData FlattenMaterials(CombineLayersList layers, Material materialAsset) |
| | 124 | | { |
| 15 | 125 | | int layersCount = layers.Count; |
| | 126 | |
|
| 15 | 127 | | int finalVertexCount = layers.TotalVerticesCount; |
| 15 | 128 | | var result = new FlattenedMaterialsData(finalVertexCount, layersCount); |
| | 129 | |
|
| 15 | 130 | | int currentVertexCount = 0; |
| | 131 | |
|
| 86 | 132 | | for (int layerIndex = 0; layerIndex < layersCount; layerIndex++) |
| | 133 | | { |
| 28 | 134 | | CombineLayer layer = layers[layerIndex]; |
| 28 | 135 | | var layerRenderers = layer.Renderers; |
| | 136 | |
|
| 28 | 137 | | Material newMaterial = new Material(materialAsset); |
| | 138 | |
|
| 28 | 139 | | CullMode cullMode = layer.cullMode; |
| 28 | 140 | | bool isOpaque = layer.isOpaque; |
| | 141 | |
|
| 28 | 142 | | if (isOpaque) |
| 15 | 143 | | MaterialUtils.SetOpaque(newMaterial); |
| | 144 | | else |
| 13 | 145 | | MaterialUtils.SetTransparent(newMaterial); |
| | 146 | |
|
| 28 | 147 | | newMaterial.SetInt(ShaderUtils.Cull, (int)cullMode); |
| | 148 | |
|
| 28 | 149 | | result.materials[layerIndex] = newMaterial; |
| | 150 | |
|
| 28 | 151 | | int layerRenderersCount = layerRenderers.Count; |
| | 152 | |
|
| 220 | 153 | | for (int i = 0; i < layerRenderersCount; i++) |
| | 154 | | { |
| 82 | 155 | | var renderer = layerRenderers[i]; |
| | 156 | |
|
| | 157 | | // Bone Weights |
| 82 | 158 | | var sharedMesh = renderer.sharedMesh; |
| 82 | 159 | | int vertexCount = sharedMesh.vertexCount; |
| | 160 | |
|
| | 161 | | // Texture IDs |
| 82 | 162 | | Material mat = renderer.sharedMaterial; |
| | 163 | |
|
| 82 | 164 | | Texture2D baseMap = (Texture2D)mat.GetTexture(ShaderUtils.BaseMap); |
| 82 | 165 | | Texture2D emissionMap = (Texture2D)mat.GetTexture(ShaderUtils.EmissionMap); |
| 82 | 166 | | float cutoff = mat.GetFloat(ShaderUtils.Cutoff); |
| | 167 | |
|
| 82 | 168 | | int baseMapId = -1; |
| 82 | 169 | | if (baseMap != null && layer.textureToId.TryGetValue(baseMap, out baseMapId)) |
| | 170 | | { |
| 82 | 171 | | if (baseMapId < AVATAR_MAP_ID_PROPERTIES.Length) |
| | 172 | | { |
| 82 | 173 | | int targetMap = AVATAR_MAP_ID_PROPERTIES[baseMapId]; |
| 82 | 174 | | newMaterial.SetTexture(targetMap, baseMap); |
| | 175 | | } |
| | 176 | | else |
| | 177 | | { |
| 0 | 178 | | if (VERBOSE) |
| 0 | 179 | | logger.Log(LogType.Error, "FlattenMaterials", $"Base Map ID out of bounds! {baseMapId}") |
| | 180 | | } |
| | 181 | | } |
| | 182 | |
|
| 82 | 183 | | int emissionMapId = -1; |
| 82 | 184 | | if (emissionMap != null && layer.textureToId.TryGetValue(emissionMap, out emissionMapId)) |
| | 185 | | { |
| 4 | 186 | | if (emissionMapId < AVATAR_MAP_ID_PROPERTIES.Length) |
| | 187 | | { |
| 4 | 188 | | int targetMap = AVATAR_MAP_ID_PROPERTIES[emissionMapId]; |
| 4 | 189 | | newMaterial.SetTexture(targetMap, emissionMap); |
| | 190 | | } |
| | 191 | | else |
| | 192 | | { |
| 0 | 193 | | if (VERBOSE) |
| 0 | 194 | | logger.Log(LogType.Error, "FlattenMaterials", $"Emission Map ID out of bounds! {emission |
| | 195 | | } |
| | 196 | | } |
| | 197 | |
|
| 82 | 198 | | Vector4 baseColor = mat.GetVector(ShaderUtils.BaseColor); |
| 82 | 199 | | Vector4 emissionColor = mat.GetVector(ShaderUtils.EmissionColor); |
| 82 | 200 | | Vector3 texturePointerData = new Vector3(baseMapId, emissionMapId, cutoff); |
| | 201 | |
|
| 41764 | 202 | | for (int ai = 0; ai < vertexCount; ai++) |
| | 203 | | { |
| 20800 | 204 | | result.texturePointers[currentVertexCount] = texturePointerData; |
| 20800 | 205 | | result.colors[currentVertexCount] = baseColor; |
| 20800 | 206 | | result.emissionColors[currentVertexCount] = emissionColor; |
| 20800 | 207 | | currentVertexCount++; |
| | 208 | | } |
| | 209 | |
|
| 82 | 210 | | if (VERBOSE) |
| 0 | 211 | | logger.Log($"Layer {i} - vertexCount: {vertexCount} - texturePointers: ({baseMapId}, {emissionMa |
| | 212 | | } |
| | 213 | |
|
| 28 | 214 | | SRPBatchingHelper.OptimizeMaterial(newMaterial); |
| | 215 | | } |
| | 216 | |
|
| 15 | 217 | | return result; |
| | 218 | | } |
| | 219 | |
|
| | 220 | | /// <summary> |
| | 221 | | /// ComputeSubMeshes iterates over the given CombineLayer list, and returns a list that can be used to map a |
| | 222 | | /// sub-mesh for each CombineLayer object. A CombineLayer object can group more than a single mesh. |
| | 223 | | /// |
| | 224 | | /// Note that this had to be done because Mesh.CombineMeshes lack the option of controlling the sub-mesh |
| | 225 | | /// output. Currently the only options are to combine everything in a single sub-mesh, or generate a single sub- |
| | 226 | | /// per combined mesh. The CombineLayer approach may need to combine specific meshes to a single sub-mesh |
| | 227 | | /// (because they all can have the same material, if they share the same render state -- i.e. transparency or cu |
| | 228 | | /// </summary> |
| | 229 | | /// <param name="layers">A CombineLayer list. You can generate this array using CombineLayerUtils.Slice().</para |
| | 230 | | /// <returns>A SubMeshDescriptor list that can be used later to set the sub-meshes of the final combined mesh |
| | 231 | | /// in a way that each sub-mesh corresponds with its own layer.</returns> |
| | 232 | | public static NativeArray<SubMeshDescriptor> ComputeSubMeshes(CombineLayersList layers) |
| | 233 | | { |
| 14 | 234 | | int layersCount = layers.Count; |
| 14 | 235 | | int subMeshIndexOffset = 0; |
| | 236 | |
|
| 14 | 237 | | var result = new NativeArray<SubMeshDescriptor>(layersCount, Allocator.Temp, NativeArrayOptions.Uninitialize |
| | 238 | |
|
| 80 | 239 | | for (int layerIndex = 0; layerIndex < layersCount; layerIndex++) |
| | 240 | | { |
| 26 | 241 | | CombineLayer layer = layers[layerIndex]; |
| 26 | 242 | | var layerRenderers = layer.Renderers; |
| 26 | 243 | | int layerRenderersCount = layerRenderers.Count; |
| | 244 | |
|
| 26 | 245 | | int subMeshVertexCount = 0; |
| 26 | 246 | | int subMeshIndexCount = 0; |
| | 247 | |
|
| 216 | 248 | | for (int i = 0; i < layerRenderersCount; i++) |
| | 249 | | { |
| 82 | 250 | | var renderer = layerRenderers[i]; |
| | 251 | |
|
| 82 | 252 | | int vertexCount = renderer.sharedMesh.vertexCount; |
| 82 | 253 | | int indexCount = (int)renderer.sharedMesh.GetIndexCount(0); |
| | 254 | |
|
| 82 | 255 | | subMeshVertexCount += vertexCount; |
| 82 | 256 | | subMeshIndexCount += indexCount; |
| | 257 | | } |
| | 258 | |
|
| 26 | 259 | | var subMesh = new SubMeshDescriptor(subMeshIndexOffset, subMeshIndexCount) |
| | 260 | | { |
| | 261 | | vertexCount = subMeshVertexCount, |
| | 262 | | }; |
| | 263 | |
|
| 26 | 264 | | result[layerIndex] = subMesh; |
| | 265 | |
|
| 26 | 266 | | subMeshIndexOffset += subMeshIndexCount; |
| | 267 | | } |
| | 268 | |
|
| 14 | 269 | | return result; |
| | 270 | | } |
| | 271 | |
|
| | 272 | | /// <summary> |
| | 273 | | /// ComputeCombineInstancesData returns a CombineInstance list that can be used to combine all the meshes |
| | 274 | | /// specified by the given CombineLayer list. This is done via Mesh.CombineMeshes() Unity method. |
| | 275 | | /// </summary> |
| | 276 | | /// <param name="layers">A CombineLayer list. You can generate this array using CombineLayerUtils.Slice()</param |
| | 277 | | /// <returns>CombineInstance list usable by Mesh.CombineMeshes()</returns> |
| | 278 | | public static CombineInstance[] ComputeCombineInstancesData(CombineLayersList layers) |
| | 279 | | { |
| 13 | 280 | | int layersCount = layers.Count; |
| 13 | 281 | | var combineInstancesCount = layers.TotalRenderersCount; |
| | 282 | |
|
| 13 | 283 | | var result = new CombineInstance[combineInstancesCount]; |
| | 284 | |
|
| 13 | 285 | | var combineInstanceIndex = 0; |
| | 286 | |
|
| 74 | 287 | | for (int layerIndex = 0; layerIndex < layersCount; layerIndex++) |
| | 288 | | { |
| 24 | 289 | | CombineLayer layer = layers[layerIndex]; |
| 24 | 290 | | var layerRenderers = layer.Renderers; |
| 24 | 291 | | int layerRenderersCount = layerRenderers.Count; |
| | 292 | |
|
| 204 | 293 | | for (int i = 0; i < layerRenderersCount; i++) |
| | 294 | | { |
| 78 | 295 | | var renderer = layerRenderers[i]; |
| | 296 | |
|
| 78 | 297 | | Transform meshTransform = renderer.transform; |
| 78 | 298 | | Transform prevParent = meshTransform.parent; |
| 78 | 299 | | meshTransform.SetParent(null, true); |
| | 300 | |
|
| 78 | 301 | | result[combineInstanceIndex] = new CombineInstance |
| | 302 | | { |
| | 303 | | subMeshIndex = 0, // this means the source sub-mesh, not destination |
| | 304 | | mesh = renderer.sharedMesh, |
| | 305 | | transform = meshTransform.localToWorldMatrix |
| | 306 | | }; |
| | 307 | |
|
| 78 | 308 | | combineInstanceIndex++; |
| 78 | 309 | | meshTransform.SetParent(prevParent); |
| | 310 | | } |
| | 311 | | } |
| | 312 | |
|
| 13 | 313 | | return result; |
| | 314 | | } |
| | 315 | |
|
| | 316 | | public static Mesh CombineMeshesWithLayers(CombineInstance[] combineInstancesData, in CombineLayersList layers) |
| | 317 | | { |
| 13 | 318 | | Mesh result = new Mesh(); |
| | 319 | |
|
| | 320 | | // Is important to use the layerRenderers to combine (i.e. no the original renderers) |
| | 321 | | // Layer renderers are in a specific order that must be abided, or the combining will be broken. |
| 13 | 322 | | using var allRenderersRental = PoolUtils.RentList<SkinnedMeshRenderer>(); |
| 13 | 323 | | var layerRenderers = allRenderersRental.GetList(); |
| | 324 | |
|
| 74 | 325 | | for (var i = 0; i < layers.Layers.Count; i++) |
| | 326 | | { |
| 24 | 327 | | CombineLayer t = layers.Layers[i]; |
| 24 | 328 | | layerRenderers.AddRange(t.Renderers); |
| | 329 | | } |
| | 330 | |
|
| 13 | 331 | | using var bakedInstances = BakedCombineInstances.Bake(combineInstancesData, layerRenderers); |
| 13 | 332 | | result.CombineMeshes(combineInstancesData, true, true); |
| | 333 | |
|
| 13 | 334 | | return result; |
| 13 | 335 | | } |
| | 336 | |
|
| | 337 | | /// <summary> |
| | 338 | | /// ResetBones will reset the given SkinnedMeshRenderer bones to the original bindposes position. |
| | 339 | | /// |
| | 340 | | /// This is done without taking into account the rootBone. We need to do it this way because the meshes must be |
| | 341 | | /// combined posing to match the raw bindposes matrix. |
| | 342 | | /// |
| | 343 | | /// If the combined mesh don't match the bindposes matrices, the resulting skinning will not work. |
| | 344 | | /// |
| | 345 | | /// For this reason, this method doesn't resemble the original method that unity uses to reset the skeleton foun |
| | 346 | | /// https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Editor/ |
| | 347 | | /// </summary> |
| | 348 | | internal static void ResetBones(Matrix4x4[] bindPoses, IReadOnlyList<Transform> bones) |
| | 349 | | { |
| 1764 | 350 | | for (int i = 0; i < bones.Count; i++) |
| | 351 | | { |
| 868 | 352 | | Transform bone = bones[i]; |
| 868 | 353 | | Matrix4x4 bindPose = bindPoses[i].inverse; |
| 868 | 354 | | bone.position = bindPose.MultiplyPoint3x4(Vector3.zero); |
| 868 | 355 | | bone.rotation = bindPose.rotation; |
| | 356 | |
|
| 868 | 357 | | Vector3 bindPoseScale = bindPose.lossyScale; |
| 868 | 358 | | Vector3 boneScale = bone.lossyScale; |
| | 359 | |
|
| 868 | 360 | | bone.localScale = new Vector3(bindPoseScale.x / boneScale.x, |
| | 361 | | bindPoseScale.y / boneScale.y, |
| | 362 | | bindPoseScale.z / boneScale.z); |
| | 363 | | } |
| | 364 | |
|
| | 365 | | #if UNITY_EDITOR |
| 14 | 366 | | DrawDebugSkeleton(bones); |
| | 367 | | #endif |
| 14 | 368 | | } |
| | 369 | |
|
| | 370 | | internal static void DrawDebugSkeleton(IReadOnlyList<Transform> bones) |
| | 371 | | { |
| 1764 | 372 | | for (int i = 0; i < bones.Count; i++) |
| | 373 | | { |
| 868 | 374 | | Transform bone = bones[i]; |
| 868 | 375 | | Debug.DrawLine(bone.position, bone.position + bone.forward, Color.cyan, 60); |
| | 376 | |
|
| 4298 | 377 | | foreach (Transform child in bone) { Debug.DrawLine(bone.position, child.position, Color.green, 60); } |
| | 378 | | } |
| 14 | 379 | | } |
| | 380 | | } |
| | 381 | | } |