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