[英]Unity flip texture using c#
如何在FlipTextureVertically
中调用 FlipTextureVertically 方法?
我目前拍摄的照片在统一中是颠倒的,我遇到了这个纹理翻转代码,但我不知道如何应用它。
如果有人能在这里帮助我,我将不胜感激!
public static Texture2D FlipTextureVertically(Texture2D original)
{
Texture2D flipped = new Texture2D(original.width, original.height, TextureFormat.ARGB32, false);
int xN = original.width;
int yN = original.height;
for (int i = 0; i < xN; i++)
{
for (int j = 0; j < yN; j++)
{
flipped.SetPixel(i, yN - j - 1, original.GetPixel(i, j));
}
}
flipped.Apply();
return flipped;
}
public string MakePhoto(bool openIt)
{
int resWidth = Screen.width;
int resHeight = Screen.height;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
// hide the info-text, if any
if (infoText)
{
infoText.text = string.Empty;
}
// render background and foreground cameras
if (backroundCamera && backroundCamera.enabled)
{
backroundCamera.targetTexture = rt;
backroundCamera.Render();
backroundCamera.targetTexture = null;
}
if (backroundCamera2 && backroundCamera2.enabled)
{
backroundCamera2.targetTexture = rt;
backroundCamera2.Render();
backroundCamera2.targetTexture = null;
}
if (foreroundCamera && foreroundCamera.enabled)
{
foreroundCamera.targetTexture = rt;
foreroundCamera.Render();
foreroundCamera.targetTexture = null;
}
// get the screenshot
RenderTexture prevActiveTex = RenderTexture.active;
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
// clean-up
RenderTexture.active = prevActiveTex;
Destroy(rt);
byte[] btScreenShot = screenShot.EncodeToJPG();
Destroy(screenShot);
// save the screenshot as jpeg file
string sDirName = Application.persistentDataPath + "/Screenshots";
if (!Directory.Exists(sDirName))
Directory.CreateDirectory (sDirName);
string sFileName = sDirName + "/" + string.Format ("{0:F0}", Time.realtimeSinceStartup * 10f) + ".jpg";
File.WriteAllBytes(sFileName, btScreenShot);
Debug.Log("Photo saved to: " + sFileName);
if (infoText)
{
infoText.text = "Saved to: " + sFileName;
}
// open file
if(openIt)
{
System.Diagnostics.Process.Start(sFileName);
}
return sFileName;
}
我真的不明白为什么屏幕截图应该上下颠倒,但是我想你应该称呼它,例如
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
screenShot = FlipTextureVertically(screenShot);
但是可能会有更有效的方法。
例如,不创建新的Texture2D
,而是仅更改您已有的像素
public static void FlipTextureVertically(Texture2D original)
{
var originalPixels = original.GetPixels();
Color[] newPixels = new Color[originalPixels.Length];
int width = original.width;
int rows = original.height;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < rows; y++)
{
newPixels[x + y * width] = originalPixels[x + (rows - y -1) * width];
}
}
original.SetPixels(newPixels);
original.Apply();
}
并像这样使用
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
FlipTextureVertically(screenShot);
翻转图像的原因是您正在切换代码中的垂直像素。
public static Texture2D FlipTextureVertically(Texture2D original)
{
Texture2D flipped = new Texture2D(original.width, original.height, TextureFormat.ARGB32, false);
int xN = original.width;
int yN = original.height;
for (int i = 0; i < xN; i++)
{
for (int j = 0; j < yN; j++)
{
flipped.SetPixel(i, yN - j - 1, original.GetPixel(i, j));
}
}
flipped.Apply();
return flipped;
}
应该
public static Texture2D FlipTextureVertically(Texture2D original)
{
Texture2D flipped = new Texture2D(original.width, original.height, TextureFormat.ARGB32, false);
int xN = original.width;
int yN = original.height;
for (int i = 0; i < xN; i++)
{
for (int j = 0; j < yN; j++)
{
flipped.SetPixel(xN - i - 1, yN, original.GetPixel(i, j));
}
}
flipped.Apply();
return flipped;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.