using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using UnityEditor; // using Newtonsoft.Json; using UnityEngine; using UnityEngine.UI; using Debug = UnityEngine.Debug; public class RoomController : MonoBehaviour { public Action OnColorChange; public GameObject rotateController; private List lights = new(); public Texture grout; //Заглушка public Color groutColor = new Color(0.65f, 0.65f, 0.6f); //Заглушка private string modelNameS = "bed2"; //for test // public float inten = 0.8f; public Light[] lightList; // UnityEngine.LightMapData[] lightMap; UnityEngine.LightmapData[] lightMap; public List testModels; public WallCutter wallCutter; // Light lightMap; //ONLY FOR TEST public GameObject panelSetting; public InputField height; public InputField width; public InputField groutH; public InputField angle; public GameObject panelTextures; public GameObject panelSpawn; public Material furniture1; public Material furniture2; public Material shelf1; public Material bedside1; public GameObject selecteWall; private void Awake() { #if UNITY_WEBGL == true && UNITY_EDITOR == false WebGLInput.captureAllKeyboardInput = false; #endif } void Start() { lightMap = LightmapSettings.lightmaps; height.text = "25"; width.text = "25"; groutH.text = "1"; angle.text = "0"; } public void btnLoadModel() { // StartCoroutine(loadModel(modelName)); // SpawnModel(modelNameS); // CreateTile("simple,cabinet,cabinet,5,5,0.5,90"); } public void BtnTile() { panelSpawn.SetActive(false); panelTextures.SetActive(false); panelSetting.SetActive(true); } public void BtnTextures() { panelSpawn.SetActive(false); panelSetting.SetActive(false); panelTextures.SetActive(true); } public void BntSpawn() { SpawnModel(modelNameS); } public void BntSSpawn(string m) { SpawnModel(m); } public void CreateTile(string param) { JsonTile tile = JsonUtility.FromJson(param); Color color = new Color(tile.groutColor[0]/255, tile.groutColor[1]/255, tile.groutColor[2]/255,1); Debug.Log(color); StartCoroutine(createTile(tile.pattern, tile.tileWidth, tile.tileHeight, tile.groutWidth, tile.angle, tile.firstName,color,tile.secondName)); } public void SpawnModel(string modelName) { LoadModel(modelName); } public void ChangeTexture(string name) { StartCoroutine(LoadTexture(name)); } public void UploadTexture(string param) { Texture2D texture = ConvertBase64ToTexture(param); Renderer renderer = BuildingRoom.Instance.GetSelectRenderer(); changeMaterial(renderer, texture, Color.white); } public Texture2D ConvertBase64ToTexture(string base64) { byte[] imageData = System.Convert.FromBase64String(base64); Texture2D texture = new Texture2D(2, 2); if (texture.LoadImage(imageData)) { return texture; } return null; } public void EnableDivideMode() { BuildingRoom.Instance.EnableDivideModeAction(); } public void addLight(Light light) { lights.Add(light); } public void turnLight(float range) { foreach (Light l in lights) { l.range = range; } } public void setIntensity(float value) { foreach (Light l in lightList) { l.intensity = value; } } public void changeLight(string json) { JsonObject obj = JsonUtility.FromJson(json); int temp = obj.temp; int pow = obj.pow; Color color = new Color(1, 1, 1); if (temp == 2700) color = new Color(1, 0.85f, 0.8f); if (temp == 3000) color = new Color(1, 0.9f, 0.9f); if (temp == 3200) color = new Color(1, 1, 1); if (temp == 4000) color = new Color(0.82f, 0.84f, 1); if (temp == 5000) color = new Color(0.75f, 0.8f, 1); GameObject selectedObj = BuildingRoom.Instance.GetSelectedObj(); Light lightComp = selectedObj.GetComponentInChildren(); lightComp.color = color; lightComp.intensity = 0.2f + (float)pow / 100; } public IEnumerator createTile(string pattern, float tileWidth, float tileHeight, float groutWidth, float angle, string firstname,Color groutColor,string secondName = null) { Texture2D tileTexture = null; if (firstname.Length > 500) { tileTexture = ConvertBase64ToTexture(firstname); } else { yield return StartCoroutine(Loader.Instance.LoadTexture(firstname)); tileTexture = Loader.Instance.GetTexture(); } Texture2D secondTexture = null; if (secondName != null && secondName != "") { if (secondName.Length > 500) { secondTexture = ConvertBase64ToTexture(secondName); } else { yield return StartCoroutine(Loader.Instance.LoadTexture(secondName)); secondTexture = Loader.Instance.GetTexture(); } } GameObject wall = BuildingRoom.Instance.GetSelectedObj(); if (pattern == "simple") TileController.Instance.SetStandartPatternMaterial(wall, tileTexture,null ,grout, groutColor, tileWidth, tileHeight, groutWidth, angle, 3); if (pattern == "oneRot") TileController.Instance.SetOnTileRotatePattern(wall, tileTexture, null,grout,groutColor, tileWidth, tileHeight, groutWidth, angle,3); if (pattern == "twoRot") TileController.Instance.SetTwoTileRotatePattern(wall, tileTexture, null,secondTexture, null,grout, groutColor, tileWidth, tileHeight, groutWidth, angle, 1); if (pattern == "brick") TileController.Instance.SetBrickWallPattern(wall, tileTexture, null,grout, groutColor, tileWidth, tileHeight, groutWidth,angle,1); if (pattern == "block") TileController.Instance.SetBlockPattern(wall, tileTexture, null,grout, groutColor, tileWidth, tileHeight, groutWidth,angle,1); } private IEnumerator LoadTexture(string name) { Stopwatch stop = new Stopwatch(); stop.Start(); Texture2D texture = Config.Instance.GetTexture(name); stop.Stop(); Debug.Log($"Время получение закешированой текстуры: {stop.ElapsedMilliseconds}"); if (texture == null) { Debug.Log("Downloading"); yield return StartCoroutine(Loader.Instance.LoadTexture(name)); texture = Loader.Instance.GetTexture(); } texture.name = name; Renderer renderer = BuildingRoom.Instance.GetSelectRenderer(); changeMaterial(renderer, texture, Color.white); } private void changeMaterial(Renderer selectedObj, Texture texture, Color color) { UpdateMaterial(selectedObj, texture, color); } private void UpdateMaterial(Renderer renderer, Texture texture, Color color) { Material prevMaterial = renderer.material; Material newMaterial = new Material(prevMaterial); newMaterial.shader = Shader.Find("Universal Render Pipeline/Lit"); newMaterial.mainTexture = texture; newMaterial.color = color; newMaterial.SetTexture("_BumpMap", null); renderer.material = newMaterial; GameObject selectedObj = BuildingRoom.Instance.GetSelectedObj(); SaveChangeModel.Instance.SaveChangeMaterial(selectedObj, renderer, newMaterial, prevMaterial); } public void DeleteTexture() { Renderer renderer = BuildingRoom.Instance.GetSelectRenderer(); if (renderer == null) return; renderer.material.shader = Shader.Find("Universal Render Pipeline/Lit"); renderer.material.mainTexture = null; renderer.material.color = Color.white; } public void ChangeColor(string param) { string[] col = param.Split(','); Color colorTest = new Color32(byte.Parse(col[0]),byte.Parse(col[1]), byte.Parse(col[2]),byte.Parse(col[3])); Renderer render = BuildingRoom.Instance.GetSelectRenderer(); Color oldColor = render.material.color; render.material.color = colorTest; if (!render.CompareTag("Ground") || !render.CompareTag("Wall")) { if (BuildingRoom.Instance.IsSoloMesh) { render.gameObject.GetComponent().defaultMat = colorTest; } else { render.gameObject.GetComponent().defMaterial = colorTest; } } SaveChangeModel.Instance.SaveChangeColor(render.gameObject, render, colorTest, oldColor); } public void LoadModel(string modelName) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); modelNameS = modelName; ModelData modelData = Config.Instance.GetModel(modelName); stopwatch.Stop(); Debug.Log($"Загрузка закешированого обекта : {stopwatch.ElapsedMilliseconds}"); //if (modelData == null) // { Loader.Instance.LoadModel(modelName); //} // else // { // SpawnSetting(modelData.GameObject); // } } public void SpawnSetting(GameObject spawObj) { BuildingRoom.Instance.DeactiveParents(); GameObject currentRoom = BuildingRoom.Instance.CurrentRoon; GameObject spawner = new("Spawner"); spawner.transform.SetParent(currentRoom.transform); spawner.AddComponent(); spawner.tag = "SpawnObject"; spawObj.transform.SetParent(spawner.transform); spawObj.name = "SpawnObject"; spawObj.tag = "SpawnObject"; if (name == "light") { spawner.transform.SetPositionAndRotation(new Vector3(0, 2.74f, 0), Quaternion.Euler(180, 0, 0)); spawObj.transform.position = new Vector3(0, 2.74f, 0); addLight(spawObj.GetComponentInChildren()); } if (name == "lamp") { addLight(spawObj.GetComponentInChildren()); } spawObj.transform.position = new Vector3(0, 0f, 0); spawObj.name = modelNameS; Debug.Log($"NAME: {spawObj.name}"); if (spawObj.name == "Bedside") { spawObj.GetComponentInChildren().material = bedside1; } else if (spawObj.name == "Furniture") { var renderers = spawObj.GetComponentsInChildren(); renderers[0].material = furniture1; renderers[1].material = furniture2; } else if (spawObj.name == "Shelf") { spawObj.GetComponentInChildren().material = shelf1; } GameObject rotate = Instantiate(rotateController, spawner.transform); ModelCreateComponents.Instance.CreateComponents(spawObj); rotate.GetComponent().RotateSettings(spawObj); BuildingRoom.Instance.SetParent(spawObj.GetComponent()); BuildingRoom.Instance.SelectObject(spawObj); ApplyUniversalRenderPipelineLitShader(spawObj); SaveChangeModel.Instance.SaveChangeSpawn(spawner, true); } private void ApplyUniversalRenderPipelineLitShader(GameObject loadObject) { Renderer[] renderers = loadObject.GetComponentsInChildren(); foreach (Renderer renderer in renderers) { foreach (Material material in renderer.materials) { material.shader = Shader.Find(material.shader.name); } } } private void Update() { // foreach (Light l in lightList) // { // l.intensity = inten; // } if (Input.GetKey(KeyCode.L)) { LightmapSettings.lightmaps = null; } if (Input.GetKey(KeyCode.K)) { LightmapSettings.lightmaps = lightMap; } if (Input.GetKeyDown(KeyCode.D)) { EnableDivideMode(); } } public void DeactiveObject() { BuildingRoom.Instance.DeactiveObjects(); } public void DivideWall() { // wallCutter.CanDivide = true; wallCutter.CutMesh(); } } public class JsonObject { public int temp; public int pow; } public class JsonTexture { public string value; public string[] meshNames; } public class JsonTile { public string pattern; public string firstName; public string secondName; public float tileWidth; public float tileHeight; public float groutWidth; public float angle; public float[] groutColor; }