解决Unity3D中RenderTexture转存png文件,颜色变暗的问题

关于在Unity3D当中截屏内容,或者某个相机的内容,保存到图片文件的方法。

一般的做法是将Camera的targetTexture指定一个RenderTexture,然后将这个RenderTexture转成PNG文件。下面是代码,一般搞Unity应该都能看懂。但是会有一个问题,就是输出的PNG文件会变暗。

2022-09-01 针对这个问题,又有新的认识。

可以参考官方Unity文档,讲的很清楚。链接关于sRGB

核心就在于创建RenderTexture时构造函数的参数和TextureFormat类型。

描述
RenderTexture 的色彩空间转换模式。

使用 Gamma 颜色空间时,不会进行任何类型的转换,也不会使用此设置。

当使用线性颜色空间时,默认情况下非 HDR 渲染纹理被认为包含 sRGB 数据(即“常规颜色”),并且片段着色器被认为输出线性颜色值。所以默认情况下,片段着色器颜色值在渲染成纹理时会转换为 sRGB;当在着色器中对纹理进行采样时,sRGB 颜色会转换为线性值。这是sRGB读写模式;并且默认模式与使用线性色彩空间时的模式相匹配。在渲染纹理上设置此模式时,RenderTexture.sRGB 将返回 true。

但是,如果您的渲染纹理将包含非颜色数据(法线、速度、其他自定义值),那么您不希望发生 Linear<->sRGB 转换。这是线性读写模式。在渲染纹理上设置此模式时,RenderTexture.sRGB 将返回 false。

请注意,某些渲染纹理格式始终被认为包含“线性”数据,并且无论读写设置如何,都不会对其执行 sRGB 转换。这适用于所有“HDR”(浮点)格式以及其他格式,如深度或阴影贴图。

另请参阅:线性色彩空间、RenderTexture.sRGB、PlayerSettings.colorSpace、GL.sRGBWrite。

特性
默认 基于项目设置的默认色彩空间转换。
线性渲染纹理包含线性(非颜色)数据;不要对其执行颜色转换。
sRGB 渲染纹理包含 sRGB(颜色)数据,对其执行线性<->sRGB 转换。

 Texture2D CaptureCamera(Camera camera, Rect rect, RenderTexture rt, string fname)
    {
        // 创建一个RenderTexture对象  
        //RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);

        // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机  
        camera.targetTexture = rt;
        camera.Render();

        // 激活这个rt, 并从中中读取像素。
        RenderTexture backup = RenderTexture.active;
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素  
        screenShot.Apply();

        // 重置相关参数,以使用camera继续在屏幕上显示  
        camera.targetTexture = null;
        RenderTexture.active = backup;
        
        GameObject.Destroy(rt);

        // 最后将这些纹理数据,成一个png图片文件  
        byte[] bytes = screenShot.EncodeToPNG();
        string fullname = Application.dataPath + "/" + fname + ".png";
        System.IO.File.WriteAllBytes(fullname, bytes);

        return screenShot;
    }

画面中的样子

 输出图片的样子

 这是因为Texture的sRGB的问题。由于在编辑器中建立的RenderTexture默认的sRGB是false的,

所以输出内容,是没有经过gamma矫正的,所以是线性颜色空间的内容。导成png就会变暗。

解决办法:

rendertexture不通过editor创建,而是通过代码动态创建。

RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);

或者

RenderTexture rt = new RenderTexture(rt.width, rt.height, rt.depth, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);

这样得到的rt.sRGB是true的。

最终encodePNG以后得到图片不会变黑。

Unity中,如果你想要从RenderTexture上移除白色的背景并将其保存为PNG图片,你可以通过以下步骤操作: 1. 首先,确保你有一个渲染到RenderTexture的游戏物体,这个物体通常会有一个Camera组件用于捕获画面。 2. 创建一个脚本来处理RenderTexture。你可以创建一个新的C#脚本,例如`RemoveBackgroundScript.cs`,然后添加以下内容: ```csharp using UnityEngine; using System.IO; using UnityEngine.UI; public class RemoveBackgroundScript : MonoBehaviour { public RenderTexture renderTexture; // 你的RenderTexture组件 private Texture2D texture; void Start() { // 获取RenderTexture的内容并转换为Texture2D if (renderTexture != null) { texture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGB24, false); texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); texture.Apply(); } } public void SaveAsPNG(string filePath) { // 如果有白色背景,可以尝试将Alpha通道设置为非透明 Color[] colors = texture.GetPixels32(); for (int i = 0; i < colors.Length; i++) { if (colors[i].a == 1 && Vector3接近白色(colors)) { colors[i].a = 0; } } texture.SetPixels32(colors); texture.Apply(); // 保存为PNG文件 byte[] bytes = texture.EncodeToPNG(); File.WriteAllBytes(filePath, bytes); } private bool Vector3接近白色(Color color) { float r = Mathf.Abs(color.r - 1); float g = Mathf.Abs(color.g - 1); float b = Mathf.Abs(color.b - 1); return r <= 0.05 && g <= 0.05 && b <= 0.05; } } ``` 3. 将此脚本附加到游戏中的某个物体上,并在SaveAsPNG()方法中指定你想要保存的文件路径。 4. 使用的时候,可以在适当的时间调用`SaveAsPNG(path)`函数,其中`path`是你希望保存的PNG文件的完整路径,比如`"Assets/ExportedImages/myImage.png"`。 注意:这种方法可能并不能完美地移除所有的白色背景,特别是如果背景中的白点不是纯白色的情况。对于更复杂的需求,你可能需要使用专门的图像处理库或算法来精确处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值