Exemplo n.º 1
0
        public static void InvertGradient(RenderTarget2D textureInput, ref RenderTarget2D textureOutput, float state,
            Color c1, Color c2)
        {
            if (textureInput.Width != textureOutput.Width || textureInput.Height != textureOutput.Height)
                throw new Exception("Textures must be the same size!");

            Color[] c = new Color[textureInput.Width * textureInput.Height];
            textureInput.GetData<Color>(c);

            float cr = (float)(c1.R + c2.R) / 2 / 255;
            float cg = (float)(c1.G + c2.G) / 2 / 255;
            float cb = (float)(c1.B + c2.B) / 2 / 255;

            float dr = (float)(c1.R - c2.R) / 255;
            float dg = (float)(c1.G - c2.G) / 255;
            float db = (float)(c1.B - c2.B) / 255;

            float tr, tg, tb;

            for (int i = 0; i < c.Length && c[i].A != 0; i++)
            {
                tr = (float)c[i].R / 255;
                tg = (float)c[i].G / 255;
                tb = (float)c[i].B / 255;

                tr = tr - (tr - cr) * 2 * state;
                tg = tg - (tg - cg) * 2 * state;
                tb = tb - (tb - cb) * 2 * state;

                c[i].R = (byte)(tr * 255);
                c[i].G = (byte)(tg * 255);
                c[i].B = (byte)(tb * 255);
            }

            textureOutput.SetData<Color>(c);
        }
Exemplo n.º 2
0
		public void Dump() {
			try {
				Texture2D texture = Texture2D.FromStream(this.GraphicsDevice, File.OpenRead(@"C:\Users\Oxysoft\Desktop\_splitter_tool\input.png"));
				string dumploc = @"C:\Users\Oxysoft\Desktop\_splitter_tool\newdump\";
				const int cols = 10;
				const int rows = 10;
				TileableTexture tileableTexture = new TileableTexture(texture, cols, rows);
				List<RenderTarget2D> results = new List<RenderTarget2D>();
				Color chroma1 = new Color(0xBF, 0xC8, 0xFF);
				Color chroma2 = new Color(0xD8, 0xDE, 0xFF);
				for (int y = 0; y < rows; y++) {
					for (int x = 0; x < cols; x++) {
						int[] order = new int[4 * 4];

						order[0] = 5;
						order[1] = 11;
						order[2] = 5;
						order[3] = 8;
						order[4] = 6;
						order[5] = 3;
						order[6] = 6;
						order[7] = 9;
						order[8] = 1;
						order[9] = 4;
						order[10] = 1;
						order[11] = 7;
						order[12] = 0;
						order[13] = 2;
						order[14] = 0;
						order[15] = 10;

						RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, 96, 128);
						TileableTexture tileableRenderTarget = new TileableTexture(renderTarget, 3, 4);
						GraphicsDevice.SetRenderTarget(renderTarget);
						SpriteBatch spriteBatch = new SpriteBatch(GraphicsDevice);
						spriteBatch.Begin();
						spriteBatch.Draw(texture, new Rectangle(0, 0, 96, 128), tileableTexture.GetSource(tileableTexture.GetIndex(x, y)), Color.White);
						spriteBatch.End();
						GraphicsDevice.SetRenderTarget(null);
						RenderTarget2D resultTarget = new RenderTarget2D(GraphicsDevice, 128, 128);
						TileableTexture tileableResultTarget = new TileableTexture(resultTarget, 4, 4);
						GraphicsDevice.SetRenderTarget(resultTarget);
						GraphicsDevice.Clear(Color.Transparent);

						spriteBatch.Begin();
						for (int i = 0; i < order.Length; i++) {
							Rectangle target = tileableResultTarget.GetSource(i);
							spriteBatch.Draw(renderTarget, target, tileableRenderTarget.GetSource(order[i]), Color.White);
						}
						spriteBatch.End();
						GraphicsDevice.SetRenderTarget(null);

						//----> Start of CHROMA CLEARING
						Color[] colors = new Color[resultTarget.Width * resultTarget.Height];
						resultTarget.GetData<Color>(colors);

						for (int i = 0; i < colors.Length; i++) {
							if (colors[i] == chroma1 || colors[i] == chroma2) colors[i] = Color.Transparent; 
						}

						resultTarget.SetData<Color>(colors);
						//End of CHROMA CLEARING <----
						
						results.Add(resultTarget);
					}
				}
				for (int i = 0; i < results.Count; i++) {
					results[i].SaveAsPng(File.OpenWrite(dumploc + i + ".png"), results[i].Width, results[i].Height);
				}
			} catch (Exception e) {
				Console.WriteLine("what da fack");
			}
		}
 public Texture2D XuatAnhKyTu(string chuoi, int rongkhung, int daikhung, int dancachchu, int dancachdong, int canle, int candong, Rectangle bovien, Color maunen, Color mauchu)
 {
     //sao chep lai cac thong tin truoc khi xu ly
     scchuoi = chuoi;
     scrongkhung = rongkhung;
     scdaikhung = daikhung;
     scdancachchu = dancachchu;
     scdancachdong = dancachdong;
     sccanle = canle;
     sccandong = candong;
     scbovien = bovien;
     scmaunen = maunen;
     scmauchu = mauchu;
     baotran = false;
     //bat dau tien xu ly
     if (bonenvunganh != null) bonenvunganh.Dispose();
     int kieutudongrong = 0, kieutudongdai = 0;
     if (rongkhung <= 0)
     {
         rongkhung = 640;
         kieutudongrong = 1;
     }
     if (daikhung <= 0)
     {
         daikhung = 480;
         kieutudongdai = 1;
     }
     //bat dau thuc hien ghep chuoi ky tu
     int vitrighepx = 0, vitrighepy = 0, demdong = 0;
     int sldong = (int)(daikhung / (dodaikytu + dancachdong));
     if (sldong == 0) sldong = 1;
     int[] mangdorongdong = new int[sldong];//khai bao mang do rong cua cac dong
     int[] demkytu = new int[sldong];//dem so luong ky tu tren moi dong
     int[,] matranvanban = new int[cmax, dmax];//ma tran van ban co do lon co the chua cmax x dmax ky tu anh con
     for (int i = 0; i < chuoi.Length; i++)
     {
         for (int j = 0; j < giatrikytu.Length; j++)
         {
             if (chuoi.Substring(i, 1) == giatrikytu[j])
             {
                 if (chuoi.Substring(i, 1) == " ")
                 {
                     int vitricach = i;
                     for (int l = i + 1; l < chuoi.Length; l++)
                     {
                         vitricach = l;
                         if ((chuoi.Substring(l, 1) == " ") | (chuoi.Substring(l, 1) == "|")) break;
                     }
                     int tucuoi = vitricach;
                     if ((chuoi.Substring(vitricach, 1) == " ") | (chuoi.Substring(vitricach, 1) == " ")) tucuoi = vitricach - 1;
                     int dodaitumoi = 0;
                     for (int l = i + 1; l <= tucuoi; l++)
                     {
                         for (int k = 0; k < giatrikytu.Length; k++)
                         {
                             if (chuoi.Substring(l, 1) == giatrikytu[k])
                             {
                                 dodaitumoi += dorongkytu[k] + dancachchu;
                                 break;
                             }
                         }
                     }
                     if (vitrighepx + dodaitumoi + 3 * dorongkytucham > rongkhung)//neu la tu cuoi
                     {
                         if (vitrighepy + 2 * dodaikytu + dancachdong > daikhung)//neu la dong cuoi
                         {
                             int dorongdongcuoi = 0;
                             int dorongdongcuoimax = vitrighepx + dorongkytu[j] + 3 * dorongkytucham;
                             if (dorongdongcuoimax < rongkhung) dorongdongcuoi = dorongdongcuoimax;
                             else dorongdongcuoi = rongkhung;
                             mangdorongdong[demdong] = dorongdongcuoi;
                             matranvanban[demkytu[demdong], demdong] = -1;
                             demkytu[demdong]++;
                             matranvanban[demkytu[demdong], demdong] = -1;
                             demkytu[demdong]++;
                             matranvanban[demkytu[demdong], demdong] = -1;
                             demkytu[demdong]++;
                             demdong++;
                             baotran = true;
                         }
                         else//neu khong phai dong cuoi
                         {
                             mangdorongdong[demdong] = vitrighepx;
                             demdong = demdong + 1;
                             vitrighepx = 0;
                             vitrighepy += dodaikytu + dancachdong;
                             matranvanban[demkytu[demdong], demdong] = j;
                             demkytu[demdong]++;
                         }
                     }
                     else//neu khong phai tu cuoi
                     {
                         vitrighepx += dorongkytu[j] + dancachchu;
                         mangdorongdong[demdong] = vitrighepx;
                         matranvanban[demkytu[demdong], demdong] = j;
                         demkytu[demdong]++;
                     }
                 }
                 else if (chuoi.Substring(i, 1) == "|")//ky tu xuong dong
                 {
                     if (vitrighepy + 2 * dodaikytu + dancachdong > daikhung)//neu la dong cuoi
                     {
                         int dorongdongcuoi = 0;
                         int dorongdongcuoimax = vitrighepx + dorongkytu[j] + 3 * dorongkytucham;
                         if (dorongdongcuoimax < rongkhung) dorongdongcuoi = dorongdongcuoimax;
                         else dorongdongcuoi = rongkhung;
                         mangdorongdong[demdong] = dorongdongcuoi;
                         matranvanban[demkytu[demdong], demdong] = -1;
                         demkytu[demdong]++;
                         matranvanban[demkytu[demdong], demdong] = -1;
                         demkytu[demdong]++;
                         matranvanban[demkytu[demdong], demdong] = -1;
                         demkytu[demdong]++;
                         demdong++;
                         baotran = true;
                     }
                     else//neu khong phai dong cuoi
                     {
                         mangdorongdong[demdong] = vitrighepx;
                         demdong = demdong + 1;
                         vitrighepx = 0;
                         vitrighepy += dodaikytu + dancachdong;
                         matranvanban[demkytu[demdong], demdong] = j;
                         demkytu[demdong]++;
                     }
                 }
                 else//neu la cac ky tu binh thuong
                 {
                     vitrighepx += dorongkytu[j] + dancachchu;
                     mangdorongdong[demdong] = vitrighepx;
                     matranvanban[demkytu[demdong], demdong] = j;
                     demkytu[demdong]++;
                 }
                 break;
             }
         }
         if (baotran) break;
     }
     if (baotran == false)
     {
         demdong = demdong + 1;
         if (kieutudongdai == 1) daikhung = demdong * (dodaikytu + dancachdong) + Math.Abs(dancachdong);
         if (kieutudongrong == 1)
         {
             rongkhung = mangdorongdong[0];
             for (int i = 0; i < mangdorongdong.Length; i++)
             {
                 if (mangdorongdong[i] > rongkhung) rongkhung = mangdorongdong[i];
             }
         }
     }
     //thuc hien can le va can dong
     bonenvunganh = new RenderTarget2D(thietbidohoa, rongkhung + bovien.X + bovien.Width, daikhung + bovien.Y + bovien.Height);
     thietbidohoa.SetRenderTarget(bonenvunganh);
     thietbidohoa.Clear(maunen);
     nenve.Begin();
     int dodaitoanbochu = demdong * (dodaikytu + dancachdong) - dancachdong;
     int giasocandong = 0;
     if (candong == 3) giasocandong = daikhung - dodaitoanbochu;
     else if (candong == 2) giasocandong = (int)((daikhung - dodaitoanbochu) / 2);
     vitrighepx = bovien.X; vitrighepy = giasocandong + bovien.Y;
     if (canle == 3)
     {
         for (int i = 0; i < demdong; i++)
         {
             vitrighepx = rongkhung - mangdorongdong[i] + bovien.X;
             vitrighepy = i * (dodaikytu + dancachdong) + giasocandong + bovien.Y;
             for (int j = 0; j < demkytu[i]; j++)
             {
                 if (matranvanban[j, i] == -1)
                 {
                     nenve.Draw(anhkytucham, new Vector2(vitrighepx, vitrighepy), Color.White);
                     vitrighepx += dorongkytucham + dancachchu;
                 }
                 else
                 {
                     if ((j == 0) & ((giatrikytu[matranvanban[j, i]] == " ") | (giatrikytu[matranvanban[j, i]] == "|"))) continue;
                     nenve.Draw(anhkytu[matranvanban[j, i]], new Vector2(vitrighepx, vitrighepy), Color.White);
                     vitrighepx += dorongkytu[matranvanban[j, i]] + dancachchu;
                 }
             }
         }
     }
     else if (canle == 2)
     {
         for (int i = 0; i < demdong; i++)
         {
             vitrighepx = (int)((rongkhung - mangdorongdong[i]) / 2) + bovien.X;
             vitrighepy = i * (dodaikytu + dancachdong) + giasocandong + bovien.Y;
             for (int j = 0; j < demkytu[i]; j++)
             {
                 if (matranvanban[j, i] == -1)
                 {
                     nenve.Draw(anhkytucham, new Vector2(vitrighepx, vitrighepy), Color.White);
                     vitrighepx += dorongkytucham + dancachchu;
                 }
                 else
                 {
                     if ((j == 0) & ((giatrikytu[matranvanban[j, i]] == " ") | (giatrikytu[matranvanban[j, i]] == "|"))) continue;
                     nenve.Draw(anhkytu[matranvanban[j, i]], new Vector2(vitrighepx, vitrighepy), Color.White);
                     vitrighepx += dorongkytu[matranvanban[j, i]] + dancachchu;
                 }
             }
         }
     }
     else
     {
         for (int i = 0; i < demdong; i++)
         {
             vitrighepx = bovien.X;
             vitrighepy = i * (dodaikytu + dancachdong) + giasocandong + bovien.Y;
             for (int j = 0; j < demkytu[i]; j++)
             {
                 if (matranvanban[j, i] == -1)
                 {
                     nenve.Draw(anhkytucham, new Vector2(vitrighepx, vitrighepy), Color.White);
                     vitrighepx += dorongkytucham + dancachchu;
                 }
                 else
                 {
                     if ((j == 0) & ((giatrikytu[matranvanban[j, i]] == " ") | (giatrikytu[matranvanban[j, i]] == "|"))) continue;
                     nenve.Draw(anhkytu[matranvanban[j, i]], new Vector2(vitrighepx, vitrighepy), Color.White);
                     vitrighepx += dorongkytu[matranvanban[j, i]] + dancachchu;
                 }
             }
         }
     }
     nenve.End();
     thietbidohoa.SetRenderTarget(null);
     if (mauchu != Color.White)
     {
         Color[] dulieumau = new Color[bonenvunganh.Width * bonenvunganh.Height];
         bonenvunganh.GetData(dulieumau);
         for (int x = 0; x < bonenvunganh.Width; x++)
         {
             for (int y = 0; y < bonenvunganh.Height; y++)
             {
                 int vitri = x + y * bonenvunganh.Width;
                 if (dulieumau[vitri].R > 126)
                 {
                     dulieumau[vitri] = mauchu;
                 }
             }
         }
         bonenvunganh.SetData(dulieumau);
     }
     return bonenvunganh;
 }
Exemplo n.º 4
0
        //TODO optimize
        public void Render(RenderTarget2D fbo, RenderTarget2D bufferFBO, Renderer renderer, Texture2D distortionTexture = null)
        {
            if (fbo == null || bufferFBO == null || fbo.Width != W || fbo.Height != H || bufferFBO.Width != W || bufferFBO.Height != H)
                return;
            if (distortionTexture == null)
                distortionTexture = MicroWorld.Graphics.Effects.Effects.liquidDistortionTexture;

            bool b = renderer.IsDrawing;
            bool s = renderer.IsScaeld;
            var cfbo = renderer.CurFBO;
            if (b)
                renderer.End();
            if (cfbo == bufferFBO)
                renderer.DisableFBO();
            renderer.GraphicsDevice.Textures[0] = null;

            Color[] fboarr = new Color[W * H];

            fixed(TileMapElement* ptrmap = tileMap)
            fixed (Color* ptrfbo = fboarr)
            {
                for (int x = 0; x < W; x++)
                {
                    for (int y = 0; y < H; y++)
                    {
                        *(ptrfbo + x + y * W) = (*(ptrmap + y + x * H)).level <= 1 ? Color.Transparent : Color.White;
                    }
                }
            }
            bufferFBO.SetData<Color>(fboarr);

            renderer.EnableFBO(fbo);
            renderer.GraphicsDevice.Clear(Color.Transparent);
            _setUpShader(distortionTexture);
            renderer.BeginUnscaled(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone,
                MicroWorld.Graphics.Effects.Effects.liquid);
            renderer.Draw(bufferFBO, new Vector2(), new Color(0, 108, 255));
            renderer.End();

            if (cfbo == fbo)
                renderer.EnableFBO(bufferFBO);
            if (b)
                renderer.Begin(s);
        }
 public override global::System.Object createImage(global::org.xmlvm._nArrayAdapter<int> n1, int n2, int n3)
 {
     RenderTarget2D rt = new RenderTarget2D(SharedGraphicsDeviceManager.Current.GraphicsDevice, n2, n3);
     int[] arr = n1.getCSharpArray();
     rt.SetData(arr);
     return new XNAImage(rt);
 }
Exemplo n.º 6
0
        /// <summary>
        /// Creates the surfaces used by the effect
        /// </summary>
        private void Create_Surfaces()
        {
            //Initialize the positions and velocities of the point masses
            Vector4[] initial_Positions = new Vector4[sizex * sizey];
            Vector4[] initial_Velocities = new Vector4[sizex * sizey];
            for (int i = 0; i < sizex * sizey; i++)
            {
                var x = i % sizex;
                var y = i / sizey;

                var coords = helper.Clip_Coords_of_point_UL(x, y);
                initial_Positions[i] = new Vector4(coords.X, coords.Y, 0, 0);

            }

            for (int i = 0; i < sizex * sizey; i++)
            {

                initial_Velocities[i] = new Vector4(0, 0, 0, 1);// + new Vector2(0, 0));

            }

            var frame_buffer_format = device.PresentationParameters.BackBufferFormat;

            //Something that has to do with gpu implementation details, it's explained in the comment lines below
            var format_choice =  SurfaceFormat.HdrBlendable;

            //Most of the surfaces use the vector4 format, though it would probably also work using just vector2
            //The gpu can't additively blend using IEEE floats
            //so certain buffers that need to be written in additive blending for the effect to work,
            //namely those gathering forces geometrically and per index,
            //they instead use a surface suitable for hdr rendering that XNA provides
            //which seems accurate enough, and supports additive blending
            Initial_Position = new RenderTarget2D(device, sizex, sizey, false, SurfaceFormat.Vector4, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
            Position0 = new RenderTarget2D(device, sizex, sizey, false, SurfaceFormat.Vector4, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
            Position1 = new RenderTarget2D(device, sizex, sizey, false, SurfaceFormat.Vector4, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
            Velocity0 = new RenderTarget2D(device, sizex, sizey, false, SurfaceFormat.Vector4, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
            Velocity1 = new RenderTarget2D(device, sizex, sizey, false, SurfaceFormat.Vector4, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
            Force_By_Index = new RenderTarget2D(device, sizex, sizey, false, SurfaceFormat.HdrBlendable, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
            Force_By_Position_Accumulator = new RenderTarget2D(device, sizex, sizey, false, SurfaceFormat.HdrBlendable, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);

            Initial_Position.SetData(initial_Positions);
            Position0.SetData(initial_Positions);
            Position1.SetData(initial_Positions);
            Velocity0.SetData(initial_Velocities);
            Velocity1.SetData(initial_Velocities);
        }
Exemplo n.º 7
0
        public static RenderTarget2D CloneRenderTarget(RenderTarget2D target)
        {
            var clone = new RenderTarget2D(target.GraphicsDevice, target.Width,
                target.Height, target.LevelCount > 1, target.Format,
                target.DepthStencilFormat, target.MultiSampleCount,
                target.RenderTargetUsage);

            for (int i = 0; i < target.LevelCount; i++)
            {
                double rawMipWidth = target.Width / Math.Pow(2, i);
                double rawMipHeight = target.Height / Math.Pow(2, i);

                // make sure that mipmap dimensions are always > 0.
                int mipWidth = (rawMipWidth < 1) ? 1 : (int)rawMipWidth;
                int mipHeight = (rawMipHeight < 1) ? 1 : (int)rawMipHeight;

                var mipData = new Color[mipWidth * mipHeight];
                target.GetData(i, null, mipData, 0, mipData.Length);
                clone.SetData(i, null, mipData, 0, mipData.Length);
            }

            return clone;
        }
Exemplo n.º 8
0
        void depthFrameSetUp(DepthImageFrame frame, DepthImagePixel[] depthPixels, Color[] depthArray, RenderTarget2D depthTarget)
        {
            depthFramyBusy = true;
            using (frame)
            {
                if (frame != null)
                {
                    //Console.WriteLine("Has frame");
                    frame.CopyDepthImagePixelDataTo(depthPixels);

                    for (int i = 0; i < depthPixels.Length; i++)
                    {

                        int b = (depthPixels[i].Depth >= frame.MinDepth && depthPixels[i].Depth <= frame.MaxDepth && depthPixels[i].IsKnownDepth) ? depthPixels[i].Depth : 0;
                        if (depthPixels[i].Depth >= frame.MaxDepth)
                        {
                            b = frame.MaxDepth;
                        }
                        float f = (float)((float)b - frame.MinDepth) / (float)(frame.MaxDepth - frame.MinDepth);

                        depthArray[i] = new Color(f, f, f, 1);
                    }

                    depthTarget.SetData(depthArray);
                    DepthImagePoint[] dip = new DepthImagePoint[kinect.ColorStream.FrameWidth * kinect.ColorStream.FrameHeight];
                    kinect.CoordinateMapper.MapColorFrameToDepthFrame(kinect.ColorStream.Format,
                        kinect.DepthStream.Format, depthPixels, dip);
                    for (int i = 0; i < depthCoordArray.Length; i++)
                    {
                        depthCoordArray[i] = new Color((float)dip[i].X / (float)kinect.ColorStream.FrameWidth, (float)dip[i].Y / (float)kinect.ColorStream.FrameHeight, 0, 1);
                    }
                    depthCoordMap.SetData(depthCoordArray);

                }
                else
                {

                }
            }
            depthFramyBusy = false;
        }
Exemplo n.º 9
0
        void colorFrameSetUp(ColorImageFrame frame, byte[] colorArray, RenderTarget2D colorTarget)
        {
            colorFrameBusy = true;
            using (frame)
            {

                if (frame != null)
                {
                    frame.CopyPixelDataTo(colorArray);
                    if (frame.Format == ColorImageFormat.RgbResolution640x480Fps30)
                    {
                        for (int i = 0; i < colorArray.Length; i += 4)
                        {
                            if (i < colorArray.Length - 3)
                            {
                                 byte b = colorArray[i];
                                 byte g = colorArray[i + 1];
                                 byte r = colorArray[i + 2];
                                 byte a = colorArray[i + 3];
                                 colorArray[i] = r;
                                 colorArray[i + 1] = g;
                                 colorArray[i + 2] = b;
                                 colorArray[i + 3] = 255;
                            }
                        }
                    }
                    else if (frame.Format == ColorImageFormat.InfraredResolution640x480Fps30)
                    {
                        UInt16 int16 = 0;
                        for (int i = 0; i < colorArray.Length; i += 2)
                        {
                            if (i < colorArray.Length - 1)
                            {
                                byte b1 = colorArray[i], b2 = colorArray[i + 1];
                                int16 = (UInt16)(b2 << 8 | b1);
                                float f = (int16 / (float)ushort.MaxValue) * (16f);
                                byte b = (byte)f;
                                byte b12 = (byte)(b << 4 | b), b22 = (byte)(b << 4 | b);
                                colorArray[i] = b12;
                                colorArray[i + 1] = b22;
                            }
                        }
                    }
                    //kinect.CoordinateMapper.MapDepthFrameToColorFrame(
                    GraphicsDevice.Textures[0] = null;
                    colorTarget.SetData(colorArray);
                }
            }
            colorFrameBusy = false;
        }