Unity Shader 学习笔记(十二) 创建程序纹理贴图
来源:互联网 发布:linux 抓取log日志 编辑:程序博客网 时间:2024/05/17 04:32
Unity Shader 学习笔记(十二) 创建程序纹理贴图
创建一个脚本 附加到一个游戏体上
using UnityEngine;using System.Collections;public class ProceduralTexture : MonoBehaviour{#region Public Variables //纹理的宽高 public int widthHeight = 512; //程序生成的纹理 public Texture2D generaterdTexture;#endregion#region Private Variables private Material currentMaterial; private Vector2 centerPosition;#endregion// Use this for initializationvoid Start () { if (!currentMaterial) { currentMaterial = transform.renderer.sharedMaterial; if (!currentMaterial) { Debug.LogWarning("Cannot find a material on : " + transform.name); } } if (currentMaterial) { centerPosition = new Vector2(0.5f, 0.5f); generaterdTexture = GenerateParabola(); //设置纹理 currentMaterial.SetTexture("_MainTex",generaterdTexture); } } private Texture2D GenerateParabola() { //创建一个新的纹理 Texture2D proceduralTexture = new Texture2D(widthHeight, widthHeight); //获取当前纹理的中心点 Vector2 centerPixelPosition = centerPosition * widthHeight; //循环遍历纹理的宽高 , 来为每个像素点赋值 for (int x = 0; x < widthHeight; x++) { for (int y = 0; y < widthHeight; y++) { //Get the distance from the center of the texture to //our currently selected pixel Vector2 currentPosition = new Vector2(x, y); //当前像素点到中心点的距离 / 纹理宽高的一半 float pixelDistance = Vector2.Distance(currentPosition, centerPixelPosition) / (widthHeight * 0.5f); // Mathf.Abs : 取绝对值 Mathf.Clamp 0-1 pixelDistance = Mathf.Abs(1 - Mathf.Clamp(pixelDistance, 0f, 1f)); pixelDistance = (Mathf.Sin(pixelDistance * 30.0f) * pixelDistance); //you can also do some more advanced vector calculations to achieve //other types of data about the model itself and its uvs and //pixels //以中心为原点 获取到当前像素点的向量 Vector2 pixelDirection = centerPixelPosition - currentPosition; pixelDirection.Normalize(); //当前像素点向量 和 上左右三个方向的向量之间的夹角 float rightDirection = Vector2.Angle(pixelDirection, Vector3.right) / 360; float leftDirection = Vector2.Angle(pixelDirection, Vector3.left) / 360; float upDirection = Vector2.Angle(pixelDirection, Vector3.up) / 360; //确保像素点的颜色值在 (0,1) 之间 //Create a new color value based off of our //Color pixelColor = new Color(Mathf.Max(0.0f, rightDirection),Mathf.Max(0.0f, leftDirection), Mathf.Max(0.0f,upDirection), 1f); Color pixelColor = new Color(pixelDistance, pixelDistance, pixelDistance, 1.0f); //Color pixelColor = new Color(rightDirection, leftDirection, upDirection, 1.0f); proceduralTexture.SetPixel(x, y, pixelColor); //将中间的像素点设置成红色 if (x == widthHeight / 2 || y == widthHeight / 2) { Color middlePixelColor = new Color(1, 0, 0, 1.0f); proceduralTexture.SetPixel(x, y, middlePixelColor); } } } //Finally force the application of the new pixels to the texture proceduralTexture.Apply(); //return the texture to the main program. return proceduralTexture; }// Update is called once per framevoid Update () {}}
0 0
- Unity Shader 学习笔记(十二) 创建程序纹理贴图
- Unity Shader入门精要笔记(十二):纹理属性
- unity之shader学习笔记(五)--纹理
- Unity Shader学习笔记:基础纹理
- Unity Shader学习笔记:渐变纹理
- Unity Shader学习笔记:遮罩纹理
- Unity Shader学习笔记:渲染纹理
- Unity Shader学习笔记:纹理动画
- Unity Shader 学习笔记(24) 深度纹理、法线纹理
- Unity Shader 学习笔记(十一) 混合纹理Shader实例
- Unity Shader 学习笔记(17) 程序纹理(Procedural Texture)、程序材质(Procedural Materials)
- Unity Shader 学习笔记(8) 纹理映射、凹凸映射
- unity shader学习笔记(四)——Unity中的基础纹理之单张纹理
- unity shader学习笔记(六)——Unity中的基础纹理之渐变纹理
- unity shader学习笔记(七)——Unity中的基础纹理之遮罩纹理
- Unity Shader 学习笔记(十二)漫反射卷积光照模型
- 《unity shader入门精要》学习笔记--渐变纹理与遮罩纹理
- Unity Shader 学习笔记(9) 渐变纹理、遮罩纹理
- Gson详解
- 如何将Android Studio的工程导入到eclipse中
- nagios nrpe监控Linux
- http访问url的问题
- [Erlang]系统内部监控工具
- Unity Shader 学习笔记(十二) 创建程序纹理贴图
- 毕设周记Ⅱ
- iOS开发之记录用户登陆代码实现
- Echarts柱状图js代码
- javadongdong testtest
- MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN
- 消息队列实现数据同步
- hadoop安装
- vi 怎么 保存, 退出编辑