Unity (64bit) - Test.unity - New Unity Project 55 - PC, Mac & Linux Standalone DX11_2

Unity (64bit) - Test.unity - New Unity Project 55 - PC, Mac & Linux Standalone DX11  

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

public UITexture canvas;

public int canvasWidth = 800, canvasHeight = 600;
public Color canvasColor;

public Color drawColor1;

public Texture2D tempCanvas;

void Awake () {
CanvasInit ();
}

void CanvasInit(){
/**************************************************************
* Set Canvas BG Color
**************************************************************/

Texture2D t = new Texture2D (canvasWidth, canvasHeight);
for(int y = 0; y < canvasHeight; y++){
for(int x = 0; x < canvasWidth; x++){
t.SetPixel(x, y, canvasColor);
}
}
t.Apply ();
tempCanvas = t;
canvas.mainTexture = tempCanvas;

/**************************************************************
* Set Canvas Width & Height
**************************************************************/

canvas.width = canvasWidth;
canvas.height = canvasHeight;
}

void Update () {

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

if(Physics.Raycast(ray,out hit)){
if(Input.GetMouseButton(0)){ // If Press Left Mouse Button

/**************************************************************
* World Position To Canvas Texture Position
**************************************************************/

int drawX = (int)((hit.point.x) * Screen.width/2.666666f + canvasWidth/2);
int drawY = (int)((hit.point.y) * Screen.height/2 + canvasHeight/2);

/**************************************************************
* Compute Rotation Position
**************************************************************/

// Euler Angle To Theta Angle;
float theta = canvas.transform.localEulerAngles.z/(180/Mathf.PI);
Vector2 P = RotateSquare(drawX, drawY, canvasWidth, canvasHeight, theta);
drawX = (int) P.x;
drawY = (int) P.y;

/**************************************************************
* Large Brush
**************************************************************/

tempCanvas.SetPixel(drawX, drawY, drawColor1);
tempCanvas.SetPixel(drawX, drawY+1, drawColor1);
tempCanvas.SetPixel(drawX, drawY-1, drawColor1);
tempCanvas.SetPixel(drawX+1, drawY, drawColor1);
tempCanvas.SetPixel(drawX-1, drawY, drawColor1);

/**************************************************************
* Apply New Canvas
**************************************************************/
tempCanvas.Apply ();
canvas.mainTexture = tempCanvas;
}
}

}

/**************************************************************
* Rotation Old X, Y To New X, Y
**************************************************************/
Vector2 RotateSquare(int X, int Y, int W, int H, float phi){

float sin = Mathf.Sin(phi), cos = Mathf.Cos(phi);
int xc = W/2, yc = H/2;
int x = (int)(cos*(X-xc)+sin*(Y-yc)+xc);
int y = (int)(-sin*(X-xc)+cos*(Y-yc)+yc);

return new Vector2(x, y);
}


}

arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(1) 人氣()