码迷,mamicode.com
首页 > 编程语言 > 详细

Unity Shader 创建程序纹理贴图

时间:2017-11-06 11:16:55      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:红色   创建   get   direction   variables   遍历   中心   sha   osi   

 

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

 

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 创建程序纹理贴图

标签:红色   创建   get   direction   variables   遍历   中心   sha   osi   

原文地址:http://www.cnblogs.com/alps/p/7791745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!