当前位置:   article > 正文

unity 代码创建纹理_unity 动态创建纹理

unity 动态创建纹理
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CreatTexture : MonoBehaviour
  5. {
  6. public Color color1 = new Color();
  7. public Color color2 = new Color();
  8. public Color color;
  9. public float dis = 2;
  10. public bool pixelx = false;
  11. //纹理的宽高
  12. public int widthHeight = 512;
  13. //程序生成的纹理
  14. public Texture2D generaterdTexture;
  15. private Material currentMaterial;
  16. private Vector2 centerPosition;
  17. public float a;
  18. private void Start()
  19. {
  20. if (!currentMaterial)
  21. {
  22. currentMaterial = transform.GetComponent<Renderer>().sharedMaterial;
  23. if (!currentMaterial)
  24. {
  25. Debug.LogWarning("Cannot find a material on : " + transform.name);
  26. }
  27. }
  28. SetTex();
  29. }
  30. private void Update()
  31. {
  32. SetTex();
  33. }
  34. private void SetTex()
  35. {
  36. if (currentMaterial)
  37. {
  38. generaterdTexture = GenerateParabola();
  39. //设置纹理
  40. currentMaterial.SetTexture("_WaterTex", generaterdTexture);
  41. }
  42. }
  43. private Texture2D GenerateParabola()
  44. {
  45. //创建一个新的纹理
  46. Texture2D proceduralTexture = new Texture2D(widthHeight, widthHeight);
  47. //获取当前纹理的中心点
  48. //Vector2 centerPixelPosition = centerPosition * widthHeight;
  49. //循环遍历纹理的宽高 , 来为每个像素点赋值
  50. for (int x = 0; x < widthHeight; x++)
  51. {
  52. a = x % dis;
  53. if (x % dis == 0)
  54. {
  55. pixelx = !pixelx;
  56. }
  57. for (int y = 0; y < widthHeight; y++)
  58. {
  59. //Vector2 currentPosition = new Vector2(x, y);
  60. //float pixelDistance = currentPosition.x - centerPixelPosition.x;
  61. if (pixelx)
  62. {
  63. color = Color.Lerp(color1, color2, a/dis );
  64. proceduralTexture.SetPixel(x, y, color);
  65. }
  66. else
  67. {
  68. color = Color.Lerp(color1, color2, 1-(a/dis ));
  69. proceduralTexture.SetPixel(x, y, color);
  70. }
  71. //当前像素点到中心点的距离 / 纹理宽高的一半
  72. //float pixelDistance = Vector2.Distance(currentPosition, centerPixelPosition) / (widthHeight * 0.5f);
  73. // Mathf.Abs : 取绝对值 Mathf.Clamp 0-1
  74. //pixelDistance = Mathf.Abs(1 - Mathf.Clamp(pixelDistance, 0f, 1f));
  75. //pixelDistance = (Mathf.Sin(pixelDistance * 30.0f) * pixelDistance);
  76. //以中心为原点 获取到当前像素点的向量
  77. //Vector2 pixelDirection = centerPixelPosition - currentPosition;
  78. //pixelDirection.Normalize();
  79. //当前像素点向量 和 上左右三个方向的向量之间的夹角
  80. //float rightDirection = Vector2.Angle(pixelDirection, Vector3.right) / 360;
  81. //float leftDirection = Vector2.Angle(pixelDirection, Vector3.left) / 360;
  82. //float upDirection = Vector2.Angle(pixelDirection, Vector3.up) / 360;
  83. //确保像素点的颜色值在 (0,1) 之间
  84. //Color pixelColor = new Color(pixelDistance, pixelDistance, pixelDistance, 1.0f);
  85. //Color pixelColor = new Color(rightDirection, leftDirection, upDirection, 1.0f);
  86. //proceduralTexture.SetPixel(x, y, pixelColor);
  87. //将中间的像素点设置成红色
  88. //if (x == widthHeight / 2 || y == widthHeight / 2)
  89. //{
  90. // Color middlePixelColor = new Color(1, 0, 0, 1.0f);
  91. // proceduralTexture.SetPixel(x, y, middlePixelColor);
  92. //}
  93. }
  94. }
  95. proceduralTexture.Apply();
  96. return proceduralTexture;
  97. }
  98. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/108322
推荐阅读
相关标签
  

闽ICP备14008679号