Magically import images and more into Gamecraft as blocks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.6KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. using GamecraftModdingAPI;
  7. using GamecraftModdingAPI.Blocks;
  8. using GamecraftModdingAPI.Players;
  9. using GamecraftModdingAPI.Utility;
  10. using Pixi.Common;
  11. namespace Pixi.Images
  12. {
  13. public class ImageCanvasImporter : Importer
  14. {
  15. public static float3 Rotation = float3.zero;
  16. public static uint Thiccness = 1;
  17. public int Priority { get; } = 1;
  18. public bool Optimisable { get; } = true;
  19. public string Name { get; } = "ImageCanvas~Spell";
  20. public BlueprintProvider BlueprintProvider { get; } = null;
  21. public ImageCanvasImporter()
  22. {
  23. GamecraftModdingAPI.App.Client.EnterMenu += ColorSpaceUtility.LoadColorMenuEvent;
  24. }
  25. public bool Qualifies(string name)
  26. {
  27. //Logging.MetaLog($"Qualifies received name {name}");
  28. return name.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)
  29. || name.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase);
  30. }
  31. public BlockJsonInfo[] Import(string name)
  32. {
  33. // Load image file and convert to Gamecraft blocks
  34. Texture2D img = new Texture2D(64, 64);
  35. // load file into texture
  36. try
  37. {
  38. byte[] imgData = File.ReadAllBytes(name);
  39. img.LoadImage(imgData);
  40. }
  41. catch (Exception e)
  42. {
  43. Logging.CommandLogError($"Failed to load picture data. Reason: {e.Message}");
  44. Logging.MetaLog(e.Message + "\n" + e.StackTrace);
  45. return new BlockJsonInfo[0];
  46. }
  47. //Logging.CommandLog($"Image size: {img.width}x{img.height}");
  48. Player p = new Player(PlayerType.Local);
  49. string pickedBlock = p.SelectedBlock == BlockIDs.Invalid ? BlockIDs.AluminiumCube.ToString() : p.SelectedBlock.ToString();
  50. Quaternion imgRotation = Quaternion.Euler(Rotation);
  51. BlockJsonInfo[] blocks = new BlockJsonInfo[img.width * img.height];
  52. // convert the image to blocks
  53. // optimisation occurs later
  54. for (int x = 0; x < img.width; x++)
  55. {
  56. for (int y = 0; y < img.height; y++)
  57. {
  58. Color pixel = img.GetPixel(x, y);
  59. float3 position = (imgRotation * (new float3((x * CommandRoot.BLOCK_SIZE),y * CommandRoot.BLOCK_SIZE,0)));
  60. BlockJsonInfo qPixel = new BlockJsonInfo
  61. {
  62. name = pixel.a > 0.75 ? pickedBlock : BlockIDs.GlassCube.ToString(),
  63. color = new float[] {pixel.r, pixel.g, pixel.b},
  64. rotation = ConversionUtility.Float3ToFloatArray(Rotation),
  65. position = ConversionUtility.Float3ToFloatArray(position),
  66. scale = new float[] { 1, 1, Thiccness},
  67. };
  68. if (pixel.a < 0.5f) qPixel.name = BlockIDs.Invalid.ToString();
  69. blocks[(x * img.height) + y] = qPixel;
  70. }
  71. }
  72. return blocks;
  73. }
  74. public void PreProcess(string name, ref ProcessedVoxelObjectNotation[] blocks)
  75. {
  76. Player p = new Player(PlayerType.Local);
  77. float3 pos = p.Position;
  78. for (int i = 0; i < blocks.Length; i++)
  79. {
  80. blocks[i].position += pos;
  81. }
  82. }
  83. public void PostProcess(string name, ref Block[] blocks) { }
  84. }
  85. }