static void EncodeMesh(Mesh mesh, string directory) { Debug.Log($"Encode mesh {mesh.name} to {directory}"); if (!mesh.isReadable) { Debug.LogError($"Mesh {mesh.name} is not readable!"); return; } var dracoData = DracoEncoder.EncodeMesh(mesh); if (dracoData.Length > 1) { var filename = string.IsNullOrEmpty(mesh.name) ? "Mesh-submesh-{0}.drc.bytes" : $"{mesh.name}-submesh-{{0}}.drc.bytes"; for (var submesh = 0; submesh < dracoData.Length; submesh++) { File.WriteAllBytes(Path.Combine(directory, string.Format(filename, submesh)), dracoData[submesh].data.ToArray()); dracoData[submesh].Dispose(); } } else { var filename = string.IsNullOrEmpty(mesh.name) ? "Mesh.drc.bytes" : $"{mesh.name}.drc.bytes"; File.WriteAllBytes(Path.Combine(directory, filename), dracoData[0].data.ToArray()); dracoData[0].Dispose(); } }
static void CompressMeshFilters(MeshFilter[] meshFilters, string directory = null) { var instances = new Dictionary <TextAsset, DracoDecodeInstance>(); var meshDecoder = Object.FindObjectOfType <DracoDecoder>(); if (meshDecoder == null) { meshDecoder = new GameObject("MeshDecoder").AddComponent <DracoDecoder>(); } directory = directory ?? $"Assets/{k_CompressedMeshesDirName}"; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } var dracoMeshes = new List <DracoMesh>(); var dracoFilesUpdated = false; foreach (var meshFilter in meshFilters) { var mesh = meshFilter.sharedMesh; if (mesh == null) { continue; } if (!mesh.isReadable) { Debug.LogError("Mesh is not readable!"); return; } var dracoMesh = new DracoMesh(meshFilter, directory); var dracoFilesMissing = !dracoMesh.TryLoadDracoAssets(); if (dracoFilesMissing) { var scale = meshFilter.transform.localToWorldMatrix.lossyScale; var dracoData = DracoEncoder.EncodeMesh(mesh, scale, .0001f); if (dracoData != null && dracoData.Length > 0) { for (var submesh = 0; submesh < dracoData.Length; submesh++) { if (submesh > 0) { Debug.LogWarning("more than one submesh. not supported yet."); } File.WriteAllBytes(dracoMesh.GetSubmeshAssetPath(submesh), dracoData[submesh].data.ToArray()); dracoData[submesh].Dispose(); dracoFilesUpdated = true; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } else { Debug.LogError("Draco encoding failed"); return; } } dracoMeshes.Add(dracoMesh); } if (dracoFilesUpdated) { foreach (var dracoMesh in dracoMeshes) { if (!dracoMesh.TryLoadDracoAssets()) { Debug.LogError("Loading draco assets failed"); return; } } } foreach (var dracoMesh in dracoMeshes) { for (int submesh = 0; submesh < dracoMesh.submeshCount; submesh++) { // meshDecoder.AddTarget(dracoMesh.target,dracoMesh.dracoAssets[submesh]); var dracoAsset = dracoMesh.dracoAssets[submesh]; if (instances.TryGetValue(dracoAsset, out var instance)) { instance.AddTarget(dracoMesh.target); } else { var newInstance = ScriptableObject.CreateInstance <DracoDecodeInstance>(); var bounds = dracoMesh.target.sharedMesh.bounds; newInstance.SetAsset(dracoAsset, bounds); newInstance.AddTarget(dracoMesh.target); instances[dracoAsset] = newInstance; } } dracoMesh.target.mesh = null; } meshDecoder.instances = instances.Values.ToArray(); }