- Home /
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
Answer by Ricardo Arango · Jun 09, 2011 at 05:04 PM
Try this code in a Scene with this setup:
A camera.
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 (); } } } }
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.
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.
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>
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.
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();
}
Your answer
Follow this Question
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