public void PrintFinalResults(LSS_Models.LightingScenarioModel lightingScenariosData)
        {
            string message = BuildMessageString(lightingScenariosData.rendererInfos, " meshrenderers, ");

            message += BuildMessageString(lightingScenariosData.lightmaps, " ligthmaps, ");
            message += BuildMessageString(lightingScenariosData.materials, " materials, ");
            message += BuildMessageString(lightingScenariosData.lightInfos, " lights, ");
            message += BuildMessageString(lightingScenariosData.flareInfos, " lens flares, ");
            message += BuildMessageString(lightingScenariosData.gameObjectInfos, " gameobjects, ");
            Debug.Log("Stored info for " + message);
        }
示例#2
0
        public void LoadFromSO(int light)
        {
            lightingScenariosData = custom_lightmap_data[light].GetJsonFile(true /* forced */);

            //The JSON file was " + lightingScenariosData == null ? " null or not present!"
                        #if UNITY_EDITOR
            if (!CheckLightingScenarioValidForScene(lightingScenariosData))
            {
                int option = EditorUtility.DisplayDialogComplex(
                    "JSON File Not Valid For Scene!",
                    "The file about to be loaded has wrong unique ID for this scene.",
                    "Do Nothing",
                    "Proceed",
                    "Change Permanterly"
                    );

                // switch (option)	{
                // case 0: // Do Nothing
                //  return;
                // case 1: // Proceed
                //  break;
                // case 2: // Change UID Permanterly
                //  lightingScenariosData.uid = GetUniqueID (gameObject);
                //  WriteJsonFile (lightingScenariosData, GetResourcesStorageDirectory());
                //  break;
                // }
            }
                        #endif


            LightmapSettings.lightmaps = custom_lightmap_data[light].GetLightmaps();

            RegisterAllUIDs();              // Ensure that all LSS_UID unique id's have been generated
            StartCoroutine(ApplySceneInfo(lightingScenariosData.gameObjectInfos));
            StartCoroutine(ApplySceneInfo(lightingScenariosData.terrainInfos));
            StartCoroutine(ApplySceneInfo(lightingScenariosData.rendererInfos));
            StartCoroutine(ApplySceneInfo(lightingScenariosData.lightInfos));
            StartCoroutine(ApplyMaterialsInfo(lightingScenariosData.materials));
            StartCoroutine(LoadLightProbes(lightingScenariosData.lightProbes));

                        #if UNITY_EDITOR
            Debug.Log("Loaded Lighting Scenario: \"" + custom_lightmap_data[light].lightmap_id + "\"");
                        #endif
        }
 public bool CheckLightingScenarioValidForScene(LSS_Models.LightingScenarioModel scenario)
 {
     return(scenario.uid == GetOrCreateUniqueID(gameObject));            // Given the deserialized and loaded JSON data (as LightingScenarioData), check if the uid field matches the UniqueID attached to this gameobject (if they match then that JSON file was built by this script in this scene).
 }
 public void WriteJsonFile(LSS_Models.LightingScenarioModel lightingScenariosData, string path)
 {
     File.WriteAllText(path + m_jsonFileName, JsonUtility.ToJson(lightingScenariosData));              // Write all the data to the file.
 }
        public void Load()            // Call this to load whatever json exists in the currently set m_resourceStorageFolder
        {
            lightingScenariosData = LoadJsonFile();

            //The JSON file was " + lightingScenariosData == null ? " null or not present!"
                        #if UNITY_EDITOR
            if (!CheckLightingScenarioValidForScene(lightingScenariosData))
            {
                int option = EditorUtility.DisplayDialogComplex(
                    "JSON File Not Valid For Scene!",
                    "The file about to be loaded has wrong unique ID for this scene.",
                    "Do Nothing",
                    "Proceed",
                    "Change Permanterly"
                    );

                switch (option)
                {
                case 0:                 // Do Nothing
                    return;

                case 1:                 // Proceed
                    break;

                case 2:                 // Change UID Permanterly
                    lightingScenariosData.uid = GetUniqueID(gameObject);
                    WriteJsonFile(lightingScenariosData, GetResourcesStorageDirectory());
                    break;
                }
            }
                        #endif

            var newLightmaps = new LightmapData[lightingScenariosData.lightmaps.Length];
            for (int i = 0; i < newLightmaps.Length; i++)
            {
                newLightmaps[i] = new LightmapData();
                newLightmaps[i].lightmapColor = Resources.Load <Texture2D>(resourceFolder + "/" + lightingScenariosData.lightmaps[i]);
                if (lightingScenariosData.lightmapsMode != LightmapsMode.NonDirectional)
                {
                    if (lightingScenariosData.lightmapsDir.Length > i && lightingScenariosData.lightmapsDir [i] != null)                       // If the textuer existed and was set in the data file.
                    {
                        newLightmaps [i].lightmapDir = Resources.Load <Texture2D> (resourceFolder + "/" + lightingScenariosData.lightmapsDir [i]);
                    }
                    if (lightingScenariosData.lightmapsShadow.Length > i && lightingScenariosData.lightmapsShadow [i] != null)                       // If the textuer existed and was set in the data file.
                    {
                        newLightmaps [i].shadowMask = Resources.Load <Texture2D> (resourceFolder + "/" + lightingScenariosData.lightmapsShadow [i]);
                    }
                }
            }
            LightmapSettings.lightmaps = newLightmaps;

            RegisterAllUIDs();              // Ensure that all LSS_UID unique id's have been generated
            StartCoroutine(ApplySceneInfo(lightingScenariosData.gameObjectInfos));
            StartCoroutine(ApplySceneInfo(lightingScenariosData.terrainInfos));
            StartCoroutine(ApplySceneInfo(lightingScenariosData.rendererInfos));
            StartCoroutine(ApplySceneInfo(lightingScenariosData.lightInfos));
            StartCoroutine(ApplyMaterialsInfo(lightingScenariosData.materials));
            StartCoroutine(LoadLightProbes(lightingScenariosData.lightProbes));

                        #if UNITY_EDITOR
            Debug.Log("Loaded Lighting Scenario: \"" + resourceFolder + "\"");
                        #endif
        }
        //
        // Generate JSON Data From Resources InfosArrays Then Copy Scene Files To Resources Folder
        //
        public void GenerateLightmapInfoStore()
        {
            // Do some heavy lifting by generating all nescessary arrays for the json file and copying all textures to the Resources folder

            RegisterAllUIDs();              // Ensure that all LSS_UID unique id's have been generated

            lightingScenariosData = new LSS_Models.LightingScenarioModel();

            lightingScenariosData.uid = GetOrCreateUniqueID(gameObject);              // The ChangeLightmapUniqueID.uniqueId from the component attached to this gameobject. Ensures this data is for this scene.

            var newLightsInfos                 = new List <LSS_Models.LightModel>();
            var newLensFlareInfos              = new List <LSS_Models.FlareModel>();
            var newGameObjectInfos             = new List <LSS_Models.GameObjectModel>();
            var newRendererModels              = new List <LSS_Models.RendererModel>();
            var newTerrainsInfos               = new List <LSS_Models.TerrainModel>();
            var newSphericalHarmonicsModelList = new List <LSS_Models.SphericalHarmonicsModel>();

            var newLightmapsTextures       = new List <Texture2D>();
            var newLightmapsTexturesDir    = new List <Texture2D>();
            var newLightmapsTexturesShadow = new List <Texture2D>();

            var newLightmapsMode = LightmapSettings.lightmapsMode;

            lightingScenariosData.lightmapsMode = newLightmapsMode;

            //
            // Begin Appending Scene Lights Info To InfosArrays.
            //
            var lightParams = Resources.FindObjectsOfTypeAll(typeof(LSS_Light));

            foreach (LSS_Light lightParam in lightParams)
            {
                GetOrCreateUniqueID(lightParam.gameObject);
                newLightsInfos.Add((LSS_Models.LightModel)lightParam.GetComponentInfo());
            }
            lightingScenariosData.lightInfos = newLightsInfos.ToArray();

            //
            // Begin Appending LensFlare Info To InfosArrays.
            //
            var flareParams = Resources.FindObjectsOfTypeAll(typeof(LSS_LensFlare));

            foreach (LSS_LensFlare flareParam in flareParams)
            {
                GetOrCreateUniqueID(flareParam.gameObject);
                newLensFlareInfos.Add((LSS_Models.FlareModel)flareParam.GetComponentInfo());
            }
            lightingScenariosData.flareInfos = newLensFlareInfos.ToArray();


            //
            // Begin Appending LensFlare Info To InfosArrays.
            //
            var gameObjcetParams = Resources.FindObjectsOfTypeAll(typeof(LSS_GameObject));

            foreach (LSS_GameObject gameObjcetParam in gameObjcetParams)
            {
                GetOrCreateUniqueID(gameObjcetParam.gameObject);
                newGameObjectInfos.Add((LSS_Models.GameObjectModel)gameObjcetParam.GetComponentInfo());
            }
            lightingScenariosData.gameObjectInfos = newGameObjectInfos.ToArray();


            //
            // Begin Appending Terrain Lightmaps Info To InfosArrays.
            //
            foreach (Terrain terrain in Resources.FindObjectsOfTypeAll(typeof(Terrain)))
            {
                if (GetLightmapIndexValid(terrain.lightmapIndex))
                {
                    string uid = GetOrCreateUniqueID(terrain.gameObject);

                    var info = new LSS_Models.TerrainModel();
                    info.uniqueId            = uid;
                    info.lightmapScaleOffset = terrain.lightmapScaleOffset;

                    if (newLightmapsMode != LightmapsMode.NonDirectional)
                    {
                        //first directional lighting
                        Texture2D lightmapdir = LightmapSettings.lightmaps[terrain.lightmapIndex].lightmapDir;
                        if (lightmapdir != null)
                        {
                            if (newLightmapsTexturesDir.IndexOf(lightmapdir) == -1)
                            {
                                newLightmapsTexturesDir.Add(lightmapdir);
                            }
                        }
                        //now the shadowmask
                        Texture2D lightmapshadow = LightmapSettings.lightmaps[terrain.lightmapIndex].shadowMask;
                        if (lightmapshadow != null)
                        {
                            if (newLightmapsTexturesShadow.IndexOf(lightmapshadow) == -1)
                            {
                                newLightmapsTexturesShadow.Add(lightmapshadow);
                            }
                        }
                    }
                    Texture2D lightmaplight = LightmapSettings.lightmaps[terrain.lightmapIndex].lightmapColor;
                    info.lightmapIndex = newLightmapsTextures.IndexOf(lightmaplight);
                    if (lightmaplight != null)
                    {
                        if (newLightmapsTextures.IndexOf(lightmaplight) == -1)                           // A value of -1 means no lightmap has been assigned,
                        {
                            info.lightmapIndex = newLightmapsTextures.Count;
                            newLightmapsTextures.Add(lightmaplight);
                        }
                    }
                    newTerrainsInfos.Add(info);
                }
            }
            //lightingScenariosData.lightmaps = TextureListToArrayOfNames(newLightmapsTextures);
            lightingScenariosData.terrainInfos = newTerrainsInfos.ToArray();


            //
            // Begin Appending MeshRenderes Lightmaps Info To InfosArrays
            //
            foreach (MeshRenderer renderer in Resources.FindObjectsOfTypeAll(typeof(MeshRenderer)))
            {
                if (GetLightmapIndexValid(renderer.lightmapIndex))
                {
                    string uid = GetOrCreateUniqueID(renderer.gameObject);

                    if (String.IsNullOrEmpty(uid))                        // if object is not part of a scene then it is a prefab so do not attempt to set the id
                    {
                        if (isVerbose == true)
                        {
                            Debug.Log("Object " + renderer.name + " is part of a prefab, skipping.");
                        }
                        continue;
                    }

                    var info = new LSS_Models.RendererModel();
                    info.uniqueId = uid;
                    //info.renderer = renderer; // REMOVED - added uniqueId as the new lookup method
                    info.lightmapOffsetScale = renderer.lightmapScaleOffset;

                    if (newLightmapsMode != LightmapsMode.NonDirectional)
                    {
                        //first directional lighting
                        Texture2D lightmapdir = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapDir;
                        if (lightmapdir != null)
                        {
                            if (newLightmapsTexturesDir.IndexOf(lightmapdir) == -1)
                            {
                                newLightmapsTexturesDir.Add(lightmapdir);
                            }
                        }
                        //now the shadowmask
                        Texture2D lightmapshadow = LightmapSettings.lightmaps[renderer.lightmapIndex].shadowMask;
                        if (lightmapshadow != null)
                        {
                            if (newLightmapsTexturesShadow.IndexOf(lightmapshadow) == -1)
                            {
                                newLightmapsTexturesShadow.Add(lightmapshadow);
                            }
                        }
                    }
                    Texture2D lightmaplight = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapColor;
                    info.lightmapIndex = newLightmapsTextures.IndexOf(lightmaplight);
                    if (lightmaplight != null)
                    {
                        if (newLightmapsTextures.IndexOf(lightmaplight) == -1)                           // A value of -1 means no lightmap has been assigned,
                        {
                            info.lightmapIndex = newLightmapsTextures.Count;
                            newLightmapsTextures.Add(lightmaplight);
                        }
                    }
                    newRendererModels.Add(info);
                }
            }
            lightingScenariosData.lightmapsMode = newLightmapsMode;
            lightingScenariosData.lightmaps     = TextureListToArrayOfNames(newLightmapsTextures);

            if (newLightmapsMode != LightmapsMode.NonDirectional)
            {
                lightingScenariosData.lightmapsDir    = TextureListToArrayOfNames(newLightmapsTexturesDir);
                lightingScenariosData.lightmapsShadow = TextureListToArrayOfNames(newLightmapsTexturesShadow);
            }
            else
            {
                if (isVerbose == true)
                {
                    Debug.Log("Lightmap settings are non-directional. Not saving directional and shadow textures.");
                }
            }

            lightingScenariosData.rendererInfos = newRendererModels.ToArray();

            var scene_LightProbes = new SphericalHarmonicsL2[LightmapSettings.lightProbes.bakedProbes.Length];

            scene_LightProbes = LightmapSettings.lightProbes.bakedProbes;

            for (int i = 0; i < scene_LightProbes.Length; i++)
            {
                var SHCoeff = new LSS_Models.SphericalHarmonicsModel();

                // j is coefficient
                for (int j = 0; j < 3; j++)
                {
                    //k is channel ( r g b )
                    for (int k = 0; k < 9; k++)
                    {
                        SHCoeff.coefficients[j * 9 + k] = scene_LightProbes[i][j, k];
                    }
                }
                newSphericalHarmonicsModelList.Add(SHCoeff);
            }
            lightingScenariosData.lightProbes = newSphericalHarmonicsModelList.ToArray();


            //
            // Start Creating/Copying Resources Files To The Temporary Resources Folder
            //
            CreateResourcesTemporaryDirectory();


            //
            // Copy Textures To Temporary Folder
            //
            CopyTextureToResources(lightingScenariosData.lightmaps);
            CopyTextureToResources(lightingScenariosData.lightmapsDir);
            CopyTextureToResources(lightingScenariosData.lightmapsShadow);


            //
            // Copy Materials To Temporary Folder
            //
            var materialsParams = GetComponent <LSS_Material> ();

            if (materialsParams != null)
            {
                Material[] _materials = new Material[materialsParams.materials.Length];
                for (int i = 0; i < materialsParams.materials.Length; i++)
                {
                    var mat = materialsParams.materials [i];
                    if (mat == null)
                    {
                        continue;
                    }
                    _materials [i] = materialsParams.materials [i];
                }
                lightingScenariosData.materials = CopyMaterialToResources(_materials);
            }


            //
            // Write JSON File To Temporary Folder
            //
            WriteJsonFile(lightingScenariosData, GetResourcesTemporaryDirectory()); // Write the fully configured JSON file to the temporary folder.
            AssetDatabase.Refresh();                                                // Refresh so the new files can be found and loaded.


            //
            // Transfer Temporary Files Then Load The New Resourcesses
            //
            TransferTemporaryFilesToStorage();        // Final step before successful completion, this deletes old resources folder, creats a new one and transfers all temp files over.
            Load();                                   // After all processing and file transfering the current lightmaps may have become invald (if they were the ones currently loaded from the old deleted resources folder) so we should just reload everything!

            PrintFinalResults(lightingScenariosData); // Display a successfully completed message to the user showing how many resources have been created.


            //
            // Clean Up
            //
            DeleteResourcesTemporaryDirectory();
            AssetDatabase.SaveAssets();              // Refresh so the new files can be found and loaded.
        }