VoxelContent.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #region License
  2. // Copyright 2020 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using Microsoft.Xna.Framework;
  21. namespace nkast.Aether.Content.Pipeline
  22. {
  23. public class VoxelContent
  24. {
  25. internal Vector3 GridSize = Vector3.Zero;
  26. internal Vector3 RealSize = Vector3.One;
  27. internal XYZI[] Voxels;
  28. internal uint[] Palette;
  29. internal void MarkSharedSides()
  30. {
  31. HashSet<Point3> blocks = new HashSet<Point3>(Voxels.Select<XYZI, Point3>((v) => { return v.Point; }));
  32. //for(int i = 0; i< Voxels.Length;i++)
  33. System.Threading.Tasks.Parallel.For(0, Voxels.Length, (i) =>
  34. {
  35. Point3 vpt = Voxels[i].Point;
  36. if (blocks.Contains(new Point3(vpt.X - 1, vpt.Y, vpt.Z)))
  37. Voxels[i].SharedSides |= Sides.Left;
  38. if (blocks.Contains(new Point3(vpt.X + 1, vpt.Y, vpt.Z)))
  39. Voxels[i].SharedSides |= Sides.Right;
  40. if (blocks.Contains(new Point3(vpt.X, vpt.Y - 1, vpt.Z)))
  41. Voxels[i].SharedSides |= Sides.Down;
  42. if (blocks.Contains(new Point3(vpt.X, vpt.Y + 1, vpt.Z)))
  43. Voxels[i].SharedSides |= Sides.Up;
  44. if (blocks.Contains(new Point3(vpt.X, vpt.Y, vpt.Z + 1)))
  45. Voxels[i].SharedSides |= Sides.Backward;
  46. if (blocks.Contains(new Point3(vpt.X, vpt.Y, vpt.Z - 1)))
  47. Voxels[i].SharedSides |= Sides.Forward;
  48. //}
  49. });
  50. return;
  51. }
  52. internal void RemoveHiddenBlocks()
  53. {
  54. IEnumerable<XYZI> visibleBlocks2 = Voxels.Where((v) => { return v.SharedSides != Sides.All; });
  55. Voxels = visibleBlocks2.ToArray();
  56. //HashSet<Point3> blocks = new HashSet<Point3>( Voxels.Select<XYZI, Point3>((v) => { return v.Point; }) );
  57. //HashSet<Point3> insideBlocks = new HashSet<Point3>();
  58. //for (int z = 1; z < GridSize.Z - 1; z++)
  59. //{
  60. // for (int y = 1; y < GridSize.Y - 1; y++)
  61. // {
  62. // for (int x = 0; x < GridSize.X - 1; x++)
  63. // {
  64. // if (blocks.Contains(new Point3(x-1, y, z)) &&
  65. // blocks.Contains(new Point3(x+1, y, z)) &&
  66. // blocks.Contains(new Point3(x, y-1, z)) &&
  67. // blocks.Contains(new Point3(x, y+1, z)) &&
  68. // blocks.Contains(new Point3(x, y, z-1)) &&
  69. // blocks.Contains(new Point3(x, y, z+1))
  70. // )
  71. // {
  72. // insideBlocks.Add(new Point3(x, y, z));
  73. // }
  74. // }
  75. // }
  76. //}
  77. //IEnumerable<XYZI> visibleBlocks = Voxels.Where((v) => { return !insideBlocks.Contains(v.Point); });
  78. //Voxels = visibleBlocks.ToArray();
  79. return;
  80. }
  81. }
  82. internal struct Point3
  83. {
  84. public readonly short X, Y, Z;
  85. public Point3(byte x, byte y, byte z)
  86. {
  87. X = x;
  88. Y = y;
  89. Z = z;
  90. }
  91. public Point3(int x, int y, int z)
  92. {
  93. X = (byte)x;
  94. Y = (byte)y;
  95. Z = (byte)z;
  96. }
  97. public override int GetHashCode()
  98. {
  99. return ((X) ^ (Y*256) ^ (Z*(256*256)));
  100. }
  101. internal Point3 Add(ref Point3 right)
  102. {
  103. return new Point3(
  104. (short)(X + right.X),
  105. (short)(Y + right.Y),
  106. (short)(Z + right.Z)
  107. );
  108. }
  109. internal Vector3 ToVector3()
  110. {
  111. Vector3 result;
  112. result.X = X;
  113. result.Y = Y;
  114. result.Z = Z;
  115. return result;
  116. }
  117. }
  118. internal enum Sides
  119. {
  120. None = 0,
  121. Right = 1,
  122. Left = 2,
  123. Up = 4,
  124. Down = 8,
  125. Forward = 16,
  126. Backward = 32,
  127. All = 63
  128. }
  129. }