shader篇-程序纹理

shader篇-程序纹理


简介

程序纹理指的是那些由计算机生成的图像。使用程序纹理的好处在于能创建个性化的图案或真实的自然元素,并可以使用各种参数控制纹理的外观,从而得到丰富的动画和视觉效果。

脚本

脚本创建

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//[ExecuteInEditMode]是为让脚本能在编辑器模式下运行
[ExecuteInEditMode]
public class ProceduralTextureGeneration : MonoBehaviour {

声明材质

//这个材质将使用脚本生成的程序纹理
public Material material = null;

声明程序纹理使用的各种参数
这里使用了一个开源插件SetProperty,可以在修改属性时执行set函数。

#region Material properties
    //纹理大小
    [SerializeField, SetProperty("textureWidth")]
    private int m_textureWidth = 512;
    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

在start函数中进行相应检查,以得到需要使用该程序纹理的材质

void Start () {
    if (material == null) {
        Renderer renderer = gameObject.GetComponent<Renderer>();
        if (renderer == null) {
            Debug.LogWarning("Cannot find a renderer.");
            return;
        }

        material = renderer.sharedMaterial;
    }

    _UpdateMaterial();
}

生成程序纹理并赋给材质(材质的_MainTex属性)

private void _UpdateMaterial() {
        if (material != null) {
            m_generatedTexture = _GenerateProceduralTexture();
            material.SetTexture("_MainTex", m_generatedTexture);
        }
    }
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;

            // Draw nine circles one by one
            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;
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值