Question by
UnityGISTech · Nov 06, 2017 at 11:38 AM ·
textureintersectionsetpixels
Clear intersection Texture
Hi All . Im trying to make a code to clear a texutre using mouse like brush.i wort this code to clear the intersection between tow cercles (sprirtes) i can't do that automaticaly for that i tryed SetPixels but that not work well too. Any one can help for that please!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test_03 : MonoBehaviour {
public Camera cam;
private float Pixel_x;
private float Pixel_y;
private Texture2D tex;
//float Brush_Size=0.1f;
void Start()
{
cam = GetComponent<Camera>();
}
private void DrawCircle(float x,float y, float r)
{
float PI = 3.14159226535f;
float i, angle;
double x1, y1;
for (i=0;i<360;i+=10f)
{
angle = i;
x1 =(float) r * System.Math.Cos(angle * PI / 180);
y1= (float)r * System.Math.Sin(angle * PI / 180);
var pixelUV = new Vector2(x+(float)x1, y+(float)y1);
Debug.Log(pixelUV);
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
tex.SetPixel((int)pixelUV.x, (int)pixelUV.y, Color.clear);
tex.Apply();
}
}
void Update()
{
if (Input.GetMouseButton(0))
{
RaycastHit hit;
if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit))
return;
Renderer rend = hit.transform.GetComponent<Renderer>();
MeshCollider meshCollider = hit.collider as MeshCollider;
if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
return;
tex = rend.material.mainTexture as Texture2D;
Pixel_x = hit.textureCoord.x;
Pixel_y = hit.textureCoord.y;
var r = 0.001f;
DrawCircle(Pixel_x, Pixel_y, r);
}
}
}
sans-titre.png
(59.3 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612142452im_/https://answers.unity.com/themes/thub/images/avi.jpg)