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. vue 数据传递的方法

    组件(Component)是 Vue.js 最强大的功能.组件可以封装可重用的代码,通过传入对象的不同,实现组件的复用,但组件传值就成为一个需要解决的问题. 1.父组件向子组件传值 组件实例的作用域是 ...

  2. Sublime Text3 打开文档乱码

    一.安装包管理器使用Ctrl+~快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码 import urllib.request,os; pf = 'Package C ...

  3. HAproxy 介绍

    HAproxy 介绍 (1)HAProxy 是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProxy ...

  4. PHP 最大化资源配置 Resource Limits 错误两则

    报错信息1:PHP Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 67108888 b ...

  5. async/await方法解析

    欲了解await,必须先了解Promise,可参考: http://www.cnblogs.com/yanze/p/6347646.html 支持度: ES6已支持Promise,ES7也决定支持aw ...

  6. 读完这篇文章,就基本搞定了Redis数据库

    简单来说Redis就是一个数据库,不过与传统的数据库不同的是Redis的数据是存在内存中的,所以存写速度非常快,因此Redis被广泛应用于缓存方向. 另外,Redis也经常用来做分布式锁.Redis提 ...

  7. 20145239《网络对抗》- 逆向及Bof基础实践

    1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串.该程序同 ...

  8. Ubuntu: 无法使用su命令

    Ubuntu 无法使用su命令解决方案 在Ubuntu上编译Qt环境时发现无法使用su命令切换到root用户,通过网上查找发现解决方案如下: xt@xt-ubuntu:~$ su密码: su:认证失败 ...

  9. K8s 日常操作

    1.获取所有Pods kubectl get pods --namespace=default 2.获取所有Deployments kubectl get deployments --namespac ...

  10. MSSqlserver的锁模式介绍

    一 SQL Server 锁类型的说明 在SQL Server数据库中加锁时,除了可以对不同的资源加锁,还可以使用不同程度的加锁方式,即有多种模式,SQL Server中锁模式包括: 1.共享锁(S) ...