博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity Shader 创建程序纹理贴图
阅读量:6904 次
发布时间:2019-06-27

本文共 2792 字,大约阅读时间需要 9 分钟。

 

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

 

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 () {
}
}

 

转载于:https://www.cnblogs.com/alps/p/7791745.html

你可能感兴趣的文章
redis+ssh-keygen免认证登录案例
查看>>
HTML_后台框架全屏界面_fixed形式布局
查看>>
为什么使用 SLF4J 而不是 Log4J 来做 Java 日志
查看>>
顺丰快递接口
查看>>
淘宝技术发展(个人网站)
查看>>
处理 emoji 表情
查看>>
Dev-No.02 HTTP Status状态汇总
查看>>
linux svn命令
查看>>
Android中获取CPU负载和进程cpu时间
查看>>
docker容器启动后添加端口映射
查看>>
Android新姿势:3D翻转效果原理
查看>>
Xtrabackup系列之:二进制安装
查看>>
Context []startup failed due to previous errors 错误
查看>>
RPM(RedHat Package Manager)
查看>>
iOS开源项目周报0302
查看>>
linux入门介绍
查看>>
JCaptcha报异常
查看>>
oracle dataguard 之nologing
查看>>
asp.net如何正确判断上传文件格式
查看>>
使用cocoaPods遇到Updating local specs repositories时的解决
查看>>