public void NormalizedByte4() { Assert.AreEqual(0x0, new NormalizedByte4(Vector4.Zero).PackedValue); Assert.AreEqual(0x7F7F7F7F, new NormalizedByte4(Vector4.One).PackedValue); Assert.AreEqual(0x81818181, new NormalizedByte4(-Vector4.One).PackedValue); Assert.AreEqual(Vector4.One, new NormalizedByte4(Vector4.One).ToVector4()); Assert.AreEqual(Vector4.Zero, new NormalizedByte4(Vector4.Zero).ToVector4()); Assert.AreEqual(-Vector4.One, new NormalizedByte4(-Vector4.One).ToVector4()); Assert.AreEqual(Vector4.One, new NormalizedByte4(Vector4.One * 1234.0f).ToVector4()); Assert.AreEqual(-Vector4.One, new NormalizedByte4(Vector4.One * -1234.0f).ToVector4()); //Test Ordering float x = 0.1f; float y = -0.3f; float z = 0.5f; float w = -0.7f; Assert.AreEqual(0xA740DA0D, new NormalizedByte4(x, y, z, w).PackedValue); Assert.AreEqual(958796544, new NormalizedByte4(0.0008f, 0.15f, 0.30f, 0.45f).PackedValue); var packed = new NormalizedByte4(x, y, z, w).PackedValue; var unpacked = new NormalizedByte4() { PackedValue = packed }.ToVector4(); Assert.AreEqual(x, unpacked.X, 0.01f); Assert.AreEqual(y, unpacked.Y, 0.01f); Assert.AreEqual(z, unpacked.Z, 0.01f); Assert.AreEqual(w, unpacked.W, 0.01f); }
private void GenerateGradTexture4d() { if (gradTexture4d != null) { gradTexture4d.Dispose(); } //NormalizedByte4 gradTexture4d = new Texture(MyMinerGame.Static.GraphicsDevice, 32, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default); NormalizedByte4[] data = new NormalizedByte4[32 * 1]; for (int x = 0; x < 32; x++) { for (int y = 0; y < 1; y++) { data[x + (y * 32)] = new NormalizedByte4(g4[x, 0], g4[x, 1], g4[x, 2], g4[x, 3]); //data[31 - x + (y * 32)] = new NormalizedByte4(0, 0, 0, 0); } } SharpDX.DataStream ds; DataRectangle dr = gradTexture4d.LockRectangle(0, LockFlags.None, out ds); ds.WriteRange(data); gradTexture4d.UnlockRectangle(0); }
/// <summary> /// Gets the optimized gradient lookup texture for 3D Perlin noise. /// </summary> /// <param name="graphicsService">The graphics service.</param> /// <returns>The gradient lookup texture.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="graphicsService"/> is <see langword="null"/>. /// </exception> public static Texture2D GetGradient3DTexture(IGraphicsService graphicsService) { if (graphicsService == null) { throw new ArgumentNullException("graphicsService"); } var textures = GetNoiseTextures(graphicsService); if (textures.Gradient3DTexture == null || textures.Gradient3DTexture.IsDisposed) { var permutation = PerlinNoise.Permutation; // The Improved Perlin Noise permutation table. // ----- Create texture. // The texture contains not only the gradients. It contains some precomputations // for optimized noise too (the Permutation lookup). textures.Gradient3DTexture = new Texture2D(graphicsService.GraphicsDevice, 256, 1, false, SurfaceFormat.NormalizedByte4); NormalizedByte4[] data = new NormalizedByte4[256]; for (int i = 0; i < 256; i++) { int p = permutation[i] % 16; data[i] = new NormalizedByte4(Gradients3D[p * 3 + 0], Gradients3D[p * 3 + 1], Gradients3D[p * 3 + 2], 0); } textures.Gradient3DTexture.SetData(data); } return(textures.Gradient3DTexture); }
/// <summary> /// Gets the gradient lookup texture for 4D Perlin noise. /// </summary> /// <param name="graphicsService">The graphics service.</param> /// <returns>The gradient lookup texture.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="graphicsService"/> is <see langword="null"/>. /// </exception> public static Texture2D GetGradient4DTexture(IGraphicsService graphicsService) { // TODO: Not tested yet. if (graphicsService == null) { throw new ArgumentNullException("graphicsService"); } var textures = GetNoiseTextures(graphicsService); if (textures.Gradient4DTexture == null || textures.Gradient4DTexture.IsDisposed) { // Create texture. textures.Gradient4DTexture = new Texture2D(graphicsService.GraphicsDevice, 32, 1, false, SurfaceFormat.NormalizedByte4); NormalizedByte4[] data = new NormalizedByte4[32]; for (int i = 0; i < 32; i++) { data[i] = new NormalizedByte4(Gradients4D[i * 4 + 0], Gradients4D[i * 4 + 1], Gradients4D[i * 4 + 2], Gradients4D[i * 4 + 3]); } textures.Gradient4DTexture.SetData(data); } return(textures.Gradient4DTexture); }
public void NormalizedByte4() { // Test PackedValue Assert.Equal((uint)0x0, new NormalizedByte4(Vector4.Zero).PackedValue); Assert.Equal((uint)0x7F7F7F7F, new NormalizedByte4(Vector4.One).PackedValue); Assert.Equal(0x81818181, new NormalizedByte4(-Vector4.One).PackedValue); // Test ToVector4 Assert.True(Equal(Vector4.One, new NormalizedByte4(Vector4.One).ToVector4())); Assert.True(Equal(Vector4.Zero, new NormalizedByte4(Vector4.Zero).ToVector4())); Assert.True(Equal(-Vector4.One, new NormalizedByte4(-Vector4.One).ToVector4())); Assert.True(Equal(Vector4.One, new NormalizedByte4(Vector4.One * 1234.0f).ToVector4())); Assert.True(Equal(-Vector4.One, new NormalizedByte4(Vector4.One * -1234.0f).ToVector4())); // Test Ordering float x = 0.1f; float y = -0.3f; float z = 0.5f; float w = -0.7f; Assert.Equal(0xA740DA0D, new NormalizedByte4(x, y, z, w).PackedValue); var n = new NormalizedByte4(); n.PackFromRgba32(new Rgba32(141, 90, 192, 39)); Assert.Equal(0xA740DA0D, n.PackedValue); Assert.Equal((uint)958796544, new NormalizedByte4(0.0008f, 0.15f, 0.30f, 0.45f).PackedValue); var rgb = default(Rgb24); var rgba = default(Rgba32); var bgr = default(Bgr24); var bgra = default(Bgra32); new NormalizedByte4(x, y, z, w).ToRgb24(ref rgb); Assert.Equal(rgb, new Rgb24(141, 90, 192)); new NormalizedByte4(x, y, z, w).ToRgba32(ref rgba); Assert.Equal(rgba, new Rgba32(141, 90, 192, 39)); new NormalizedByte4(x, y, z, w).ToBgr24(ref bgr); Assert.Equal(bgr, new Bgr24(141, 90, 192)); new NormalizedByte4(x, y, z, w).ToBgra32(ref bgra); Assert.Equal(bgra, new Bgra32(141, 90, 192, 39)); // http://community.monogame.net/t/normalizedbyte4-texture2d-gives-different-results-from-xna/8012/8 var r = new NormalizedByte4(); r.PackFromRgba32(new Rgba32(9, 115, 202, 127)); r.ToRgba32(ref rgba); Assert.Equal(rgba, new Rgba32(9, 115, 202, 127)); r.PackedValue = 0xff4af389; r.ToRgba32(ref rgba); Assert.Equal(rgba, new Rgba32(9, 115, 202, 127)); }
public void NormalizedByte4() { // Test PackedValue Assert.Equal((uint)0x0, new NormalizedByte4(Vector4.Zero).PackedValue); Assert.Equal((uint)0x7F7F7F7F, new NormalizedByte4(Vector4.One).PackedValue); Assert.Equal(0x81818181, new NormalizedByte4(-Vector4.One).PackedValue); // Test ToVector4 Assert.True(Equal(Vector4.One, new NormalizedByte4(Vector4.One).ToVector4())); Assert.True(Equal(Vector4.Zero, new NormalizedByte4(Vector4.Zero).ToVector4())); Assert.True(Equal(-Vector4.One, new NormalizedByte4(-Vector4.One).ToVector4())); Assert.True(Equal(Vector4.One, new NormalizedByte4(Vector4.One * 1234.0f).ToVector4())); Assert.True(Equal(-Vector4.One, new NormalizedByte4(Vector4.One * -1234.0f).ToVector4())); // Test Ordering float x = 0.1f; float y = -0.3f; float z = 0.5f; float w = -0.7f; Assert.Equal(0xA740DA0D, new NormalizedByte4(x, y, z, w).PackedValue); NormalizedByte4 n = new NormalizedByte4(); n.PackFromBytes(141, 90, 192, 39); Assert.Equal(0xA740DA0D, n.PackedValue); Assert.Equal((uint)958796544, new NormalizedByte4(0.0008f, 0.15f, 0.30f, 0.45f).PackedValue); byte[] rgb = new byte[3]; byte[] rgba = new byte[4]; byte[] bgr = new byte[3]; byte[] bgra = new byte[4]; new NormalizedByte4(x, y, z, w).ToXyzBytes(rgb, 0); Assert.Equal(rgb, new byte[] { 141, 90, 192 }); new NormalizedByte4(x, y, z, w).ToXyzwBytes(rgba, 0); Assert.Equal(rgba, new byte[] { 141, 90, 192, 39 }); new NormalizedByte4(x, y, z, w).ToZyxBytes(bgr, 0); Assert.Equal(bgr, new byte[] { 192, 90, 141 }); new NormalizedByte4(x, y, z, w).ToZyxwBytes(bgra, 0); Assert.Equal(bgra, new byte[] { 192, 90, 141, 39 }); // http://community.monogame.net/t/normalizedbyte4-texture2d-gives-different-results-from-xna/8012/8 NormalizedByte4 r = new NormalizedByte4(); r.PackFromBytes(9, 115, 202, 127); r.ToXyzwBytes(rgba, 0); Assert.Equal(rgba, new byte[] { 9, 115, 202, 127 }); r.PackedValue = 0xff4af389; r.ToXyzwBytes(rgba, 0); Assert.Equal(rgba, new byte[] { 9, 115, 202, 127 }); }
public void AreNotEqual() { var color1 = new NormalizedByte4(0.0f, 0.0f, 0.0f, 0.0f); var color2 = new NormalizedByte4(new Vector4(1.0f)); var color3 = new NormalizedByte4(new Vector4(1.0f, 0.0f, 0.0f, 1.0f)); var color4 = new NormalizedByte4(1.0f, 1.0f, 0.0f, 1.0f); Assert.NotEqual(color1, color2); Assert.NotEqual(color3, color4); }
public void NormalizedByte4_ToRgba32() { // arrange var byte4 = new NormalizedByte4(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); var expected = new Rgba32(Vector4.One); var actual = default(Rgba32); // act byte4.ToRgba32(ref actual); Assert.Equal(expected, actual); }
public void NormalizedByte4_ToRgba32() { // arrange var short4 = new NormalizedByte4(0.1f, -0.3f, 0.5f, -0.7f); var actual = default(Rgba32); var expected = new Rgba32(141, 90, 192, 39); // act short4.ToRgba32(ref actual); // assert Assert.Equal(expected, actual); }
public void NormalizedByte4_ToBgr24() { // arrange var short4 = new NormalizedByte4(0.1f, -0.3f, 0.5f, -0.7f); var actual = default(Bgr24); var expected = new Bgr24(141, 90, 192); // act short4.ToBgr24(ref actual); // assert Assert.Equal(expected, actual); }
private void GeneratePermGradTexture() { permGradTexture = new Texture2D(GraphicsDevice, 256, 1, 1, TextureUsage.None, SurfaceFormat.NormalizedByte4); NormalizedByte4[] data = new NormalizedByte4[256 * 1]; for (int x = 0; x < 256; x++) { for (int y = 0; y < 1; y++) { data[x + (y * 256)] = new NormalizedByte4(g3[perm[x] % 16, 0], g3[perm[x] % 16, 1], g3[perm[x] % 16, 2], 1); } } permGradTexture.SetData <NormalizedByte4>(data); }
private void GenerateGradTexture4d() { gradTexture4d = new Texture2D(GraphicsDevice, 32, 1, 1, TextureUsage.None, SurfaceFormat.NormalizedByte4); NormalizedByte4[] data = new NormalizedByte4[32 * 1]; for (int x = 0; x < 32; x++) { for (int y = 0; y < 1; y++) { data[x + (y * 32)] = new NormalizedByte4(g4[x, 0], g4[x, 1], g4[x, 2], g4[x, 3]); } } gradTexture4d.SetData <NormalizedByte4>(data); }
public void NormalizedByte4_FromScaledVector4() { // arrange var pixel = default(NormalizedByte4); Vector4 scaled = new NormalizedByte4(-Vector4.One).ToScaledVector4(); uint expected = 0x81818181; // act pixel.FromScaledVector4(scaled); uint actual = pixel.PackedValue; // assert Assert.Equal(expected, actual); }
public void NormalizedByte4_ToScaledVector4() { // arrange var short4 = new NormalizedByte4(-Vector4.One); // act Vector4 actual = short4.ToScaledVector4(); // assert Assert.Equal(0, actual.X); Assert.Equal(0, actual.Y); Assert.Equal(0, actual.Z); Assert.Equal(0, actual.W); }
public Texture2D GeneratePermGradTexture() { Texture2D permGradTexture = new Texture2D(device, 256, 1, true, SurfaceFormat.NormalizedByte4); NormalizedByte4[] data = new NormalizedByte4[256 * 1]; for (int x = 0; x < 256; x++) { for (int y = 0; y < 1; y++) { data[x + (y * 256)] = new NormalizedByte4(gradients[permutation[x] % 16, 0], gradients[permutation[x] % 16, 1], gradients[permutation[x] % 16, 2], 1); } } permGradTexture.SetData <NormalizedByte4>(data); return(permGradTexture); }
private void GenerateGradTexture() { //NormalizedByte4 gradTexture = new Texture(MyMinerGame.Static.GraphicsDevice, 16, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default); NormalizedByte4[] data = new NormalizedByte4[16 * 1]; for (int x = 0; x < 16; x++) { for (int y = 0; y < 1; y++) { data[x + (y * 16)] = new NormalizedByte4(g3[x, 0], g3[x, 1], g3[x, 2], 1); } } SharpDX.DataStream ds; DataRectangle dr = gradTexture.LockRectangle(0, LockFlags.None, out ds); ds.WriteRange(data); gradTexture.UnlockRectangle(0); }
private void GeneratePermGrad4dTexture() { //NormalizedByte4 permGrad4dTexture = new Texture(MyMinerGame.Static.GraphicsDevice, 256, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default); NormalizedByte4[] data = new NormalizedByte4[256 * 1]; for (int x = 0; x < 256; x++) { for (int y = 0; y < 1; y++) { data[x + (y * 256)] = new NormalizedByte4(g4[perm[x] % 32, 0], g4[perm[x] % 32, 1], g4[perm[x] % 32, 2], g4[perm[x] % 32, 3]); } } SharpDX.DataStream ds; DataRectangle dr = permGrad4dTexture.LockRectangle(0, LockFlags.None, out ds); ds.WriteRange(data); permGrad4dTexture.UnlockRectangle(0); }
public CubeVertex(ref Vector3 position, ref NormalizedByte4 normal, ref Vector2 texture, ref Cube cube, ref Cube n1, ref Cube n2, ref Cube n3, ref Cube n4) { this.VertexPosition = position; this.Normal = normal; this.Texture = texture; this.Red = cube.Red; this.Green = cube.Green; this.Blue = cube.Blue; this.Alpha = (byte)cube.Alpha; int lr = 0; int lg = 0; int lb = 0; if (n1.IsTransparent) { lr = n1.Red; lg = n1.Green; lb = n1.Blue; } if (n2.IsTransparent) { lr = lr + n2.Red; lg = lg + n2.Green; lb = lb + n2.Blue; } if (n3.IsTransparent) { lr = lr + n3.Red; lg = lg + n3.Green; lb = lb + n3.Blue; } if (n4.IsTransparent) { lr = lr + n4.Red; lg = lg + n4.Green; lb = lb + n4.Blue; } this.LocalRed = (byte)(lr >> 2); this.LocalGreen = (byte)(lg >> 2); this.LocalBlue = (byte)(lb >> 2); byte light = (byte)((n1.LightLevels + n2.LightLevels + n3.LightLevels + n4.LightLevels) >> 2); this.LocalLight = (byte)((light & 240) >> 4); this.SkyLight = (short)(light & 15); }
// permuted gradient texture for optimized version private void GeneratePermGradTexture() { if (permGradTexture != null) { permGradTexture.Dispose(); } //NormalizedByte4 permGradTexture = new Texture(MyMinerGame.Static.GraphicsDevice, 256, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default); NormalizedByte4[] data = new NormalizedByte4[256 * 1]; for (int x = 0; x < 256; x++) { for (int y = 0; y < 1; y++) { //data[x + (y * 256)] = new NormalizedByte4(g3[perm[x] % 16, 0], g3[perm[x] % 16, 1], g3[perm[x] % 16, 2], 1); data[x + (y * 256)] = new NormalizedByte4(g3[perm[x] % 16, 0], g3[perm[x] % 16, 1], g3[perm[x] % 16, 2], 1); } } /* * byte[] data = new byte[256 * 1 * 4]; * for (int x = 0; x < 256; x++) * { * for (int y = 0; y < 1; y++) * { * data[(x + (y * 256)) * 4 + 0] = (byte)(255.0f * ((g3[perm[x] % 16, 0] + 1) / 2.0f)); * data[(x + (y * 256)) * 4 + 1] = (byte)(255.0f * ((g3[perm[x] % 16, 1] + 1) / 2.0f)); * data[(x + (y * 256)) * 4 + 2] = (byte)(255.0f * ((g3[perm[x] % 16, 2] + 1) / 2.0f)); * data[(x + (y * 256)) * 4 + 3] = 1; * } * } */ SharpDX.DataStream ds; DataRectangle dr = permGradTexture.LockRectangle(0, LockFlags.None, out ds); ds.WriteRange(data); permGradTexture.UnlockRectangle(0); }
private void GenerateGradTexture() { //NormalizedByte4 gradTexture = new Texture(MyRender.GraphicsDevice, 16, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default); NormalizedByte4[] data = new NormalizedByte4[16 * 1]; for (int x = 0; x < 16; x++) { for (int y = 0; y < 1; y++) { data[x + (y * 16)] = new NormalizedByte4(g3[x, 0], g3[x, 1], g3[x, 2], 1); } } SharpDX.DataStream ds; DataRectangle dr = gradTexture.LockRectangle(0, LockFlags.None, out ds); ds.WriteRange(data); gradTexture.UnlockRectangle(0); }
/// <summary> /// Gets the gradient lookup texture for 4D Perlin noise. /// </summary> /// <param name="graphicsService">The graphics service.</param> /// <returns>The gradient lookup texture.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="graphicsService"/> is <see langword="null"/>. /// </exception> public static Texture2D GetGradient4DTexture(IGraphicsService graphicsService) { // TODO: Not tested yet. if (graphicsService == null) throw new ArgumentNullException("graphicsService"); var textures = GetNoiseTextures(graphicsService); if (textures.Gradient4DTexture == null || textures.Gradient4DTexture.IsDisposed) { // Create texture. textures.Gradient4DTexture = new Texture2D(graphicsService.GraphicsDevice, 32, 1, false, SurfaceFormat.NormalizedByte4); NormalizedByte4[] data = new NormalizedByte4[32]; for (int i = 0; i < 32; i++) { data[i] = new NormalizedByte4(Gradients4D[i * 4 + 0], Gradients4D[i * 4 + 1], Gradients4D[i * 4 + 2], Gradients4D[i * 4 + 3]); } textures.Gradient4DTexture.SetData(data); } return textures.Gradient4DTexture; }
public void NormalizedByte4() { // Test PackedValue Assert.Equal(0x0U, new NormalizedByte4(Vector4.Zero).PackedValue); Assert.Equal(0x7F7F7F7FU, new NormalizedByte4(Vector4.One).PackedValue); Assert.Equal(0x81818181, new NormalizedByte4(-Vector4.One).PackedValue); // Test ToVector4 Assert.True(Equal(Vector4.One, new NormalizedByte4(Vector4.One).ToVector4())); Assert.True(Equal(Vector4.Zero, new NormalizedByte4(Vector4.Zero).ToVector4())); Assert.True(Equal(-Vector4.One, new NormalizedByte4(-Vector4.One).ToVector4())); Assert.True(Equal(Vector4.One, new NormalizedByte4(Vector4.One * 1234.0f).ToVector4())); Assert.True(Equal(-Vector4.One, new NormalizedByte4(Vector4.One * -1234.0f).ToVector4())); // Test ToScaledVector4. Vector4 scaled = new NormalizedByte4(-Vector4.One).ToScaledVector4(); Assert.Equal(0, scaled.X); Assert.Equal(0, scaled.Y); Assert.Equal(0, scaled.Z); Assert.Equal(0, scaled.W); // Test FromScaledVector4. var pixel = default(NormalizedByte4); pixel.FromScaledVector4(scaled); Assert.Equal(0x81818181, pixel.PackedValue); // Test Ordering float x = 0.1f; float y = -0.3f; float z = 0.5f; float w = -0.7f; Assert.Equal(0xA740DA0D, new NormalizedByte4(x, y, z, w).PackedValue); var n = default(NormalizedByte4); n.FromRgba32(new Rgba32(141, 90, 192, 39)); Assert.Equal(0xA740DA0D, n.PackedValue); Assert.Equal(958796544U, new NormalizedByte4(0.0008f, 0.15f, 0.30f, 0.45f).PackedValue); // var rgb = default(Rgb24); // var rgba = default(Rgba32); // var bgr = default(Bgr24); // var bgra = default(Bgra32); // var argb = default(Argb32); // new NormalizedByte4(x, y, z, w).ToRgb24(ref rgb); // Assert.Equal(rgb, new Rgb24(141, 90, 192)); // new NormalizedByte4(x, y, z, w).ToRgba32(ref rgba); // Assert.Equal(rgba, new Rgba32(141, 90, 192, 39)); // new NormalizedByte4(x, y, z, w).ToBgr24(ref bgr); // Assert.Equal(bgr, new Bgr24(141, 90, 192)); // new NormalizedByte4(x, y, z, w).ToBgra32(ref bgra); // Assert.Equal(bgra, new Bgra32(141, 90, 192, 39)); // this assert fails in Release build on linux (#594) // new NormalizedByte4(x, y, z, w).ToArgb32(ref argb); // Assert.Equal(argb, new Argb32(141, 90, 192, 39)); // http://community.monogame.net/t/normalizedbyte4-texture2d-gives-different-results-from-xna/8012/8 // var r = default(NormalizedByte4); // r.FromRgba32(new Rgba32(9, 115, 202, 127)); // r.ToRgba32(ref rgba); // Assert.Equal(rgba, new Rgba32(9, 115, 202, 127)); // r.PackedValue = 0xff4af389; // r.ToRgba32(ref rgba); // Assert.Equal(rgba, new Rgba32(9, 115, 202, 127)); // r = default(NormalizedByte4); // r.FromArgb32(new Argb32(9, 115, 202, 127)); // r.ToArgb32(ref argb); // Assert.Equal(argb, new Argb32(9, 115, 202, 127)); // r = default(NormalizedByte4); // r.FromBgra32(new Bgra32(9, 115, 202, 127)); // r.ToBgra32(ref bgra); // Assert.Equal(bgra, new Bgra32(9, 115, 202, 127)); }
private void GenerateGradTexture4d() { if (gradTexture4d != null) gradTexture4d.Dispose(); //NormalizedByte4 gradTexture4d = new Texture(MyRender.GraphicsDevice, 32, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default); NormalizedByte4[] data = new NormalizedByte4[32 * 1]; for (int x = 0; x < 32; x++) { for (int y = 0; y < 1; y++) { data[x + (y * 32)] = new NormalizedByte4(g4[x, 0], g4[x, 1], g4[x, 2], g4[x, 3]); //data[31 - x + (y * 32)] = new NormalizedByte4(0, 0, 0, 0); } } SharpDX.DataStream ds; DataRectangle dr = gradTexture4d.LockRectangle(0, LockFlags.None, out ds); ds.WriteRange(data); gradTexture4d.UnlockRectangle(0); }
private void GeneratePermGrad4dTexture() { //NormalizedByte4 permGrad4dTexture = new Texture(MyRender.GraphicsDevice, 256, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default); NormalizedByte4[] data = new NormalizedByte4[256 * 1]; for (int x = 0; x < 256; x++) { for (int y = 0; y < 1; y++) { data[x + (y * 256)] = new NormalizedByte4(g4[perm[x] % 32, 0], g4[perm[x] % 32, 1], g4[perm[x] % 32, 2], g4[perm[x] % 32, 3]); } } SharpDX.DataStream ds; DataRectangle dr = permGrad4dTexture.LockRectangle(0, LockFlags.None, out ds); ds.WriteRange(data); permGrad4dTexture.UnlockRectangle(0); }
protected void fillPerlinNoiseData() { int[] permu = new int[512]; Random rand = new Random(); // Set empty for (int i = 0; i < 256; i++) { permu[i] = -1; } // Generate random numbers for (int i = 0; i < 256; i++) { bool found = false; int r; while (!found) { r = rand.Next() % permu.Length; if (permu[r] == -1) { permu[r] = i; found = true; } } } for (int i = 0; i < 256; i++) { permu[255 + i] = permu[i]; } // Create the permutation texture permTexture = new Texture2D(device, 256, 256, true, SurfaceFormat.Color); Color[] data = new Color[256 * 256]; // HERE: AA is just partial, we will add Z in the shader for (int x = 0; x < 256; x++) { for (int y = 0; y < 256; y++) { int A = permu[x % 256] + y; int AA = permu[A % 256]; int AB = permu[(A + 1) % 256]; int B = permu[(x + 1) % 256] + y; int BA = permu[B % 256]; int BB = permu[(B + 1) % 256]; data[x + (y * 256)] = new Color((byte)(AA), (byte)(AB), (byte)(BA), (byte)(BB)); } } permTexture.SetData <Color>(data); // Allocate the gradient texture gradTexture = new Texture2D(device, 256, 1, true, SurfaceFormat.NormalizedByte4); NormalizedByte4[] gdata = new NormalizedByte4[256 * 1]; for (int x = 0; x < 256; x++) { for (int y = 0; y < 1; y++) { gdata[x + (y * 256)] = new NormalizedByte4(gradients[permu[x] % 16, 0], gradients[permu[x] % 16, 1], gradients[permu[x] % 16, 2], 1); } } gradTexture.SetData <NormalizedByte4>(gdata); }
/// <summary> /// Gets the optimized gradient lookup texture for 3D Perlin noise. /// </summary> /// <param name="graphicsService">The graphics service.</param> /// <returns>The gradient lookup texture.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="graphicsService"/> is <see langword="null"/>. /// </exception> public static Texture2D GetGradient3DTexture(IGraphicsService graphicsService) { if (graphicsService == null) throw new ArgumentNullException("graphicsService"); var textures = GetNoiseTextures(graphicsService); if (textures.Gradient3DTexture == null || textures.Gradient3DTexture.IsDisposed) { var permutation = PerlinNoise.Permutation; // The Improved Perlin Noise permutation table. // ----- Create texture. // The texture contains not only the gradients. It contains some precomputations // for optimized noise too (the Permutation lookup). textures.Gradient3DTexture = new Texture2D(graphicsService.GraphicsDevice, 256, 1, false, SurfaceFormat.NormalizedByte4); NormalizedByte4[] data = new NormalizedByte4[256]; for (int i = 0; i < 256; i++) { int p = permutation[i] % 16; data[i] = new NormalizedByte4(Gradients3D[p * 3 + 0], Gradients3D[p * 3 + 1], Gradients3D[p * 3 + 2], 0); } textures.Gradient3DTexture.SetData(data); } return textures.Gradient3DTexture; }
private void GetColorData(Texture2D texture2D) { int colorDataLength = texture2D.Width * texture2D.Height; colorData = new Color[colorDataLength]; switch (texture2D.Format) { case SurfaceFormat.Single: var floatData = new float[colorDataLength]; texture2D.GetData <float>(floatData); for (int i = 0; i < colorDataLength; i++) { float brightness = floatData[i]; // Export as a greyscale image. colorData[i] = new Color(brightness, brightness, brightness); } break; case SurfaceFormat.Color: texture2D.GetData <Color>(colorData); break; case SurfaceFormat.Alpha8: var alpha8Data = new Alpha8[colorDataLength]; texture2D.GetData <Alpha8>(alpha8Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)alpha8Data[i]).ToVector4()); } break; case SurfaceFormat.Bgr565: var bgr565Data = new Bgr565[colorDataLength]; texture2D.GetData <Bgr565>(bgr565Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)bgr565Data[i]).ToVector4()); } break; case SurfaceFormat.Bgra4444: var bgra4444Data = new Bgra4444[colorDataLength]; texture2D.GetData <Bgra4444>(bgra4444Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)bgra4444Data[i]).ToVector4()); } break; case SurfaceFormat.Bgra5551: var bgra5551Data = new Bgra5551[colorDataLength]; texture2D.GetData <Bgra5551>(bgra5551Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)bgra5551Data[i]).ToVector4()); } break; case SurfaceFormat.HalfSingle: var halfSingleData = new HalfSingle[colorDataLength]; texture2D.GetData <HalfSingle>(halfSingleData); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)halfSingleData[i]).ToVector4()); } break; case SurfaceFormat.HalfVector2: var halfVector2Data = new HalfVector2[colorDataLength]; texture2D.GetData <HalfVector2>(halfVector2Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)halfVector2Data[i]).ToVector4()); } break; case SurfaceFormat.HalfVector4: var halfVector4Data = new HalfVector4[colorDataLength]; texture2D.GetData <HalfVector4>(halfVector4Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)halfVector4Data[i]).ToVector4()); } break; case SurfaceFormat.NormalizedByte2: var normalizedByte2Data = new NormalizedByte2[colorDataLength]; texture2D.GetData <NormalizedByte2>(normalizedByte2Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)normalizedByte2Data[i]).ToVector4()); } break; case SurfaceFormat.NormalizedByte4: var normalizedByte4Data = new NormalizedByte4[colorDataLength]; texture2D.GetData <NormalizedByte4>(normalizedByte4Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)normalizedByte4Data[i]).ToVector4()); } break; case SurfaceFormat.Rg32: var rg32Data = new Rg32[colorDataLength]; texture2D.GetData <Rg32>(rg32Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)rg32Data[i]).ToVector4()); } break; case SurfaceFormat.Rgba64: var rgba64Data = new Rgba64[colorDataLength]; texture2D.GetData <Rgba64>(rgba64Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)rgba64Data[i]).ToVector4()); } break; case SurfaceFormat.Rgba1010102: var rgba1010102Data = new Rgba1010102[colorDataLength]; texture2D.GetData <Rgba1010102>(rgba1010102Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)rgba1010102Data[i]).ToVector4()); } break; default: throw new Exception("Texture surface format not supported"); } }
private void GetColorData(Texture2D texture2D) { int colorDataLength = texture2D.Width * texture2D.Height; colorData = new Color[colorDataLength]; switch (texture2D.Format) { case SurfaceFormat.Color: texture2D.GetData<Color>(colorData); break; case SurfaceFormat.Alpha8: var alpha8Data = new Alpha8[colorDataLength]; texture2D.GetData<Alpha8>(alpha8Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)alpha8Data[i]).ToVector4()); } break; case SurfaceFormat.Bgr565: var bgr565Data = new Bgr565[colorDataLength]; texture2D.GetData<Bgr565>(bgr565Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)bgr565Data[i]).ToVector4()); } break; case SurfaceFormat.Bgra4444: var bgra4444Data = new Bgra4444[colorDataLength]; texture2D.GetData<Bgra4444>(bgra4444Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)bgra4444Data[i]).ToVector4()); } break; case SurfaceFormat.Bgra5551: var bgra5551Data = new Bgra5551[colorDataLength]; texture2D.GetData<Bgra5551>(bgra5551Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)bgra5551Data[i]).ToVector4()); } break; case SurfaceFormat.HalfSingle: var halfSingleData = new HalfSingle[colorDataLength]; texture2D.GetData<HalfSingle>(halfSingleData); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)halfSingleData[i]).ToVector4()); } break; case SurfaceFormat.HalfVector2: var halfVector2Data = new HalfVector2[colorDataLength]; texture2D.GetData<HalfVector2>(halfVector2Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)halfVector2Data[i]).ToVector4()); } break; case SurfaceFormat.HalfVector4: var halfVector4Data = new HalfVector4[colorDataLength]; texture2D.GetData<HalfVector4>(halfVector4Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)halfVector4Data[i]).ToVector4()); } break; case SurfaceFormat.NormalizedByte2: var normalizedByte2Data = new NormalizedByte2[colorDataLength]; texture2D.GetData<NormalizedByte2>(normalizedByte2Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)normalizedByte2Data[i]).ToVector4()); } break; case SurfaceFormat.NormalizedByte4: var normalizedByte4Data = new NormalizedByte4[colorDataLength]; texture2D.GetData<NormalizedByte4>(normalizedByte4Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)normalizedByte4Data[i]).ToVector4()); } break; case SurfaceFormat.Rg32: var rg32Data = new Rg32[colorDataLength]; texture2D.GetData<Rg32>(rg32Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)rg32Data[i]).ToVector4()); } break; case SurfaceFormat.Rgba64: var rgba64Data = new Rgba64[colorDataLength]; texture2D.GetData<Rgba64>(rgba64Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)rgba64Data[i]).ToVector4()); } break; case SurfaceFormat.Rgba1010102: var rgba1010102Data = new Rgba1010102[colorDataLength]; texture2D.GetData<Rgba1010102>(rgba1010102Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(((IPackedVector)rgba1010102Data[i]).ToVector4()); } break; default: throw new Exception("Texture surface format not supported"); } }
// permuted gradient texture for optimized version private void GeneratePermGradTexture() { if (permGradTexture != null) permGradTexture.Dispose(); //NormalizedByte4 permGradTexture = new Texture(MyRender.GraphicsDevice, 256, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default); NormalizedByte4[] data = new NormalizedByte4[256 * 1]; for (int x = 0; x < 256; x++) { for (int y = 0; y < 1; y++) { //data[x + (y * 256)] = new NormalizedByte4(g3[perm[x] % 16, 0], g3[perm[x] % 16, 1], g3[perm[x] % 16, 2], 1); data[x + (y * 256)] = new NormalizedByte4(g3[perm[x] % 16, 0], g3[perm[x] % 16, 1], g3[perm[x] % 16, 2], 1); } } /* byte[] data = new byte[256 * 1 * 4]; for (int x = 0; x < 256; x++) { for (int y = 0; y < 1; y++) { data[(x + (y * 256)) * 4 + 0] = (byte)(255.0f * ((g3[perm[x] % 16, 0] + 1) / 2.0f)); data[(x + (y * 256)) * 4 + 1] = (byte)(255.0f * ((g3[perm[x] % 16, 1] + 1) / 2.0f)); data[(x + (y * 256)) * 4 + 2] = (byte)(255.0f * ((g3[perm[x] % 16, 2] + 1) / 2.0f)); data[(x + (y * 256)) * 4 + 3] = 1; } } */ SharpDX.DataStream ds; DataRectangle dr = permGradTexture.LockRectangle(0, LockFlags.None, out ds); ds.WriteRange(data); permGradTexture.UnlockRectangle(0); }
internal Color[] GetColorData() { int colorDataLength = Width * Height; var colorData = new Color[colorDataLength]; switch (Format) { case SurfaceFormat.Single: var floatData = new float[colorDataLength]; GetData(floatData); for (int i = 0; i < colorDataLength; i++) { float brightness = floatData[i]; // Export as a greyscale image. colorData[i] = new Color(brightness, brightness, brightness); } break; case SurfaceFormat.Color: GetData(colorData); break; case SurfaceFormat.Alpha8: var alpha8Data = new Alpha8[colorDataLength]; GetData(alpha8Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(alpha8Data[i].ToVector4()); } break; case SurfaceFormat.Bgr565: var bgr565Data = new Bgr565[colorDataLength]; GetData(bgr565Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(bgr565Data[i].ToVector4()); } break; case SurfaceFormat.Bgra4444: var bgra4444Data = new Bgra4444[colorDataLength]; GetData(bgra4444Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(bgra4444Data[i].ToVector4()); } break; case SurfaceFormat.Bgra5551: var bgra5551Data = new Bgra5551[colorDataLength]; GetData(bgra5551Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(bgra5551Data[i].ToVector4()); } break; case SurfaceFormat.HalfSingle: var halfSingleData = new HalfSingle[colorDataLength]; GetData(halfSingleData); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(halfSingleData[i].ToVector4()); } break; case SurfaceFormat.HalfVector2: var halfVector2Data = new HalfVector2[colorDataLength]; GetData(halfVector2Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(halfVector2Data[i].ToVector4()); } break; case SurfaceFormat.HalfVector4: var halfVector4Data = new HalfVector4[colorDataLength]; GetData(halfVector4Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(halfVector4Data[i].ToVector4()); } break; case SurfaceFormat.NormalizedByte2: var normalizedByte2Data = new NormalizedByte2[colorDataLength]; GetData(normalizedByte2Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(normalizedByte2Data[i].ToVector4()); } break; case SurfaceFormat.NormalizedByte4: var normalizedByte4Data = new NormalizedByte4[colorDataLength]; GetData(normalizedByte4Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(normalizedByte4Data[i].ToVector4()); } break; case SurfaceFormat.Rg32: var rg32Data = new Rg32[colorDataLength]; GetData(rg32Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(rg32Data[i].ToVector4()); } break; case SurfaceFormat.Rgba64: var rgba64Data = new Rgba64[colorDataLength]; GetData(rgba64Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(rgba64Data[i].ToVector4()); } break; case SurfaceFormat.Rgba1010102: var rgba1010102Data = new Rgba1010102[colorDataLength]; GetData(rgba1010102Data); for (int i = 0; i < colorDataLength; i++) { colorData[i] = new Color(rgba1010102Data[i].ToVector4()); } break; default: throw new Exception("Texture surface format not supported"); } return(colorData); }