写在Shader前, unity数字图像处理 下

原文出自:   http://blog.csdn.net/u010019717/article/details/52123934



孙广东  2016.8.4  

参考:  http://lolikitty.pix.net/ 

http://blog.csdn.net/u010019717

注意:  不是Shader  代码

有上自然有下,  有下自然有上

写在Shader前, unity数字图像处理 上

 

4、模糊

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Test : MonoBehaviour  
  5. {  
  6.   
  7.     public Texture2D t;  
  8.   
  9.     [Range(0, 15)]  
  10.     public int intensity = 2;  
  11.   
  12.     Texture2D tt;  
  13.   
  14.     void Start()  
  15.     {  
  16.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  17.         //InvokeRepeating("MyUpdate", 0, 3);  
  18.   
  19.         MyUpdate();  
  20.     }  
  21.   
  22.     void MyUpdate()  
  23.     {  
  24.         if (tt != null)  
  25.         {  
  26.             Destroy(tt);  
  27.         }  
  28.         tt = new Texture2D(t.width, t.height);  
  29.         for (int y = 1; y < t.height - 1; y++)  
  30.         {  
  31.             for (int x = 1; x < t.width - 1; x++)  
  32.             {  
  33.                 Color c = t.GetPixel(x, y);  
  34.                 if (intensity > 0)  
  35.                 {  
  36.                     for (int i = 0; i < intensity - 1; i++)    // 这个循环太大会卡住!  
  37.                     {  
  38.                         for (int k = 0; k < intensity - 1; k++)  
  39.                         {  
  40.                             c += t.GetPixel(x + i, y + k);  
  41.                         }  
  42.                     }  
  43.                     c = c / Mathf.Pow(intensity, 2) * 1.2f;  
  44.                 }  
  45.                 tt.SetPixel(x, y, c);  
  46.             }  
  47.         }  
  48.         tt.Apply();  
  49.         GetComponent<Renderer>().material.mainTexture = tt;  
  50.     }  
  51. }  


5、 二值化 另一种实现方式

就是讲图片变成自己想要的两种颜色( 自己查看和 第二个Demo的区别 )比之前的好太多了!

http://blog.csdn.net/u010019717

脚本设置 (0和 1之间的值)

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Test : MonoBehaviour  
  5. {  
  6.   
  7.     public Texture2D t;  
  8.   
  9.     [Range(0, 1)]  
  10.     public float intensity = 0.8f;       // 这个代表   灰度值的   分割线  
  11.   
  12.     public Color drawColor = new Color32(240, 190, 170, 255);  
  13.     public Color defaultColor = new Color32(0, 0, 0, 0);  
  14.   
  15.   
  16.     Texture2D tt;  
  17.   
  18.     void Start()  
  19.     {  
  20.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  21.         InvokeRepeating("MyUpdate", 0, 1);  
  22.     }  
  23.   
  24.     void MyUpdate()  
  25.     {  
  26.         if (tt != null)  
  27.         {  
  28.             Destroy(tt);  
  29.         }  
  30.         tt = new Texture2D(t.width, t.height);  
  31.         for (int y = 1; y < t.height - 1; y++)  
  32.         {  
  33.             for (int x = 1; x < t.width - 1; x++)  
  34.             {  
  35.                 // 这个语句将图片变成两种颜色  
  36.                 tt.SetPixel(x, y, t.GetPixel(x, y).grayscale < intensity ? defaultColor : drawColor);  
  37.             }  
  38.         }  
  39.         tt.Apply();  
  40.         GetComponent<Renderer>().material.mainTexture = tt;  
  41.     }  
  42. }  


http://blog.csdn.net/u010019717

6、边缘检测

 

脚本的设置  :

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Test : MonoBehaviour  
  5. {  
  6.   
  7.     public Texture2D t;  
  8.   
  9.     [Range(0, 0.5f)]  
  10.     public float intensity = 0.15f;  
  11.   
  12.     public Color drawColor = new Color32(124, 89, 54, 255);  
  13.     public Color defaultColor = new Color32(0, 0, 0, 0);  
  14.   
  15.   
  16.     Texture2D tt;  
  17.   
  18.     void Start()  
  19.     {  
  20.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  21.         InvokeRepeating("MyUpdate", 0, 5);  
  22.     }  
  23.   
  24.     void MyUpdate()  
  25.     {  
  26.         if (tt != null)  
  27.         {  
  28.             Destroy(tt);  
  29.         }  
  30.         tt = new Texture2D(t.width, t.height);  
  31.         for (int y = 1; y < t.height - 1; y++)  
  32.         {  
  33.             for (int x = 1; x < t.width - 1; x++)  
  34.             {  
  35.   
  36.                 // 5个位置的灰度值  
  37.                 float g = t.GetPixel(x, y).grayscale;  
  38.                 float gL = t.GetPixel(x - 1, y).grayscale;  
  39.                 float gR = t.GetPixel(x + 1, y).grayscale;  
  40.                 float gT = t.GetPixel(x, y - 1).grayscale;  
  41.                 float gB = t.GetPixel(x, y + 1).grayscale;  
  42.   
  43.                 // 如果和上下左右 的弧度差值大于一个指定的值, 就认为这个是边缘!  
  44.                 if (Mathf.Abs(g - gL) > intensity)  
  45.                 {  
  46.                     tt.SetPixel(x, y, drawColor);  
  47.                 }  
  48.                 else if (Mathf.Abs(g - gR) > intensity)  
  49.                 {  
  50.                     tt.SetPixel(x, y, drawColor);  
  51.                 }  
  52.                 else if (Mathf.Abs(g - gT) > intensity)  
  53.                 {  
  54.                     tt.SetPixel(x, y, drawColor);  
  55.                 }  
  56.                 else if (Mathf.Abs(g - gB) > intensity)  
  57.                 {  
  58.                     tt.SetPixel(x, y, drawColor);  
  59.                 }  
  60.                 else  
  61.                 {  
  62.                     tt.SetPixel(x, y, defaultColor);  
  63.                 }  
  64.             }  
  65.         }  
  66.         tt.Apply();  
  67.         GetComponent<Renderer>().material.mainTexture = tt;  
  68.     }  
  69. }  



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值