using UnityEngine; using UnityEngine.Rendering; using UnityEngine.UI; public class ModifyTexture : MonoBehaviour { //public Texture2D sourceTexture; // The texture to modify public Color paleColor = Color.white; // The pale color public Color darkColor = Color.black; // The dark color public GameObject Cube; //private Texture2D modifiedTexture; public void ChangeValue() { // Create a new texture in a format that supports SetPixels (e.g., RGBA32) Texture2D sourceTexture = Resources.Load("CABTEXTURE/TEXT.1.2");//Cube.GetComponent().material.GetTexture("_BaseMap") as Texture2D; Texture2D NewTexture = new(sourceTexture.width, sourceTexture.height, TextureFormat.RGBA32, false); // Copy pixel data from source texture to the new texture Color[] pixels = sourceTexture.GetPixels(); // Get original pixels NewTexture.SetPixels(pixels); // Copy them to the new texture NewTexture.Apply(); // Apply the changes to make it ready for modification darkColor=DOIT.CSc(this.transform.GetComponent().text,255); // Now modify the texture's pixels ModifyTexturePixels(NewTexture, paleColor, darkColor,Color.black); // Apply the modified texture to the material or object Cube.GetComponent().material.SetTexture("_BaseMap",NewTexture); } public static void ModifyTexturePixels(Texture2D texture, Color pale, Color dark, Color Grouth) { // Get the pixels of the texture //print("--------From ModifyTexture-----------"); Color32[] pixels = texture.GetPixels32(); Color32[] newPixels = new Color32[pixels.Length]; for (int i = 0; i < pixels.Length; i++) { byte grayR = pixels[i].r; // Assumes grayscale, so R=G=B byte grayG = pixels[i].g; // Assumes grayscale, so R=G=B //byte grayB = pixels[i].r; // Assumes grayscale, so R=G=B if(grayR!=grayG){ newPixels[i] = Grouth; } else{ float normalizedValue = grayR / 255f; newPixels[i] = Color.Lerp(dark, pale, normalizedValue); } } texture.SetPixels32(newPixels); texture.Apply(); // Apply the changes to make them visible } }