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.linearTexture = true; tImporter.textureFormat = 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.linearTexture = true; tImporter.textureFormat = TextureImporterFormat.AutomaticCompressed; 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(); }
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) { //Debug.Log("!shouldUseOriginalLM"); LightmapData lmData = new LightmapData(); //AssetDatabase.Refresh(); //AssetDatabase.ImportAsset( lightmapPaths[i], ImportAssetOptions.ForceSynchronousImport ); //AssetDatabase.Refresh(); lmData.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); }