Unity Texture2D的裁剪、镜像、翻转、缩放、合并、分辨率

转载自:Unity Texture2D的裁剪、镜像、翻转、缩放、合并、分辨率

裁剪,将贴图上的某个区域裁剪

    /// <summary>
    /// 裁剪Texture2D
    /// </summary>
    /// <param name="originalTexture"></param>
    /// <param name="offsetX"></param>
    /// <param name="offsetY"></param>
    /// <param name="originalWidth"></param>
    /// <param name="originalHeight"></param>
    /// <returns></returns>
    public static Texture2D ScaleTextureCutOut(Texture2D originalTexture, int offsetX,int offsetY, float originalWidth, float originalHeight)
    {
        Texture2D newTexture = new Texture2D(Mathf.CeilToInt(originalWidth), Mathf.CeilToInt(originalHeight));
        int maxX = originalTexture.width - 1;
        int maxY = originalTexture.height - 1;
        for (int y = 0; y < newTexture.height; y++)
        {
            for (int x = 0; x < newTexture.width; x++)
            {
                float targetX = x + offsetX;
                float targetY = y + offsetY;
                int x1 = Mathf.Min(maxX, Mathf.FloorToInt(targetX));
                int y1 = Mathf.Min(maxY, Mathf.FloorToInt(targetY));
                int x2 = Mathf.Min(maxX, x1 + 1);
                int y2 = Mathf.Min(maxY, y1 + 1);
 
                float u = targetX - x1;
                float v = targetY - y1;
                float w1 = (1 - u) * (1 - v);
                float w2 = u * (1 - v);
                float w3 = (1 - u) * v;
                float w4 = u * v;
                Color color1 = originalTexture.GetPixel(x1, y1);
                Color color2 = originalTexture.GetPixel(x2, y1);
                Color color3 = originalTexture.GetPixel(x1, y2);
                Color color4 = originalTexture.GetPixel(x2, y2);
                Color color = new Color(Mathf.Clamp01(color1.r * w1 + color2.r * w2 + color3.r * w3 + color4.r * w4),
                                        Mathf.Clamp01(color1.g * w1 + color2.g * w2 + color3.g * w3 + color4.g * w4),
                                        Mathf.Clamp01(color1.b * w1 + color2.b * w2 + color3.b * w3 + color4.b * w4),
                                        Mathf.Clamp01(color1.a * w1 + color2.a * w2 + color3.a * w3 + color4.a * w4)
                                        );
                newTexture.SetPixel(x, y, color);
            }
        }
        newTexture.anisoLevel = 2;
        newTexture.Apply();
        return newTexture;
    }

缩放,缩放和放大原有贴图

/// <summary>
/// 缩放Textur2D
/// </summary>
/// <param name="source"></param>
/// <param name="targetWidth"></param>
/// <param name="targetHeight"></param>
/// <returns></returns>
public static Texture2D ScaleTexture(Texture2D source, float targetWidth, float targetHeight)
{
        Texture2D result = new Texture2D((int)targetWidth, (int)targetHeight, source.format, false);
 
        float incX = (1.0f / targetWidth);
        float incY = (1.0f / targetHeight);
 
        for (int i = 0; i < result.height; ++i)
        {
            for (int j = 0; j < result.width; ++j)
            {
                Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
                result.SetPixel(j, i, newColor);
            }
        }
 
        result.Apply();
        return result;
}

水平镜像

//水平翻转
    public static Texture2D HorizontalFlipTexture(Texture2D texture)
    {
        //得到图片的宽高
        int width = texture.width;
        int height = texture.height;
 
        Texture2D flipTexture = new Texture2D(width, height);
 
        for (int i = 0; i < width; i++)
        {
            flipTexture.SetPixels(i, 0, 1, height, texture.GetPixels(width - i - 1, 0, 1, height));
        }
        flipTexture.Apply();
 
        return flipTexture;
    }

垂直镜像

// 垂直翻转
    public static Texture2D VerticalFlipTexture(Texture2D texture)
    {
        //得到图片的宽高
        int width = texture.width;
        int height = texture.height;
 
        Texture2D flipTexture = new Texture2D(width, height);
        for (int i = 0; i < height; i++)
        {
            flipTexture.SetPixels(0, i, width, 1, texture.GetPixels(0, height - i - 1, width, 1));
        }
        flipTexture.Apply();
        return flipTexture;
    }

顺时针旋转90度

/// <summary>
    /// 图片顺时针旋转90度
    /// </summary>
    /// <param name="src">原图片二进制数据</param>
    /// <param name="srcW">原图片宽度</param>
    /// <param name="srcH">原图片高度</param>
    /// <param name="desTexture">输出目标图片</param>
    public static Texture2D RotationRight90(Color32[] src, int srcW, int srcH)
    {
 
        Color32[] des = new Color32[src.Length];
        Texture2D desTexture = new Texture2D(srcH, srcW);
 
        for (int i = 0; i < srcH; i++)
        {
            for (int j = 0; j < srcW; j++)
            {
                des[(srcW - j - 1) * srcH + i] = src[i * srcW + j];
            }
        }
 
        desTexture.SetPixels32(des);
        desTexture.Apply();
        return desTexture;
    }

逆时针旋转90度

/// <summary>
    /// 图片逆时针旋转90度
    /// </summary>
    /// <param name="src">原图片二进制数据</param>
    /// <param name="srcW">原图片宽度</param>
    /// <param name="srcH">原图片高度</param>
    /// <param name="desTexture">输出目标图片</param>
    public static Texture2D RotationLeft90(Color32[] src, int srcW, int srcH)
    {
        Color32[] des = new Color32[src.Length];
        Texture2D desTexture = new Texture2D(srcH, srcW);
        //if (desTexture.width != srcH || desTexture.height != srcW)
        //{
        //    desTexture.Resize(srcH, srcW);
        //}
 
        for (int i = 0; i < srcW; i++)
        {
            for (int j = 0; j < srcH; j++)
            {
                des[i * srcH + j] = src[(srcH - 1 - j) * srcW + i];
            }
        }
 
        desTexture.SetPixels32(des);
        desTexture.Apply();
        return desTexture;
    }

两张贴图合并,可以实现水印等功能,该代码是我实现3行3列9张相同照片排列

    /// <summary>
    /// 两张图合并
    /// </summary>
    /// <param name="_baseTexture2D"></param>
    /// <param name="_texture2D"></param>
    /// <param name="_x"></param>
    /// <param name="_y"></param>
    /// <param name="_w"></param>
    /// <param name="_h"></param>
    /// <returns></returns>
    public static Texture2D MergeImage(Texture2D _baseTexture2D, Texture2D _texture2D,int _x,int _y,int _w,int _h)
    {
        //取图
        Color32[] color = _texture2D.GetPixels32(0);
        for (int j = 0; j < 3; j++)
        {
            for (int i = 0; i < 3; i++)
            {
                _baseTexture2D.SetPixels32(_x + i * (_texture2D.width+ _w), _y + j * (_texture2D.height+_h), _texture2D.width, _texture2D.height, color); //宽度
            }
        }
        //应用
        _baseTexture2D.Apply();
        return _baseTexture2D;
    }

修改图片的水平和垂直分辨率(像素/英寸,每英寸距离的图像包含的像素数目。)

Bitmap操作需要用到System.Drawing.dll,请自行下载!

    /// <summary>
    /// 修改图片水平和垂直像素量
    /// </summary>
    public static Texture2D SetPictureResolution(string _path)
    {
        Bitmap bm = new Bitmap(_path);
        bm.SetResolution(300, 300);
        string _idPath = Application.persistentDataPath + "/";
        string _name = "print.jpg";
        bm.Save(_idPath + _name, ImageFormat.Jpeg);
        Texture2D tex = loadTexture(_idPath, _name);
        File.WriteAllBytes(Application.persistentDataPath + "/aa.jpg", tex.EncodeToJPG());
        bm.Dispose();
        return tex;
    }

转载自:Unity Texture2D的裁剪、镜像、翻转、缩放、合并、分辨率

对相机进行截图

/// <summary>
    /// 对相机截图。 
    /// </summary>
    /// <returns>The screenshot2.</returns>
    /// <param name="camera">Camera.要被截屏的相机</param>
    /// <param name="rect">Rect.截屏的区域</param>
    private Texture2D CaptureCamera(Camera camera,out Sprite sprite,string filename ="")
    {
        Rect rect = new Rect(0, 0, Screen.width, Screen.height);
        // 创建一个RenderTexture对象
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
        camera.targetTexture = rt;
        camera.Render();
        //ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
        //ps: camera2.targetTexture = rt;
        //ps: camera2.Render();
        //ps: -------------------------------------------------------------------
 
        // 激活这个rt, 并从中中读取像素。
        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();
        sprite = Sprite.Create(screenShot, new Rect(0, 0, screenShot.width, screenShot.height),
            new Vector2(0.5f, 0.5f));
    
        // 重置相关参数,以使用camera继续在屏幕上显示
        camera.targetTexture = null;
        //ps: camera2.targetTexture = null;
        RenderTexture.active = null; // JC: added to avoid errors
        GameObject.Destroy(rt);
        if (filename.Length > 0)
        {
            // 最后将这些纹理数据,成一个png图片文件
            byte[] bytes = screenShot.EncodeToPNG();
            System.IO.File.WriteAllBytes(filename, bytes);
            Debug.Log(string.Format("截屏了一张照片: {0}", filename));
        }
        return screenShot;
    }

经测试,旋转用以下代码可实现

/// <summary>
    /// 旋转纹理
    /// </summary>
    /// <param name="original">纹理数据</param>
    /// <param name="srcW">原图宽</param>
    /// <param name="srcH">原图高</param>
    /// <param name="clockwise">是否是顺时针旋转</param>
    void RotateTexture(Color32[] original, int srcW, int srcH, bool clockwise)
    {

        int w = srcW;
        int h = srcH;

        int iRotated, iOriginal;
        Color32[]des = new Color32[w * h];
        Texture2D desTexture = new Texture2D(h, w);

        for (int j = 0; j < h; ++j)
        {
            for (int i = 0; i < w; ++i)
            {
                iRotated = (i + 1) * h - j - 1;
                iOriginal = clockwise ? original.Length - 1 - (j * w + i) : j * w + i;
                des[iRotated] = original[iOriginal];
            }
        }


        desTexture.SetPixels32(des);
        desTexture.Apply();
  
    }
  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值