using UnityEngine; using System.Collections.Generic; using System.Linq; public class UIT_MATERIAL : MonoBehaviour { //private static Texture2D AdressableTexture; //DISPATCH public static Material GetMaterial(string Code) { ////////print("Code from GetMaterial===1="+Code); //none? #737373? #ffffffff? #ffffffff? 1? 0? 0.1? 0.05 // //1 //1?90 //#737373 ? GLOSSY ? _none //888888_Matt //GYPS.1,#ffffff,#595959,#939393,0.25-0.25,0,1,0.25,0,0 //none?#737373?#ffffffff?#ffffffff?1?0?0.1?0.05 //18?0 //TEXT.1.1,FCFFFEFF,FCFFFEFF,FCFFFEFF,1_1,0,1,0.3,0.05,0 if (Code.Split("_").Length == 2 && Code.Split(",").Length<2 && Code.Split("?").Length<2)//"ffffff_glossy_none { Code += "_none"; Code =Code.Replace("_","?").Replace(",","?"); //////print("Code from color====1="+Code); } if (Code.Split("_").Length == 3 && Code.Split(",").Length<2 && Code.Split("?").Length<2)//"ffffff_glossy_none { Code =Code.Replace("_","?").Replace(",","?"); //////print("Code from color====2="+Code); } Code =Code.Replace(",","?"); string[] CodeSplit = Code.Split("?"); // if (CodeSplit[0] == "Standard") // { // return StandardMaterial(Code); // } if (CodeSplit.Length == 8 && CodeSplit[0]!="none") { Code = "none?" + Code; } if (CodeSplit.Length ==1 && Code.IndexOf("MELAMINEWHITE") == -1) { ////////print("Code========" + Code); string rotation = "0"; if (!string.IsNullOrEmpty(Code)){rotation = UIT.Value(Library.CabTexture, Code, Header.Rotation);} if (rotation == Code) {rotation = "0";} Code += "?" + rotation; } CodeSplit = Code.Split("?");//From GetMaterial=== 16?16 // //////print("Code from GetMaterial===2="+Code);//none?#737373?#ffffffff?#ffffffff?1?0?0.1?0.05 //codeText = TEXT.1.1?#FFFFFF?#FFFFFF?#FFFFFF?1-1?0?1?0.5?0.05?0 key======text.1.1?#ffffff?#ffffff?#ffffff?1-1?0?1?0.5?0.05?00 //Debug.Log("codetexture : " + Code); if (CodeSplit.Length == 2) { return GetMaterialbyID(Code); }//Ex: 3?2; CabTexture ID and CabTextureColor ID else if (CodeSplit.Length == 3) { return ColorGlobalbySimpleColor(Code); }//Ex ffffff ? Glossy ? none else { return GetMaterialbyFullRecipe(Code); }//Ex none? #737373? #ffffffff? #ffffffff? 1? 0? 0.1? 0.05 //else if (Code.IndexOf("?") != -1 && Code.Split("?").Length <= 2) { return GetMaterialbyID(Code); }//ID } //Un swatch coute une copie de Texture2D + une passe pixel CPU + un Sprite.Create. //Les panneaux redessinent les memes ID a chaque ouverture: on garde le resultat. private static readonly Dictionary SpriteCache = new(); public static Sprite GetSprite(string Code) { ////////print("Code====================="+Code); //1 //1?90 //COUN.1?FFFFFF?FFFFFF?FFFFFF?1?0?0.5?0.3?1 //none?FFFFFF?FFFFFF?FFFFFF?1?0?0.5?0.3?1 //none?#FFFFFF?Flat Code =Code.Replace(",", "?").Replace("_", "?"); if (Code.IndexOf("?") == -1 ) { Code += "?" + UIT.Value(Library.CabTexture, Code, Header.Rotation); } if (SpriteCache.TryGetValue(Code, out Sprite Cached) && Cached) { return Cached; } Sprite S; if (Code.Split("?").Length == 3) { S = GetTexturebySimpleColor(Code); }//SimpleColor else if (Code.Split("?").Length <= 2) { S = GetTexturebyID(Code); }//ID else { S = GetTexturebyRecipe(Code); }//LongColor if (S) { SpriteCache[Code] = S; } return S; } //A appeler des qu'une texture de la librairie change de couleur/rotation, sinon on ressert l'ancienne public static void ClearSpriteCache() { foreach (Sprite S in SpriteCache.Values) { if (!S) { continue; } if (S.texture) { Destroy(S.texture); } Destroy(S); } SpriteCache.Clear(); } //MATERIAL//---------------------------------------------------------------------------------------------- // public static Material StandardMaterial(string code) // { // ////////print("code====================="+code);//code=====================Standard_Amazzonia_B0AEAE_0.5_0.5 // string[] CodeSplit = code.Split("_");//Standard,texture,color,tile,smoothness,Metalic // Texture2D T = Resources.Load("TEXTURES/COUNTERTOP/Laminam_" + CodeSplit[1]); // float Smoothness = DOIT.ConvertStringToNumber(CodeSplit[3]); // float Metalic = DOIT.ConvertStringToNumber(CodeSplit[4]); // float TileH = 0.25f;//DOIT.ConvertStringToNumber(CodeSplit[3]); // float TileV = 0.25f; // string Color = CodeSplit[2]; // Material MAT = Instantiate(_G.ST); // MAT.SetTexture("_BaseMap", T); // MAT.SetFloat("_Smoothness", Smoothness); // MAT.SetFloat("_Metallic", Metalic); // MAT.SetColor("_BaseColor", DOIT.CSc(Color, 255)); // MAT.mainTextureScale = new(TileH, TileV); // return MAT; // } public static Material ColorGlobalbySimpleColor(string code) { ////print("codeText====================="+code);///ffffff_Semi_Wood1 code====#1A8E7DFF_Matt_Wood.2 //#D6B780_Glossy_FLAT //string key = code.Trim().ToLowerInvariant(); //if (_G.MaterialOnScene.TryGetValue(key, out var m) && m) return m; code = code.Replace("Glossy", "Gloss").Replace("_", "?"); string[] Code = code.Split('?');//ffffff_Glossy_Fabric1 if (Code.Length == 2) { Code = new string[3] { Code[0], Code[1], "none" }; } Material Mat; if (!_C.ColorInScene.ContainsKey(code)) { Mat = Instantiate(_G.TEXTURE); //Mat.SetTexture("_Texture", null); Texture2D T = Resources.Load("TEXTURES/TEXT/TEXT.1.1"); //Textures if (Code[2] != "none") { switch (Code[2]) { case "3": T = _G.Wood[0]; break; case "4": T = _G.Wood[1]; break; case "5": T = _G.Wood[2]; break; case "6": T = _G.Wood[0]; break; case "7": T = _G.Wood[1]; break; } } Mat.SetTexture("_Texture", T); Mat.SetColor("_c1", DOIT.CSc(Code[0], 255)); //FINISH float Metallic = GetMetalnessFromSimpleColor(Code[1]); float Smoothness = GetSmoothnessFromSimpleColor(Code[1]); Mat.SetFloat("_Smoothness", Smoothness); Mat.SetFloat("_Metallic", Metallic); Mat.SetFloat("_Speculor", Smoothness); _C.ColorInScene.Add(code, Mat); } else { Mat = _C.ColorInScene[code]; } //_G.MaterialOnScene[key] = Mat; return Mat; } public static Material GetMaterialbyID(string Code) { ////////print("From GetMaterial=== "+Code);//1?90//From GetMaterial=== 17?17 16/90 // string key = Code+"0"; // if (_G.MaterialOnScene.TryGetValue(key, out var m) && m) return m; // string key90 = Code+"90"; // if (_G.MaterialOnScene.TryGetValue(key90, out var m90) && m90) return m90; string[] code = Code.Split('?'); //code[0]=UIT.IDCheckExist(Library.CabTexture, code[0]); //WOOD.1,#FFF0D8FF,#827A6E,#5B564D,1,0,0.3,0.2,1 Material M; M = Instantiate(_G.TEXTURE); Color c1 = DOIT.CSc("#F6F6F6", 255); Color c2 = DOIT.CSc("#F6F6F6", 255); Color c3 = DOIT.CSc("#F6F6F6", 255); if (code[0] == "MELAMINEWHITE" || code[0].IndexOf("Mellamine") != -1) { Texture2D T = Resources.Load("TEXTURES/TEXT/TEXT.1.1"); M.SetColor("_c1", c1); M.SetColor("_c2", c2); M.SetColor("_c3", c3); M.SetTexture("_Texture", T); M.SetFloat("_Smoothness", 0.2f); M.SetFloat("_Metallic", 0.05f); } else { string R = "0"; if (code.Length > 1) { R = code[1]; } if (R != "0" && R != "90") { R = "0"; } ////////print("R================="+R); //string[] line=UIT.GetLibraryLine(Library.CabTexture, code[0]); //////print("line[0]======="+line[0]); string Contrast = UIT.Value(Library.CabTexture, code[0], Header.Contrast); string nameTexture = UIT.Value(Library.CabTexture, code[0], Header.Texture); ////////print("From library===="+Contrast);//nameTexture====S ////////print("From library===="+nameTexture);//nameTexture====S if (string.IsNullOrEmpty(Contrast)) { Contrast = "1"; } Texture2D T = Instantiate(Resources.Load("TEXTURES/TEXT/" + nameTexture)); T.name = nameTexture; string c3value = UIT.Value(Library.CabTexture, code[0], "C3"); //Colors c1 = DOIT.CSc(UIT.Value(Library.CabTexture, code[0], "C1"), 255); c2 = DOIT.CSc(UIT.Value(Library.CabTexture, code[0], "C2"), 255); if (!string.IsNullOrEmpty(c3value)) { c3 = DOIT.CSc(UIT.Value(Library.CabTexture, code[0], "C3"), 255); } //ModifyTexture.ModifyTexturePixels(T,DOIT.CSc(c1, 255),DOIT.CSc(c2, 255),DOIT.CSc(c3, 255)); //finish string S = UIT.Value(Library.CabTexture, code[0], Header.Finish); ////////print("From library===="+S); if (S.IndexOf("G") != -1) { S = "0.95"; } else if (S.IndexOf("S") != -1 && code[0].Contains("TEXT.1")) { S = "0.8"; } else if (S.IndexOf("S") != -1 && !code[0].Contains("TEXT.1")) { S = "0.3"; } else if (S.IndexOf("M") != -1) { S = "0.1"; } else {S = "0.3"; } float smoothness = DOIT.ConvertStringToNumber(S); ////print("smoothness================="+smoothness+" From Code====="+Code); //Grouth M.SetTexture("_Texture", T); M.SetColor("_c1", c1); M.SetColor("_c2", c2); if (!string.IsNullOrEmpty(c3value)) { M.SetColor("_c3", c3); } M.SetFloat("_Metallic", 0.05f); M.SetFloat("_Smoothness", smoothness); M.SetFloat("_Speculor", smoothness * 1.2f); // M.SetFloat("_Relief", 0.3f); M.SetFloat("_Rotation", float.Parse(R)); } //key =Code+ M.GetFloat("_Rotation").ToString(); //if(!_G.MaterialOnScene.ContainsKey(key)){_G.MaterialOnScene[key]=M;} return M; } public static Material GetMaterialbyFullRecipe(string Recipe) { string StartRecipe = Recipe; //if (_G.MaterialOnScene.TryGetValue(Recipe, out var m) && m) return m; //CERA.15?#FFFFFF?#171717?#737373?1_1?-42?0.5?0.1666667?0 //Texture,C1,C2,C3,Tile,Rotation,Contrast,smoothness,Mettallic,relief //TEXT.1.1, // FCFFFEFF, // FCFFFEFF, // FCFFFEFF, // 1_1, // 0, // 1, // 0.3, // 0.05, // 0 // rotation===0 Recipe=Recipe.Replace(",", "?"); string[] CodeSplit = Recipe.Split("?"); if (CodeSplit.Length == 8)//Rotation missing { Recipe = CodeSplit[0] + "?" + CodeSplit[1] + "?" + CodeSplit[2] + "?" + CodeSplit[3] +"?" + CodeSplit[4] +"?0?" + CodeSplit[5] + "?" + CodeSplit[6] + "?0?0"; } if (CodeSplit.Length == 6) { Recipe = CodeSplit[0] + "?" + CodeSplit[1] + "?" + CodeSplit[2] + "?" + CodeSplit[3] + "?1-1?" + CodeSplit[4] + "?" + CodeSplit[5] + "?0.5?0?0?0"; } //Recipe = Recipe.Replace(",", "?"); ////////print("Recipe================2=" +Recipe); string[] code = Recipe.Split('?'); Material M = Instantiate(_G.TEXTURE); Color c1 = DOIT.CSc("#ffffff", 255); Color c2 = DOIT.CSc("#ffffff", 255); Color c3 = DOIT.CSc("#ffffff", 255); if (code[0] == "MELAMINEWHITE") { Texture2D T = Resources.Load("TEXTURES/TEXT/TEXT.1.1"); M.SetColor("_c1", c1); M.SetColor("_c2", c2); M.SetColor("_c3", c3); M.SetTexture("_Texture", T); M.SetFloat("_Rotation", 0); M.SetFloat("_BaseMap", 1); M.SetFloat("_Smoothness", 0.3f); M.SetFloat("_Metallic", 0.05f); M.SetFloat("_Speculor", 0.2f); } else { //Texture Texture2D T = null; //Colors c1 = DOIT.CSc(code[1], 255); c2 = DOIT.CSc(code[2], 255); c3 = DOIT.CSc(code[3], 255); ////////print("code[0]===="+code[0]);// GYPS.1,FFFFFFFF,313131FF,FFFFFFFF,0.25,0.2,0.2 //_G.FLCs=====CERA.13?#FFFFFF?#323232?#737373?1_1?56?0.5?0.1666667?0 //TEXTURE //BRIC.4,984B1DFF,070605FF,BDBDBDFF,2-2,0,1,0.75,0.2666667,0 if (code[0] != "none" && code[0] != "null") {//WOOD.1 string TextureCategory = code[0].Split('.')[0]; if (new List() { "WOOD", "CERA", "SPLA", "COUN", "BRIC" }.Contains(TextureCategory) && code[0] != "WOOD.1") { T = null; } else { T = Resources.Load("TEXTURES/" + code[0].Split(".")[0] + "/" + code[0]); } } ////////print("code[4]==="+code[4]); //Tile code[4]=code[4].Replace("_", "-"); string[] Code4 = code[4].Split("-"); string sizeH = Code4[0]; string sizeV = Code4[0]; if (Code4.Length > 1) { sizeV = Code4[1]; } //Size Vector2 Tile = new(float.Parse(sizeH), float.Parse(sizeV)); //Rotation float R = 0; if (!string.IsNullOrEmpty(code[5])) { R = float.Parse(code[5]); } //if (R != 0 && R != 90) { R = 0; } ////////print("_G.FLCs====3=" + R+" code 5=="+code[5]); if (code[7].Any(char.IsLetter)) {code[7] = "0";} if (code[8].Any(char.IsLetter)) {code[8] = "0.2";} //Contrast float Contrast = float.Parse(code[6]); //Smoothness float Smoothness= float.Parse(code[7]); ////print("Smoothness======="+Smoothness); //Metalique float Metallic = float.Parse(code[8]); if (Metallic == 1) {Metallic = 0;} //Relief float Relief = 0.2f; if (code.Length == 10 && code[9] != "none") { Relief = float.Parse(code[9]); } M.SetTexture("_Texture", T); M.SetColor("_c1", c1); M.SetColor("_c2", c2); M.SetColor("_c3", c3); M.SetFloat("_Rotation", R); M.SetVector("_Tile", Tile); M.SetFloat("_contrast", Contrast); M.SetFloat("_Smoothness", Smoothness); M.SetFloat("_Metallic", Metallic); M.SetFloat("_Speculor", Smoothness); M.SetFloat("_Relief", Relief); } //_G.MaterialOnScene.Add(StartRecipe,M); return M; } // public static Material GetElectroMat() // { // if (_G.MaterialOnScene.TryGetValue(Recipe, out var m) && m) return m; // Material ElectroMat = Instantiate(_G.TEXTURE); // ElectroMat.SetColor("_c1", DOIT.CSc("#ffffff", 255)); // ElectroMat.SetColor("_c2", DOIT.CSc("#000000", 255)); // ElectroMat.SetColor("_c3", DOIT.CSc("#000000", 255)); // ElectroMat.SetFloat("_Smoothness", 0.9f); // ElectroMat.SetFloat("_Metallic", 0.8f); // ElectroMat.SetFloat("_Speculor", 0.9f); // return ElectroMat; // } //TEXTURE//---------------------------------------------------------------------------------------------- public static Sprite GetTexturebySimpleColor(string SimpleColor) { ////////print("From GetTexturebySimpleColor=== "+SimpleColor); string[] code = SimpleColor.Split('_'); string nameTexture = UIT.Value(Library.CabTexture, code[0], Header.Texture); //TEXTURE Texture2D T; T = Instantiate(Resources.Load("TEXTURES/" + nameTexture.Split('.')[0] + "/" + nameTexture)); string R = "0"; //Colors string c1 = UIT.Value(Library.CabTexture, code[0], Header.C1); string c2 = UIT.Value(Library.CabTexture, code[0], Header.C2); ModifyTexture.ModifyTexturePixels(T, DOIT.CSc(c1, 255), DOIT.CSc(c2, 255), Color.black); if (R == "90") RotateTexture(T, int.Parse(R)); //Grouth return TextureToSprite(T); } public static Sprite GetTexturebyID(string CodeID) { // //////print("From GetTexture=== "+CodeID);//2?90 string[] code = CodeID.Split('?'); code[0] = UIT.IDCheckExist(Library.CabTexture, code[0]); string R = "0"; if (code.Length > 1) R = code[1]; string nameTexture = UIT.Value(Library.CabTexture, code[0], Header.Texture); ////////print("nameTexture=======" + nameTexture); Texture2D T = Instantiate(Resources.Load("TEXTURES/" + nameTexture.Split('.')[0] + "/" + nameTexture)); //Colors string c1 = UIT.Value(Library.CabTexture, code[0], Header.C1); string c2 = UIT.Value(Library.CabTexture, code[0], Header.C2); ModifyTexture.ModifyTexturePixels(T, DOIT.CSc(c1, 255), DOIT.CSc(c2, 255), Color.black); if (R == "90") { T = RotateTexture(T, int.Parse(R)); } //Grouth return TextureToSprite(T); } public static Sprite GetTexturebyRecipe(string CodeRecipe) { //COUN.1?F6F6F6?F6F6F6?F6F6F6?1?0?0.5?0.3?1 //////print("code[0]========CodeRecipe===="+CodeRecipe); CodeRecipe = CodeRecipe.Replace(",", "?"); string[] code = CodeRecipe.Split('?'); string R = "0"; if (code[0] == "none") code[0] = "TEXT.1.1"; //Colors Color c1 = DOIT.CSc(code[1], 255); Color c2 = DOIT.CSc(code[2], 255); Color c3 = DOIT.CSc(code[3], 255); Texture2D T = null; if (code[0].IndexOf("FAUC") != -1) { code[0] = null; }//Should be simpleColor if (code[0] != null) { string path = "TEXTURES/" + code[0].Split(".")[0] + "/" + code[0]; //////print("code[0]========path===="+path);//code[0]====TOPCOUNTER/Grano //code[0]========path====TEXTURES/TEXT/TEXT.1 if (code[0].IndexOf("TOPCOUNTER") != -1) { path = "TEXTURES/COUN/" + code[0]; } ////////print("path===="+path);//path====TEXTURES/COUN/TOPCOUNTER/Burgundy T = null; if (new List() { "WOOD", "CERA", "SPLA", "COUN", "BRIC","none" }.Contains(code[0].Split(".")[0]) ) { T = null; } else { T = Instantiate(Resources.Load(path)); ModifyTexture.ModifyTexturePixels(T, c1, c2, c3); if (R == "90") RotateTexture(T, int.Parse(R)); return TextureToSprite(T); } } return null; } ///Get-------------------- public static Sprite TextureToSprite(Texture2D texture) { return Sprite.Create( texture, new Rect(0, 0, texture.width, texture.height), // Use the full texture area new Vector2(0.5f, 0.5f) // Set the pivot to the center of the sprite ); } // public static void ModifyTexturePixels(Texture2D texture, Color pale, Color dark) // { // //////print("--------From UIT_MATERIAL----------"); // Color red=Color.red; // // Get the pixels of the texture // Color[] pixels = texture.GetPixels(); // // Loop through each pixel and modify its color // for (int i = 0; i < pixels.Length; i++) // { // // Get the brightness of the pixel (grayscale value) // float brightness = pixels[i].grayscale; // if (brightness == 0) // { // pixels[i] = red; // } // else{ // // Interpolate between the dark and pale colors based on brightness // pixels[i] = Color.Lerp(dark, pale, brightness); // } // } // // Set the modified pixels back to the texture // texture.SetPixels(pixels); // texture.Apply(); // Apply the changes to make them visible // } // public static Texture2D RotateTexture(Texture2D texture, int angle) // { // Texture2D result; // Color[] pixels = texture.GetPixels(); // int width = texture.width; // int height = texture.height; // // Handle 90-degree rotations // switch (angle) // { // case 90: // result = new Texture2D(height, width); // Note that width and height are swapped // for (int y = 0; y < height; y++) // { // for (int x = 0; x < width; x++) // { // result.SetPixel(y, width - x - 1, pixels[x + y * width]); // } // } // break; // case 180: // result = new Texture2D(width, height); // Keep the same dimensions // for (int y = 0; y < height; y++) // { // for (int x = 0; x < width; x++) // { // result.SetPixel(width - x - 1, height - y - 1, pixels[x + y * width]); // } // } // break; // case 270: // result = new Texture2D(height, width); // Note that width and height are swapped // for (int y = 0; y < height; y++) // { // for (int x = 0; x < width; x++) // { // result.SetPixel(height - y - 1, x, pixels[x + y * width]); // } // } // break; // default: // Debug.LogError("Angle must be 90, 180, or 270 degrees."); // return texture; // } // result.Apply(); // Apply the changes to the texture // return result; // } public static string GetRecipeColorFromSimpleColor(string SimpleRecipe) { //////print("SimpleRecipe===" + SimpleRecipe);//SimpleRecipe===#737373_Matt_FLAT SimpleRecipe =SimpleRecipe.Replace("_", "?").Replace(",", "?"); if (SimpleRecipe.Split("?").Length != 3) { return SimpleRecipe; } //#fffffff_gloss_Text.1 //Texture,c1,c2,c3,tile,rotation,smoothness,metalic string[] CodeSimple = SimpleRecipe.Split('?'); //Texture string TextureName = "none"; switch (CodeSimple[2]) { case "Wood.1": TextureName = "TEXT.1.2"; break; case "Wood.2": TextureName = "TEXT.1.2"; break; case "Fabric.1": TextureName = "TEXT.1.20"; break; case "Fabric.2": TextureName = "TEXT.1.21"; break; } string Smoothness = GetSmoothnessFromSimpleColor(CodeSimple[1]).ToString(); //Matt, Semi, Gloss string Metallic = GetMetalnessFromSimpleColor(CodeSimple[1]).ToString();//None, Wood1, wood2, fabric1 fabric2, Metal List Recipelist = new() { TextureName, CodeSimple[0], "#ffffffff", "#ffffffff", "1", "0", Smoothness, Metallic }; string newRecipe = ""; for (int i = 0; i < 8; i++) { newRecipe += Recipelist[i]; if (i < 7) newRecipe += "?"; } return newRecipe; } public static float GetSmoothnessFromSimpleColor(string Finish) { Finish=Finish[..1]; return Finish switch { "G" => 0.95f, "L" => 0.95f, "S" => 0.5f, "M" => 0.0f, _ => 0, }; } public static float GetMetalnessFromSimpleColor(string Finish) { return Finish switch { "Gloss" => 0.95f, "Semi" => 0.6f, "Matt" => 0.1f, _ => 0, }; } public static float GetMetallicFromSimpleColor(string Grain) { return Grain switch { "none" => 0.1f, "Flat" => 0.5f, "Wood1" => 0.5f, "Wood2" => 0.5f, "Fabric1" => 0.5f, "Fabric2" => 0.5f, "Metal" => 0.9f, _ => 0.5f, }; } public static string GetTextureNameFromSimpleColor(string TextureCat) { return TextureCat switch { "Wood.1" => _G.Wood[0].name, "Wood.2" => _G.Wood[1].name, "Fabric.1" => _G.Fabric[0].name, "Fabric.2" => _G.Fabric[0].name, _ => TextureCat, }; } public static Texture2D RotateTexture(Texture2D texture, float angle) { int width = texture.width; int height = texture.height; // Create a new texture to store the rotated pixels Texture2D rotatedTexture = new Texture2D(width, height); // Get the center point of the texture to rotate around Vector2 pivot = new(width / 2f, height / 2f); // Convert the angle to radians float radians = angle * Mathf.Deg2Rad; // Loop through each pixel in the new texture for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // Calculate the position relative to the pivot Vector2 position = new Vector2(x, y) - pivot; // Rotate the position around the pivot float rotatedX = position.x * Mathf.Cos(radians) - position.y * Mathf.Sin(radians); float rotatedY = position.x * Mathf.Sin(radians) + position.y * Mathf.Cos(radians); // Translate the rotated position back relative to the texture rotatedX += pivot.x; rotatedY += pivot.y; // Get the color from the original texture if within bounds Color newColor = Color.clear; if (rotatedX >= 0 && rotatedX < width && rotatedY >= 0 && rotatedY < height) { newColor = texture.GetPixel(Mathf.FloorToInt(rotatedX), Mathf.FloorToInt(rotatedY)); } // Set the pixel in the new texture rotatedTexture.SetPixel(x, y, newColor); } } // Apply changes to the rotated texture rotatedTexture.Apply(); return rotatedTexture; } }