public static Vector2 VoronoiUnit(Vector2 _v) { float sqrDstToCell = float.MaxValue; Vector2 baseCell = Swizzling.Floor(_v); Vector2 closetCell = baseCell; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { Vector2 cell = baseCell + new Vector2(i, j); Vector2 cellPos = cell + VelueUnitVec2(cell); Vector2 toCell = cellPos - _v; float sqrDistance = toCell.sqrMagnitude; if (sqrDstToCell < sqrDistance) { continue; } sqrDstToCell = sqrDistance; closetCell = cell; } } return(new Vector2(sqrDstToCell, ValueUnit(closetCell))); }
public static float SimplexUnit(Vector2 _v) { Vector2 i = Swizzling.Floor(_v + Vector2.Dot(_v, s_Simplex_C.y.ToVector2()).ToVector2()); Vector2 x0 = _v - i + Vector2.Dot(i, s_Simplex_C.x.ToVector2()).ToVector2(); Vector2 i1 = x0.x > x0.y ? new Vector2(1, 0) : new Vector2(0, 1); Vector4 x12 = new Vector4(x0.x, x0.y, x0.x, x0.y) + new Vector4(s_Simplex_C.x, s_Simplex_C.x, s_Simplex_C.z, s_Simplex_C.z); x12 -= i1.ToVector4(); i = Mod289(i); Vector3 p = Permute(Permute(new Vector3(i.y, i.y + i1.y, i.y + 1)) + new Vector3(i.x, i.x + i1.x, i.x + 1)); Vector2 x12xy = new Vector2(x12.x, x12.y); Vector2 x12zw = new Vector2(x12.z, x12.w); Vector3 m = Vector3.Max(0.5f.ToVector3() - new Vector3(Vector2.Dot(x0, x0), Vector2.Dot(x12xy, x12xy), Vector2.Dot(x12zw, x12zw)), Vector3.zero); m = m.Multiply(m); m = m.Multiply(m); Vector3 x = 2.0f * Swizzling.Frac(p * s_Simplex_C.w) - 1.0f.ToVector3(); Vector3 h = Swizzling.Abs(x) - 0.5f.ToVector3(); Vector3 ox = Swizzling.Floor(x + 0.5f.ToVector3()); Vector3 a0 = x - ox; m = m.Multiply(s_Simplex_M1 - s_Simplex_M2 * (a0.Multiply(a0) + h.Multiply(h))); float gx = a0.x * x0.x + h.x * x0.y; Vector2 gyz = new Vector2(a0.y, a0.z) * new Vector2(x12.x, x12.z) + new Vector2(h.y, h.z) * new Vector2(x12.y, x12.w); return(130f * Vector3.Dot(m, new Vector3(gx, gyz.x, gyz.y))); }
static void DecodeImage(string basename) { var Lzx = new LzxDecoder(16); var Stream = new MemoryStream(File.ReadAllBytes(basename)); var Reader = new BinaryReader(Stream); Stream.Position = 6; var OutStream = DecompressLzx(Stream); Console.WriteLine(OutStream.Length); File.WriteAllBytes(basename + ".u", OutStream.ToArray()); #if true //OutStream.Position = 0x30A; //var Bitmap = DXT5.LoadSwizzled(OutStream, 0x40, 0x80, Swizzled: true); //Bitmap.Save(basename + ".u.png"); OutStream.Position = 0x37; var Reader2 = new BinaryReader(OutStream); var Format = Reader2.ReadInt32(); var Width = Reader2.ReadInt32(); var Height = Reader2.ReadInt32(); var Levels = Reader2.ReadInt32(); for (int Level = 0; Level < Levels; Level++) { OutStream.Position = 0x4B; //var Bitmap = new Bitmap(256, 512); //var Bitmap = new Bitmap(1024, 128); //var Bitmap = new Bitmap(512, 256); //var Bitmap = new Bitmap(1024, 1024); //var Bitmap = new Bitmap(128, 128); var Bitmap = new Bitmap(Width, Height); //var Bitmap = new Bitmap(64, 64); //var BitmapData = OutStream.ReadStructVector<RGBA>((uint)(Bitmap.Width * Bitmap.Height)); //var BitmapData = OutStream.ReadBytes(Bitmap.Width * Bitmap.Height * 4); int Size = Bitmap.Width * Bitmap.Height; for (int n = 0; n < Size; n++) { int x, y; Swizzling.UnswizzledXY(n, Bitmap.Width, 4, out x, out y); var RGBA = OutStream.ReadStruct <RGBA>(); Bitmap.SetPixel(x, y, Color.FromArgb(0xFF, RGBA.R, RGBA.G, RGBA.B)); } //var Bitmap = DXT5.LoadSwizzled(OutStream, 0x40, 0x80, Swizzled: true); //Bitmap.SetChannelsDataLinear(BitmapData, BitmapChannel.Red, BitmapChannel.Green, BitmapChannel.Blue, BitmapChannel.Alpha); //Bitmap.SetChannelsDataLinear(BitmapData, BitmapChannel.Alpha, BitmapChannel.Green, BitmapChannel.Blue, BitmapChannel.Red); Bitmap.Save(basename + ".u." + Level + ".png"); } #endif }