using UnityEngine;
using System.Collections;

public class ProceduralTexture : MonoBehaviour
{

public int widthHeight = 512;

/// <summary>  
    /// 生成的图片  
    /// </summary>  
    public Texture2D generatedTexture;

/// <summary>  
    /// 当前的材质  
    /// </summary>  
    private Material currentMaterial;

/// <summary>  
    /// 中心坐标  
    /// </summary>  
    private Vector2 centerPosition;

public int i = 0;
    private int _i = 999;
    // Use this for initialization  
    void Update()
    {
        if (i == _i) return;

if (!currentMaterial)
        {
            //共享当前物体的材质  
            currentMaterial = transform.GetComponent<Renderer>().sharedMaterial;
            if (!currentMaterial)
            {
                Debug.LogWarning("Cannot find a material on:" + transform.name);
            }
        }

if (currentMaterial)
        {
            centerPosition = new Vector2(0.5f, 0.5f);
            generatedTexture = GenerateParabola();

//设置当前材质的贴图为我们生成的贴图  
            currentMaterial.SetTexture("_MainTex", generatedTexture);
        }
        _i = i;
    }

/// <summary>  
    /// 生成贴图  
    /// </summary>  
    /// <returns></returns>  
    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++)
            {
                if (i == 0) {
                    #region 颜色控制在黑白,线性变化函数是y=|1-x|,1>=x>=0  
                    Vector2 currentposition = new Vector2(x, y);
                    float pixeldistance = Vector2.Distance(currentposition, centerPixelPosition) / (widthHeight * 0.5f);

pixeldistance = Mathf.Abs(1 - Mathf.Clamp(pixeldistance, 0f, 1f));

Color pixelcolor = new Color(pixeldistance, pixeldistance, pixeldistance, 1.0f);
                    proceduralTexture.SetPixel(x, y, pixelcolor);
                    #endregion
                } else if (i == 1) {
                    #region 颜色控制在黑白随sin周期变化,变化是(2*π)/n,下面的n是30  
                    Vector2 currentPosition = new Vector2(x, y);
                    float pixelDistance = Vector2.Distance(currentPosition, centerPixelPosition) / (widthHeight * 0.5f);
                    pixelDistance = Mathf.Abs(1 - Mathf.Clamp(pixelDistance, 0f, 1f));
                    pixelDistance = (Mathf.Sin(pixelDistance * 30) * pixelDistance);
                    Color pixelColor = new Color(pixelDistance, pixelDistance, pixelDistance, 1.0f);
                    proceduralTexture.SetPixel(x, y, pixelColor);
                    #endregion
                } else if (i == 2) {
                    #region 颜色根据方向向量偏移角度控制  
                    Vector2 currentPosition = new Vector2(x, y);
                    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;
                    Color pixelColor = new Color(rightDirection, leftDirection, upDirection, 1.0f);
                    proceduralTexture.SetPixel(x, y, pixelColor);
                    #endregion
                }else if(i == 3) {
                    #region 颜色根据方向向量和方向轴的数量积  
                    Vector2 currentPosition = new Vector2(x, y);
                    Vector2 pixelDirection = centerPixelPosition - currentPosition;
                    pixelDirection.Normalize();
                    float rightDirection = Vector2.Dot(pixelDirection, Vector3.right);
                    float leftDirection = Vector2.Dot(pixelDirection, Vector3.left);
                    float upDirection = Vector2.Dot(pixelDirection, Vector3.up);
                    Color pixelColor = new Color(rightDirection, leftDirection, upDirection, 1.0f);
                    proceduralTexture.SetPixel(x, y, pixelColor);
                    #endregion
                }
            }
        }

proceduralTexture.Apply();

return proceduralTexture;
    }
}

生成Texture2D纹理图片的更多相关文章

  1. cocos2d-x 详解之 CCTexture2D(纹理图片)和 CCTextureCache(纹理缓存)

    精灵和动画都涉及到纹理图片的使用,所以在研究精灵与动画之前,我们先来了解一下纹理图片类CCTexture2D和纹理缓存CCTextureCache的原理: 当一张图片被加载到内存后,它是以纹理的形式存 ...

  2. 【代码笔记】iOS-通过颜色来生成一个纯色图片

    一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...

  3. 用ticons指令结合ImageMagickDisplay工具批量生成Android适应图片

    用ticons指令结合ImageMagickDisplay工具批量生成Android适应图片 ticons的用法可以百度 这里记录下具体的编译方法 在安装了ticons和ImageMagickDisp ...

  4. PHP 简易读取文件目录下的文件,生成css spirte图片

    因为个人不是对PS熟悉,不清楚如何在PS中生成一张横向有序的spirte图片,使用了"css sprite V4.3"版本,生成的图片会出现压缩图片大小的情况,本想修改原作者开发的 ...

  5. C#上传图片和生成缩略图以及图片预览

    因工作需要,上传图片要增加MIME类型验证和生成较小尺寸的图片用于浏览.根据网上代码加以修改做出如下效果图: 前台代码如下: <html xmlns="http://www.w3.or ...

  6. iOS 通过颜色来生成一个纯色图片

    //通过颜色来生成一个纯色图片- (UIImage *)buttonImageFromColor:(UIColor *)color{        CGRect rect = CGRectMake(0 ...

  7. PHP使用JPG生成GIF动画图片,基于php_imagick_st-Q8.dll

    PHP使用php_imagick_st-Q8.dll类库,把JPG图片连接生成GIF动画图片,需要事先下载好php_imagick_st-Q8.dll,文件,并配置php.ini文件,启用php_im ...

  8. 使用poi和jfreechart生成excel图表图片

    最近项目在频繁的操作excel,里边涉及到很多和图表有关的东西.有时候需要使用java操作excel自带的图标,比较复杂的我们都是使用excel模板的形式实现. 除此之外,也有一些功能只需要生成对应的 ...

  9. 批量生成反色图片,用PHOTOSHOP批处理功能。

    http://zhidao.baidu.com/link?url=Iz46PDPnEITummTEwo2GtUrK6AeAjlidJ7HtCPJ6NYZJbbllRwNg2iBAcNwF2TYjccP ...

随机推荐

  1. SOA和微服务之间的区别

    近几年,我们有很多文章对SOA和微服务之间的不同点和相似点进行了分析.有些人认为SOA有很多地方是值得微服务学习的,而有些人则认为区别对待微服务和SOA会更好.而Neal Ford认为,将单体迁移到面 ...

  2. Linux Shell编程 循环语法

    for循环 for 循环是固定循环,也就是在循环时已经知道需要进行几次循环.有时也把 for 循环称为计数循环.语法: for 变量 in 值1 值2 值3… do 程序 done 在这种语法中,fo ...

  3. CSS实现三角形图标的原理!!!!今天总算弄懂了。

    网页中经常有一种三角形的图标,鼠标点一下会弹出一个下拉菜单之类的(之前淘宝也有,不过现在改版好像没有了) 之前以为是个png图标背景,后来在bootstrap中看到有一个图标样式叫做caret的用来实 ...

  4. ==、equals与hashCode

    ==  首先,得说明java数据类型分为基本数据类型和引用数据类型, 基本数据类型有8种: 浮点型:float(4 byte), double(8 byte) 整型:byte(1 byte), sho ...

  5. get_class_methods--返回由类的方法名组成的数组

    get_class_methods--返回由类的方法名组成的数组 array get_class_methods ( mixed $class_name ) 返回由类的方法名组成的数组. <?p ...

  6. 用requests登录微信网页版,并接收发送消息

    首先,网页版微信登录大致分为以下几个流程(都是大家可以通过抓包得到): 1.登陆主页后,会生成一个UUID,这是个用户标识,在后面请求二维码会用到 def get_uuid(self): '''获取u ...

  7. 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】

    Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

  8. java基础学习总结——java环境变量配置(转)

    只为成功找方法,不为失败找借口! 永不放弃,一切皆有可能!!! java基础学习总结——java环境变量配置 前言 学习java的第一步就要搭建java的学习环境,首先是要安装 JDK,JDK安装好之 ...

  9. 实现利用公钥私钥免密码登录Linux服务器

    原理 客户端生成公钥私钥,把公钥拷贝给linux服务器,用自己的私钥连接服务器.实现如下: 如果是两台Linux服用器A和B,A来实现免密码登录B A执行ssh-keygen -t rsa 就会在/r ...

  10. Centos6.5下ElasticSearch1.4.4的安装

    1.下载ElasticSearch 官网地址 https://www.elastic.co/ 2.安装elasticsearch-1.4.4.tar.gz tar -zxvf elasticsearc ...