Exemple #1
0
        private static string[] PrepareLightmaps(GameObject[] affectedObjects)
        {
            //
            //Duplicate original lightmaps
            List <int> lightmapsToBackup = new List <int>();

            foreach (GameObject obj in affectedObjects)
            {
                int lightmapIndex = -1;

                if (obj.GetComponent <Renderer>() != null)
                {
                    lightmapIndex = obj.GetComponent <Renderer>().lightmapIndex;
                }
                else
                {
                    Terrain terrain = obj.GetComponent <Terrain>();
                    if (terrain != null)
                    {
                        lightmapIndex = terrain.lightmapIndex;
                    }
                }

                if (lightmapIndex < 0)
                {
                    continue;
                }

                //Make sure that we keep only lightmapped objects
                if (lightmapIndex < 0 || lightmapIndex >= LightmapSettings.lightmaps.Length)
                {
                    continue;
                }

                if (!lightmapsToBackup.Contains(lightmapIndex))
                {
                    if (LightmapSettings.lightmaps[lightmapIndex] != null)
                    {
                        lightmapsToBackup.Add(lightmapIndex);
                    }
                }

                //Find to what triangle does the point belong to
                //UpdateLightmap(obj);
            }

            string[] lightmapPaths = new string[LightmapSettings.lightmaps.Length];

            int lightmapsCount = 0;

            foreach (int lightmapIndex in lightmapsToBackup)
            {
                Texture2D lightmapFar = LightmapSettings.lightmaps[lightmapIndex].lightmapColor;

                if (lightmapFar == null)
                {
                    continue;
                }

                string lightMapAssetPath = AssetDatabase.GetAssetPath(lightmapFar);

                //Debug.Log("lightMapAssetPath for " + lightmapIndex + " is " + lightMapAssetPath);

                if (lightMapAssetPath == null)
                {
                    continue;
                }

                if (lightMapAssetPath.Length <= 0)
                {
                    continue;
                }

                if (WpHelper.HasSuffix(lightMapAssetPath, "__WP"))
                {
                    string originalLightmapPath = WpHelper.RemoveSuffixFromFilename(lightMapAssetPath, "__WP");
                    //Debug.Log("WaterPlus lightmap already exists. Will be using the original one at path " + originalLightmapPath);

                    if (!File.Exists(originalLightmapPath))
                    {
                        Debug.LogError("Cannot find the original lightmap at path " + originalLightmapPath +
                                       ". Aborting");
                        return(null);
                    }

                    lightMapAssetPath = originalLightmapPath;
                }

                string waterPlusLightmapAssetPath = WpHelper.AddSuffixToFilename(lightMapAssetPath, "__WP");

                AssetDatabase.DeleteAsset(waterPlusLightmapAssetPath);
                AssetDatabase.CopyAsset(lightMapAssetPath, waterPlusLightmapAssetPath);

                AssetDatabase.ImportAsset(waterPlusLightmapAssetPath, ImportAssetOptions.ForceUpdate);
                AssetDatabase.Refresh();

                lightmapPaths[lightmapIndex] = WpHelper.AssetPathToFilePath(waterPlusLightmapAssetPath);

                lightmapsCount++;

                //LightmapSettings.lightmaps[ lightmapIndex ].lightmapFar = AssetDatabase.LoadAssetAtPath( waterPlusLightmapAssetPath,
                //																		typeof(Texture2D) ) as Texture2D;
            }

            //Load the new lightmaps
            LightmapData[] lightmapsData = new LightmapData[LightmapSettings.lightmaps.Length];

            for (int i = 0; i < lightmapPaths.Length; i++)
            {
                bool shouldUseOriginalLm = false;

                if (lightmapPaths[i] == null)
                {
                    shouldUseOriginalLm = true;
                }
                else if (lightmapPaths[i].Length <= 0)
                {
                    shouldUseOriginalLm = true;
                }

                if (!shouldUseOriginalLm)
                {
                    LightmapData lmData = new LightmapData
                    {
                        lightmapColor =
                            AssetDatabase.LoadAssetAtPath(WpHelper.FilePathToAssetPath(lightmapPaths[i]),
                                                          typeof(Texture2D))
                            as Texture2D
                    };
                    if (lmData.lightmapColor == null)
                    {
                        Debug.LogWarning("lmData.lightmapFar == null for " +
                                         WpHelper.FilePathToAssetPath(lightmapPaths[i]));
                        lmData.lightmapColor = LightmapSettings.lightmaps[i].lightmapColor;
                    }

                    //if (LightmapSettings.lightmaps[i].lightmapNear != null)
                    lmData.lightmapDir = LightmapSettings.lightmaps[i].lightmapDir;

                    lightmapsData[i] = lmData;
                }
                else
                {
                    lightmapsData[i] = LightmapSettings.lightmaps[i];
                }
            }

            LightmapSettings.lightmaps = lightmapsData;

            if (lightmapsCount <= 0)
            {
                Debug.LogError("Nothing to bake - no lightmaps found.");
                return(null);
            }

            return(lightmapPaths);
        }
Exemple #2
0
        public ImageData(string filePath)
        {
            FilePath = filePath;

            if (!File.Exists(FilePath))
            {
                Debug.LogError("lightmap doesn't exist at path: '" + FilePath + "'");
            }

            AssetPath = WpHelper.FilePathToAssetPath(FilePath);

            TextureImporter tImporter = AssetImporter.GetAtPath(AssetPath) as TextureImporter;

            if (tImporter != null)
            {
                tImporter.textureType = TextureImporterType.Default;
                tImporter.sRGBTexture = true;
                tImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings()
                {
                    format = TextureImporterFormat.RGBA32
                });
                tImporter.isReadable = true;
                AssetDatabase.ImportAsset(AssetPath);
            }

            Texture2D tex = AssetDatabase.LoadAssetAtPath(AssetPath, typeof(Texture2D)) as Texture2D;

            if (null == tex)
            {
                Debug.LogError("Failed to load the lightmap at path: " + AssetPath);
                return;
            }

            Width  = tex.width;
            Height = tex.height;

            SrcPixels = tex.GetPixels();
            DstPixels = tex.GetPixels();

            if (tImporter != null)
            {
                tImporter.textureType        = TextureImporterType.Lightmap;
                tImporter.sRGBTexture        = true;
                tImporter.textureCompression = TextureImporterCompression.CompressedLQ;
                tImporter.isReadable         = false;
                AssetDatabase.ImportAsset(AssetPath);
            }

            //WPHelper.MakeTextureReadable( tex, false );

//			dstPixels = new Color[width * height];
//
//			for (int i = 0; i < width*height; i++) {
//				dstPixels[i] = new Color(.5f, .5f, .5f, 1f);
//			}

//			for (int x = 0; x < width; x++) {
//				//Debug.Log(dstPixels[x].r * 255f + " " + dstPixels[x].g * 255f + " " + dstPixels[x].b * 255f);
//			}

            Save();
        }