using UnityEngine;
using System.Collections;
public class Erasure : MonoBehaviour {
private UIWidget _UIWidget;
private Texture2D _texture2d;
public int brushSize;
private Vector2 mousePoint;
private int texHigh;
private int texWidth;
private Color[] colors;
private float texSizeX;
private float texSizeY;
private float proportionX;
private float proportionY;
private Vector3 screenPosition;
private float degree;
// Use this for initialization
void Start () {
_UIWidget = GetComponent<UIWidget>();
_texture2d = (Texture2D)_UIWidget.mainTexture;
texHigh = _texture2d.height;
texWidth = _texture2d.width;
texSizeX = _UIWidget.width;
texSizeY = _UIWidget.height;
UIRoot.list[0].manualWidth = Screen.width;
UIRoot.list[0].maximumHeight = Screen.height;
colors = _texture2d.GetPixels();
proportion\= proportionBetweenScreenAndTextureX();
proportion\= proportionBetweenScreenAndTextureY();
screenPosition = UICamera.mainCamera.WorldToScreenPoint(transform.position);
brushSize = 20;
degree = transform.eulerAngles.z;
Debug.Log(degree);
Debug.Log(degree * Mathf.Deg2Rad);
}
// Update is called once per frame
void FixedUpdate() {
if(Input.GetMouseButton(0))
{
Erase(Input.mousePosition);
}
}
void Erase(Vector2 mousePosition)
{
mousePoint.x = mousePosition.x - screenPosition.x;
mousePoint.y = mousePosition.y - screenPosition.y;
int x = (int)(mousePoint.x - (transform.position.x - texSizeX / 2));
int y = (int)(mousePoint.y - (transform.position.y - texSizeY / 2));
float tempX = x - texWidth / 2;
float tempY = y - texHigh / 2;
float ragit1 = Mathf.Atan2(tempY, tempX);
float r = Mathf.Sqrt(tempX * tempX + tempY * tempY);
float x2 = (r * (Mathf.Cos(ragit1) * Mathf.Cos(degree * -Mathf.Deg2Rad) - Mathf.Sin(ragit1) * Mathf.Sin(degree * -Mathf.Deg2Rad)));
float y2 = (r * (Mathf.Sin(ragit1) * Mathf.Cos(degree * -Mathf.Deg2Rad) + Mathf.Cos(ragit1) * Mathf.Sin(degree * -Mathf.Deg2Rad)));
x2 += texWidth / 2;
y2 += texHigh / 2;
for(int i = -brushSize;i<=brushSize;i++)
for(int j = -brushSize;j<=brushSize;j++)
{
if (i*i + j*j < brushSize * brushSize)
{
_texture2d.SetPixel((int)((i + x2) / proportionX), (int)((j + y2) / proportionY), new Color(0, 0, 0, 0));
}
}
_texture2d.Apply();
}
float proportionBetweenScreenAndTextureX()
{
return texSizeX / texWidth;
}
float proportionBetweenScreenAndTextureY()
{
return texSizeY / texHigh;
}
void OnGUI()
{
if (GUI.Button(new Rect(Screen.width * 0.1f, Screen.height * 0.1f, 100, 50), "Rest Color"))
{
_texture2d.SetPixels(colors);
_texture2d.Apply();
}
}
void OnEnable()
{
}
void OnDisable()
{
_texture2d.SetPixels(colors);
_texture2d.Apply();
}
void OnDrag(Vector2 delta)
{
}
void OnClick()
{
}
void OnDragEnd()
{
}
}
U3D橡皮擦效果 同时自适应图片缩放 位移和旋转
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...