//using System.Numerics; using System; using System.Collections.Generic; using UnityEngine; public class ConstructOpen : MonoBehaviour { static float os = 1f; public static void AddOpen(int nO) { string[] C = _G.OBJs[nO]; string Name = C[0]; float w = DOIT.ConvertStringToNumber(C[6]); float h = DOIT.ConvertStringToNumber(C[7]); float d = DOIT.ConvertStringToNumber(C[8]); //float t = DOIT.ConvertStringToNumber(_G.OBJs[nO][9]); float px = DOIT.ConvertStringToNumber(C[15]); float py = DOIT.ConvertStringToNumber(C[16]); float pz = DOIT.ConvertStringToNumber(C[17]); float rx = DOIT.ConvertStringToNumber(C[18]); float ry = DOIT.ConvertStringToNumber(C[19]); float rz = DOIT.ConvertStringToNumber(C[20]); string model = C[1]; List AllPoints=new(); //create arche GameObject BASE = GameObject.CreatePrimitive(PrimitiveType.Cube); BASE.transform.localScale = new Vector3(w + 0.5f, h + 0.5f, d + 0.5f); BASE.name = Name; //Create Frame GameObject FRAME = new ("Edges"); FRAME.transform.parent = BASE.transform; //Anchor points CREATE.Point(BASE, "PBLU", -w * 0.5f, h * 0.5f, 0f);//P1 CREATE.Point(BASE, "PBRU", w * 0.5f, h * 0.5f, 0f);//P2 CREATE.Point(BASE, "PFLU", -w * 0.5f, h * 0.5f, 0f);//P3 CREATE.Point(BASE, "PFRU", w * 0.5f, h * 0.5f, 0f);//P4 CREATE.Point(BASE, "PBLB", -w * 0.5f, -h * 0.5f, 0f);//P1 CREATE.Point(BASE, "PBRB", w * 0.5f, -h * 0.5f, 0f);//P2 CREATE.Point(BASE, "PFLB", -w * 0.5f, -h * 0.5f, 0f);//P3 CREATE.Point(BASE, "PFRB", w * 0.5f, -h * 0.5f, 0f);//P4 CREATE.Point(BASE, "P1", w * 0.5f, -h * 0.5f+0.1f, 0f); CREATE.Point(BASE, "P2", -w * 0.5f, -h * 0.5f+0.1f, 0f); AllPoints.Add(new(-w * 0.5f, -h * 0.5f, 0f)); float FramePos=d*0.5f; //CuttWall points if(model=="OPEN101"){ CREATE.Point(BASE, "P3", -w * 0.5f, h * 0.5f, 0f); CREATE.Point(BASE, "P4", w * 0.5f, h * 0.5f, 0f); AllPoints.Add(new(-w * 0.5f, h * 0.5f, 0f)); AllPoints.Add(new(w * 0.5f, h * 0.5f, 0f)); } if(model=="OPEN102"){ float Topheight= 6; CREATE.Point(BASE, "P3", -w * 0.5f, h * 0.5f-Topheight, 0f); AllPoints.Add(new(-w * 0.5f, h * 0.5f-Topheight, 0f)); Vector3 StartArc=new(-w * 0.5f,h * 0.5f-Topheight,0); Vector3 TopArc=new(0,h * 0.5f,0); Vector3 EndArc=new(w * 0.5f,h * 0.5f-Topheight,0); float CenterHeight = h * 0.5f- CalculateRadius(StartArc, TopArc, EndArc); Vector3 Origine=new(0,CenterHeight,0); Vector3 Direcion=(StartArc-Origine).normalized; float RadiantLenght=Vector3.Distance(Origine,StartArc); Vector3 NextPoint; float Angle=CalculateAngle(StartArc, EndArc,Origine); for(int i=4; i<15; i++){ Quaternion rotation = Quaternion.Euler(0, 0,-Angle/12); Direcion=rotation*Direcion; NextPoint=Origine+RadiantLenght*Direcion; CREATE.Point(BASE, "P"+i.ToString(), NextPoint.x, NextPoint.y, 0f); AllPoints.Add(NextPoint); } CREATE.Point(BASE, "P15", w * 0.5f, h * 0.5f-Topheight, 0f); AllPoints.Add(new( w * 0.5f, h * 0.5f-Topheight, 0f)); } if(model=="OPEN103"){ float Topheight= w*0.5f; CREATE.Point(BASE, "P3", -w * 0.5f, h * 0.5f-Topheight, 0f); AllPoints.Add(new(-w * 0.5f, h * 0.5f-Topheight, 0f)); Vector3 StartArc=new(-w * 0.5f,h * 0.5f-Topheight,0); Vector3 TopArc=new(0,h * 0.5f,0); Vector3 EndArc=new(w * 0.5f,h * 0.5f-Topheight,0); float CenterHeight = h * 0.5f- CalculateRadius(StartArc, TopArc, EndArc); Vector3 Origine=new(0,CenterHeight,0); Vector3 Direcion=(StartArc-Origine).normalized; float RadiantLenght=Vector3.Distance(Origine,StartArc); Vector3 NextPoint; float Angle=CalculateAngle(StartArc, EndArc,Origine); for(int i=4; i<16; i++){ Quaternion rotation = Quaternion.Euler(0, 0,-Angle/12); Direcion=rotation*Direcion; NextPoint=Origine+RadiantLenght*Direcion; CREATE.Point(BASE, "P"+i.ToString(), NextPoint.x, NextPoint.y, 0f); AllPoints.Add(NextPoint); } CREATE.Point(BASE, "P16", w * 0.5f, h * 0.5f-Topheight, 0f); AllPoints.Add(new( w * 0.5f, h * 0.5f-Topheight, 0f)); } if(model=="OPEN104"){ float Topheight = 6f; CREATE.Point(BASE, "P3", -w * 0.5f, h * 0.5f-Topheight, 0f); CREATE.Point(BASE, "P4", -w * 0.5f+Topheight, h * 0.5f, 0f); CREATE.Point(BASE, "P5", w * 0.5f-Topheight, h * 0.5f, 0f); CREATE.Point(BASE, "P6", w * 0.5f, h * 0.5f-Topheight, 0f); AllPoints.Add(new(-w * 0.5f, h * 0.5f-Topheight, 0f)); AllPoints.Add(new(-w * 0.5f+Topheight, h * 0.5f, 0f)); AllPoints.Add(new(w * 0.5f-Topheight, h * 0.5f, 0f)); AllPoints.Add(new(w * 0.5f, h * 0.5f-Topheight, 0f)); } if(model=="OPEN105"){ float Topheight= 6f; Vector3 NextPoint; CREATE.Point(BASE, "P3", -w * 0.5f, h * 0.5f-Topheight, 0f); AllPoints.Add(new(-w * 0.5f, h * 0.5f-Topheight, 0f)); float Angle=90; Vector3 StartArc=new(-w * 0.5f,h * 0.5f-Topheight,0); Vector3 Origine=new( -w * 0.5f+Topheight,h * 0.5f-Topheight,0); Vector3 Direcion=(StartArc-Origine).normalized; float RadiantLenght=Topheight; for(int i=4; i<7; i++){ Quaternion rotation = Quaternion.Euler(0, 0,-Angle/4); Direcion=rotation*Direcion; NextPoint=Origine+RadiantLenght*Direcion; CREATE.Point(BASE, "P"+i.ToString(), NextPoint.x, NextPoint.y, 0f); AllPoints.Add(NextPoint); } CREATE.Point(BASE, "P7", -w * 0.5f+Topheight, h * 0.5f, 0f); AllPoints.Add(new(-w * 0.5f+Topheight, h * 0.5f, 0f)); CREATE.Point(BASE, "P8", w * 0.5f-Topheight, h * 0.5f, 0f); AllPoints.Add(new(w * 0.5f-Topheight, h * 0.5f, 0f)); StartArc=new(w * 0.5f-Topheight,h * 0.5f,0); Origine=new( w * 0.5f-Topheight,h * 0.5f-Topheight,0); Direcion=(StartArc-Origine).normalized; for(int i=9; i<12; i++){ Quaternion rotation = Quaternion.Euler(0, 0,-Angle/4); Direcion=rotation*Direcion; NextPoint=Origine+RadiantLenght*Direcion; CREATE.Point(BASE, "P"+i.ToString(), NextPoint.x, NextPoint.y, 0f); AllPoints.Add(NextPoint); } CREATE.Point(BASE, "P12", w * 0.5f, h * 0.5f-Topheight, 0f); AllPoints.Add(new(w * 0.5f, h * 0.5f-Topheight, 0f)); } AllPoints.Add(new(w * 0.5f, -h * 0.5f, 0f)); // //BACKGROUND GameObject bg = CreateMcx("bg", w + 10, 1, h + 10, 0, 0, d-1, -90, 0, 0); bg.GetComponent().material.color = new Color32(114, 115, 118, 255); bg.GetComponent().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided; bg.transform.parent = BASE.transform; //EDGES FLOOR GameObject ff = CreateMcx("ff", w, 1, d, 0, -h / 2, FramePos-1, 0, 0, 0); ff.GetComponent().material = SceneModeManager.Floor.GetComponent().material; ff.transform.parent = BASE.transform; DOIT.SetSizeTex(ff, 48, 48, 0, 0, 0, 0, 0, 0); for(int i=0; i().material.color = col; // fl.GetComponent().material.color = col; // fr.GetComponent().material.color = col; // ft.transform.parent = BASE.transform; // fr.transform.parent = BASE.transform; // fl.transform.parent = BASE.transform; Mesure.Addpoints("WALL",BASE); //RigidBody BASE.AddComponent(); BASE.GetComponent().useGravity = false; BASE.GetComponent().isKinematic = true; BASE.GetComponent().detectCollisions = true; BASE.GetComponent().isTrigger = true; //Set in scene BASE.transform.position = new Vector3(px, py, pz); BASE.transform.localRotation = Quaternion.Euler(rx, ry, rz); BASE.AddComponent(typeof(MoveObject)); BASE.GetComponent().material = _G.INV; // DOIT.SetSizeTex(ft, 48, 48, 0, 0, 0, 0, 0, 0); // DOIT.SetSizeTex(fr, 48, 48, 0, 0, 0, 0, 90, 90); // DOIT.SetSizeTex(fl, 48, 48, 0, 0, 0, 0, 90, 90); DOIT.SetSizeTex(ff, 48, 48, 0, 0, 0, 0, 0, 0); BASE.tag = "opening"; BASE.transform.SetParent(SceneModeManager.Scene); RedrawOpen(); } public static GameObject CreateMcx(string N, float sx, float sy, float sz, float px, float py, float pz, float rx, float ry, float rz) { GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Plane); obj.transform.localScale = new Vector3(sx*0.1f, sy*0.1f, sz*0.1f); obj.transform.position = new Vector3(px, py, pz + os); obj.transform.rotation = Quaternion.Euler(rx, ry, rz); //obj.tag = "open"; obj.name = N; Material Mat=Instantiate(_G.ST); obj.GetComponent().material = Mat;//new Material(Shader.Find("Standard")); obj.GetComponent().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided; Mat.SetFloat("_Smoothness",0f); Mat.SetFloat("_Metallic",0f); return obj; } public static void RedrawOpen() // { GameObject[] openings; openings = GameObject.FindGameObjectsWithTag("opening"); foreach (GameObject obj in openings) { //print("obj.name-------------------"+ obj.name); int nO = int.Parse(obj.name.Replace("open", "")); string wall = _G.OBJs[nO][22]; Transform wallObject = SceneModeManager.GetObjectInRoom(wall); if (wallObject == null) { Debug.Log($"No wall found for {wall}"); } Transform s1 = wallObject.Find("s1"); if (s1 == null) { Debug.Log($"No s1 found for in {wall}"); } if (!s1.TryGetComponent(out var renderer)) { Debug.Log($"No renderer found for in s1 in {wall}"); } Material mat = renderer.material; mat.SetFloat("_Smoothness",0f); mat.SetFloat("_Metallic",0f); foreach (Transform Side in obj.transform.Find("Edges").transform) { Side.GetComponent().material = mat; if (Side.GetComponent().material.GetTexture("_Texture") != null) { DOIT.SetSizeTex(Side.gameObject, 48, 48, 0, 0, 0, 0, 90, 90); } } // obj.transform.Find("ft").GetComponent().material = mat; // obj.transform.Find("fr").GetComponent().material = mat; // obj.transform.Find("fl").GetComponent().material = mat; obj.transform.Find("ff").GetComponent().material = UIT_MATERIAL.GetMaterial(_G.FLCs); obj.transform.Find("ff").GetComponent().probeAnchor = GameObject.Find("Reflection Probe").transform; //_G.OBJs[nO][60]= ColorUtility.ToHtmlStringRGBA(col); } } static void CreateFrame(GameObject O, Vector3 p1,Vector3 p2,float w, float d,float MenoPos){ float Length= Vector3.Distance(p1,p2); Vector3 PositionCenter=Vector3.Lerp(p1,p2,0.5f); float angle = Mathf.Rad2Deg * Mathf.Atan2(p1.y -p2.y, p1.x - p2.x); //print("angle===="+angle); CreateSide(O, "Side", Length, w, d, PositionCenter.x, PositionCenter.y, MenoPos,angle); } static void CreateSide(GameObject O, string n, float sx, float sy, float sz, float px, float py, float pz ,float RotationZ ) { GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Plane); //obj.GetComponent().enabled = false; obj.transform.position = new Vector3(px, py, pz); obj.transform.eulerAngles=new(0,0,RotationZ); obj.transform.localScale = new Vector3(sx*0.1f, sy*0.1f, sz*0.1f); //Color Frame Material Mat=Instantiate(_G.ST); obj.GetComponent().material = Mat;//new Material(Shader.Find("Diffuse")); obj.GetComponent().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided; Mat.SetFloat("_Smoothness",0f); Mat.SetFloat("_Metallic",0f); obj.name = n; obj.transform.SetParent(O.transform); //obj.GetComponent().enabled = false; } //Calculate public static float CalculateRadius(Vector3 point1, Vector3 point2, Vector3 point3) { // Ensure the points lie on the same plane (XY-plane is assumed, Z is ignored) Vector2 p1 = new Vector2(point1.x, point1.y); Vector2 p2 = new Vector2(point2.x, point2.y); Vector2 p3 = new Vector2(point3.x, point3.y); // Calculate the midpoints of two chords Vector2 mid1 = (p1 + p2) / 2; Vector2 mid2 = (p2 + p3) / 2; // Calculate perpendicular slopes of the chords Vector2 dir1 = (p2 - p1).normalized; Vector2 dir2 = (p3 - p2).normalized; Vector2 perp1 = new Vector2(-dir1.y, dir1.x); // Perpendicular to dir1 Vector2 perp2 = new Vector2(-dir2.y, dir2.x); // Perpendicular to dir2 // Calculate intersection of the perpendicular bisectors // Solve for center point (Cx, Cy) if (!LineIntersection(mid1, perp1, mid2, perp2, out Vector2 center)) { Debug.LogError("Points are collinear or invalid for a circle."); return -1f; } // Calculate the radius as the distance from the center to one of the points float radius = Vector2.Distance(center, p1); return radius; } private static bool LineIntersection(Vector2 pointA, Vector2 dirA, Vector2 pointB, Vector2 dirB, out Vector2 intersection) { float determinant = dirA.x * dirB.y - dirA.y * dirB.x; if (Mathf.Abs(determinant) < 1e-6) { intersection = Vector2.zero; return false; // Lines are parallel } float t = ((pointB.x - pointA.x) * dirB.y - (pointB.y - pointA.y) * dirB.x) / determinant; intersection = pointA + t * dirA; return true; } public static float CalculateAngle(Vector3 point1, Vector3 point3, Vector2 center) { Vector2 p1 = new Vector2(point1.x, point1.y); Vector2 p3 = new Vector2(point3.x, point3.y); // Vectors from the center to the points Vector2 v1 = p1 - center; Vector2 v2 = p3 - center; // Calculate the cosine of the angle using the dot product formula float dotProduct = Vector2.Dot(v1.normalized, v2.normalized); float angleRadians = Mathf.Acos(dotProduct); // Result is in radians // Convert to degrees float angleDegrees = angleRadians * Mathf.Rad2Deg; return angleDegrees; } }