unity图片后期处理
处理算法如下,在Start方法中分别调用想要的效果就行了。其中,将你需要处理的 图片 拖拽到 image参数上。注意,如果想要图片保持原来的尺寸不被压缩,需要更改图片的导入设置如下图,主要的Texture Type 和 Non Power of 2 这两个参数。

using UnityEngine;
using System.Collections;
using System.IO; public class ImageDeal : MonoBehaviour
{
public Texture2D image;
private Texture2D outPut;
int Width;
int Height;
// Use this for initialization
void Start ()
{
Width = image.width;
Height = image.height;
outPut = new Texture2D (Width, Height);
Flur (); writeImage ();
} /// <summary>
/// 底片效果.
/// </summary>
private void Dipian ()
{
Color pixel;
for (int x = ; x < Width; x++) {
for (int y = ; y < Height; y++) {
float r, g, b;
pixel = image.GetPixel (x, y);
r = 1f - pixel.r;
g = 1f - pixel.g;
b = 1f - pixel.b;
outPut.SetPixel (x, y, new Color (r, g, b));
}
}
} /// <summary>
/// 浮雕效果
/// </summary>
private void Fudiao ()
{
//以浮雕效果显示图像
Color pixel1, pixel2;
for (int x = ; x < Width - ; x++) {
for (int y = ; y < Height - ; y++) {
float r = , g = , b = ;
pixel1 = image.GetPixel (x, y);
pixel2 = image.GetPixel (x + , y + );
r = Mathf.Abs (pixel1.r - pixel2.r + / );
g = Mathf.Abs (pixel1.g - pixel2.g + / );
b = Mathf.Abs (pixel1.b - pixel2.b + / ); outPut.SetPixel (x, y, new Color (r, g, b));
}
} }
/// <summary>
/// 黑白效果
/// </summary>
private void Heibai(){
Color pixel;
for (int x = ; x < Width; x++)
for (int y = ; y < Height; y++) {
pixel = image.GetPixel (x, y);
float r, g, b, Result = ;
r = pixel.r;
g = pixel.g;
b = pixel.b;
//实例程序以加权平均值法产生黑白图像
int iType = ;
switch (iType) {
case ://平均值法
Result = ((r + g + b) / );
break;
case ://最大值法
Result = r > g ? r : g;
Result = Result > b ? Result : b;
break;
case ://加权平均值法
Result = ((0.7f * r) + (0.2f * g) + (0.1f * b));
break;
}
outPut.SetPixel (x, y, new Color (Result, Result, Result));
}
}
/// <summary>
/// 柔化3x3高斯模型
/// </summary>
private void Rouhua3(){
Color pixel;
int[] Gauss ={ , , , , , , , , };
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
float r = , g = , b = ;
int Index = ;
for (int col = -; col <= ; col++)
for (int row = -; row <= ; row++)
{
pixel = image.GetPixel(x + row, y + col);
r += pixel.r * Gauss[Index];
g += pixel.g * Gauss[Index];
b += pixel.b * Gauss[Index];
Index++;
}
r /= ;
g /= ;
b /= ;
outPut.SetPixel(x - , y - , new Color(r, g, b));
}
}
/// <summary>
/// 柔化5x5高斯模型
/// </summary>
private void Rouhua5(){
Color pixel;
int[] Gauss = { , , , , , , , , , , , , , , , , , , , , , , , , };
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
float r = , g = , b = ;
int Index = ;
for (int col = -; col <= ; col++)
for (int row = -; row <= ; row++)
{
pixel = image.GetPixel(x + row, y + col);
r += pixel.r * Gauss[Index];
g += pixel.g * Gauss[Index];
b += pixel.b * Gauss[Index];
Index++;
}
r /= ;
g /= ;
b /= ;
outPut.SetPixel(x - , y - , new Color(r, g, b));
}
}
/// <summary>
/// 柔化,高斯模糊通用方法
/// </summary>
private void Flur(){
Color pixel;
float[] Gauss = GaussianSmooth (10.0f);
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
float r = , g = , b = ;
int Index = ;
int length = (int)Mathf.Sqrt (Gauss.Length) / ;
for (int col = -length; col <= length; col++)
for (int row = -length; row <= length; row++)
{
pixel = image.GetPixel(x + row, y + col);
r += pixel.r * Gauss[Index];
g += pixel.g * Gauss[Index];
b += pixel.b * Gauss[Index];
Index++;
}
outPut.SetPixel(x - , y - , new Color(r, g, b));
}
}
/// <summary>
/// 锐化效果
/// </summary>
private void Ruihua(){
Color pixel;
//拉普拉斯模板
int[] Laplacian ={ -, -, -, -, , -, -, -, - };
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
float r = , g = , b = ;
int Index = ;
for (int col = -; col <= ; col++)
for (int row = -; row <= ; row++)
{
pixel = image.GetPixel(x + row, y + col);
r += pixel.r * Laplacian[Index];
g += pixel.g * Laplacian[Index];
b += pixel.b * Laplacian[Index];
Index++;
}
outPut.SetPixel(x - , y - , new Color(r, g, b));
}
}
/// <summary>
/// 写文件
/// </summary>
private void writeImage ()
{
byte[] bytes = outPut.EncodeToJPG ();
File.WriteAllBytes (Application.dataPath + "/test.jpg", bytes);
} /// <summary>
/// 动态生成高斯模型
/// </summary>
/// <returns>The smooth.</returns>
/// <param name="sigma">Sigma.</param>
private float[] GaussianSmooth( float sigma)
{
sigma = sigma > ? sigma : -sigma;
//高斯核矩阵的大小为(6*sigma+1)*(6*sigma+1)
//ksize为奇数
int ksize = (int)Mathf.Ceil(sigma * ) * + ; //计算一维高斯核
float[] kernel = new float[ksize]; float scale = -0.5f / (sigma * sigma);
const float PI = 3.141592653f;
float cons = / Mathf.Sqrt(-scale / PI); float sum = ;
int kcenter = ksize/;
int i = , j = ;
for(i = ; i < ksize; i++)
{
int x = i - kcenter;
kernel[i] = cons * Mathf.Exp(x * x * scale);//一维高斯函数
sum +=kernel[i]; }
//归一化,确保高斯权值在[0,1]之间
for(i = ; i < ksize; i++)
{
kernel[i] = kernel[i] / sum;
}
return kernel;
}
}
unity图片后期处理的更多相关文章
- Unity 图片的灰度处理
我们平时在做项目时,经常遇到按钮的点击而且还要区分悬浮,点击,禁用的状态,美术要针对一张图片做多个状态图片,资源图片的数量也就增大了,那么打出的包的大小也就跟着上去了,所以我们可以针对原始图片进行Sh ...
- Unity 图片分割将spirte保存在本地
如果你拿到的是一张整图,你想分割之后使用NGUI sprite来使用! 下面就能解决的需求. 步骤: 1. 使用Unity自带的spirte进行分割图片 2. 使用代码把分割出来的2DSpirte转 ...
- Unity图片处理类,包括压缩、截屏和滤镜
先上代码: 1 using System.Threading; using UnityEngine; using System.IO; using System.Collections; public ...
- unity 图片 粉碎效果 破碎效果
效果: 点击按钮后: 这些碎片具有物理碰撞效果,下面会有隐形的支柱垫着碎片,n秒后支柱消失,碎片落下 当然你也可以控制生成的碎片,让他们从下而上一块一块地落下 插件源码: https://github ...
- unity 图片变纯色填充
unity自带shader 即可
- unity图片保留周边,中间延伸
1.先把图片切割,类似下面这样的 2.然后在使用的时候(选择图片类型的时候选择sliced)
- Unity图片变灰的方式
http://www.tuicool.com/articles/Vruuqme NGUI中的Button差点儿是最经常使用到的控件之中的一个,而且能够组合各种组件(比方UIButtonColor,UI ...
- Unity Texture 2D Compress
测试了一下 unity 图片 对 apk 的影响. 上两种测试环境 1024 * 1024 带 alpha的话 默认压缩就是RBA 16bit就是2M 不带的话就是 etc 的话 ...
- 【优秀的图片后期编辑工具】Luminar 3.1 for Mac
[简介] 今天和大家分享最新的 Luminar for Mac 3.1 版本,支持中文界面,Luminar是一款Mac上优秀的图片后期处理工具,功能类似 Photoshop Lightroom 等软 ...
随机推荐
- 工程启动加载.properties/.xml配置文件
工程目录: demo.properties today=2017-06-10 PropertiesMap.java package com.dzpykj.common.utils; import ja ...
- Tp3.2 和 Tp5.0之间的区别
5.0版本和之前版本的差异较大,本篇对熟悉3.2版本的用户给出了一些5.0的主要区别. URL和路由 5.0的URL访问不再支持普通URL模式,路由也不支持正则路由定义,而是全部改为规则路由配合变量规 ...
- SQL Server Profiler追踪数据库死锁
- day2、Linux别名
Linux中修改配置别名 ####用到的命令: alias是用来查看系统中有什么别名 source 让配置生效 临时取消别名的方法 unalias 临时取消别名 \cp /mnt/test.txt / ...
- Mysql----关于内联,左联,右联,全联的使用和理解
准备工作:新建两张表 表一:student 填充内容:编号,姓名,班级 表二:school 填充内容:编号,班级,专业 这两张表建好了,意为班级选课表,两张表没有任何主外键的关系,下面进行内联,左联, ...
- TCP/IP具体解释--UDP数据报中的IP分片
1.UDP首部 2.UDP分片 在第二章,讲链路层是,提到过以太网.刨除数据帧帧头.最多传输的长度为1500.也就是说,假设一个ip数据报,长度大于1500,则须要分片. 分片方法: 在ip头中3位标 ...
- 最全Pycharm教程(32)——依据FHS在Linux上安装Pycharm
1.主题 怎样在Linux上安装Pycharm,依据FHS. 2.平台要求 Intel Pentium III/800 MHz或更高. 内存最小512M.建议1G以上 屏幕最小分辨率1024x768 ...
- 【日常学习】【线性DP】codevs1044 拦截导弹题解
题目描写叙述 Description 某国为了防御敌国的导弹突击,发展出一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以到达随意的高度,可是以后每一发炮弹都不能高于前一发的高 ...
- F01:金融学第一定律:时间的价值
很小的时候,我们就听大人们讲过:时间就是金钱. 长大了,也觉得这句话应该是对的.我们很珍惜时间,但是似乎没有看到金钱的急剧增加啊.总之,这是模糊,体验感又不强的一句话. 既然谈到时间的价值,价值最容易 ...
- spring boot项目如何测试,如何部署
有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...