生成Texture2D纹理图片
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纹理图片的更多相关文章
- cocos2d-x 详解之 CCTexture2D(纹理图片)和 CCTextureCache(纹理缓存)
精灵和动画都涉及到纹理图片的使用,所以在研究精灵与动画之前,我们先来了解一下纹理图片类CCTexture2D和纹理缓存CCTextureCache的原理: 当一张图片被加载到内存后,它是以纹理的形式存 ...
- 【代码笔记】iOS-通过颜色来生成一个纯色图片
一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...
- 用ticons指令结合ImageMagickDisplay工具批量生成Android适应图片
用ticons指令结合ImageMagickDisplay工具批量生成Android适应图片 ticons的用法可以百度 这里记录下具体的编译方法 在安装了ticons和ImageMagickDisp ...
- PHP 简易读取文件目录下的文件,生成css spirte图片
因为个人不是对PS熟悉,不清楚如何在PS中生成一张横向有序的spirte图片,使用了"css sprite V4.3"版本,生成的图片会出现压缩图片大小的情况,本想修改原作者开发的 ...
- C#上传图片和生成缩略图以及图片预览
因工作需要,上传图片要增加MIME类型验证和生成较小尺寸的图片用于浏览.根据网上代码加以修改做出如下效果图: 前台代码如下: <html xmlns="http://www.w3.or ...
- iOS 通过颜色来生成一个纯色图片
//通过颜色来生成一个纯色图片- (UIImage *)buttonImageFromColor:(UIColor *)color{ CGRect rect = CGRectMake(0 ...
- 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 ...
- 使用poi和jfreechart生成excel图表图片
最近项目在频繁的操作excel,里边涉及到很多和图表有关的东西.有时候需要使用java操作excel自带的图标,比较复杂的我们都是使用excel模板的形式实现. 除此之外,也有一些功能只需要生成对应的 ...
- 批量生成反色图片,用PHOTOSHOP批处理功能。
http://zhidao.baidu.com/link?url=Iz46PDPnEITummTEwo2GtUrK6AeAjlidJ7HtCPJ6NYZJbbllRwNg2iBAcNwF2TYjccP ...
随机推荐
- C语言中auto,register,static,const,volatile的区别
1)auto 这个关键字用于声明变量的生存期为自动,即将不在任何类.结构.枚举.联合和函数中定义的变量视为全局变量,而在函数中定义的变量视为局部变量.这个关键字不怎么多写,因为所有的变量默认就是aut ...
- sql两张表关联更新字段
记录,推荐使用第三种,个人觉得比较好 --第一种写法,之前一直用,没有在意 UPDATE sr_t_TCodeUserReciveCfg SET fUserID=a.fUserID,fImportFl ...
- 结合canvas做雨滴特效
雨滴特效 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- Introduction to vSphere Integrated Containers
vSphere Integrated Containers enables IT teams to seamlessly run traditional workloads and container ...
- relativePath
比如: com.tenace tenace 2.0.1 ../pom.xml //刚开始无此句 com.spider engine 2.6.0-SNAPSHOT tenace作为pom项目已经发布至r ...
- maven 一个简单项目 —— maven权威指南学习笔记(三)
目标: 对构建生命周期 (build lifecycle),Maven仓库 (repositories),依赖管理 (dependency management)和项目对象模型 (Project O ...
- request.getPathInfo();
request.getPathInfo(); 这个方法返回请求的实际URL相对于请求的serlvet的url的路径.(个人理解.)比如,有一个Servlet的映射是这样配置的: <servlet ...
- Anton and School - 2 (组合数学)
题意:给你一串只有‘(’与‘)’的字符串,问你多少对括号,括号一定是左边一半的‘(’,右边一半是‘)’ )(()() 答案是:6 题解:枚举每个‘(’,此时设左括号左边有n个‘(’,它右边有m个‘ ...
- jquery02-jQuery效果=隐藏和显示+切换+淡入淡出+滑动+动画+回调+链
隐藏和显示 $(selector).hide(speed,callback); $(selector).show(speed,callback); 可选的 speed 参数规定隐藏/显示的速度, ...
- SpringMVC中使用ModelAndView遇到的问题
本文记录我在SpringMVC中使用ModelAndView,添加模型数据到ModelAndView中时遇到的问题: 1.jsp页面用EL表达式来获取值时直接显示EL表达式,JSP不解析EL表达式: ...