Exemple #1
0
        public void HLODDataTest()
        {
            using (Stream stream = new FileStream(mHlodArtifactName, FileMode.Open, FileAccess.Read))
            {
                HLODData data = HLODDataSerializer.Read(stream);

                Assert.AreEqual(0, data.GetMaterialCount());
                Assert.AreEqual(85, data.GetObjects().Count);
            }
        }
Exemple #2
0
        public override void OnEnable()
        {
            base.OnEnable();

            string path = AssetDatabase.GetAssetPath(serializedObject.targetObject);

            using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                HLODData data = HLODDataSerializer.Read(stream);

                long totalTextureSize = 0;
                long totalMeshSize    = 0;

                var serializableMaterials = data.GetMaterials();
                for (int mi = 0; mi < serializableMaterials.Count; ++mi)
                {
                    var material = serializableMaterials[mi];
                    for (int ti = 0; ti < material.GetTextureCount(); ++ti)
                    {
                        var texture = material.GetTexture(ti);
                        totalTextureSize += texture.BytesLength;

                        m_texture.Add(new KeyValuePair <string, string>(texture.TextureName, FormattingSize(texture.BytesLength)));
                    }
                }


                var serializableObjects = data.GetObjects();
                for (int oi = 0; oi < serializableObjects.Count; ++oi)
                {
                    var mesh           = serializableObjects[oi].GetMesh();
                    int meshSpaceUsage = mesh.GetSpaceUsage();

                    totalMeshSize += meshSpaceUsage;
                    m_mesh.Add(new KeyValuePair <string, string>(mesh.Name, FormattingSize(meshSpaceUsage)));
                }

                m_totalTexture = FormattingSize(totalTextureSize);
                m_totalMesh    = FormattingSize(totalMeshSize);
            }
        }
        public override void OnImportAsset(AssetImportContext ctx)
        {
            ctx.DependsOnCustomDependency("HLODSystemPlatform");

            var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(ctx.selectedBuildTarget);

            try
            {
                UpdateProgress(ctx.assetPath, 0, 1);
                using (Stream stream = new FileStream(ctx.assetPath, FileMode.Open, FileAccess.Read))
                {
                    HLODData      data           = HLODDataSerializer.Read(stream);
                    RootData      rootData       = RootData.CreateInstance <RootData>();
                    TextureFormat compressFormat = GetCompressFormat(data, buildTargetGroup);

                    int currentProgress = 0;
                    int maxProgress     = 0;

                    if (data.GetMaterials() != null)
                    {
                        maxProgress += data.GetMaterials().Count;
                    }
                    if (data.GetObjects() != null)
                    {
                        maxProgress += data.GetObjects().Count;
                    }
                    if (data.GetColliders() != null)
                    {
                        maxProgress += data.GetColliders().Count;
                    }

                    rootData.name = "Root";

                    var serializableMaterials = data.GetMaterials();
                    var loadedMaterials       = new Dictionary <string, Material>();
                    if (serializableMaterials != null)
                    {
                        for (int mi = 0; mi < serializableMaterials.Count; ++mi)
                        {
                            UpdateProgress(ctx.assetPath, currentProgress++, maxProgress);
                            var sm = serializableMaterials[mi];

                            if (loadedMaterials.ContainsKey(sm.ID))
                            {
                                continue;
                            }

                            Material mat = sm.To();
                            loadedMaterials.Add(sm.ID, mat);

                            if (string.IsNullOrEmpty(AssetDatabase.GetAssetPath(mat)) == false)
                            {
                                continue;
                            }

                            ctx.AddObjectToAsset(mat.name, mat);

                            for (int ti = 0; ti < sm.GetTextureCount(); ++ti)
                            {
                                HLODData.SerializableTexture st = sm.GetTexture(ti);
                                Texture2D texture = st.To();
                                EditorUtility.CompressTexture(texture, compressFormat,
                                                              TextureCompressionQuality.Normal);

                                mat.SetTexture(st.Name, texture);
                                ctx.AddObjectToAsset(texture.name, texture);
                            }

                            mat.EnableKeyword("_NORMALMAP");
                        }
                    }

                    var serializableObjects   = data.GetObjects();
                    var serializableColliders = data.GetColliders();
                    Dictionary <string, List <GameObject> >
                    createdGameObjects = new Dictionary <string, List <GameObject> >();
                    Dictionary <string, GameObject> createdColliders = new Dictionary <string, GameObject>();

                    if (serializableObjects != null)
                    {
                        for (int oi = 0; oi < serializableObjects.Count; ++oi)
                        {
                            UpdateProgress(ctx.assetPath, currentProgress++, maxProgress);

                            var        so = serializableObjects[oi];
                            GameObject go = new GameObject();
                            go.name = so.Name;

                            MeshFilter      mf          = go.AddComponent <MeshFilter>();
                            MeshRenderer    mr          = go.AddComponent <MeshRenderer>();
                            List <string>   materialIds = so.GetMaterialIds();
                            List <Material> materials   = new List <Material>();

                            for (int mi = 0; mi < materialIds.Count; ++mi)
                            {
                                string id = materialIds[mi];
                                if (loadedMaterials.ContainsKey(id))
                                {
                                    materials.Add(loadedMaterials[id]);
                                }
                                else
                                {
                                    string path = AssetDatabase.GUIDToAssetPath(id);
                                    if (string.IsNullOrEmpty(path) == false)
                                    {
                                        materials.Add(AssetDatabase.LoadAssetAtPath <Material>(path));
                                    }
                                    else
                                    {
                                        materials.Add(null);
                                    }
                                }
                            }

                            Mesh mesh = so.GetMesh().To();
                            mf.sharedMesh      = mesh;
                            mr.sharedMaterials = materials.ToArray();
                            mr.lightProbeUsage = so.LightProbeUsage;

                            ctx.AddObjectToAsset(mesh.name, mesh);

                            if (createdGameObjects.ContainsKey(go.name) == false)
                            {
                                createdGameObjects.Add(go.name, new List <GameObject>());
                            }

                            createdGameObjects[go.name].Add(go);
                        }
                    }

                    if (serializableColliders != null)
                    {
                        for (int ci = 0; ci < serializableColliders.Count; ++ci)
                        {
                            UpdateProgress(ctx.assetPath, currentProgress++, maxProgress);

                            var        sc = serializableColliders[ci];
                            GameObject go;

                            if (createdColliders.ContainsKey(sc.Name) == false)
                            {
                                createdColliders[sc.Name] = new GameObject("Collider");
                            }

                            go = createdColliders[sc.Name];

                            var collider = sc.CreateGameObject();
                            if (collider != null)
                            {
                                collider.name = "Collider" + ci;
                                collider.transform.SetParent(go.transform, true);
                            }
                        }
                    }

                    foreach (var objects in createdGameObjects.Values)
                    {
                        GameObject root;
                        if (objects.Count > 1)
                        {
                            root      = new GameObject();
                            root.name = objects[0].name;
                            for (int i = 0; i < objects.Count; ++i)
                            {
                                objects[i].name = objects[i].name + "_" + i;
                                objects[i].transform.SetParent(root.transform, true);
                            }
                        }
                        else
                        {
                            root = objects[0];
                        }

                        if (createdColliders.ContainsKey(root.name))
                        {
                            createdColliders[root.name].transform.SetParent(root.transform, true);
                        }

                        rootData.SetRootObject(root.name, root);
                        ctx.AddObjectToAsset(root.name, root);
                    }

                    ctx.AddObjectToAsset("Root", rootData);
                    ctx.SetMainObject(rootData);
                }
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
Exemple #4
0
        private void ExtractMaterial(HLODData hlodData, string filenamePrefix)
        {
            Dictionary <string, HLODData.SerializableMaterial> extractedMaterials = new Dictionary <string, HLODData.SerializableMaterial>();

            //save files to disk
            foreach (var hlodMaterial in hlodData.GetMaterials())
            {
                string id = hlodMaterial.ID;
                hlodMaterial.GetTextureCount();
                Material mat = hlodMaterial.To();

                for (int ti = 0; ti < hlodMaterial.GetTextureCount(); ++ti)
                {
                    var       serializeTexture = hlodMaterial.GetTexture(ti);
                    Texture2D texture          = serializeTexture.To();
                    byte[]    bytes            = texture.EncodeToPNG();
                    string    textureFilename  = $"{filenamePrefix}_{mat.name}_{serializeTexture.TextureName}.png";
                    File.WriteAllBytes(textureFilename, bytes);

                    AssetDatabase.ImportAsset(textureFilename);

                    var assetImporter   = AssetImporter.GetAtPath(textureFilename);
                    var textureImporter = assetImporter as TextureImporter;

                    if (textureImporter)
                    {
                        textureImporter.wrapMode    = serializeTexture.WrapMode;
                        textureImporter.sRGBTexture = GraphicsFormatUtility.IsSRGBFormat(serializeTexture.GraphicsFormat);
                        textureImporter.SaveAndReimport();
                    }

                    var storedTexture = AssetDatabase.LoadAssetAtPath <Texture>(textureFilename);
                    m_manager.AddGeneratedResource(storedTexture);
                    mat.SetTexture(serializeTexture.Name, storedTexture);
                }

                string matFilename = $"{filenamePrefix}_{mat.name}.mat";
                AssetDatabase.CreateAsset(mat, matFilename);
                AssetDatabase.ImportAsset(matFilename);

                var storedMaterial = AssetDatabase.LoadAssetAtPath <Material>(matFilename);
                m_manager.AddGeneratedResource(storedMaterial);


                using (WorkingMaterial newWM = new WorkingMaterial(Collections.Allocator.Temp, storedMaterial))
                {
                    var newSM = new HLODData.SerializableMaterial();
                    newSM.From(newWM);

                    extractedMaterials.Add(id, newSM);
                }
            }

            //apply to HLODData
            var materials = hlodData.GetMaterials();

            for (int i = 0; i < materials.Count; ++i)
            {
                if (extractedMaterials.ContainsKey(materials[i].ID) == false)
                {
                    continue;
                }

                materials[i] = extractedMaterials[materials[i].ID];
            }

            var objects = hlodData.GetObjects();

            for (int oi = 0; oi < objects.Count; ++oi)
            {
                var matIds = objects[oi].GetMaterialIds();

                for (int mi = 0; mi < matIds.Count; ++mi)
                {
                    if (extractedMaterials.ContainsKey(matIds[mi]) == false)
                    {
                        continue;
                    }

                    matIds[mi] = extractedMaterials[matIds[mi]].ID;
                }
            }
        }