using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.AddressableAssets; using UnityEngine; using System.Collections; public class MCXMP : MonoBehaviour { static string Texs; static Material Mat; public static GameObject MCXV(string Category, float t, Vector3[] points, string ctex, string tag) { Material Mat; string Rs = "0"; GameObject S = new("Sides"); GameObject top = new(); GameObject Under = new(); top.AddComponent(typeof(MeshFilter)); top.AddComponent(typeof(MeshRenderer)); float direction = 90; if(Category=="CounterTop2"){direction = 0;} top.GetComponent().mesh = CreateMesh(points, "f", 90);//CreateMeshShape7VR(t, dd, w, P1, P2, P3, P4, P5, P6, P7); Under.AddComponent(typeof(MeshFilter)); Under.AddComponent(typeof(MeshRenderer)); Under.GetComponent().mesh = CreateMesh(points, "r", 90);//CreateMeshShape7VR(t, dd, w, P1, P2, P3, P4, P5, P6, P7); //MAterial if (ctex.Split('?').Length == 2) { Texs = ctex.Split('?')[0]; if (ctex != "MELAMINEWHITE") {Rs = ctex.Split('?')[1];} Mat = GetMat(); Mat.SetFloat("_Cull", 0); } else { //Mat = UIT_MATERIAL.GetCounterTexture(ctex,"SCENE"); Texs = ctex; Mat = UIT_MATERIAL.GetMaterial(ctex); top.tag = tag;//"counter"; Mat.SetFloat("_Cull", 0); } top.GetComponent().material = Mat; DOIT.SetSizeTex(top, 24, 24, 0, 0, 0, 0, direction, 90); top.GetComponent().probeAnchor = GameObject.Find("Reflection Probe").transform; top.transform.parent = S.transform; top.name = "Top"; top.tag = tag;//"counter"; Under.GetComponent().material = Mat; DOIT.SetSizeTex(Under, 24, 24, 0, 0, 0, 0, direction, 90); Under.GetComponent().probeAnchor = GameObject.Find("Reflection Probe").transform; Under.transform.parent = S.transform; Under.name = "Under"; Under.tag = tag;//"counter"; Under.transform.position = new(0, -t, 0); Vector3[] pointsY = new Vector3[points.Length]; Vector3[] sidepoints; for (int i = 0; i < points.Length; i++) { pointsY[i] = new(points[i].x, points[i].y - t, points[i].z); } for (int i = 0; i < points.Length; i++) { int PreviousPoint = i - 1; if (PreviousPoint < 0) PreviousPoint = points.Length - 1; pointsY[i] = new(points[i].x, points[i].y - t, points[i].z); pointsY[PreviousPoint] = new(points[PreviousPoint].x, points[PreviousPoint].y - t, points[PreviousPoint].z); sidepoints = new Vector3[4] { pointsY[i], points[i], points[PreviousPoint], pointsY[PreviousPoint] }; SideV(S, "s" + i.ToString(), sidepoints, Mat);//front } if (Category.Contains ("CounterTop")) { StaticCoroutine.Start(GORETURN(S)); } return S; } static Mesh CreateMesh(Vector3[] vector3Array, string s, float AngleRotation) { Vector2[] vector2Array = new Vector2[vector3Array.Length]; for (int i = 0; i < vector3Array.Length; i++) { vector2Array[i] = new Vector2(vector3Array[i].x, vector3Array[i].z); } // Use the triangulator to get indices for creating triangles Triangulator tr = new(vector2Array); int[] indices = tr.Triangulate(s); // Create the Vector3 vertices Vector3[] vertices = new Vector3[vector2Array.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vector3(vector2Array[i].x, vector2Array[i].y, 0); } // Create the mesh Mesh msh = new() { vertices = vertices }; vertices = msh.vertices; Quaternion rotation = Quaternion.Euler(AngleRotation, 0, 0f); for (int i = 0; i < vertices.Length; i++) { vertices[i] = rotation * vertices[i]; } msh.vertices = vertices; msh.triangles = indices; msh.RecalculateNormals(); msh.RecalculateBounds(); return msh; } static float GetSize(Vector3[] points, string WH) { float minX = float.MaxValue; float maxX = float.MinValue; float minZ = float.MaxValue; float maxZ = float.MinValue; foreach (Vector3 point in points) { if (point.x < minX) minX = point.x; if (point.x > maxX) maxX = point.x; if (point.z < minZ) minZ = point.z; if (point.z > maxZ) maxZ = point.z; } if (WH == "Wided") return maxX - minX; return maxZ - minZ; } //Sides public static GameObject SideV(GameObject S, string N, Vector3[] points, Material Mat) { GameObject s = new(N); s.AddComponent(typeof(MeshFilter)); s.AddComponent(typeof(MeshRenderer)); s.GetComponent().mesh = CreateMeshShape4V(points); float w = GetSize(points, "wide"); float h = GetSize(points, "height"); s.GetComponent().material = Mat; s.GetComponent().probeAnchor = GameObject.Find("Reflection Probe").transform; s.transform.parent = S.transform; s.tag = "counter"; DOIT.SetSizeTex(s, 24, 24, 0, 0, 0, 0, 90, 90); return s; } public static Mesh CreateMeshShape4V(Vector3[] points) { Mesh m = new() { vertices = points, uv = new Vector2[] { new (0, 0), new (0, 1), new (1, 1), new (1, 0), }, triangles = new int[] { 0, 1, 2, 0, 2, 3 } }; m.RecalculateNormals(); m.RecalculateTangents(); return m; } public static Material GetMat() { return UIT_MATERIAL.GetMaterial(Texs); } public static IEnumerator GORETURN(GameObject GO) { // print("From CounterMCXctex===="+Texs);//COUN.1,1,1,FFFFFF,FFFFFF,1,90,S,ffffff,1,none //From CounterMCXctex====COUN.1?#6060A6?#0062AD?#737373?0.9121307_0.7266289?0?0.5?0.1666667?0 if (string.IsNullOrEmpty(Texs)) Texs = "MELAMINEWHITE"; Mat = UIT_MATERIAL.GetMaterial(Texs); Texs.Replace(",", "?"); Mat.name = Texs.Split("?")[0]; //print("Mat.name====" + Mat.name); if(Mat.name!="MELAMINEWHITE" && Texs.Split("?").Length>6){ Addressables.LoadAssetAsync(Mat.name).Completed += handle => { if (handle.Status == AsyncOperationStatus.Succeeded) { Texture2D tex = Instantiate(handle.Result); foreach (Transform Side in GO.transform) { if (Side.childCount == 0) { Side.GetComponent().material.SetTexture("_Texture", tex); } else { foreach (Transform mcx in Side.transform) { mcx.GetComponent().material.SetTexture("_Texture", tex); } } } } else { Debug.LogError("Failed to load texture: " + Mat.name); } }; } yield return null; //new WaitForSeconds(2); } }