Object texture painter

Hi,

I’m trying to add the ability to draw on a model using other models I create for the users within the scene. I give the user a cube that they can resize and move around the model to determine where they want it. The update below is what happens once they click on start drawing. The only way I can think of filling it in is to create a camera then have the camera move around the box which its a child of.

My question would be is if there is an easier way to do this? One of the abilities I want is to allow the user to erase parts of the model, but the proper way would be to give them a small block that would be used as the delete tool.

I could use the same method below, but it would only circle around the object and not delete everything in the block. It would be nice if there was a way to check what the texture coordinates are where the cube collides with the model, but the only way I can find to get texture coordinates was Physics.Raycast.

var gotostep = 0;
var temp=0.5;

function Update () {
    if (Input.GetMouseButton (0)){
		for (i=0; i<=Globals.drawwidth; i++){
			var hit : RaycastHit;
			height = Screen.height / 2;
			height = height - i;
			if (!Physics.Raycast (camera.ScreenPointToRay(Vector3(Screen.width / 2, height, 0.0)), hit)){
				hitsomething=0;
			} else {
				hitsomething=1;
			}
			
			if (hitsomething==1){
				if (hit.transform.gameObject.name == "material1"){
					var renderer : Renderer = hit.collider.renderer;
					var tex : Texture2D = renderer.sharedMaterials[0].GetTexture ("_Tex1");
					var pixelUV = hit.textureCoord2;
					pixelUV.x *= tex.width;
					pixelUV.y *= tex.height;
					tex.SetPixel(pixelUV.x, pixelUV.y, Globals.drawcolor);
				}
			}
		}
		if (hitsomething==1){
			if (hit.transform.gameObject.name == "material1"){
				tex.Apply();
			}
		}
		//Move camera along plane
		rotatespeed=500;
		if (Globals.drawingcamera.transform.localPosition.x > -temp  gotostep == 0){
			move=Globals.drawspeed;
			Globals.drawingcamera.transform.localPosition.x = Globals.drawingcamera.transform.localPosition.x-move;
		} else if(gotostep == 0){
			gotostep=1;
		}
		if (Globals.drawingcamera.transform.localEulerAngles.z < 90  gotostep == 1){
			move=Globals.drawspeed * rotatespeed;
			Globals.drawingcamera.transform.localEulerAngles.z = Globals.drawingcamera.transform.localEulerAngles.z+move;
		} else if (gotostep == 1){
			gotostep=2;
			Globals.drawingcamera.transform.localEulerAngles.z=90;
		}
		if (Globals.drawingcamera.transform.localPosition.y > -temp  gotostep == 2){
			move=Globals.drawspeed;
			Globals.drawingcamera.transform.localPosition.y = Globals.drawingcamera.transform.localPosition.y-move;
		} else if (gotostep == 2){
			gotostep=3;
		}
		if (Globals.drawingcamera.transform.localEulerAngles.z < 180  gotostep == 3){
			move=Globals.drawspeed * rotatespeed;
			Globals.drawingcamera.transform.localEulerAngles.z = Globals.drawingcamera.transform.localEulerAngles.z+move;
		} else if (gotostep == 3){
			gotostep=4;
			Globals.drawingcamera.transform.localEulerAngles.z=180;
		}
		if (Globals.drawingcamera.transform.localPosition.x < temp  gotostep == 4){
			move=Globals.drawspeed;
			Globals.drawingcamera.transform.localPosition.x = Globals.drawingcamera.transform.localPosition.x+move;
		} else if (gotostep == 4){
			gotostep=5;
		}
		if (Globals.drawingcamera.transform.localEulerAngles.z < 270  gotostep == 5){
			move=Globals.drawspeed * rotatespeed;
			Globals.drawingcamera.transform.localEulerAngles.z = Globals.drawingcamera.transform.localEulerAngles.z+move;
		} else if (gotostep == 5){
			gotostep=6;
			Globals.drawingcamera.transform.localEulerAngles.z=270;
		}
		if (Globals.drawingcamera.transform.localPosition.y < temp  gotostep == 6){
			move=Globals.drawspeed;
			Globals.drawingcamera.transform.localPosition.y = Globals.drawingcamera.transform.localPosition.y+move;
		} else if (gotostep == 6){
			gotostep=7;
		}
		if (Globals.drawingcamera.transform.localEulerAngles.z >= 270  gotostep == 7  Globals.drawingcamera.transform.localEulerAngles.z < 359.9999 ){
			move=Globals.drawspeed * rotatespeed;
			Globals.drawingcamera.transform.localEulerAngles.z = Globals.drawingcamera.transform.localEulerAngles.z+move;
		} else if (gotostep == 7){
			print("Stopping because camera is at "+Globals.drawingcamera.transform.localEulerAngles.z);
			GameObject.Find("camera1").camera.enabled = true;
			Globals.drawingobject.renderer.enabled = true;
			Globals.drawingcamera.camera.enabled = false;
			Destroy (this);
		}
	} else if (Input.GetMouseButton (1)){
	//Move camera along plane
		rotatespeed=500;
		if (Globals.drawingcamera.transform.localPosition.x > -temp  gotostep == 0){
			move=Globals.drawspeed;
			Globals.drawingcamera.transform.localPosition.x = Globals.drawingcamera.transform.localPosition.x-move;
		} else if(gotostep == 0){
			gotostep=1;
		}
		if (Globals.drawingcamera.transform.localEulerAngles.z < 90  gotostep == 1){
			move=Globals.drawspeed * rotatespeed;
			Globals.drawingcamera.transform.localEulerAngles.z = Globals.drawingcamera.transform.localEulerAngles.z+move;
		} else if (gotostep == 1){
			gotostep=2;
			Globals.drawingcamera.transform.localEulerAngles.z=90;
		}
		if (Globals.drawingcamera.transform.localPosition.y > -temp  gotostep == 2){
			move=Globals.drawspeed;
			Globals.drawingcamera.transform.localPosition.y = Globals.drawingcamera.transform.localPosition.y-move;
		} else if (gotostep == 2){
			gotostep=3;
		}
		if (Globals.drawingcamera.transform.localEulerAngles.z < 180  gotostep == 3){
			move=Globals.drawspeed * rotatespeed;
			Globals.drawingcamera.transform.localEulerAngles.z = Globals.drawingcamera.transform.localEulerAngles.z+move;
		} else if (gotostep == 3){
			gotostep=4;
			Globals.drawingcamera.transform.localEulerAngles.z=180;
		}
		if (Globals.drawingcamera.transform.localPosition.x < temp  gotostep == 4){
			move=Globals.drawspeed;
			Globals.drawingcamera.transform.localPosition.x = Globals.drawingcamera.transform.localPosition.x+move;
		} else if (gotostep == 4){
			gotostep=5;
		}
		if (Globals.drawingcamera.transform.localEulerAngles.z < 270  gotostep == 5){
			move=Globals.drawspeed * rotatespeed;
			Globals.drawingcamera.transform.localEulerAngles.z = Globals.drawingcamera.transform.localEulerAngles.z+move;
		} else if (gotostep == 5){
			gotostep=6;
			Globals.drawingcamera.transform.localEulerAngles.z=270;
		}
		if (Globals.drawingcamera.transform.localPosition.y < temp  gotostep == 6){
			move=Globals.drawspeed;
			Globals.drawingcamera.transform.localPosition.y = Globals.drawingcamera.transform.localPosition.y+move;
		} else if (gotostep == 6){
			gotostep=7;
		}
		if (Globals.drawingcamera.transform.localEulerAngles.z >= 270  gotostep == 7  Globals.drawingcamera.transform.localEulerAngles.z < 359.9999 ){
			move=Globals.drawspeed * rotatespeed;
			Globals.drawingcamera.transform.localEulerAngles.z = Globals.drawingcamera.transform.localEulerAngles.z+move;
		} else if (gotostep == 7){
			print("Stopping because camera is at "+Globals.drawingcamera.transform.localEulerAngles.z);
			GameObject.Find("camera1").camera.enabled = true;
			Globals.drawingobject.renderer.enabled = true;
			Globals.drawingcamera.camera.enabled = false;
			Destroy (this);
		}
	}
}

bump

Well I decided to perform the same process I have in the script I posted, but I’ll move the camera closer and closer to the center until I cover the entire square. The only downside to this will be the time it takes for it to complete. It won’t be an instant delete or fill like in photoshop or ms paint, but it will work until I figure something else out.