打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Unity Shader 学习笔记(十二) 创建程序纹理贴图
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class ProceduralTexture : MonoBehaviour  
  5. {  
  6. #region Public Variables  
  7.     //纹理的宽高  
  8.     public int widthHeight = 512;  
  9.     //程序生成的纹理  
  10.     public Texture2D generaterdTexture;  
  11. #endregion  
  12.  
  13. #region Private Variables  
  14.     private Material currentMaterial;  
  15.     private Vector2 centerPosition;  
  16. #endregion  
  17.   
  18.   
  19.     // Use this for initialization  
  20.     void Start () {  
  21.       
  22.         if (!currentMaterial)  
  23.         {  
  24.             currentMaterial = transform.renderer.sharedMaterial;  
  25.             if (!currentMaterial)  
  26.             {  
  27.                 Debug.LogWarning("Cannot find a material on : " + transform.name);  
  28.             }  
  29.               
  30.         }  
  31.   
  32.         if (currentMaterial)  
  33.         {  
  34.             centerPosition = new Vector2(0.5f, 0.5f);  
  35.             generaterdTexture = GenerateParabola();  
  36.   
  37.             //设置纹理  
  38.             currentMaterial.SetTexture("_MainTex",generaterdTexture);  
  39.         }  
  40.           
  41.           
  42.     }  
  43.     private Texture2D GenerateParabola()  
  44.     {  
  45.         //创建一个新的纹理  
  46.         Texture2D proceduralTexture = new Texture2D(widthHeight, widthHeight);  
  47.   
  48.         //获取当前纹理的中心点  
  49.         Vector2 centerPixelPosition = centerPosition * widthHeight;  
  50.   
  51.         //循环遍历纹理的宽高 , 来为每个像素点赋值  
  52.         for (int x = 0; x < widthHeight; x++)  
  53.         {  
  54.             for (int y = 0; y < widthHeight; y++)  
  55.             {  
  56.                 //Get the distance from the center of the texture to  
  57.                 //our currently selected pixel  
  58.                 Vector2 currentPosition = new Vector2(x, y);  
  59.                 //当前像素点到中心点的距离 / 纹理宽高的一半  
  60.                 float pixelDistance = Vector2.Distance(currentPosition, centerPixelPosition) / (widthHeight * 0.5f);  
  61.                 // Mathf.Abs : 取绝对值   Mathf.Clamp  0-1  
  62.                 pixelDistance = Mathf.Abs(1 - Mathf.Clamp(pixelDistance, 0f, 1f));  
  63.                 pixelDistance = (Mathf.Sin(pixelDistance * 30.0f) * pixelDistance);  
  64.   
  65.                 //you can also do some more advanced vector calculations to achieve  
  66.                 //other types of data about the model itself and its uvs and  
  67.                 //pixels  
  68.   
  69.                 //以中心为原点 获取到当前像素点的向量  
  70.                 Vector2 pixelDirection = centerPixelPosition - currentPosition;  
  71.                 pixelDirection.Normalize();  
  72.   
  73.                 //当前像素点向量 和 上左右三个方向的向量之间的夹角  
  74.                 float rightDirection = Vector2.Angle(pixelDirection, Vector3.right) / 360;  
  75.                 float leftDirection = Vector2.Angle(pixelDirection, Vector3.left) / 360;  
  76.                 float upDirection = Vector2.Angle(pixelDirection, Vector3.up) / 360;  
  77.   
  78.                   
  79.                 //确保像素点的颜色值在 (0,1) 之间  
  80.   
  81.                 //Create a new color value based off of our  
  82.                 //Color pixelColor = new Color(Mathf.Max(0.0f, rightDirection),Mathf.Max(0.0f, leftDirection), Mathf.Max(0.0f,upDirection), 1f);  
  83.                 Color pixelColor = new Color(pixelDistance, pixelDistance, pixelDistance, 1.0f);  
  84.                 //Color pixelColor = new Color(rightDirection, leftDirection, upDirection, 1.0f);  
  85.                 proceduralTexture.SetPixel(x, y, pixelColor);  
  86.   
  87.                 //将中间的像素点设置成红色  
  88.                 if (x == widthHeight / 2 || y == widthHeight / 2)  
  89.                 {  
  90.                     Color middlePixelColor = new Color(1, 0, 0, 1.0f);  
  91.                     proceduralTexture.SetPixel(x, y, middlePixelColor);  
  92.                 }  
  93.             }  
  94.         }  
  95.         //Finally force the application of the new pixels to the texture  
  96.         proceduralTexture.Apply();  
  97.   
  98.         //return the texture to the main program.  
  99.         return proceduralTexture;  
  100.     }  
  101.       
  102.     // Update is called once per frame  
  103.     void Update () {  
  104.       
  105.     }  
  106. }  


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
基于内容的图片检索CBIR(Content Based Image Retrieval)简介
向量的点乘和叉乘在游戏中的应用
强迫症之把unity自带的第三人称的js脚本ThirdPersonCamera和ThirdPersonController的转换为C#脚本
unity 移动物体到指定位置的四种方法
Unity相机平滑跟随
Unity3D倒计时两种方法 | Unity3D教程手册
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服