Ejemplo n.º 1
0
        public static void SetPixel(this Texture2D tex, byte data, Point pos)
        {
            pixelbyte[0] = new HalfSingle(data);
            int mulpos = pos.Y * tex.Width + pos.X;

            tex.SetData <HalfSingle>(0, new Rectangle(pos, new Point(1)), pixelbyte, 0, 1);
        }
        /// <summary>
        /// Reads a half precision Single written using WriteHalfPrecision(float)
        /// </summary>
        public static float ReadHalfPrecisionSingle(this NetIncomingMessage message)
        {
            HalfSingle h = new HalfSingle();

            h.PackedValue = message.ReadUInt16();
            return(h.ToSingle());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads a half precision Single written using WriteHalfPrecision(float)
        /// </summary>
        public static float ReadHalfPrecisionSingle(NetBuffer buffer)
        {
            HalfSingle h = new HalfSingle();

            h.PackedValue = buffer.ReadUInt16();
            return(h.ToSingle());
        }
Ejemplo n.º 4
0
        public void HalfSingle_ToVector4()
        {
            // arrange
            var halfSingle = new HalfSingle(0.5f);
            var expected   = new Vector4(0.5f, 0, 0, 1);

            // act
            var actual = halfSingle.ToVector4();

            // assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 5
0
        public void HalfSingle_ToBgr24()
        {
            // arrange
            var halfVector = new HalfSingle(.5F);
            var actual     = default(Bgr24);
            var expected   = new Bgr24(128, 0, 0);

            // act
            halfVector.ToBgr24(ref actual);

            // assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 6
0
        public void HalfSingle_Rgba32()
        {
            // arrange
            var halfVector = new HalfSingle(.5F);
            var actual     = default(Rgba32);
            var expected   = new Rgba32(128, 0, 0, 255);

            // act
            halfVector.ToRgba32(ref actual);

            // assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 7
0
        public void HalfSingle_ToScaledVector4()
        {
            // arrange
            var halfSingle = new HalfSingle(-1F);

            // act
            Vector4 actual = halfSingle.ToScaledVector4();

            // assert
            Assert.Equal(0, actual.X);
            Assert.Equal(0, actual.Y);
            Assert.Equal(0, actual.Z);
            Assert.Equal(1, actual.W);
        }
Ejemplo n.º 8
0
        public void HalfSingle_PackFromScaledVector4()
        {
            // arrange
            Vector4 scaled     = new HalfSingle(-1F).ToScaledVector4();
            int     expected   = 48128;
            var     halfSingle = default(HalfSingle);

            // act
            halfSingle.PackFromScaledVector4(scaled);
            ushort actual = halfSingle.PackedValue;

            // assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 9
0
        public void HalfSingle()
        {
            // Test limits
            Assert.Equal(15360, new HalfSingle(1F).PackedValue);
            Assert.Equal(0, new HalfSingle(0F).PackedValue);
            Assert.Equal(48128, new HalfSingle(-1F).PackedValue);

            // Test values
            Assert.Equal(11878, new HalfSingle(0.1F).PackedValue);
            Assert.Equal(46285, new HalfSingle(-0.3F).PackedValue);

            // Test ordering
            float x = .5F;

            Assert.True(Equal(new Vector4(x, 0, 0, 1), new HalfSingle(x).ToVector4()));

            // Test ToScaledVector4.
            Vector4 scaled = new HalfSingle(-1F).ToScaledVector4();

            Assert.Equal(0, scaled.X);
            Assert.Equal(0, scaled.Y);
            Assert.Equal(0, scaled.Z);
            Assert.Equal(1, scaled.W);

            // Test PackFromScaledVector4.
            var pixel = default(HalfSingle);

            pixel.PackFromScaledVector4(scaled);
            Assert.Equal(48128, pixel.PackedValue);

            var rgb  = default(Rgb24);
            var rgba = default(Rgba32);
            var bgr  = default(Bgr24);
            var bgra = default(Bgra32);

            new HalfSingle(x).ToRgb24(ref rgb);
            Assert.Equal(rgb, new Rgb24(128, 0, 0));

            new HalfSingle(x).ToRgba32(ref rgba);
            Assert.Equal(rgba, new Rgba32(128, 0, 0, 255));

            new HalfSingle(x).ToBgr24(ref bgr);
            Assert.Equal(bgr, new Bgr24(128, 0, 0));

            new HalfSingle(x).ToBgra32(ref bgra);
            Assert.Equal(bgra, new Bgra32(128, 0, 0, 255));
        }
Ejemplo n.º 10
0
        private static void _MutateAutoLevels(this Image <HalfSingle> source, Rectangle sourceRectangle)
        {
            // stage 1: gather range

            float min = float.MaxValue;
            float max = float.MinValue;

            for (int y = sourceRectangle.Top; y < sourceRectangle.Bottom; ++y)
            {
                for (int x = sourceRectangle.Left; x < sourceRectangle.Right; ++x)
                {
                    var p = source[x, y].ToSingle();

                    if (min > p)
                    {
                        min = p;
                    }
                    if (max < p)
                    {
                        max = p;
                    }
                }
            }

            // stage 2: apply normalization

            var range = max - min; if (range <= float.Epsilon)
            {
                return;
            }

            float invmaxrange = 1.0f / range;

            for (int y = sourceRectangle.Top; y < sourceRectangle.Bottom; ++y)
            {
                for (int x = sourceRectangle.Left; x < sourceRectangle.Right; ++x)
                {
                    var p = source[x, y].ToSingle();

                    p = (p - min) * invmaxrange;
                    p = p.Clamp(0, 1);

                    source[x, y] = new HalfSingle(p);
                }
            }
        }
Ejemplo n.º 11
0
        public void HalfSingle()
        {
            //Test limits
            Assert.AreEqual(15360, new HalfSingle(1f).PackedValue);
            Assert.AreEqual(0, new HalfSingle(0f).PackedValue);
            Assert.AreEqual(48128, new HalfSingle(-1f).PackedValue);

            //Test values
            Assert.AreEqual(11878, new HalfSingle(0.1f).PackedValue);
            Assert.AreEqual(46285, new HalfSingle(-0.3f).PackedValue);

            var packed   = new HalfSingle(0.5f).PackedValue;
            var unpacked = new HalfSingle()
            {
                PackedValue = packed
            }.ToSingle();

            Assert.AreEqual(0.5f, unpacked, 0.01f);
        }
Ejemplo n.º 12
0
        public void InizializeComponentDrag(int ID)
        {
            if (true)
            {
                UI_Handler.UI_IsWindowHide = true;

                App.simulator.ChangeToolmode(Simulator.TOOL_SELECT);
                IsCompDrag = true;
                HalfSingle[]          data      = new HalfSingle[81 * 81];
                List <ComponentPixel> datapixel = Components_Data[ID].data[Components_Data[ID].currentrotation];
                for (int i = 0; i < datapixel.Count; ++i)
                {
                    data[(datapixel[i].pos.Y + 40) * 81 + (datapixel[i].pos.X + 40)] = new HalfSingle((float)datapixel[i].type);
                }

                placementtex.SetData(data);
                sim_effect.Parameters["placementtex"].SetValue(placementtex);
            }
        }
        /// <summary>
        /// Writes a Vector3 at half precision
        /// </summary>
        public static Vector3 ReadHalfPrecisionVector3(this NetIncomingMessage message)
        {
            HalfSingle hx = new HalfSingle();

            hx.PackedValue = message.ReadUInt16();

            HalfSingle hy = new HalfSingle();

            hy.PackedValue = message.ReadUInt16();

            HalfSingle hz = new HalfSingle();

            hz.PackedValue = message.ReadUInt16();

            Vector3 retval = new Vector3();

            retval.X = hx.ToSingle();
            retval.Y = hy.ToSingle();
            retval.Z = hz.ToSingle();
            return(retval);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Writes a Vector3 at half precision
        /// </summary>
        public static Vector3 ReadHalfPrecisionVector3(this NetBuffer buffer)
        {
            HalfSingle hx = new HalfSingle();

            hx.PackedValue = buffer.ReadUInt16();

            HalfSingle hy = new HalfSingle();

            hy.PackedValue = buffer.ReadUInt16();

            HalfSingle hz = new HalfSingle();

            hz.PackedValue = buffer.ReadUInt16();

            Vector3 retval;

            retval.X = hx.ToSingle();
            retval.Y = hy.ToSingle();
            retval.Z = hz.ToSingle();
            return(retval);
        }
Ejemplo n.º 15
0
        private static void _FillPerlinNoise(this Image <HalfSingle> image, float scale = 16, int repeat = 0, int octaves = 8, double persistence = 0.1f, int randomSeed = 177)
        {
            var generator = new Perlin_Tileable(randomSeed, repeat);

            for (int y = 0; y < image.Height; ++y)
            {
                for (int x = 0; x < image.Width; ++x)
                {
                    var xx = (float)x / scale;
                    var yy = (float)y / scale;

                    var p = (float)generator.OctavePerlin(xx, yy, 0, octaves, persistence);

                    var pp = new HalfSingle(p);

                    image[x, y] = pp;
                }
            }

            image._MutateAutoLevels();
        }
Ejemplo n.º 16
0
        private static void _FillRandomNoise(this Image <HalfSingle> image, int blurRadius = 0, int randomSeed = 177)
        {
            var generator = new Random(randomSeed);

            for (int y = 0; y < image.Height; ++y)
            {
                for (int x = 0; x < image.Width; ++x)
                {
                    var p = (float)generator.NextDouble();

                    var pp = new HalfSingle(p);

                    image[x, y] = pp;
                }
            }

            if (blurRadius > 0)
            {
                image.Mutate(dc => dc.Blur(BlurMode.Box, blurRadius));
                image._MutateAutoLevels();
            }
        }
Ejemplo n.º 17
0
        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");
            }
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Reads a half precision Single written using WriteHalfPrecision(float)
 /// </summary>
 public static float ReadHalfPrecisionSingle(this NetBuffer buffer)
 {
     HalfSingle h = new HalfSingle();
     h.PackedValue = buffer.ReadUInt16();
     return h.ToSingle();
 }
Ejemplo n.º 19
0
 void ExtractDensityTextureData(ref byte[] byteField, int z)
 {
     //In the lines below, we copy the texture data into the density field buffer
     if (GFX.Inst.ByteSurfaceDataType == GFXTextureDataType.BYTE)
         srcTarget.GetTexture().GetData<byte>(byteField, z * DensityFieldWidth * DensityFieldHeight, DensityFieldWidth * DensityFieldHeight);
     else
     {
         byte[] densityData = new byte[srcTarget.Width * srcTarget.Height];
         switch (GFX.Inst.ByteSurfaceDataType)
         {
             case GFXTextureDataType.COLOR:
                 Color[] colorData = new Color[densityData.Length];
                 srcTarget.GetTexture().GetData<Color>(colorData);
                 for (int i = 0; i < colorData.Length; i++)
                     densityData[i] = colorData[i].R;
                 Array.Copy(densityData, 0, byteField, z * DensityFieldWidth * DensityFieldHeight, DensityFieldWidth * DensityFieldHeight);
                 break;
             case GFXTextureDataType.HALFSINGLE:
                 HalfSingle[] hsingData = new HalfSingle[densityData.Length];
                 srcTarget.GetTexture().GetData<HalfSingle>(hsingData);
                 for (int i = 0; i < hsingData.Length; i++)
                     densityData[i] = (byte)(hsingData[i].ToSingle() * 255.0f);
                 Array.Copy(densityData, 0, byteField, z * DensityFieldWidth * DensityFieldHeight, DensityFieldWidth * DensityFieldHeight);
                 break;
             case GFXTextureDataType.SINGLE:
                 float[] singData = new float[densityData.Length];
                 srcTarget.GetTexture().GetData<float>(singData);
                 for (int i = 0; i < singData.Length; i++)
                     densityData[i] = (byte)(singData[i] * 255.0f);
                 Array.Copy(densityData, 0, byteField, z * DensityFieldWidth * DensityFieldHeight, DensityFieldWidth * DensityFieldHeight);
                 break;
         }
     }
 }
Ejemplo n.º 20
0
 void CopyDensityTextureData(ref byte[] byteField, Texture3D texture)
 {
     //In the lines below, we copy the texture data into the density field buffer
     if (GFX.Inst.ByteSurfaceDataType == GFXTextureDataType.BYTE)
         texture.SetData<byte>(byteField);
     else
     {
         int elementCount = texture.Width * texture.Height * texture.Depth;
         switch (GFX.Inst.ByteSurfaceDataType)
         {
             case GFXTextureDataType.COLOR:
                 Color[] colorData = new Color[elementCount];
                 srcTarget.GetTexture().GetData<Color>(colorData);
                 for (int i = 0; i < colorData.Length; i++)
                 {
                     colorData[i].R = byteField[i];
                     colorData[i].G = byteField[i];
                     colorData[i].B = byteField[i];
                     colorData[i].A = byteField[i];
                 }
                 texture.SetData<Color>(colorData);
                 break;
             case GFXTextureDataType.HALFSINGLE:
                 HalfSingle[] hsingData = new HalfSingle[elementCount];
                 for (int i = 0; i < hsingData.Length; i++)
                     hsingData[i] = new HalfSingle((float)byteField[i] / 255.0f);
                 texture.SetData<HalfSingle>(hsingData);
                 break;
             case GFXTextureDataType.SINGLE:
                 float[] singData = new float[elementCount];
                 for (int i = 0; i < singData.Length; i++)
                     singData[i] = (float)byteField[i] / 255.0f;
                 texture.SetData<float>(singData);
                 break;
         }
     }
 }
Ejemplo n.º 21
0
        /// <summary>
        /// Writes a Vector3 at half precision
        /// </summary>
        public static Vector3 ReadHalfPrecisionVector3(this NetBuffer buffer)
        {
            HalfSingle hx = new HalfSingle();
            hx.PackedValue = buffer.ReadUInt16();

            HalfSingle hy = new HalfSingle();
            hy.PackedValue = buffer.ReadUInt16();

            HalfSingle hz = new HalfSingle();
            hz.PackedValue = buffer.ReadUInt16();

            Vector3 retval;
            retval.X = hx.ToSingle();
            retval.Y = hy.ToSingle();
            retval.Z = hz.ToSingle();
            return retval;
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
0
        //--------------------------------------------------------------
        /// <summary>
        /// Gets the texture data of the specified mipmap level as a <see cref="Vector4"/> array.
        /// </summary>
        /// <param name="texture">The texture.</param>
        /// <param name="level">The mipmap level to read. Currently only 0 is supported!</param>
        /// <returns>
        /// The array containing the data of the specified mipmap level.
        /// (One <see cref="Vector4"/> element per pixel.)
        /// </returns>
        /// <remarks>
        /// <para>
        /// This method can be used with following texture surface formats:
        /// <see cref="SurfaceFormat.Alpha8"/>, <see cref="SurfaceFormat.Color"/>, 
        /// <see cref="SurfaceFormat.Rg32"/>, <see cref="SurfaceFormat.Rgba64"/>,
        /// <see cref="SurfaceFormat.Single"/>, <see cref="SurfaceFormat.Vector2"/>,
        /// <see cref="SurfaceFormat.Vector4"/>, <see cref="SurfaceFormat.HalfSingle"/>,
        /// <see cref="SurfaceFormat.HalfVector2"/>, <see cref="SurfaceFormat.HalfVector4"/>
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="texture"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="NotImplementedException">
        /// Invalid mipmap level. Extracting mipmap levels other than 0 is not yet implemented.
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// Texture format is not yet supported.
        /// </exception>
        public static Vector4[] GetTextureLevelVector4(Texture2D texture, int level)
        {
            if (texture == null)
            throw new ArgumentNullException("texture");
              if (level < 0)
            throw new ArgumentOutOfRangeException("level");
              if (level > 0)
            throw new NotImplementedException("GetTextureLevelVector4 for levels other than 0 is not yet implemented.");

              var bufferLevel0 = new Vector4[texture.Width * texture.Height];

              if (texture.Format == SurfaceFormat.Alpha8)
              {
            var buffer = new byte[bufferLevel0.Length];
            texture.GetData(buffer);
            for (int i = 0; i < buffer.Length; i++)
              bufferLevel0[i] = new Vector4(0, 0, 0, buffer[i] / 255.0f);
              }
              else if (texture.Format == SurfaceFormat.Color)
              {
            var buffer = new Color[bufferLevel0.Length];
            texture.GetData(buffer);
            for (int i = 0; i < buffer.Length; i++)
              bufferLevel0[i] = buffer[i].ToVector4();
              }
              else if (texture.Format == SurfaceFormat.Rg32)
              {
            var buffer = new Rg32[bufferLevel0.Length];
            texture.GetData(buffer);
            for (int i = 0; i < buffer.Length; i++)
            {
              var v = buffer[i].ToVector2();
              bufferLevel0[i] = new Vector4(v.X, v.Y, 0, 0);
            }
              }
              else if (texture.Format == SurfaceFormat.Rgba64)
              {
            var buffer = new Rgba64[bufferLevel0.Length];
            texture.GetData(buffer);
            for (int i = 0; i < buffer.Length; i++)
              bufferLevel0[i] = buffer[i].ToVector4();
              }
              else if (texture.Format == SurfaceFormat.Single)
              {
            var buffer = new Single[bufferLevel0.Length];
            texture.GetData(buffer);
            for (int i = 0; i < buffer.Length; i++)
              bufferLevel0[i] = new Vector4(buffer[i]);
              }
              else if (texture.Format == SurfaceFormat.Vector2)
              {
            var buffer = new Vector2[bufferLevel0.Length];
            texture.GetData(buffer);
            for (int i = 0; i < buffer.Length; i++)
              bufferLevel0[i] = new Vector4(buffer[i].X, buffer[i].Y, 0, 0);
              }
              else if (texture.Format == SurfaceFormat.Vector4)
              {
            texture.GetData(bufferLevel0);
              }
              else if (texture.Format == SurfaceFormat.HalfSingle)
              {
            var buffer = new HalfSingle[bufferLevel0.Length];
            texture.GetData(buffer);
            for (int i = 0; i < buffer.Length; i++)
              bufferLevel0[i] = new Vector4(buffer[i].ToSingle(), 0, 0, 0);
              }
              else if (texture.Format == SurfaceFormat.HalfVector2)
              {
            var buffer = new HalfVector2[bufferLevel0.Length];
            texture.GetData(buffer);
            for (int i = 0; i < buffer.Length; i++)
            {
              var v = buffer[i].ToVector2();
              bufferLevel0[i] = new Vector4(v.X, v.Y, 0, 0);
            }
              }
              else if (texture.Format == SurfaceFormat.HalfVector4)
              {
            var buffer = new HalfVector4[bufferLevel0.Length];
            texture.GetData(buffer);
            for (int i = 0; i < buffer.Length; i++)
              bufferLevel0[i] = buffer[i].ToVector4();
              }
              else
              {
            throw new NotSupportedException("Texture format '" + texture.Format + "' is not yet supported.");
              }

              return bufferLevel0;
        }
Ejemplo n.º 24
0
        public static void SetTextureLevel(Texture2D texture, int level, float[] data)
        {
            if (texture == null)
            throw new ArgumentNullException("texture");
              if (data == null)
            throw new ArgumentNullException("data");

              if (texture.Format == SurfaceFormat.Alpha8)
              {
            var buffer = new byte[data.Length];
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = (byte)(data[i] * 255.0f);
            texture.SetData(level, null, buffer, 0, buffer.Length);
              }
              else if (texture.Format == SurfaceFormat.Color)
              {
            var buffer = new Color[data.Length];
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = new Color(data[i], data[i], data[i], data[i]);
            texture.SetData(level, null, buffer, 0, buffer.Length);
              }
              else if (texture.Format == SurfaceFormat.Rg32)
              {
            var buffer = new Rg32[data.Length];
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = new Rg32(data[i], data[i]);
            texture.SetData(level, null, buffer, 0, buffer.Length);
              }
              else if (texture.Format == SurfaceFormat.Rgba64)
              {
            var buffer = new Rgba64[data.Length];
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = new Rgba64(data[i], data[i], data[i], data[i]);
            texture.SetData(level, null, buffer, 0, buffer.Length);
              }
              else if (texture.Format == SurfaceFormat.Single)
              {
            texture.SetData(level, null, data, 0, data.Length);
              }
              else if (texture.Format == SurfaceFormat.Vector2)
              {
            var buffer = new Vector2[data.Length];
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = new Vector2(data[i], data[i]);
            texture.SetData(level, null, buffer, 0, buffer.Length);
              }
              else if (texture.Format == SurfaceFormat.Vector4)
              {
            var buffer = new Vector4[data.Length];
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = new Vector4(data[i], data[i], data[i], data[i]);
            texture.SetData(level, null, buffer, 0, buffer.Length);
              }
              else if (texture.Format == SurfaceFormat.HalfSingle)
              {
            var buffer = new HalfSingle[data.Length];
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = new HalfSingle(data[i]);
            texture.SetData(level, null, buffer, 0, buffer.Length);
              }
              else if (texture.Format == SurfaceFormat.HalfVector2)
              {
            var buffer = new HalfVector2[data.Length];
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = new HalfVector2(data[i], data[i]);
            texture.SetData(level, null, buffer, 0, buffer.Length);
              }
              else if (texture.Format == SurfaceFormat.HalfVector4)
              {
            var buffer = new HalfVector4[data.Length];
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = new HalfVector4(data[i], data[i], data[i], data[i]);
            texture.SetData(level, null, buffer, 0, buffer.Length);
              }
              else
              {
            throw new NotSupportedException("Texture format '" + texture.Format + "' is not yet supported.");
              }
        }
Ejemplo n.º 25
0
        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");
            }
        }