美文网首页Unity技术分享Unity教程合集
U3D橡皮擦效果 同时自适应图片缩放 位移和旋转

U3D橡皮擦效果 同时自适应图片缩放 位移和旋转

作者: Babybus_Unity | 来源:发表于2015-12-17 17:24 被阅读1138次
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橡皮擦效果 同时自适应图片缩放 位移和旋转

    本文链接:https://www.haomeiwen.com/subject/mkjphttx.html