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