zoukankan      html  css  js  c++  java
  • Unity Shader 创建程序纹理贴图

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

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

    }
    }

  • 相关阅读:
    python 字典转对象实现
    漏洞复现的整理
    mlx90614红外传感器
    【小程序码】登录的技术实现案例
    Spring mvc Aop
    MSHFLEXGRID常用的属性,方法事件
    InsertAuditEntryNew
    Froggy 的 CSP-2021 S1 游记
    JOI Open 偷学记录
    生产订单工序新增、修改、删除——CO_SE_PRODORD_OPR_CREATE、CO_SE_PRODORD_OPR_CHANGE、CO_SE_PRODORD_OPR_DELETE
  • 原文地址:https://www.cnblogs.com/alps/p/7791745.html
Copyright © 2011-2022 走看看