Unity3d Brush Implementation Series 04-texture2d

Source: Internet
Author: User

Wake up by the toilet, continue to draw the line. Find the SetPixel method in the Texture2d class in the document.

1  Public classExample:monobehaviour {2     voidStart () {3Texture2d texture =NewTexture2d ( -, -);4Renderer.material.mainTexture =Texture;5         inty =0;6          while(Y <texture.height) {7             intx =0;8              while(X <texture.width) {9Color color = ((x & y)?Color.white:Color.gray);Ten texture. SetPixel (x, y, color); One++x; A             } -++y; -         } the texture. Apply (); -     } -}
Examples of manuals

See the Avenue of light in an instant. (whining, finally found ...) Set the pixel who knows with whom

The first step is to create a blank image,

New false

The second step is to get the coordinates of the mouse (finger) on the screen.

var pos =input.mouseposition;

The third step is to convert the screen coordinates to the picture coordinates.

When the picture background is as large as the screen, the mouse is in the same screen coordinates as the UV coordinates on the picture (lazy)

When it's not the same

if  out pixeluv))            {                2;                 2 ;            }

Fourth step, set the color value of the corresponding position.

I've used two different methods.

     Public  voidDrawRect (intXinty, color color, Vector2 drawrect) {        intStartX = X-(int) (Drawrect.x)/2; intStarty = y-(int) (DRAWRECT.Y)/2;  for(inti = startx; I < StartX + (int) (Drawrect.x); i++)        {             for(intj = Starty; J < Starty + (int) (DRAWRECT.Y); J + +) {t2d.            SetPixel (i, J, color); }} t2d.    Apply (); }
1, Draw rectangle
  Public  voidDrawcircle (intXintY,color Color,intRadius =1)    {               intr2 = radius *radius; intarea = R2 <<2; intrr = Radius <<1;  for(inti =0; I < area; i++)        {            inttx = (i% RR)-radius; intty = (I/RR)-radius; if(TX * TX + ty * Ty <R2) {t2d. SetPixel (x+TX, y+ty, color); }} t2d.    Apply (); }
2, draw a circle

Fifth step, interpolation draw line

1   Public   voidDrawLine (Vector2 start, Vector2 end,intoffset,color Color)2     {3         intx0 = (int) Start.x;4         intY0 = (int) Start.y;5         intX1 = (int) end.x;6         intY1 = (int) End.y;7         intDX = mathf.abs (x1-x0);8         intDY = mathf.abs (Y1-y0);9         intsx, SY;Ten         if(X0 < x1) {SX =1; }Else{SX =-1; } One         if(Y0 < Y1) {sy =1; }Else{sy =-1; } A         intErr = DX-dy; -         BOOLloop =true; -         intMindistance = (int) (Offset >>1); the         intPixelcount =0; -         intE2; -          while(Loop) -         { +pixelcount++; -             if(Pixelcount >mindistance) +             { APixelcount =0; at T2D. SetPixel (x0, y0, color); -  -             } -             if((x0 = = x1) && (y0 = = y1)) loop =false; -E2 =2*err; -             if(E2 >-dy) in             { -Err = err-dy; tox0 = x0 +SX; +             } -             if(E2 <DX) the             { *Err = err +DX; $y0 = y0 +Sy;Panax Notoginseng             } -         } the T2D. Apply (); +}
Bresenham fast line drawing algorithm

Sixth step, record all the points saved,

Seventh step, restore.

All right. Happily on the phone test ... And I have to change my phone

Unity3d Brush Implementation Series 04-texture2d

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.