Unity功能-抠图和裁剪

本文详细介绍了在Unity中如何实现游戏截图的抠图和裁剪功能。通过使用RenderTexture和Camera类,首先进行黑色背景和白色背景的两次渲染,然后通过像素颜色对比计算得到透明度,实现抠图效果。接着,提供了裁剪或拷贝纹理的方法,允许指定源图像的裁剪区域并将其复制到新的目标纹理中。这些技术对于游戏开发中的屏幕截图和UI元素处理非常实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:项目里需要对游戏截图进行抠图和裁剪。

 		/// <summary>
        /// 抠图
        /// </summary>
        /// <param name="texture2D"></param>
        /// <returns></returns>
        private Texture2D GetTexture2D(Texture2D texture2D,int CaptureWidth,int CaptureHeight)
        {
            var camera = Camera.main;
            if (camera == null) return null;

            RenderTexture target = RenderTexture.GetTemporary(CaptureWidth, CaptureHeight, 24, RenderTextureFormat.DefaultHDR);

            CameraClearFlags preClearFlags = camera.clearFlags;
            RenderTexture preTargetTexture = camera.targetTexture;
            RenderTexture preActiveTexture = RenderTexture.active;
            Color preBackgroundColor = camera.backgroundColor;

            camera.clearFlags = CameraClearFlags.Color;
            camera.targetTexture = target;
            RenderTexture.active = target;

            camera.backgroundColor = Color.black;
            camera.Render();
            Texture2D captureBlack = new Texture2D(CaptureWidth, CaptureHeight, TextureFormat.RGBAFloat, false);
            captureBlack.ReadPixels(new Rect(0f, 0f, CaptureWidth, CaptureHeight), 0, 0, false);
            captureBlack.Apply();

            camera.backgroundColor = Color.white;
            camera.Render();
            Texture2D captureWhite = new Texture2D(CaptureWidth, CaptureHeight, TextureFormat.RGBAFloat, false);
            captureWhite.ReadPixels(new Rect(0f, 0f, CaptureWidth, CaptureHeight), 0, 0, false);
            captureWhite.Apply();

            texture2D = new Texture2D(CaptureWidth, CaptureHeight, TextureFormat.RGBAFloat, false);

            for (int x = 0; x < CaptureWidth; ++x)
            {
                for (int y = 0; y < CaptureHeight; ++y)
                {
                    Color lColorWhenBlack = captureBlack.GetPixel(x, y);
                    Color lColorWhenWhite = captureWhite.GetPixel(x, y);
                    Color lColorCombined = (lColorWhenBlack + lColorWhenWhite) * 0.5f;
                    float alphaR = 1 + lColorWhenBlack.r - lColorWhenWhite.r;
                    float alphaG = 1 + lColorWhenBlack.g - lColorWhenWhite.g;
                    float alphaB = 1 + lColorWhenBlack.b - lColorWhenWhite.b;
                    float alpha = (alphaR + alphaG + alphaB) / 3f;
                    Color color = new Color(lColorCombined.r, lColorCombined.g, lColorCombined.b, alpha);
                    texture2D.SetPixel(x, y, color);
                }
            }
            texture2D.Apply();

            RenderTexture.active = preActiveTexture;
            camera.targetTexture = preTargetTexture;
            camera.backgroundColor = preBackgroundColor;
            camera.clearFlags = preClearFlags;

            return texture2D;
        }

        /// <summary>
        /// 裁剪或者拷贝图片,图片的原点在左下角
        /// </summary>
        /// <param name="原图"></param>
        /// <param name="x,y表示从原图的什么位置开始裁剪,w,h表示裁剪的宽高"></param>
        /// <param name="x,y表示拷贝到新的图片中的什么位置,w,h新的图片的宽高"></param>
        /// <returns></returns>
        Texture2D CutOrCopyTexture(Texture2D source, RectInt cutScope, RectInt targetScope)
        {
            Texture2D target = new Texture2D(targetScope.width, targetScope.height, source.format, false);
            Graphics.CopyTexture(source, 0, 0, cutScope.x, cutScope.y, cutScope.width, cutScope.height, target, 0, 0, targetScope.x, targetScope.y);
            return target;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海尔辛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值