using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class ConstructMolding : MonoBehaviour { static float moldHeightUnscaled; static float md = 1.25f; static float ph; static float moldHeightRotated; static float ofl; static float ofr; static float x0; static float x1; static float x2; static float x3; static float x4; static float x5; static float x6; static float x7; static float dw1; static float dw2; static float dw3; static float dx1; static float dx2; static float dx3; static float w; static float h; static float px; static float py; static float pz; static float os = 1; public static void AddMolding(string cat) { if (cat == "B") { _G.SC[6] = _G.WMK[0]; _G.SC[2] = _G.WMK[1]; _G.SC[5] = _G.WMK[2]; } if (cat == "O") { _G.SC[6] = _G.WMO[0]; _G.SC[2] = _G.WMO[1]; _G.SC[5] = _G.WMO[2]; } moldHeightUnscaled = DOIT.ConvertStringToNumber(_G.SC[2]); //molding height ConvertStringToNumber moldHeightRotated = moldHeightUnscaled * Mathf.Sin(45.0f * Mathf.Deg2Rad);//molding height string ko = ""; ko = cat == "B" ? "k" : ko; ko = cat == "O" ? "o" : ko; //Debug.Log("ko value : " + ko); if (ko == "") { return; } //Debug.Log("mould height value : " + moldHeightUnscaled); if (moldHeightUnscaled == 0f) { //Debug.Log("delete mould"); deleteKO(ko); return; } if (cat == "B") { ph = -_G.HEIGHT / 2 + moldHeightUnscaled / 2; deleteKO(ko); addK(); } if (cat == "O") { moldHeightRotated = moldHeightUnscaled * Mathf.Sin(45.0f * Mathf.Deg2Rad); ph = _G.HEIGHT / 2 - moldHeightRotated / 2; deleteKO(ko); addO(); } GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("moulding"); for (var i = 0; i < gameObjects.Length; i++) { gameObjects[i].transform.SetParent(SceneModeManager.Scene); } } public static IEnumerator AddMoldingCoroutine(string cat) { if (cat == "B") { _G.SC[6] = _G.WMK[0]; _G.SC[2] = _G.WMK[1]; _G.SC[5] = _G.WMK[2]; } if (cat == "O") { _G.SC[6] = _G.WMO[0]; _G.SC[2] = _G.WMO[1]; _G.SC[5] = _G.WMO[2]; } moldHeightUnscaled = DOIT.ConvertStringToNumber(_G.SC[2]); //molding height moldHeightRotated = moldHeightUnscaled * Mathf.Sin(45.0f * Mathf.Deg2Rad);//molding height string ko = ""; ko = cat == "B" ? "k" : ko; ko = cat == "O" ? "o" : ko; Debug.Log("ko value : " + ko); if (ko == "") { yield break; } //Debug.Log("mould height value : " + moldHeightUnscaled); if (moldHeightUnscaled == 0f) { Debug.Log("delete mould"); deleteKO(ko); yield break; } if (cat == "B") { ph = -_G.HEIGHT / 2 + moldHeightUnscaled / 2; deleteKO(ko); addK(); } if (cat == "O") { moldHeightRotated = moldHeightUnscaled * Mathf.Sin(45.0f * Mathf.Deg2Rad); ph = _G.HEIGHT / 2 - moldHeightRotated / 2; deleteKO(ko); addO(); } GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("moulding"); for (var i = 0; i < gameObjects.Length; i++) { gameObjects[i].transform.SetParent(SceneModeManager.Scene); yield return null; } } static void addK() { // if (_G.ROOM == "R8") { addKon("w1"); addKon("w4"); } // if (_G.ROOM == "R7") { addKon("w1"); addKon("w2"); addKon("w4"); } // if (_G.ROOM != "R7" && _G.ROOM != "R8") // { for (int i = 1; i < _G.NW + 1; i++) { addKon("w" + i.ToString()); } //} } static void addKon(string wallName) { List doorsOnWall = dON(wallName); int n = int.Parse(wallName[1..]); if (doorsOnWall.Count == 0) AddKOnWall_0D(n.ToString(), _G. WallsWidth[n - 1], _G. WallsPointCenter[n - 1].x, 0, _G. WallsPointCenter[n - 1].y, _G. WallsAngle[n - 1]); if (doorsOnWall.Count == 1) AddKOnWall_1D(doorsOnWall, n.ToString(), _G. WallsWidth[n - 1], _G. WallsPointCenter[n - 1].x, 0, _G. WallsPointCenter[n - 1].y, _G. WallsAngle[n - 1]); if (doorsOnWall.Count == 2) AddKOnWall_2D(doorsOnWall, n.ToString(), _G. WallsWidth[n - 1], _G. WallsPointCenter[n - 1].x, 0, _G. WallsPointCenter[n - 1].y, _G. WallsAngle[n - 1]); if (doorsOnWall.Count == 3) AddKOnWall_3D(doorsOnWall, n.ToString(), _G. WallsWidth[n - 1], _G. WallsPointCenter[n - 1].x, 0, _G. WallsPointCenter[n - 1].y, _G. WallsAngle[n - 1]); Debug.Log("Dw count : " + doorsOnWall.Count); } static void AddKOnWall() { } static void AddKOnWall_0D(string wallNumber, float wallWidth, float wallX, float wallY, float wallZ, float wallAngle) { //print("wn=="+wn); GameObject Wall = GameObject.Find("w" + wallNumber); Wall.transform.localRotation = Quaternion.Euler(0, 0, 0); Wall.transform.position = new Vector3(0, 0, 0); createK("k" + wallNumber + "a", wallWidth, 0, ph, 0, 0, 0, 180); GameObject.Find("k" + wallNumber + "a").transform.SetParent(Wall.transform); Wall.transform.localRotation = Quaternion.Euler(0, wallAngle, 0); Wall.transform.position = new Vector3(wallX, wallY, wallZ); } static void AddKOnWall_1D(List D, string wn, float ww, float wx, float wy, float wz, float wa) { GameObject Wall = GameObject.Find("w" + wn).gameObject; Wall.transform.localRotation = Quaternion.Euler(0, 0, 0); Wall.transform.position = new Vector3(0, 0, 0); int nO = int.Parse(D[0][4..]);//object number float xf = Mathf.Sin(DOIT.ConvertStringToNumber(_G.OBJs[nO][19]) * Mathf.Deg2Rad); float zf = Mathf.Cos(DOIT.ConvertStringToNumber(_G.OBJs[nO][19]) * Mathf.Deg2Rad); float dw = DOIT.ConvertStringToNumber(_G.OBJs[nO][6]); float dx = (DOIT.ConvertStringToNumber(_G.OBJs[nO][15]) - wx) * zf - (DOIT.ConvertStringToNumber(_G.OBJs[nO][17]) - wz) * xf; x0 = -ww / 2; x1 = dx - dw / 2; x2 = dx + dw / 2; x3 = ww / 2; w = Mathf.Abs(x0 - x1); px = x0 + w / 2; //pz = _G.wz[2-1] - md / 2; createK("k" + wn + "a", w, px, ph, 0, 0, 0, 180); w = Mathf.Abs(x2 - x3); px = x3 - w / 2; createK("k" + wn + "b", w, px, ph, 0, 0, 0, 180); GameObject.Find("k" + wn + "a").transform.SetParent(Wall.transform); GameObject.Find("k" + wn + "b").transform.SetParent(Wall.transform); Wall.transform.localRotation = Quaternion.Euler(0, wa, 0); Wall.transform.position = new Vector3(wx, wy, wz); } static void AddKOnWall_2D(List D, string wn, float ww, float wx, float wy, float wz, float wa) { GameObject Wall = GameObject.Find("w" + wn); Wall.transform.localRotation = Quaternion.Euler(0, 0, 0); Wall.transform.position = new Vector3(0, 0, 0); int nO = int.Parse(D[0].Substring(4, D[0].Length - 4));//object number float xf = Mathf.Sin(DOIT.ConvertStringToNumber(_G.OBJs[nO][19]) * Mathf.Deg2Rad); float zf = Mathf.Cos(DOIT.ConvertStringToNumber(_G.OBJs[nO][19]) * Mathf.Deg2Rad); float dx = (DOIT.ConvertStringToNumber(_G.OBJs[nO][15]) - wx) * zf - (DOIT.ConvertStringToNumber(_G.OBJs[nO][17]) - wz) * xf; int nO1 = int.Parse(D[1].Substring(4, D[1].Length - 4));//object number float dx1 = (DOIT.ConvertStringToNumber(_G.OBJs[nO1][15]) - wx) * zf - (DOIT.ConvertStringToNumber(_G.OBJs[nO1][17]) - wz) * xf; float dw = DOIT.ConvertStringToNumber(_G.OBJs[nO][6]); float dw1 = DOIT.ConvertStringToNumber(_G.OBJs[nO1][6]); SortedDictionary FH = new SortedDictionary(); FH.Add(dx, dw); FH.Add(dx1, dw1); FH.OrderBy(i => i.Key); x0 = -ww / 2; x1 = FH.ElementAt(0).Key - FH.ElementAt(0).Value / 2; x2 = FH.ElementAt(0).Key + FH.ElementAt(0).Value / 2; x3 = FH.ElementAt(1).Key - FH.ElementAt(1).Value / 2; x4 = FH.ElementAt(1).Key + FH.ElementAt(1).Value / 2; x5 = ww / 2; w = Mathf.Abs(x0 - x1); px = x0 + w / 2; //pz = _G.wz[1-1] - md / 2; createK("k" + wn + "a", w, px, ph, 0, 0, 0, 180); w = Mathf.Abs(x2 - x3); px = x2 + w / 2; createK("k" + wn + "b", w, px, ph, 0, 0, 0, 180); w = Mathf.Abs(x4 - x5); px = x4 + w / 2; createK("k" + wn + "c", w, px, ph, 0, 0, 0, 180); GameObject.Find("k" + wn + "a").transform.SetParent(Wall.transform); GameObject.Find("k" + wn + "b").transform.SetParent(Wall.transform); GameObject.Find("k" + wn + "c").transform.SetParent(Wall.transform); Wall.transform.localRotation = Quaternion.Euler(0, wa, 0); Wall.transform.position = new Vector3(wx, wy, wz); } static void AddKOnWall_3D(List D, string wn, float ww, float wx, float wy, float wz, float wa) { GameObject Wall = GameObject.Find("w" + wn).gameObject; Wall.transform.localRotation = Quaternion.Euler(0, 0, 0); Wall.transform.position = new Vector3(0, 0, 0); int nO1 = int.Parse(D[0].Substring(4, D[0].Length - 4));//object number float xf = Mathf.Sin(DOIT.ConvertStringToNumber(_G.OBJs[nO1][19]) * Mathf.Deg2Rad); float zf = Mathf.Cos(DOIT.ConvertStringToNumber(_G.OBJs[nO1][19]) * Mathf.Deg2Rad); float dw1 = DOIT.ConvertStringToNumber(_G.OBJs[nO1][6]); float dx1 = (DOIT.ConvertStringToNumber(_G.OBJs[nO1][15]) - wx) * zf - (DOIT.ConvertStringToNumber(_G.OBJs[nO1][17]) - wz) * xf; int nO2 = int.Parse(D[1].Substring(4, D[1].Length - 4));//object number float dw2 = DOIT.ConvertStringToNumber(_G.OBJs[nO2][6]); float dx2 = (DOIT.ConvertStringToNumber(_G.OBJs[nO2][15]) - wx) * zf - (DOIT.ConvertStringToNumber(_G.OBJs[nO2][17]) - wz) * xf; int nO3 = int.Parse(D[2].Substring(4, D[2].Length - 4));//object number float dw3 = DOIT.ConvertStringToNumber(_G.OBJs[nO3][6]); float dx3 = (DOIT.ConvertStringToNumber(_G.OBJs[nO3][15]) - wx) * zf - (DOIT.ConvertStringToNumber(_G.OBJs[nO3][17]) - wz) * xf; SortedDictionary FH = new SortedDictionary(); FH.Add(dx1, dw1); FH.Add(dx2, dw2); FH.Add(dx3, dw3); FH.OrderBy(i => i.Key); x0 = -ww / 2; x1 = FH.ElementAt(0).Key - FH.ElementAt(0).Value / 2; x2 = FH.ElementAt(0).Key + FH.ElementAt(0).Value / 2; x3 = FH.ElementAt(1).Key - FH.ElementAt(1).Value / 2; x4 = FH.ElementAt(1).Key + FH.ElementAt(1).Value / 2; x5 = FH.ElementAt(2).Key - FH.ElementAt(2).Value / 2; x6 = FH.ElementAt(2).Key + FH.ElementAt(2).Value / 2; w = Mathf.Abs(x0 - x1); px = x0 + w / 2; //pz = _G.wz[1-1] - md / 2; createK("k" + wn + "a", w, px, ph, 0, 0, 0, 180); w = Mathf.Abs(x2 - x3); px = x2 + w / 2; createK("k" + wn + "b", w, px, ph, 0, 0, 0, 180); w = Mathf.Abs(x4 - x5); px = x4 + w / 2; createK("k" + wn + "c", w, px, ph, 0, 0, 0, 180); w = Mathf.Abs(x6 - x7); px = x6 + w / 2; createK("k" + wn + "d", w, px, ph, 0, 0, 0, 180); GameObject.Find("k" + wn + "a").transform.SetParent(Wall.transform); GameObject.Find("k" + wn + "b").transform.SetParent(Wall.transform); GameObject.Find("k" + wn + "c").transform.SetParent(Wall.transform); GameObject.Find("k" + wn + "d").transform.SetParent(Wall.transform); Wall.transform.localRotation = Quaternion.Euler(0, wa, 0); Wall.transform.position = new Vector3(wx, wy, wz); //GameObject.Find("k" + wn + "a").transform.SetParent(GameObject.Find("SCENE").transform); //GameObject.Find("k" + wn + "b").transform.SetParent(GameObject.Find("SCENE").transform); //GameObject.Find("k" + wn + "c").transform.SetParent(GameObject.Find("SCENE").transform); //GameObject.Find("k" + wn + "d").transform.SetParent(GameObject.Find("SCENE").transform); } static void addO() // { for (int i = 0; i < _G.NW; i++) { createO(i); } } public static void deleteKO(string ko) { //foreach (Transform O in GameObject.Find("SCENE").transform) //{ // O.gameObject.SetActive(true); //} GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("moulding"); List MK = new List(); List MO = new List(); for (var i = 0; i < gameObjects.Length; i++) { if (gameObjects[i].name.Substring(0, 1) == "k") MK.Add(gameObjects[i].transform); if (gameObjects[i].name.Substring(0, 1) == "o") MO.Add(gameObjects[i].transform); } if (ko == "k") foreach (Transform childk in MK) { GameObject.DestroyImmediate(childk.gameObject); } if (ko == "o") foreach (Transform childo in MO) { GameObject.DestroyImmediate(childo.gameObject); } } static void createK(string N, float w, float x, float y, float z, float rx, float ry, float rz) { GameObject M = GameObject.CreatePrimitive(PrimitiveType.Cube); M.GetComponent().enabled = false; M.transform.localScale = new Vector3(w, moldHeightUnscaled, md); M.transform.position = new Vector3(x, y, z - 0.5f); M.transform.localRotation = Quaternion.Euler(rx, ry, rz); M.name = N; M.GetComponent().material = Instantiate(UIT_MATERIAL.GetMaterial("#ffffff_Semi"));//new MaterialInstantiate(_G.WHITE_G );//; M.GetComponent().material.SetTexture("_Texture" ,Resources.Load("Molding/" + _G.SC[6]) as Texture2D); M.GetComponent().material.SetColor("_c2", DOIT.CSc("#000000", 255)); M.GetComponent().material.SetColor("_c3", DOIT.CSc("#000000", 255)); M.GetComponent().material.SetFloat("_tile",1); M.GetComponent().material.SetFloat("_contrast",1.3f); M.GetComponent().probeAnchor = GameObject.Find("Reflection Probe").transform; M.tag = "moulding"; } static void createO(int index) { // find offset length for mold edges, then create object // 1. get mold positions Vector3 currentMoldPosition = GetOgeePosition(index); int previousIndex = (int)Mathf.Repeat(index - 1, _G.NW); Vector3 previousMoldPosition = GetOgeePosition(previousIndex); int nextIndex = (index + 1) % _G.NW; Vector3 nextMoldPosition = GetOgeePosition(nextIndex); // 2. get mold vectors off of wall Vector2 currentMold = (new Vector2(currentMoldPosition.x, currentMoldPosition.z) - _G. WallsPointCenter[index]) * 2.0f; Vector2 previousMold = (new Vector2(previousMoldPosition.x, previousMoldPosition.z) - _G. WallsPointCenter[previousIndex]) * 2.0f; Vector2 nextMold = (new Vector2(nextMoldPosition.x, nextMoldPosition.z) - _G. WallsPointCenter[nextIndex]) * 2.0f; // 3. get middle vectors for offset directions Vector2 previousOffset = Vector2.Lerp(currentMold, previousMold, 0.5f); Vector2 nextOffset = Vector2.Lerp(currentMold, nextMold, 0.5f); // 4. get angles of offsets towards wall to use with trigonometry formluas Vector2 halfWall = _G. WallsPointCenter[index] - _G. WallsPointStart[index]; float previousFrontHalfAngle = Vector2.Angle(previousOffset, halfWall); float nextFrontHalfAngle = Vector2.Angle(nextOffset, -halfWall); // 5. get the adjacents for offset lengths ( trigonometry : adjacent = opposite / tan(angle) ) float previousOffsetLength = moldHeightRotated / Mathf.Tan(previousFrontHalfAngle * Mathf.Deg2Rad); float nextOffsetLength = moldHeightRotated / Mathf.Tan(nextFrontHalfAngle * Mathf.Deg2Rad); // 6. setup mesh point values float wallWidth = _G. WallsWidth[index]; float previousFrontUpX = -wallWidth / 2.0f + previousOffsetLength; float previousFrontUpY = moldHeightUnscaled / 2.0f; float nextFrontUpX = wallWidth / 2.0f - nextOffsetLength; float nextFrontUpY = moldHeightUnscaled / 2.0f; float nextBackDownX = wallWidth / 2.0f; float nextBackDownY = -moldHeightUnscaled / 2.0f; float previousBackDownX = -wallWidth / 2.0f; float previousBackDownY = -moldHeightUnscaled / 2.0f; // 7. create object GameObject M = new($"o{index + 1}"); M.AddComponent(typeof(MeshFilter)); M.AddComponent(typeof(MeshRenderer)); M.GetComponent().mesh = CreateMesh(previousFrontUpX, previousFrontUpY, nextFrontUpX, nextFrontUpY, nextBackDownX, nextBackDownY, previousBackDownX, previousBackDownY); M.transform.position = currentMoldPosition; Vector3 eulerRotation = new(-45.0f, _G. WallsAngle[index], 0.0f); M.transform.localRotation = Quaternion.Euler(eulerRotation); M.GetComponent().material = Instantiate(UIT_MATERIAL.GetMaterial("#ffffff_Semi"));//new MaterialInstantiate(_G.WHITE_G );//; M.GetComponent().material.SetTexture("_Texture" ,Resources.Load("Molding/" + _G.SC[6]) as Texture2D); M.GetComponent().material.SetColor("_c2", DOIT.CSc("#000000", 255)); M.GetComponent().material.SetFloat("_tile",1); M.GetComponent().material.SetFloat("_contrast",1.3f); M.GetComponent().probeAnchor = GameObject.Find("Reflection Probe").transform; // M.GetComponent().material = Instantiate(UIT_MATERIAL.GetMaterial("ffffff_Glossy"));// new Material(Shader.Find("Standard")); // //M.GetComponent().material.mainTexture = Resources.Load("Molding/" + _G.SC[6]) as Texture2D; // M.GetComponent().material.SetTexture("_Texture" ,Resources.Load("Molding/" + _G.SC[6]) as Texture2D); // M.GetComponent().material.SetColor("_c2", DOIT.CSc("#000000", 255)); // //M.GetComponent().material.color = DOIT.CSc(_G.SC[5], 255); // M.GetComponent().material.mainTextureScale = new Vector2(wallWidth / 12.0f, 1.0f); M.tag = "moulding"; } private static Vector3 GetOgeePosition(int index) { float x = _G. WallsPointCenter[index].x - moldHeightRotated / 2.0f * Mathf.Sin(_G. WallsAngle[index] * Mathf.Deg2Rad); float y = ph; float z = _G. WallsPointCenter[index].y - moldHeightRotated / 2.0f * Mathf.Cos(_G. WallsAngle[index] * Mathf.Deg2Rad); return new Vector3(x, y, z); } public static Mesh CreateMesh(float P1x, float P1y, float P2x, float P2y, float P3x, float P3y, float P4x, float P4y) { Mesh m = new Mesh(); m.name = "ScriptedMesh"; m.vertices = new Vector3[] { new Vector3(P1x, P1y, 0.01f), new Vector3(P2x, P2y, 0.01f), new Vector3(P3x, P3y, 0.01f), new Vector3(P4x, P4y, 0.01f) }; m.uv = new Vector2[] { new Vector2 (0, 1), new Vector2 (1, 1), new Vector2(1, 0), new Vector2 (0, 0) }; m.triangles = new int[] { 0, 1, 2, 0, 2, 3 }; m.RecalculateNormals(); return m; } static List dON(string w) { int nD = 0;//Doors on wall List LnD = new List(); for (int i = 0; i < _G.OBJnum; i++) { if (_G.OBJs[i] != null) { if (_G.OBJs[i][0][..4] == "door" && _G.OBJs[i][22] == w) { nD += 1; LnD.Add(_G.OBJs[i][0]); } if (_G.OBJs[i][0][..4] == "pati" && _G.OBJs[i][22] == w) { nD += 1; LnD.Add(_G.OBJs[i][0]); } if (_G.OBJs[i][0][..4] == "open" && _G.OBJs[i][22] == w) { nD += 1; LnD.Add(_G.OBJs[i][0]); } } } return LnD; } }