创建一个脚本 附加到一个游戏体上

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 创建程序纹理贴图的更多相关文章

  1. 【Unity Shader】五、Shader纹理映射,及纹理的缩放和偏移

    将漫反射的颜色改为从纹理贴图中获取,逐像素计算. Shader "Custom/11-Texture" { // 纹理贴图,BlinnPhong光照模型 Properties{ / ...

  2. 【转】《Unity Shader入门精要》冯乐乐著 书中彩图

    为方便个人手机学习时候查阅,从网上转来这些彩图. 如属过当行为,联系本人删除. 勘错表 http://candycat1992.github.io/unity_shaders_book/unity_s ...

  3. Unity Shader 知识点总结(二)

    紧接着上一篇文章的shader入门知识的总结,本文主要总结shader中的纹理贴图.透明度混合.顶点动画.后期特效处理等操作.如果有什么地方有错,请指出更正,谢谢.本文的代码主要来自开源书:unity ...

  4. 【Unity Shader学习笔记】Unity基础纹理-法线贴图

    1 高度纹理 使用一张纹理改变物体表面法线,为模型提供更多细节. 有两种主要方法: 1.高度映射:使用一张高度纹理(height map)来模拟表面位移(displacement).得到一个修改后的法 ...

  5. [Unity] Shader(着色器)之纹理贴图

    在Shader中,我们除了可以设定各种光线处理外,还可以增加纹理贴图. 使用 settexture 命令可以为着色器指定纹理. 示例代码: Shader "Sbin/ff2" { ...

  6. Unity Shader入门精要学习笔记 - 第10章 高级纹理

    转载自 冯乐乐的 <Unity Shader入门精要> 立方体纹理 在图形学中,立方体纹理是环境映射的一种实现方法.环境映射可以模拟物体周围的环境,而使用了环境映射的物体可以看起来像镀了层 ...

  7. 【Unity Shader】六、使用法线贴图(Normal Map)的Shader

    学习资料: http://www.sikiedu.com/course/37/task/456/show# http://www.sikiedu.com/course/37/task/458/show ...

  8. 【Unity Shader】(四) ------ 纹理之法线纹理、单张纹理及遮罩纹理的实现

    笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题. [Unity Shader](三) ----- ...

  9. 【Unity Shader】(九) ------ 高级纹理之渲染纹理及镜子与玻璃效果的实现

    笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题. [Unity Shader](三) ----- ...

随机推荐

  1. ORACLE对象模式

    在oracle数据库中,数据对象是以模式(Schema)为单位进行组织和管理的.所谓模式是指一系列逻辑数据结构或对象的集合. 模式与用户相对应,一个模式只能被一个数据库用户所拥有,并且模式名称与这个用 ...

  2. Oracle 9i Unix Manager

    在Unix上被迫终止ORACLE进程时,必须做以下事情: (1) 杀掉所有Oracle进程.    ps -ef|grep $ORACLE_SID|grep -v grep|awk '{print $ ...

  3. 【leetcode刷题笔记】Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  4. 主攻ASP.NET MVC4.0之重生:使用反射获取Controller的ActionResult

    示例代码 public ActionResult TypeOfForName() { Type typeinfo = typeof(CustomerClassController); //typeof ...

  5. Android开发 -- Bootloader

    本文转载自:http://blog.csdn.net/jmq_0000/article/details/7378348 LK是什么 LK 是 Little Kernel 它是 appsbl (Appl ...

  6. 【转载】如何简单地理解Python中的if __name__ == '__main__'

    原帖:https://blog.csdn.net/yjk13703623757/article/details/77918633 通俗的理解__name__ == '__main__':假如你叫小明. ...

  7. EF与手写Model的区别以及联系

    1.在数据库表名上,EF是随意的,但是如果是Model的话,就应该在建立数据库的时候考虑到讲数据库表名变为复数,如Movie.cs 数据库应该为Movies

  8. spark学习2(hive0.13安装)

    第一步:hive安装 通过WinSCP将apache-hive-0.13.1-bin.tar.gz上传到/usr/hive/目录下 [root@spark1 hive]# chmod u+x apac ...

  9. mysql基础(3)-高级查询

    聚合函数 count 返回查询结果的条数 max 返回查询结果的最大值 min 返回查询结果的最小值 sum 返回查询结果的和 avg 返回查询结果的平均值   统计分数大于等于90的人数: mysq ...

  10. sql处理数据库锁的存储过程

    /*--处理死锁 查看当前进程,或死锁进程,并能自动杀掉死进程 因为是针对死的,所以如果有死锁进程,只能查看死锁进程 当然,你可以通过参数控制,不管有没有死锁,都只查看死锁进程 --邹建 2004.4 ...