简单的程序纹理
程序纹理是指那些由计算机生成的图像,我们通常使用一些特定的算法来创建个性化的图案或者非常真实的自然元素。
使用的shader是之前用到过得一个纹理光照计算,然后创建一个cs代码,并赋给立方体来生成纹理:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class ProceduralTextureGeneration : MonoBehaviour {
public Material material = null;
#region
[SerializeField, SetProperty("textureWidth")] //为了能在面板修改参数
private int m_textureWidth = 512; //纹理大小 通常是2的整数幂
public int textureWidth
{
get
{
return m_textureWidth;
}
set
{
m_textureWidth = value;
_UpdateMaterial();
}
}
[SerializeField, SetProperty("backgroundColor")]
private Color m_backgroundColor = Color.white; //纹理的背景颜色
public Color backgroundColor
{
get
{
return m_backgroundColor;
}
set
{
m_backgroundColor = value;
_UpdateMaterial();
}
}
[SerializeField, SetProperty("circleColor")]
private Color m_circleColor = Color.yellow; //圆点的颜色
public Color circleColor
{
get
{
return m_circleColor;
}
set
{
m_circleColor = value;
_UpdateMaterial();
}
}
[SerializeField, SetProperty("blurFactor")]
private float m_blurFactor = 2.0f; //模糊因子 用来模糊圆形边界
public float blurFactor
{
get
{
return m_blurFactor;
}
set
{
m_blurFactor = value;
_UpdateMaterial();
}
}
#endregion
private Texture2D m_generatedTexture = null;//保存生成的程序纹理
// Use this for initialization
void Start () {
if (material == null)
{
Renderer renderer = gameObject.GetComponent<Renderer>();
if (renderer == null)
{
Debug.Log("Cannot find a renderer.");
return;
}
material = renderer.sharedMaterial;
}
_UpdateMaterial();
}
// Update is called once per frame
void Update () {
}
private void _UpdateMaterial()
{
if (material != null)
{
m_generatedTexture = _GenerateProceduralTexture();
material.SetTexture("_MainTex",m_generatedTexture);//材质中需要有一个叫_MainTex的纹理属性
}
}
private Texture2D _GenerateProceduralTexture()
{
Texture2D proceduralTexture = new Texture2D(textureWidth, textureWidth);
float circleInterval = textureWidth / 4.0f;//圆之间的距离
float radius = textureWidth / 10.0f;//圆的半径
float edgeBlur = 1.0f / blurFactor;//模糊系数
for (int w = 0; w < textureWidth; w++)
{
for (int h = 0; h < textureWidth; h++)
{
Color pixel = backgroundColor;//初始化背景颜色
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
//计算圆心位置
Vector2 circleCenter = new Vector2(circleInterval * (i + 1), circleInterval * (j + 1));
//计算当前像素与圆心的距离
float dist = Vector2.Distance(new Vector2(w, h), circleCenter) - radius;
//模糊边界
Color color = _MixColor(circleColor, new Color(pixel.r, pixel.g, pixel.b, 0.0f), Mathf.SmoothStep(0f, 1.0f, dist * edgeBlur));
//与之前的边界进行混合
pixel = _MixColor(pixel, color, color.a);
}
}
proceduralTexture.SetPixel(w, h, pixel);
}
}
proceduralTexture.Apply();
return proceduralTexture;
}
private Color _MixColor(Color color0, Color color1, float mixFactor)
{
Color mixColor = Color.white;
mixColor.r = Mathf.Lerp(color0.r, color1.r, mixFactor);
mixColor.g = Mathf.Lerp(color0.g, color1.g, mixFactor);
mixColor.b = Mathf.Lerp(color0.b, color1.b, mixFactor);
mixColor.a = Mathf.Lerp(color0.a, color1.a, mixFactor);
return mixColor;
}
}
代码首先初始化一张二维纹理,并且提前计算了一些生成纹理时需要的变量。然后使用了一个两层嵌套循环遍历纹理中的每个像素,并且在纹理上依次绘制9个圆形。最后调用proceduralTexture.Apply()来强制把像素值写入纹理中,并返回该程序纹理。
效果图: