Unity Texture2D的裁剪、镜像、翻转、缩放、合并、分辨率 您所在的位置:网站首页 阿迪达斯镜像反转 Unity Texture2D的裁剪、镜像、翻转、缩放、合并、分辨率

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

2024-07-12 05:54| 来源: 网络整理| 查看: 265

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

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

/// /// 裁剪Texture2D /// /// /// /// /// /// /// 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 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; }

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

/// /// 缩放Textur2D /// /// /// /// /// 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 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 //得到图片的宽高 int width = texture.width; int height = texture.height; Texture2D flipTexture = new Texture2D(width, height); for (int i = 0; i Color32[] des = new Color32[src.Length]; Texture2D desTexture = new Texture2D(srcH, srcW); for (int i = 0; i des[(srcW - j - 1) * srcH + i] = src[i * srcW + j]; } } desTexture.SetPixels32(des); desTexture.Apply(); return desTexture; }

逆时针旋转90度

/// /// 图片逆时针旋转90度 /// /// 原图片二进制数据 /// 原图片宽度 /// 原图片高度 /// 输出目标图片 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 des[i * srcH + j] = src[(srcH - 1 - j) * srcW + i]; } } desTexture.SetPixels32(des); desTexture.Apply(); return desTexture; }

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

/// /// 两张图合并 /// /// /// /// /// /// /// /// 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 _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,请自行下载!

/// /// 修改图片水平和垂直像素量 /// 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的裁剪、镜像、翻转、缩放、合并、分辨率

对相机进行截图

/// /// 对相机截图。 /// /// The screenshot2. /// Camera.要被截屏的相机 /// Rect.截屏的区域 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; }

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

/// /// 旋转纹理 /// /// 纹理数据 /// 原图宽 /// 原图高 /// 是否是顺时针旋转 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 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(); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有