private static int AddTexture(string baseDirectory, string filePath, Dictionary <string, int> textureLookup, TexturePack texturePack, float scale) { if (!textureLookup.TryGetValue(filePath, out var textureId)) { var fileExists = true; var path = filePath; if (!File.Exists(path)) { // Assume it's a relative path path = Path.Combine(baseDirectory, path); if (!File.Exists(path)) { fileExists = false; } } var bitmap = fileExists ? TextureImportHelper.ImportBitmap(path) : new Bitmap(32, 32); if (scale != 1) { bitmap = new Bitmap(bitmap, new Size(( int )(bitmap.Width / scale), ( int )(bitmap.Height / scale))); } var name = Path.GetFileNameWithoutExtension(path); var texture = new Texture(bitmap, PS2.GS.GSPixelFormat.PSMT8, name); textureId = texturePack.Count; texturePack.Add(texture); textureLookup[filePath] = textureId; } return(textureId); }
public void RebuildAtlas() { RefreshTextureLookup(); if (!TextureLookup.ContainsKey("transparent")) { RequireTransparentTexture(); } TextureImportHelper.SetTexturesReadable(Textures); var supertexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); supertexture.name = $"{name}_atlas"; AtlasRects = supertexture.PackTextures(Textures.ToArray(), 2, 4096, false); var copyTexture = new Texture2D(supertexture.width, supertexture.height, TextureFormat.RGBA32, false); var pixels = supertexture.GetPixels(0, 0, supertexture.width, supertexture.height, 0); //Debug.Log(pixels.Length); copyTexture.SetPixels(pixels); copyTexture.name = supertexture.name; supertexture = copyTexture; supertexture.wrapMode = TextureWrapMode.Clamp; TextureImportHelper.PatchAtlasEdges(supertexture, AtlasRects); if (Atlas != null) { AssetDatabase.RemoveObjectFromAsset(Atlas); } var path = Path.GetDirectoryName(AssetDatabase.GetAssetPath(this)); path = Path.Combine(path, "atlas").Replace("\\", "/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //File.WriteAllBytes(@"../atlas.png", Atlas.EncodeToPNG()); supertexture = TextureImportHelper.SaveAndUpdateTexture(supertexture, Path.Combine(path, supertexture.name + ".png")); //Debug.Log(path); // AssetDatabase.CreateAsset(supertexture, Path.Combine(path, supertexture.name + ".asset").Replace("\\", "/")); Atlas = supertexture; EditorUtility.SetDirty(this); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
private void ReadTextures(string basePath, string outPath) { var count = br.ReadInt32(); var length = br.ReadInt32(); textures = new string[count]; textureData = new Texture2D[count]; for (var i = 0; i < count; i++) { var bytes = br.ReadBytes(length); var tex = Encoding.GetEncoding(949).GetString(bytes); var nulladdr = tex.IndexOf('\0'); textures[i] = tex.Substring(0, nulladdr); var texout = TextureImportHelper.GetOrImportTextureToProject(textures[i], basePath, outPath); //var texPath = Path.Combine(basePath, "texture", textures[i]); ////Debug.Log(texPath); //if (File.Exists(texPath)) //{ // var bpath = Path.GetDirectoryName(textures[i]); // var fname = Path.GetFileNameWithoutExtension(textures[i]); // var texOutPath = Path.Combine(outPath, "texture", bpath); // var pngPath = Path.Combine(texOutPath, fname + ".png"); // if (!File.Exists(pngPath)) // { // var tex2D = LoadTexture(texPath); // tex2D.name = textures[i]; // PathHelper.CreateDirectoryIfNotExists(texOutPath); // File.WriteAllBytes(pngPath, tex2D.EncodeToPNG()); // AssetDatabase.Refresh(); // } // var texout = AssetDatabase.LoadAssetAtPath(pngPath, typeof(Texture2D)) as Texture2D; // //Debug.Log("Our texture is currently: " + tex2D); textures[i] = Path.GetFileNameWithoutExtension(texout.name); textureData[i] = texout; //} //Debug.Log(textures[i]); } }
public void LoadTextures(string savePath) { var path = Path.Combine("Assets/models/atlas", savePath); //Path.Combine(savePath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var atlasName = $"{model.Name.Replace("\\", "_")}_atlas"; var atlasPath = Path.Combine(path, atlasName + ".png"); model.Textures = new List <string>(); var textures = new List <Texture2D>(); var texCount = br.ReadInt32(); for (var i = 0; i < texCount; i++) { var tName = br.ReadKoreanString(40); model.Textures.Add(tName); var texout = TextureImportHelper.GetOrImportTextureToProject(tName, RagnarokDirectory.GetRagnarokDataDirectory, "Assets/models"); textures.Add(texout); } TextureImportHelper.SetTexturesReadable(textures); var extratexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); textures.Add(extratexture); var supertexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); supertexture.name = atlasName; atlasRects = supertexture.PackTextures(textures.ToArray(), 2, 4096, false); if (File.Exists(atlasPath)) { //we still needed to make the atlas to get the rects. We assume they're the same, but they might not be? GameObject.DestroyImmediate(supertexture); atlas = AssetDatabase.LoadAssetAtPath <Texture2D>(atlasPath); return; } TextureImportHelper.PatchAtlasEdges(supertexture, atlasRects); supertexture = TextureImportHelper.SaveAndUpdateTexture(supertexture, atlasPath); //var bytes = supertexture.EncodeToPNG(); //File.WriteAllBytes(atlasPath, bytes); //TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(atlasPath); //importer.textureType = TextureImporterType.Default; //importer.npotScale = TextureImporterNPOTScale.None; //importer.textureFormat = TextureImporterFormat.Automatic; //importer.textureCompression = TextureImporterCompression.CompressedHQ; //importer.wrapMode = TextureWrapMode.Clamp; //importer.isReadable = false; //importer.mipmapEnabled = false; //importer.alphaIsTransparency = true; //importer.maxTextureSize = 4096; //importer.SaveAndReimport(); //supertexture = AssetDatabase.LoadAssetAtPath<Texture2D>(atlasPath); //Get rid of mipmaps by copying the texture data. We have no need of mipmaps in our world. //var copyTexture = new Texture2D(supertexture.width, supertexture.height, TextureFormat.RGBA32, false); ////var pixels = supertexture.GetPixels(0, 0, supertexture.width, supertexture.height, 0); ////copyTexture.SetPixels(pixels); //copyTexture.name = supertexture.name; //supertexture = copyTexture; //supertexture.wrapMode = TextureWrapMode.Clamp; //AssetDatabase.CreateAsset(supertexture, atlasPath); atlas = supertexture; }
public void MakeAtlas(string path) { var atlasName = $"{baseName.Replace("\\", "_")}_atlas"; var atlasPath = Path.Combine(path, atlasName + ".png"); //if (!Directory.Exists(path)) // Directory.CreateDirectory(path); //if (!Directory.Exists("Assets/Effects/Sprites/" + baseName)) // Directory.CreateDirectory("Assets/Effects/Sprites/" + baseName); for (var i = 0; i < textureNames.Count; i++) { var texout = TextureImportHelper.GetOrImportTextureToProject(textureNames[i], basePath, "Assets/Effects/Textures/" + baseName); textures.Add(texout); } TextureImportHelper.SetTexturesReadable(textures); var extratexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); textures.Add(extratexture); var supertexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); supertexture.name = atlasName; var atlasRects = supertexture.PackTextures(textures.ToArray(), 2, 4096, false); if (File.Exists(atlasPath)) { File.Delete(atlasPath); ////we still needed to make the atlas to get the rects. We assume they're the same, but they might not be? //GameObject.DestroyImmediate(supertexture); //atlas = AssetDatabase.LoadAssetAtPath<Texture2D>(atlasPath); //return; } TextureImportHelper.PatchAtlasEdges(supertexture, atlasRects); anim.Atlas = supertexture; anim.AtlasRects = atlasRects; //atlas = TextureImportHelper.SaveAndUpdateTexture(supertexture, atlasPath); AssetDatabase.AddObjectToAsset(supertexture, anim); sprites = new List <Sprite>(textureNames.Count); for (var i = 0; i < textureNames.Count; i++) { var texrect = new Rect(atlasRects[i].x * supertexture.width, atlasRects[i].y * supertexture.height, atlasRects[i].width * supertexture.width, atlasRects[i].height * supertexture.height); var sprite = Sprite.Create(supertexture, texrect, new Vector2(0.5f, 0.5f), 50, 0, SpriteMeshType.FullRect); sprite.name = Path.GetFileNameWithoutExtension(textureNames[i]); AssetDatabase.AddObjectToAsset(sprite, anim); //AssetDatabase.CreateAsset(sprite, "Assets/Effects/Sprites/" + baseName + "/" + sprite.name); } AssetDatabase.SetMainObject(anim, Path.Combine("Assets/Effects/", baseName + ".asset")); AssetDatabase.SaveAssets(); }