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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

94 lines
3.4KB

  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 bool Qualifies(string name)
  22. {
  23. //Logging.MetaLog($"Qualifies received name {name}");
  24. return name.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)
  25. || name.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase);
  26. }
  27. public BlockJsonInfo[] Import(string name)
  28. {
  29. // Load image file and convert to Gamecraft blocks
  30. Texture2D img = new Texture2D(64, 64);
  31. // load file into texture
  32. try
  33. {
  34. byte[] imgData = File.ReadAllBytes(name);
  35. img.LoadImage(imgData);
  36. }
  37. catch (Exception e)
  38. {
  39. Logging.CommandLogError($"Failed to load picture data. Reason: {e.Message}");
  40. Logging.MetaLog(e.Message + "\n" + e.StackTrace);
  41. return new BlockJsonInfo[0];
  42. }
  43. //Logging.CommandLog($"Image size: {img.width}x{img.height}");
  44. Player p = new Player(PlayerType.Local);
  45. string pickedBlock = p.SelectedBlock == BlockIDs.Invalid ? BlockIDs.AluminiumCube.ToString() : p.SelectedBlock.ToString();
  46. Quaternion imgRotation = Quaternion.Euler(Rotation);
  47. BlockJsonInfo[] blocks = new BlockJsonInfo[img.width * img.height];
  48. // convert the image to blocks
  49. // optimisation occurs later
  50. for (int x = 0; x < img.width; x++)
  51. {
  52. for (int y = 0; y < img.height; y++)
  53. {
  54. Color pixel = img.GetPixel(x, y);
  55. float3 position = (imgRotation * (new float3((x * CommandRoot.BLOCK_SIZE),y * CommandRoot.BLOCK_SIZE,0)));
  56. BlockJsonInfo qPixel = new BlockJsonInfo
  57. {
  58. name = pixel.a > 0.75 ? pickedBlock : BlockIDs.GlassCube.ToString(),
  59. color = new float[] {pixel.r, pixel.g, pixel.b},
  60. rotation = ConversionUtility.Float3ToFloatArray(Rotation),
  61. position = ConversionUtility.Float3ToFloatArray(position),
  62. scale = new float[] { 1, 1, Thiccness},
  63. };
  64. if (pixel.a < 0.5f) qPixel.name = BlockIDs.Invalid.ToString();
  65. blocks[(x * img.height) + y] = qPixel;
  66. }
  67. }
  68. return blocks;
  69. }
  70. public void PreProcess(string name, ref ProcessedVoxelObjectNotation[] blocks)
  71. {
  72. Player p = new Player(PlayerType.Local);
  73. float3 pos = p.Position;
  74. for (int i = 0; i < blocks.Length; i++)
  75. {
  76. blocks[i].position += pos;
  77. }
  78. }
  79. public void PostProcess(string name, ref Block[] blocks) { }
  80. }
  81. }