Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by silver16 · Mar 26, 2017 at 05:13 PM · 2dtexture2dpainting

painting program not drawing at cursor point on texture

I'm attempting to make a script that lets colors be painted on a texture, in kind of an MS Paint/Photoshop kind of fashion. It works by using setpixel and repeatedly applying to a blank texture set on an object layered below the camera while the mouse is pressed.

The script basically works, but there is one problem. When it paints the color does not appear at the cursor point, but rather offset to the right or left depending on which side the mouse is on.

The screenshot shows where the color dot appears when the cursor is clicked where it's positioned. I'm sure this has something to do with the mouse input not correlating with the position of pixels on the texture, but I'm not sure how to deal with it.

Any help would be greatly appreciated.

 public class colorScript : MonoBehaviour {
 
         //holds each drawing of the texture
     Texture2D destTex;
 
         //a vector to store the position of the mouse.
     public Vector3 mousepos = new Vector3 (0, 0, 0);
 
         //just a test color
     public Color col = Color.red;
 
 
     void Start() 
     {
             //the screen width and height are store for the size of the new texture
         int width = Mathf.FloorToInt(Screen.width);
         int height = Mathf.FloorToInt(Screen.height);
 
                 //create a new texture to draw on
                 destTex = new Texture2D(width, height);
 
                //create a new sprite using the texture
         GetComponent<SpriteRenderer>().sprite =                                                                                         Sprite.Create(destTex, new Rect(Camera.main.rect.x, Camera.main.rect.y, destTex.width, destTex.height), new Vector2(0.5f, 0.5f),100f);
     } 
 
     void Update()
     {
         //get the mouse position and feed it to paint() for coordinates if the button is pressed
         mousepos = Input.mousePosition;
 
         if (Input.GetMouseButton(0))
             paint((int)mousepos.x, (int)mousepos.y);
 
     }//end update
 
         //a modified version of the circle script to draw full circles
     public void paint (int cirX, int cirY) 
     {
 
         int x, y, px, nx, py, ny, d;
 
         int r = 10;
 
         for (x = 0; x <= r; x++)
         {
             d = (int)Mathf.Ceil(Mathf.Sqrt(r * r - x * x));
             for (y = 0; y <= d; y++)
             {
                 px = cirX + x;
                 nx = cirX - x;
                 py = cirY + y;
                 ny = cirY - y;
 
                 destTex.SetPixel(px, py, col);
                 destTex.SetPixel(nx, py, col);
 
                 destTex.SetPixel(px, ny, col);
                 destTex.SetPixel(nx, ny, col);
 
             }//end for
         
         }//end for
         destTex.Apply();
         GetComponent<SpriteRenderer>().sprite = Sprite.Create(destTex, new Rect(Camera.main.rect.x, Camera.main.rect.y, destTex.width, destTex.height), new Vector2(0.5f, 0.5f),100f);`
 
     }//end paint
 
 }
screenshot-from-2017-03-23-22-59-39.png (370.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

RaycastHit2D hit to texture coordinate 0 Answers

Texture Max Size Value related rule 1 Answer

Fade between two moving cameras 1 Answer

Dynamic Shader to Outline Adjacent Textures? 0 Answers

Sprite created from texture not displaying 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges