unity黑白滤镜_Unity图片处理类,包括压缩、截屏和滤镜

http://www.cnblogs.com/EndOfYear/p/4334952.html

先上代码:

using System.Threading;

using UnityEngine;

using System.IO;

using System.Collections;

public class TextureUtility

{

public class ThreadData

{

public int start;

public int end;

public ThreadData (int s, int e) {

start = s;

end = e;

}

}

private static Color[] texColors;

private static Color[] newColors;

private static int w;

private static float ratioX;

private static float ratioY;

private static int w2;

private static int finishCount;

private static Mutex mutex;

public static void ScalePoint (Texture2D tex, int newWidth, int newHeight)

{

ThreadedScale (tex, newWidth, newHeight, false);

}

public static void ScaleBilinear (Texture2D tex, int newWidth, int newHeight)

{

ThreadedScale (tex, newWidth, newHeight, true);

}

private static void ThreadedScale (Texture2D tex, int newWidth, int newHeight, bool useBilinear)

{

texColors = tex.GetPixels();

newColors = new Color[newWidth * newHeight];

if (useBilinear)

{

ratioX = 1.0f / ((float)newWidth / (tex.width-1));

ratioY = 1.0f / ((float)newHeight / (tex.height-1));

}

else {

ratioX = ((float)tex.width) / newWidth;

ratioY = ((float)tex.height) / newHeight;

}

w = tex.width;

w2 = newWidth;

var cores = Mathf.Min(SystemInfo.processorCount, newHeight);

var slice = newHeight/cores;

finishCount = 0;

if (mutex == null) {

mutex = new Mutex(false);

}

if (cores > 1)

{

int i = 0;

ThreadData threadData;

for (i = 0; i < cores-1; i++) {

threadData = new ThreadData(slice * i, slice * (i + 1));

ParameterizedThreadStart ts = useBilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale);

Thread thread = new Thread(ts);

thread.Start(threadData);

}

threadData = new ThreadData(slice*i, newHeight);

if (useBilinear)

{

BilinearScale(threadData);

}

else

{

PointScale(threadData);

}

while (finishCount < cores)

{

Thread.Sleep(1);

}

}

else

{

ThreadData threadData = new ThreadData(0, newHeight);

if (useBilinear)

{

BilinearScale(threadData);

}

else

{

PointScale(threadData);

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值