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 等软 ...
随机推荐
- Java集合(1)一 集合框架
目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) java集合(4)一 红黑树. ...
- NYOJ 119 士兵杀敌(三) RMQ ST
NYOJ 119 士兵杀敌(三) RMQ ST 题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=119 思路: ST在线 预处理O(nlog ...
- 高质量PHP代码的50个实用技巧必备(上)
1.不要使用相对路径 常常会看到: ? 1 require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录. ...
- Python3实现简单可学习的手写体识别
0.目录 1.前言 2.通过pymssql与数据库的交互 3.通过pyqt与界面的交互 4.UI与数据库的交互 5.最后的main主函数 1.前言 版本:Python3.6.1 + PyQt5 + S ...
- pipelineDB里Combine用法
combine only works on aggregate columns that belong to continuous views. 创建CONTINUOUS CREATE CONTINU ...
- 《Qt on Android核心编程》介绍
<Qt on Android核心编程>最终尘埃落定.付梓印刷了. 2014-11-02更新:china-pub的预售链接出来了.折扣非常低哦. 封面 看看封面的效果吧,历经几版,最终就成了 ...
- 豌豆夹Redis解决方式Codis源代码剖析:Proxy代理
豌豆夹Redis解决方式Codis源代码剖析:Proxy代理 1.预备知识 1.1 Codis Codis就不详细说了,摘抄一下GitHub上的一些项目描写叙述: Codis is a proxy b ...
- laravel会话驱动扩展—连接自定义会话管理系统
laravel 版本:5.3.* 用laravel开发公司信息系统过程中,由于业务或安全问题的考虑,会有一些特殊的用户会话管理方面的需求,如多个子系统会话统一管理或A系统业务操作导致B系统中某些在线用 ...
- JAVA入门[1]--安装JDK
1.下载JDK并安装 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- 在PowerShell脚本中集成Microsoft Graph
作者:陈希章 发表于2017年4月23日 我旗帜鲜明地表态,我很喜欢PowerShell,相比较于此前的Cmd Shell,它有一些重大的创新,例如基于.NET的类型系统,以及管道.模块的概念等等.那 ...