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 initialization
void 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 frame
void Update () {
}
}
Unity Shader 创建程序纹理贴图的更多相关文章
- 【Unity Shader】五、Shader纹理映射,及纹理的缩放和偏移
将漫反射的颜色改为从纹理贴图中获取,逐像素计算. Shader "Custom/11-Texture" { // 纹理贴图,BlinnPhong光照模型 Properties{ / ...
- 【转】《Unity Shader入门精要》冯乐乐著 书中彩图
为方便个人手机学习时候查阅,从网上转来这些彩图. 如属过当行为,联系本人删除. 勘错表 http://candycat1992.github.io/unity_shaders_book/unity_s ...
- Unity Shader 知识点总结(二)
紧接着上一篇文章的shader入门知识的总结,本文主要总结shader中的纹理贴图.透明度混合.顶点动画.后期特效处理等操作.如果有什么地方有错,请指出更正,谢谢.本文的代码主要来自开源书:unity ...
- 【Unity Shader学习笔记】Unity基础纹理-法线贴图
1 高度纹理 使用一张纹理改变物体表面法线,为模型提供更多细节. 有两种主要方法: 1.高度映射:使用一张高度纹理(height map)来模拟表面位移(displacement).得到一个修改后的法 ...
- [Unity] Shader(着色器)之纹理贴图
在Shader中,我们除了可以设定各种光线处理外,还可以增加纹理贴图. 使用 settexture 命令可以为着色器指定纹理. 示例代码: Shader "Sbin/ff2" { ...
- Unity Shader入门精要学习笔记 - 第10章 高级纹理
转载自 冯乐乐的 <Unity Shader入门精要> 立方体纹理 在图形学中,立方体纹理是环境映射的一种实现方法.环境映射可以模拟物体周围的环境,而使用了环境映射的物体可以看起来像镀了层 ...
- 【Unity Shader】六、使用法线贴图(Normal Map)的Shader
学习资料: http://www.sikiedu.com/course/37/task/456/show# http://www.sikiedu.com/course/37/task/458/show ...
- 【Unity Shader】(四) ------ 纹理之法线纹理、单张纹理及遮罩纹理的实现
笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题. [Unity Shader](三) ----- ...
- 【Unity Shader】(九) ------ 高级纹理之渲染纹理及镜子与玻璃效果的实现
笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题. [Unity Shader](三) ----- ...
随机推荐
- Linux CentOS7安装Mysql5.7
一.下载mysql mkdir /home/install #创建install目录 在/home/install目录下下载mysql5.7 wget https://cdn.mysql.com//D ...
- Linux基本命令 关机命令
linux下常用的关机命令有:shutdown.halt.poweroff.init:重启命令有:reboot.下面本文就主要介绍一些常用的关机命令以及各种关机命令之间的区别和具体用法. 首先来看一下 ...
- [SCOI2013]火柴棍数字(背包)
题目 做饭 由于越高位越好,我们先得出能组成的最高位 \(f[i][j][k]\)表示从低到高位第\(i\)位,手里拿着\(j\)根火柴,第\(i\)位是否为\(0\)所需要的最少火柴 我们转移仅需得 ...
- cocos2dx打飞机项目笔记四:Enemy类和EnemyLayer类
Enemy类没什么内容,就create和init方法,根据参数来创建不同的敌机,头文件代码如下: //飞机的类型 enum planeType {smallPlane, midPlane, bigPl ...
- DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。
DATE_FORMAT(date,format) format参数的格式有 %a 缩写星期名 %b 缩写月名 %c 月,数值 %D 带有英文前缀的月中的天 %d 月的天,数值(00-31) %e 月的 ...
- Luogu-4022 [CTSC2012]熟悉的文章
广义后缀自动机+DP 对于作文库建出广义后缀自动机,广义自动机就是在每次添加一个字符串之前把\(last=0\),然后正常添加就好了 对于每个询问串,预处理出每个位置\(i\)能向前匹配的最长长度\( ...
- 更新gcc_Install gcc 4.7.x/4.8.x on CentOS
参考一:How to Install gcc 4.7.x/4.8.x on CentOS 参考二:CentOS 升级 gcc 和 g++ 的方法 参考三:linux yum命令详解 参考四:每天一个l ...
- MyEclipse+PyDev配置搭建Python开发环境
打开help-> install from catalog 输入pydev查找并安装pydev 根据提示进行安装,安装完后重启myeclipse
- JSON解析字符串
JSON解析字符串 JSON 解析字符串时,应按严格的标准,否则无法解析: str1 = '{"str":"string","number" ...
- 汇编笔记 RET
assume cs:code,ss:stack stack segment db dup() stack ends code segment mov ax,4c00h int 21h start: m ...