zoukankan      html  css  js  c++  java
  • 生成Texture2D纹理图片

    using UnityEngine;
    using System.Collections;

    public class ProceduralTexture : MonoBehaviour
    {

        public int widthHeight = 512;

        /// <summary>  
        /// 生成的图片  
        /// </summary>  
        public Texture2D generatedTexture;

        /// <summary>  
        /// 当前的材质  
        /// </summary>  
        private Material currentMaterial;

        /// <summary>  
        /// 中心坐标  
        /// </summary>  
        private Vector2 centerPosition;

        public int i = 0;
        private int _i = 999;
        // Use this for initialization  
        void Update()
        {
            if (i == _i) return;

            if (!currentMaterial)
            {
                //共享当前物体的材质  
                currentMaterial = transform.GetComponent<Renderer>().sharedMaterial;
                if (!currentMaterial)
                {
                    Debug.LogWarning("Cannot find a material on:" + transform.name);
                }
            }

            if (currentMaterial)
            {
                centerPosition = new Vector2(0.5f, 0.5f);
                generatedTexture = GenerateParabola();

                //设置当前材质的贴图为我们生成的贴图  
                currentMaterial.SetTexture("_MainTex", generatedTexture);
            }
            _i = i;
        }

        /// <summary>  
        /// 生成贴图  
        /// </summary>  
        /// <returns></returns>  
        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++)
                {
                    if (i == 0) {
                        #region 颜色控制在黑白,线性变化函数是y=|1-x|,1>=x>=0  
                        Vector2 currentposition = new Vector2(x, y);
                        float pixeldistance = Vector2.Distance(currentposition, centerPixelPosition) / (widthHeight * 0.5f);

                        pixeldistance = Mathf.Abs(1 - Mathf.Clamp(pixeldistance, 0f, 1f));

                        Color pixelcolor = new Color(pixeldistance, pixeldistance, pixeldistance, 1.0f);
                        proceduralTexture.SetPixel(x, y, pixelcolor);
                        #endregion
                    } else if (i == 1) {
                        #region 颜色控制在黑白随sin周期变化,变化是(2*π)/n,下面的n是30  
                        Vector2 currentPosition = new Vector2(x, y);
                        float pixelDistance = Vector2.Distance(currentPosition, centerPixelPosition) / (widthHeight * 0.5f);
                        pixelDistance = Mathf.Abs(1 - Mathf.Clamp(pixelDistance, 0f, 1f));
                        pixelDistance = (Mathf.Sin(pixelDistance * 30) * pixelDistance);
                        Color pixelColor = new Color(pixelDistance, pixelDistance, pixelDistance, 1.0f);
                        proceduralTexture.SetPixel(x, y, pixelColor);
                        #endregion
                    } else if (i == 2) {
                        #region 颜色根据方向向量偏移角度控制  
                        Vector2 currentPosition = new Vector2(x, y);
                        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;
                        Color pixelColor = new Color(rightDirection, leftDirection, upDirection, 1.0f);
                        proceduralTexture.SetPixel(x, y, pixelColor);
                        #endregion
                    }else if(i == 3) {
                        #region 颜色根据方向向量和方向轴的数量积  
                        Vector2 currentPosition = new Vector2(x, y);
                        Vector2 pixelDirection = centerPixelPosition - currentPosition;
                        pixelDirection.Normalize();
                        float rightDirection = Vector2.Dot(pixelDirection, Vector3.right);
                        float leftDirection = Vector2.Dot(pixelDirection, Vector3.left);
                        float upDirection = Vector2.Dot(pixelDirection, Vector3.up);
                        Color pixelColor = new Color(rightDirection, leftDirection, upDirection, 1.0f);
                        proceduralTexture.SetPixel(x, y, pixelColor);
                        #endregion
                    }
                }
            }

            proceduralTexture.Apply();

            return proceduralTexture;
        }
    }

  • 相关阅读:
    约束
    TCL(事务控制语言)
    MySQL常见约束
    “三大范式”及数据库设计
    同义词(别名)
    分享35个非常漂亮的单页网站设计案例
    Eclipse智能提示 (原创)
    java架构师之路:JAVA程序员必看的15本书的电子版下载地址
    Java初级学习笔记
    Java程序的汉化
  • 原文地址:https://www.cnblogs.com/alps/p/7795814.html
Copyright © 2011-2022 走看看