public IEnumerator FinalizeTextures(System.Diagnostics.Stopwatch stopWatch) { if (coordList.Count == 0) // There's nothing that uses this. { yield break; } int scaleFactor = 1; tileArray = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true); normalArray = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true, true); for (int i = 0; i < coordList.Count; i++) { var coord = coordList[i]; var tileSource = originalPage.GetPixels(coord.x * tileWidth, (pageHeight - coord.y - 1) * tileHeight, tileWidth, tileHeight); var tileSource32 = new Color32[tileSource.Length]; for (int j = 0; j < tileSource.Length; j++) { tileSource32[j] = tileSource[j]; } var tileDest32 = new Color32[tileWidth * scaleFactor * tileHeight * scaleFactor]; switch (scaleFactor) { case 4: HqxSharp.Scale4(tileSource32, tileDest32, tileWidth, tileHeight); break; case 3: HqxSharp.Scale3(tileSource32, tileDest32, tileWidth, tileHeight); break; case 2: HqxSharp.Scale2(tileSource32, tileDest32, tileWidth, tileHeight); break; default: tileDest32 = tileSource32; break; } Texture2D texture = new Texture2D(tileWidth * scaleFactor, tileHeight * scaleFactor, TextureFormat.ARGB32, false); texture.SetPixels32(tileDest32); TextureScale.Bilinear(texture, Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor)); tileArray.SetPixels(texture.GetPixels(), i); normalArray.SetPixels(TextureTools.Bevel(texture.GetPixels(), texture.width, texture.height), i); if (stopWatch.ElapsedMilliseconds > ContentLoader.LoadFrameTimeout) { yield return(null); stopWatch.Reset(); stopWatch.Start(); } } tileArray.Apply(); normalArray.Apply(); }
IEnumerator LoadImages() { var stopWatch = System.Diagnostics.Stopwatch.StartNew(); List <Texture2D> textureList = new List <Texture2D>(); //DFTiles: { int sourceWidth = dfSpriteMap.width / 16; int sourceHeight = dfSpriteMap.height / 16; for (int y = 16 - 1; y >= 0; y--) { for (int x = 0; x < 16; x++) { var pixels = dfSpriteMap.GetPixels(sourceWidth * x, sourceHeight * y, sourceWidth, sourceHeight); var tempTex = new Texture2D(sourceWidth, sourceHeight, TextureFormat.ARGB32, false); tempTex.SetPixels(pixels); tempTex.Apply(); if (tempTex.width != 32 || tempTex.height != 32) { TextureScale.Bilinear(tempTex, 32, 32); } textureList.Add(tempTex); } } } //IMAGE_CREATURE: foreach (var creature in DFConnection.Instance.CreatureRaws) { string token = creature.creature_id; Texture2D sprite = Resources.Load <Texture2D>("Images/Creatures/" + token); if (sprite == null) { //Try again without stupid numbers token.TrimEnd('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '_'); sprite = Resources.Load <Texture2D>("Images/Creatures/" + token); } if (sprite == null) { Debug.LogWarning("Could not find art image for " + token); continue; } if (sprite.width != 32 || sprite.height != 32) { TextureScale.Bilinear(sprite, 32, 32); } CreatureSpriteMap[creature.index] = textureList.Count; textureList.Add(sprite); if (stopWatch.ElapsedMilliseconds > ContentLoader.LoadFrameTimeout) { yield return(null); stopWatch.Reset(); stopWatch.Start(); } } //IMAGE_PLANT: //IMAGE_TREE: //IMAGE_SHAPE: //IMAGE_ITEM: foreach (var item in DFConnection.Instance.NetItemList.material_list) { string token = item.id; Texture2D sprite = Resources.Load <Texture2D>("Images/Items/" + token); if (sprite == null) { //Try again without stupid numbers token.TrimEnd('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '_'); sprite = Resources.Load <Texture2D>("Images/Items/" + token); } if (sprite == null) { Debug.LogWarning("Could not find art image for " + token); continue; } if (sprite.width != 32 || sprite.height != 32) { TextureScale.Bilinear(sprite, 32, 32); } ItemSpriteMap[item.mat_pair] = textureList.Count; if (item.mat_pair.mat_type == 53) //Plant { PlantSpriteMap[item.mat_pair.mat_index] = textureList.Count; } textureList.Add(sprite); if (stopWatch.ElapsedMilliseconds > ContentLoader.LoadFrameTimeout) { yield return(null); stopWatch.Reset(); stopWatch.Start(); } } imageSpriteArray = new Texture2DArray(32, 32, textureList.Count, TextureFormat.ARGB32, false); imageSpriteNormals = new Texture2DArray(32, 32, textureList.Count, TextureFormat.ARGB32, false); for (int i = 0; i < textureList.Count; i++) { var pixels = textureList[i].GetPixels(); imageSpriteArray.SetPixels(pixels, i); imageSpriteNormals.SetPixels(TextureTools.Bevel(pixels, 32, 32), i); } imageSpriteArray.Apply(); imageSpriteNormals.Apply(); Shader.SetGlobalTexture("_ImageAtlas", imageSpriteArray); Shader.SetGlobalTexture("_ImageBumpAtlas", imageSpriteNormals); }
private void OnGUI() { scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); GUILayout.Label("Texure array maker", EditorStyles.boldLabel); baseTexture = (Texture2D)EditorGUILayout.ObjectField("Base texture image", baseTexture, typeof(Texture2D), false); tiles_x = EditorGUILayout.IntField("Horizontal Tiles", tiles_x); tiles_y = EditorGUILayout.IntField("Vertical Tiles", tiles_y); if (tiles_x <= 0) { tiles_x = 1; } if (tiles_y <= 0) { tiles_y = 1; } mipmaps = EditorGUILayout.Toggle("Enable Mipmaps", mipmaps); skipEmpty = EditorGUILayout.Toggle("Skip Empty", skipEmpty); scaleMode = (ScaleMode)EditorGUILayout.EnumPopup("Scale mode", scaleMode); int scale = 1; switch (scaleMode) { case ScaleMode.None: scale = 1; break; case ScaleMode.HQ2x: scale = 2; break; case ScaleMode.HQ3x: scale = 3; break; case ScaleMode.HQ4x: scale = 4; break; default: break; } if (baseTexture != null) { int sourceWidth = baseTexture.width / tiles_x; int sourceHeight = baseTexture.height / tiles_y; GUILayout.Label("Source Width: " + sourceWidth); GUILayout.Label("Source Height: " + sourceHeight); int scaledWidth = sourceWidth * scale; int scaledHeight = sourceHeight * scale; GUILayout.Label("Scaled Width: " + scaledWidth); GUILayout.Label("Scaled Height: " + scaledHeight); int potWidth = Mathf.ClosestPowerOfTwo(scaledWidth); int potHeight = Mathf.ClosestPowerOfTwo(scaledHeight); GUILayout.Label("Final Width: " + potWidth); GUILayout.Label("Final Height: " + potHeight); if (GUILayout.Button("Build Array")) { var tempList = new List <Texture2D>(); for (int y = tiles_y - 1; y >= 0; y--) { for (int x = 0; x < tiles_x; x++) { var pixels = baseTexture.GetPixels(sourceWidth * x, sourceHeight * y, sourceWidth, sourceHeight); if (skipEmpty) { float totalAlpha = 0; foreach (var pixel in pixels) { totalAlpha += pixel.a; } if (totalAlpha < 1) //it's empty, bro. { continue; } } var tempTex = new Texture2D(sourceWidth, sourceHeight, TextureFormat.ARGB32, false); tempTex.SetPixels(pixels); switch (scaleMode) { case ScaleMode.HQ2x: HqxSharp.Scale2(tempTex); break; case ScaleMode.HQ3x: HqxSharp.Scale3(tempTex); break; case ScaleMode.HQ4x: HqxSharp.Scale4(tempTex); break; default: break; } TextureScale.Bilinear(tempTex, potWidth, potHeight); tempList.Add(tempTex); } } texArray = new Texture2DArray(potWidth, potHeight, tempList.Count, TextureFormat.ARGB32, mipmaps); for (int i = 0; i < tempList.Count; i++) { texArray.SetPixels(tempList[i].GetPixels(), i); } texArray.Apply(); texArray.name = baseTexture.name + "Array"; tempList.Clear(); Resources.UnloadUnusedAssets(); } } texArray = (Texture2DArray)EditorGUILayout.ObjectField("Texture Array", texArray, typeof(Texture2DArray), true); if (texArray != null) { previewIndex = EditorGUILayout.IntSlider(previewIndex, 0, texArray.depth - 1); if (previewTexture == null) { previewTexture = new Texture2D(texArray.width, texArray.height, TextureFormat.ARGB32, false); } if (previewTexture.width != texArray.width || previewTexture.height != texArray.height) { previewTexture.Resize(texArray.width, texArray.height); } var previewPixels = texArray.GetPixels(previewIndex); for (int i = 0; i < previewPixels.Length; i++) { previewPixels[i] = Color.Lerp(Color.magenta, previewPixels[i], previewPixels[i].a); } previewTexture.SetPixels(previewPixels); previewTexture.Apply(); EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(false, previewTexture.height), previewTexture, null, UnityEngine.ScaleMode.ScaleToFit); if (GUILayout.Button("Save Texture Array")) { var path = EditorUtility.SaveFilePanelInProject("Save texture array to asset", texArray.name + ".asset", "asset", "Please select a filename to save the texture atlas to."); AssetDatabase.CreateAsset(texArray, path); AssetDatabase.Refresh(); } if (GUILayout.Button("Generate Bump")) { normalArray = new Texture2DArray(texArray.width, texArray.height, texArray.depth, TextureFormat.ARGB32, true, true); for (int i = 0; i < normalArray.depth; i++) { normalArray.SetPixels(TextureTools.Bevel(texArray.GetPixels(i), texArray.width, texArray.height), i); } normalArray.Apply(); } } if (normalArray != null) { previewIndex = EditorGUILayout.IntSlider(previewIndex, 0, normalArray.depth - 1); if (normalPreviewTexture == null) { normalPreviewTexture = new Texture2D(normalArray.width, normalArray.height, TextureFormat.ARGB32, false); } if (normalPreviewTexture.width != normalArray.width || normalPreviewTexture.height != normalArray.height) { normalPreviewTexture.Resize(normalArray.width, normalArray.height); } var previewPixels = normalArray.GetPixels(previewIndex); for (int i = 0; i < previewPixels.Length; i++) { Color color = previewPixels[i]; previewPixels[i] = new Color(color.a, color.g, Mathf.Sqrt(1 - (color.a * 2 - 1) * (color.a * 2 - 1) - (color.g * 2 - 1) * (color.g * 2 - 1))); } normalPreviewTexture.SetPixels(previewPixels); normalPreviewTexture.Apply(); EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(false, normalPreviewTexture.height), normalPreviewTexture, null, UnityEngine.ScaleMode.ScaleToFit); if (GUILayout.Button("Save Texture Array")) { var path = EditorUtility.SaveFilePanelInProject("Save texture array to asset", normalArray.name + ".asset", "asset", "Please select a filename to save the texture atlas to."); AssetDatabase.CreateAsset(normalArray, path); AssetDatabase.Refresh(); } } EditorGUILayout.EndScrollView(); }
public void FinalizeTextures() { if (coordList.Count == 0) // There's nothing that uses this. { Debug.LogWarningFormat("Tile page \"{0}\" has no sprites used!", pageName); return; } int scaleFactor = 1; if (tileWidth * 4 <= GameSettings.Instance.rendering.maxTextureSize && tileHeight * 4 <= GameSettings.Instance.rendering.maxTextureSize) { scaleFactor = 4; } else if (tileWidth * 3 <= GameSettings.Instance.rendering.maxTextureSize && tileHeight * 3 <= GameSettings.Instance.rendering.maxTextureSize) { scaleFactor = 3; } else if (tileWidth * 2 <= GameSettings.Instance.rendering.maxTextureSize && tileHeight * 2 <= GameSettings.Instance.rendering.maxTextureSize) { scaleFactor = 2; } tileArray = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true); normalArray = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true, true); for (int i = 0; i < coordList.Count; i++) { var coord = coordList[i]; var tileSource = originalPage.GetPixels(coord.x * tileWidth, (pageHeight - coord.y - 1) * tileHeight, tileWidth, tileHeight); var tileSource32 = new Color32[tileSource.Length]; for (int j = 0; j < tileSource.Length; j++) { tileSource32[j] = tileSource[j]; } var tileDest32 = new Color32[tileWidth * scaleFactor * tileHeight * scaleFactor]; switch (scaleFactor) { case 4: HqxSharp.Scale4(tileSource32, tileDest32, tileWidth, tileHeight); break; case 3: HqxSharp.Scale3(tileSource32, tileDest32, tileWidth, tileHeight); break; case 2: HqxSharp.Scale2(tileSource32, tileDest32, tileWidth, tileHeight); break; default: tileDest32 = tileSource32; break; } Texture2D texture = new Texture2D(tileWidth * scaleFactor, tileHeight * scaleFactor, TextureFormat.ARGB32, false); texture.SetPixels32(tileDest32); TextureScale.Bilinear(texture, Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor)); tileArray.SetPixels(texture.GetPixels(), i); normalArray.SetPixels(TextureTools.Bevel(texture.GetPixels(), texture.width, texture.height), i); } tileArray.Apply(); normalArray.Apply(); }