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 Johan 4 · Jun 09, 2011 at 02:27 PM · texturedrawingsetpixels

Setting a pixel color based on where the cursor is?

I have a plane and I want to make a drawing game. So what I want is so when your mouse is over the plane, the texture it has will change the pixels where mouse is pointing. Though there doesn't seem to be a method to do it?

Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Ricardo Arango · Jun 09, 2011 at 05:04 PM

Try this code in a Scene with this setup:

  1. A camera.

  2. A plane Axis Aligned (parelel to the X and Y axes).

    using UnityEngine;

    public class ChangePixelColor : MonoBehaviour {

    void OnGUI ()

    {

      Event evt = Event.current;
    
         if (evt.isMouse && Input.GetMouseButton (0))
    
         {
    
             // Send a ray to collide with the plane
    
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    
             RaycastHit hit;
    
             if (collider.Raycast (ray, out hit, Mathf.Infinity))
    
             {
    
                 // Find the u,v coordinate of the Texture
    
                 Vector2 uv;
    
                 uv.x = (hit.point.x - hit.collider.bounds.min.x) / hit.collider.bounds.size.x;
    
                 uv.y = (hit.point.y - hit.collider.bounds.min.y) / hit.collider.bounds.size.y;
    
                 // Paint it red
    
                 Texture2D tex = (Texture2D)hit.transform.gameObject.renderer.sharedMaterial.mainTexture;
    
                 tex.SetPixel ((int)(uv.x * tex.width), (int)(uv.y * tex.height), Color.red);
    
                 tex.Apply ();
    
             }
    
         }
    
     }
     
     }
    
Comment
Add comment · Share
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
avatar image
3

Answer by iggy · Jun 09, 2011 at 04:04 PM

 function Start () {
 // Create a new texture and assign it to the renderer's material
 var texture = new Texture2D(128, 128);
 renderer.material.mainTexture = texture;
 
 // Fill the texture with white pixels!
 for (var y : int = 0; y < texture.height; ++y) {
     for (var x : int = 0; x < texture.width; ++x) {
         var color = Color.white;
         texture.SetPixel (x, y, color);
     }
 }
 // Apply all SetPixel calls
 texture.Apply();

}

read about it textures here:

http://unity3d.com/support/documentation/ScriptReference/Texture2D.html

and about drawing pixels here:

http://unity3d.com/support/documentation/ScriptReference/Texture2D.SetPixel.html


and to get mouse coordinates use:

 Input.mousePosition

http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html

or cast ray, etc. depends on the drawing mode you want. there are lots of ways to calculate that, you will have to be more specific if you want detailed help.

Comment
Add comment · Show 3 · Share
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
avatar image Johan 4 · Jun 09, 2011 at 04:39 PM 0
Share

I just want to be able to draw. Chance pixel color at the place where cursor is pointing. I don't know how to convert cursor position to pixel position in the texture.

I know how to convert mouse post to a world ray and I also know how to set pixels, I don't know how to set the right pixels based on the ray.

avatar image iggy · Jun 09, 2011 at 05:10 PM 0
Share

oh, ok.

does this help.

http://unity3d.com/support/documentation/ScriptReference/RaycastHit-textureCoord.html

var hit : RaycastHit; if (!Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit)) return; var tex : Texture2D = renderer.material.mainTexture; var pixelUV = hit.textureCoord; pixelUV.x *= tex.width; pixelUV.y *= tex.height;

 tex.SetPixel(pixelUV.x, pixelUV.y, Color.black);</pre>
avatar image Johan 4 · Jun 09, 2011 at 06:21 PM 0
Share

Yeah I am aware of this, sadly it doesn't work with nonmesh colliders. While it would work for this example, it wouldn't work for all of them. I asked a similar question once already and I got that it's not possible in unity without a mesh collider, which sucks.

avatar image
0

Answer by saddam751 · Dec 30, 2013 at 10:18 AM

Just attach this script to Main camera and you are done-

pragma strict

function Update () {

     if (!Input.GetMouseButton (0))
         return;
    
     var hit : RaycastHit;
     if (!Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit))
         return;
    
     var renderer : Renderer = hit.collider.renderer;
        var meshCollider = hit.collider as MeshCollider;
     if (renderer == null || renderer.sharedMaterial == null ||
         renderer.sharedMaterial.mainTexture == null || meshCollider == null)
         return;
  
     var sum=70*30;
     
     var colors = new Color[sum];
     for (var i = 0; i < sum; i++)

       {

         colors[i] = Color.black;

       }
     
     
     var tex : Texture2D = renderer.material.mainTexture;
     var pixelUV = hit.textureCoord2;
     pixelUV.x *= tex.width;
     pixelUV.y *= tex.height;
     
     tex.SetPixels(pixelUV.x, pixelUV.y, 70, 30 , colors);
  
     tex.Apply();
 }
Comment
Add comment · Share
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Drawing a solid circle onto texture 2 Answers

How do I fill a texture with one color? 2 Answers

Drawing a targeting reticule 1 Answer

Drawing and saving during gameplay 0 Answers

change part of texture with color or other texture 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