242
2015-04-13 09:44:06
1
public static Texture2D RotateTexture(Texture2D texture, float angle, FilterMode filterMode)
{
Point textureSize = new Point(texture.width, texture.height);
Texture2D resultTexture = new Texture2D(textureSize.x, textureSize.y, texture.format, false, false);
resultTexture.filterMode = filterMode;
Vector2 rotation = Vector2.zero;
rotation.x = RotationX(angle, -textureSize.x * 0.5f, -textureSize.y * 0.5f) + (textureSize.x * 0.5f + 0.5f);
rotation.y = RotationY(angle, -textureSize.x * 0.5f, -textureSize.y * 0.5f) + (textureSize.y * 0.5f - 1.0f);
for (float x = 0; x < textureSize.x; x++)
{
Vector2 temp = rotation;
for (float y = 0; y < textureSize.y; y++)
{
temp.x += RotationX(angle, 0.0f, 1.0f);
temp.y += RotationY(angle, 0.0f, 1.0f);
Color color = GetPixel(texture, temp.x, temp.y);
resultTexture.SetPixel((int)Mathf.Floor(x), (int)Mathf.Floor(y), color);
}//for(y)
rotation.x += RotationX(angle, 1.0f, 0.0f);
rotation.y += RotationY(angle, 1.0f, 0.0f);
}//for(x)
resultTexture.Apply();
return resultTexture;
}//Rotate()