unity shader程序纹理

本文介绍了如何在Unity3D中利用C#脚本生成程序纹理,实现动态改变物体表面纹理的效果。通过创建材质,挂载脚本并设置材质属性,结合数学函数和颜色混合,可以实现如圆点图案等自定义纹理。脚本中包含了颜色、模糊度等参数的控制,允许实时调整纹理样式。
摘要由CSDN通过智能技术生成

程序纹理

利用脚本来生成一些有规律的一些纹理。
实现效果:
在这里插入图片描述
实现方式:

  1. 首先新建一个材质,但是不对其进行任何操作。
  2. 在需要渲染的物体上,挂载脚本,并且将新建的材质赋给它,通过脚本生成程序纹理,并且赋值给使用的材质,从而得到图上效果。
    脚本:
    请添加图片描述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class ProceduralTextureGeneration : MonoBehaviour
{
    //材质
    public Material material = null;
    //生成的程序纹理
    private Texture2D m_generatedTexture = null;
    //设置各种变量,并且设置set和get函数,在set函数中调用_UpdateMaterial()方法,从而实时更新程序纹理
    #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("textureWidth")]
    private Color m_circleColor = Color.yellow;
    public Color circleColor
    {
        get
        {
            return m_circleColor;
        }
        set
        {
            m_circleColor = value;
            _UpdateMaterial();
        }
    }
    [SerializeField, SetProperty("textureWidth")]
    private float m_blurFactor = 2.0f;
    public float blurFactor
    {
        get
        {
            return m_blurFactor;
        }
        set
        {
            m_blurFactor = value;
            _UpdateMaterial();
        }
    }
    #endregion
    // Start is called before the first frame update
    void Start()
    {
        if (material == null)
        {
            Renderer renderer = gameObject.GetComponent<Renderer>();
            if (renderer == null)
            {
                Debug.Log("Cannot find a renderer");
            }
            //从脚本所在的物体上获得材质
            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);
        }
    }

    //生成程序纹理
    private Texture2D _GenerateProceduralTexture()
    {
        //新建一张纹理
        Texture2D proceduralTexture = new Texture2D(textureWidth, textureWidth);
        //圆与圆之间的距离
        float circleInterval = textureWidth / 4.0f;
        float radius = textureWidth / 10.0f;
        //模糊系数
        float edgeBlur = 1.0f / m_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));
                        //计算该像素到每个中心坐标的距离与半径做比较,如果小于0,则是圆形内的像素,反之则不是
                        float dist = Vector2.Distance(new Vector2(w, h), circleCenter) - radius;
                        //Mathf.SmoothStep平滑差值,根据离圆的距离和模糊系数计算得到颜色的混合差值,计算得到最后的颜色
                        //如果dist * edgeBlur的结果小于0,则背景颜色的系数为0,全部为圆形的颜色,因为这时候在圆形内。
                        //如果dist * edgeBlur的结果大于1,则背景颜色的系数为1,全部为背景的颜色,因为这时候在圆形外。
                        //如果dist * edgeBlur的结果在[0,1]之间,则说明在边缘处,所以为两个颜色的混合。
                        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;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值