Пример #1
0
        IEnumerator TextureStreamer(AssetSchematic schematic, MCSObjectLoader loader)
        {
            Dictionary <string, Texture2D> textures     = new Dictionary <string, Texture2D> ();
            Dictionary <string, string>    texturePaths = new Dictionary <string, string> ();

            if (schematic.structure_and_physics.material_structure.albedo != null && schematic.structure_and_physics.material_structure.albedo != "")
            {
                texturePaths.Add("albedo", schematic.structure_and_physics.material_structure.albedo);
            }
            if (schematic.structure_and_physics.material_structure.metal != null && schematic.structure_and_physics.material_structure.metal != "")
            {
                texturePaths.Add("metal", schematic.structure_and_physics.material_structure.metal);
            }
            if (schematic.structure_and_physics.material_structure.normal != null && schematic.structure_and_physics.material_structure.normal != "")
            {
                texturePaths.Add("normal", schematic.structure_and_physics.material_structure.normal);
            }

            var datetime = DateTime.Now;

            //TODO: this is commented out to prevent errors with Wii U compiles

            /*
             *          while (!Caching.ready)
             *                  yield return null;
             */

            foreach (KeyValuePair <string, string> entry in texturePaths)
            {
                using (WWW www = new WWW(entry.Value)) {
                    while (!www.isDone)
                    {
                        loader.progress += www.progress * 100 / texturePaths.Count;
                        yield return(0);
                    }

                    if (www.error != null)
                    {
                        throw new Exception("Download of " + entry.Key + "from: " + entry.Value + "  has failed. Error: " + www.error);
                    }
                    Texture2D streamedTexture = www.texture;
                    if (streamedTexture != null)
                    {
                        textures.Add(entry.Key, streamedTexture);
                    }
                }
            }
            AssetCreator ac         = new AssetCreator();
            float        difference = (float)System.DateTime.Now.Subtract(datetime).TotalMilliseconds;

            loader.Complete(ac.CreateMorphMaterial(schematic, textures), difference);
        }
    Material FindLoadAndAssignFigureMaterial(Material fbxMaterial, string rendererName, string dirPath)
    {
        Material dstMaterial;
        bool     useLods      = true;
        string   materialName = fbxMaterial.name;

        if (materialName == "EyeSheen" || materialName == "EyeAndLash")
        {
            materialName = "EyeAndLash";
            useLods      = false;
        }
        string lodSuffix = "";

        if (useLods)
        {
            int lodPos = rendererName.LastIndexOf("_LOD");
            lodSuffix = rendererName.Substring(lodPos);
        }

        string dstMatBaseName = materialName + lodSuffix;

        string matPath = dirPath + "/Materials/" + dstMatBaseName + ".mat";

        //UnityEngine.Debug.Log("MatPath: " + matPath);

        dstMaterial = AssetDatabase.LoadAssetAtPath <Material>(matPath);
        if (dstMaterial != null)
        {
            return(dstMaterial);
        }

        string dirMon = dirPath + "/Materials";

        string monPath = dirPath + "/Materials/" + dstMatBaseName + ".mon";
        //AssetDatabase.ImportAsset(monPath, ImportAssetOptions.Default | ImportAssetOptions.ForceSynchronousImport);
        MonDeserializer monDes = new MonDeserializer();

        AssetSchematic[] schematics   = monDes.DeserializeMonFile(monPath);
        AssetCreator     assetCreator = new AssetCreator();

        foreach (AssetSchematic schematic in schematics)
        {
            if (schematic.type_and_function.primary_function != Morph3d.Utility.Schematic.Enumeration.PrimaryFunction.material)
            {
                continue;
            }
            Material newMaterial = assetCreator.CreateMorphMaterial(schematic, TextureLoader.GetTextures(schematic, dirMon));

            if (schematic.stream_and_path.generated_path == "" || schematic.stream_and_path.generated_path == null)
            {
                schematic.stream_and_path.generated_path = dirMon + "/" + schematic.origin_and_description.name + ".mat";
            }

            int    pos           = schematic.stream_and_path.generated_path.LastIndexOf('/');
            string directoryPath = schematic.stream_and_path.generated_path.Substring(0, pos);

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            //does a material already exist, if so, replace over it
            string dstPath = MCS_Utilities.Paths.ConvertRelativeToAbsolute(dirMon, schematic.stream_and_path.generated_path);
            if (!File.Exists(dstPath))
            {
                AssetDatabase.CreateAsset(newMaterial, dstPath);
            }
            else
            {
                Material oldMat = AssetDatabase.LoadAssetAtPath <Material>(dstPath);
                oldMat.CopyPropertiesFromMaterial(newMaterial);
            }
            dstMaterial = AssetDatabase.LoadAssetAtPath <Material>(dstPath);
            return(dstMaterial);
        }

        dstMaterial = AssetDatabase.LoadAssetAtPath <Material>(matPath);


        return(dstMaterial);
    }
Пример #3
0
        public static bool TryToImportMaterialsFromSchematics(string str, string dirMon, AssetSchematic[] schematics)
        {
            bool         materialCreationStatus = true;
            AssetCreator ac = new AssetCreator();

            //let's look for any materials in the mon file, if we find any create and import them now before we move on
            for (int i = 0; i < schematics.Length && materialCreationStatus; i++)
            {
                AssetSchematic mon = schematics[i];
                if (mon.origin_and_description != null && !String.IsNullOrEmpty(mon.origin_and_description.mcs_id))
                {
                    schematicLookup[mon.origin_and_description.mcs_id] = mon;
                }
                if (mon.type_and_function.artisttools_function == ArtistToolsFunction.material || mon.type_and_function.primary_function == PrimaryFunction.material)
                {
                    try
                    {
                        Dictionary <string, Texture2D> textureDict = new Dictionary <string, Texture2D>();
                        bool textureLoadStatus = TextureLoader.GetTextures(mon, dirMon, out textureDict);
                        if (textureLoadStatus == false)
                        {
                            //UnityEngine.Debug.LogError("Failed to find textures for material: " + str);
                            continue;
                        }

                        Material mat = ac.CreateMorphMaterial(mon, textureDict);

                        if (mon.stream_and_path.generated_path == "" || mon.stream_and_path.generated_path == null)
                        {
                            //mon.stream_and_path.generated_path = GENERATED_MATERIALS_FOLDER + "/" + mon.origin_and_description.name + ".mat";
                            string newDir = dirMon + "/Materials";
                            if (!Directory.Exists(newDir))
                            {
                                Directory.CreateDirectory(newDir);
                            }
                            mon.stream_and_path.generated_path = "Materials/" + mon.origin_and_description.name + ".mat";
                        }

                        int    pos           = mon.stream_and_path.generated_path.LastIndexOf('/');
                        string directoryPath = mon.stream_and_path.generated_path.Substring(0, pos);

                        if (!Directory.Exists(directoryPath))
                        {
                            Directory.CreateDirectory(directoryPath);
                        }

                        //does a material already exist, if so, replace over it
                        string dstPath = MCS_Utilities.Paths.ConvertRelativeToAbsolute(dirMon, mon.stream_and_path.generated_path);

                        //Convert incompatible maya ":" to "_"
                        dstPath = dstPath.Replace(":", "_");

                        //UnityEngine.Debug.Log("dstPath: " + dstPath);

                        if (!File.Exists(dstPath))
                        {
                            AssetDatabase.CreateAsset(mat, dstPath);
                        }
                        else
                        {
                            Material oldMat = AssetDatabase.LoadAssetAtPath <Material>(dstPath);
                            if (oldMat == null)
                            {
                                //UnityEngine.Debug.LogError("Unable to update material because we can't load it: " + dstPath);
                            }
                            else
                            {
                                oldMat.CopyPropertiesFromMaterial(mat);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogWarning("Material creation failed. " + e.Message);
                        materialCreationStatus = false;
                    }
                }
            }

            return(materialCreationStatus);
        }