赞
踩
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class CreatTexture : MonoBehaviour
- {
-
- public Color color1 = new Color();
- public Color color2 = new Color();
- public Color color;
- public float dis = 2;
- public bool pixelx = false;
- //纹理的宽高
- public int widthHeight = 512;
- //程序生成的纹理
- public Texture2D generaterdTexture;
-
- private Material currentMaterial;
- private Vector2 centerPosition;
- public float a;
- private void Start()
- {
- if (!currentMaterial)
- {
- currentMaterial = transform.GetComponent<Renderer>().sharedMaterial;
- if (!currentMaterial)
- {
- Debug.LogWarning("Cannot find a material on : " + transform.name);
- }
- }
- SetTex();
- }
-
- private void Update()
- {
- SetTex();
- }
-
- private void SetTex()
- {
- if (currentMaterial)
- {
- generaterdTexture = GenerateParabola();
- //设置纹理
- currentMaterial.SetTexture("_WaterTex", generaterdTexture);
- }
- }
-
- private Texture2D GenerateParabola()
- {
- //创建一个新的纹理
- Texture2D proceduralTexture = new Texture2D(widthHeight, widthHeight);
- //获取当前纹理的中心点
- //Vector2 centerPixelPosition = centerPosition * widthHeight;
- //循环遍历纹理的宽高 , 来为每个像素点赋值
- for (int x = 0; x < widthHeight; x++)
- {
- a = x % dis;
- if (x % dis == 0)
- {
- pixelx = !pixelx;
- }
- for (int y = 0; y < widthHeight; y++)
- {
- //Vector2 currentPosition = new Vector2(x, y);
- //float pixelDistance = currentPosition.x - centerPixelPosition.x;
-
- if (pixelx)
- {
- color = Color.Lerp(color1, color2, a/dis );
- proceduralTexture.SetPixel(x, y, color);
- }
- else
- {
- color = Color.Lerp(color1, color2, 1-(a/dis ));
- proceduralTexture.SetPixel(x, y, color);
- }
- //当前像素点到中心点的距离 / 纹理宽高的一半
- //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);
-
- //以中心为原点 获取到当前像素点的向量
- //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) 之间
- //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);
- //}
- }
- }
- proceduralTexture.Apply();
- return proceduralTexture;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。