c# - Unity使用c#翻转纹理

标签 c# unity-game-engine

如何在 MakePhoto 中调用方法 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();

    var newPixels = new Color[originalPixels.Length];

    var width = original.width;
    var rows = original.height;

    for (var x = 0; x < width; x++)
    {
        for (var y = 0; y < rows; y++)
        {
            newPixels[x + y * width] = originalPixels[x + (rows - y -1) * width];
        }
    }

    original.SetPixels(newPixels);
    original.Apply();
}

public static void FlipTextureHorizontally(Texture2D original)
{
    var originalPixels = original.GetPixels();

    var newPixels = new Color[originalPixels.Length];

    var width = original.width;
    var rows = original.height;

    for (var x = 0; x < width; x++)
    {
        for (var y = 0; y < rows; y++)
        {
            newPixels[x + y * width] = originalPixels[(width - x - 1) + y * width];
        }
    }

    original.SetPixels(newPixels);
    original.Apply();
}

并像这样使用它

screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);

FlipTextureVertically(screenShot);

关于c# - Unity使用c#翻转纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54623187/

相关文章:

c# - 无法使用 Ninject 将依赖项注入(inject)从 Angular 服务调用的 ASP.NET Web API Controller

unity-game-engine - Unity 游戏在集成 admob 广告后出现滞后

c# - 如何在Unity(c#)中创建sqlite数据库?

unity-game-engine - Unity .unity-文件(场景)在 IDE 中打开,而不是在 Unity3D 中打开

c# - 自动登录到我们自己的 OAuth 应用程序

c# - 添加图钉以从 Windows Phone 8 中的代码隐藏进行映射

c# - 使用 C# 和 .NET 从集会下载附件

unity-game-engine - Unity 创建点与点之间有间隙的线

android - Facebook Android SDK 4.2.4 无法登录并且 isLogged 使用 Unity 4.3 返回 false

c# - ASP.NET MVC 静态存储库?