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
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
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.
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 ();
}
}
}
}
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 *= 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();*
}*