// Start is called before the first frame update void Start() { this.transform.position = new Vector3(-6.0f, 1.0f, 2.0f); coordinate = new IntVector2(1, 0); Target.position = new Vector3(-18.0f, 0.0f, 18.0f); maze_size = 5; //Height, Width => (z, x) //"NSEW" Maze = new string[5, 5] { { "1101", "1000", "1010", "1101", "1010" }, { "1011", "0011", "0011", "1001", "0010" }, { "0101", "0110", "0011", "0011", "0011" }, { "1001", "1010", "0011", "0011", "0011" }, { "0111", "0101", "0100", "0110", "0111" } }; // Debug.Log(GetCellState(coordinate).ToString()); mjr = mujoco.GetComponent <MJRemote>(); }
// run importer private unsafe void RunImport() { Resources.UnloadUnusedAssets(); BuildAssetDatabase(modelFile); // adjust global settings Time.fixedDeltaTime = 0.005f; //PlayerSettings.runInBackground = true; if (enableRemote) { QualitySettings.vSyncCount = (noVSync ? 0 : 1); } else { QualitySettings.vSyncCount = 1; } // disable active cameras /* Camera[] activecam = FindObjectsOfType<Camera>(); * foreach( Camera ac in activecam ) * ac.gameObject.SetActive(false); */ fileName = Path.GetFileName(modelFile); // initialize plugin and load model MJP.Close(); MJP.Initialize(); MJP.LoadModel(modelFile); // get model sizes MJP.TSize size; MJP.GetSize(&size); // import materials //if( size.nmaterial>0 ) { // MakeDirectory("Assets", "Materials"); ImportMaterials(size.nmaterial); } // create root, destroy old if present root = GameObject.Find("MuJoCo"); if (root != null) { Destroy(root); } root = new GameObject("MuJoCo"); if (root == null) { throw new System.Exception("Could not create root MuJoCo object"); } root.transform.localPosition = transform.localPosition; root.transform.localRotation = transform.localRotation; root.transform.localScale = transform.localScale; // add camera to root AddCamera(); // import renderable objects under root ImportObjects(size.nobject); // ImportLights(size.nlight); // attach script to root if (enableRemote) { // add remote MJRemote extsim = root.GetComponent <MJRemote>(); if (extsim == null) { extsim = root.AddComponent <MJRemote>(); } extsim.root = root; extsim.modelFile = fileName + ".mjb"; MJTCPInterface tcpif = this.gameObject.GetComponent <MJTCPInterface>(); if (tcpif == null) { tcpif = this.gameObject.AddComponent <MJTCPInterface>(); } tcpif.root = root; tcpif.tcpAddress = tcpAddress; tcpif.tcpPort = tcpPort; // destroy simulate if present if (root.GetComponent <MJInternalSimulation>()) { Destroy(root.GetComponent <MJInternalSimulation>()); } } else { // add simulate MJInternalSimulation sim = root.GetComponent <MJInternalSimulation>(); if (sim == null) { sim = root.AddComponent <MJInternalSimulation>(); } sim.root = root; sim.modelFile = fileName + ".mjb"; // destroy remote if present if (root.GetComponent <MJRemote>()) { Destroy(root.GetComponent <MJRemote>()); } // destroy remote if present if (this.GetComponent <MJTCPInterface>()) { Destroy(root.GetComponent <MJTCPInterface>()); } } // close plugin // MJP.Close(); }
// Update is called once per frame void Update() { MJRemote ext = root.GetComponent <MJRemote>(); if (ext == null) { return; } // check conection each 0.1 sec if (lastcheck + 0.1f < Time.time) { // broken connection: clear if (!CheckConnection()) { client = null; stream = null; } lastcheck = Time.time; } // not connected: accept connection if pending if (client == null || !client.Connected) { if (listener != null && listener.Pending()) { // make connection client = listener.AcceptTcpClient(); stream = client.GetStream(); ext.writeSettings(stream); } } // data available: handle communication while (client != null && client.Connected && stream != null && stream.DataAvailable) { // get command ReadAll(stream, 4); int cmd = BitConverter.ToInt32(buffer, 0); // process command switch ((Command)cmd) { // GetInput: send lastkey, select, active, refpos[3], refquat[4] case Command.GetInput: ext.writeInput(stream); break; // GetImage: send 3*width*height bytes case Command.GetImage: ext.writeColorImage(stream); break; case Command.GetSegmentationImage: ext.writeSegmentationImage(stream); break; // SaveSnapshot: no data exchange case Command.SaveSnapshot: ext.writeSnapshot(); break; // SaveVideoframe: no data exchange case Command.SaveVideoframe: ext.writeVideo(); break; // SetCamera: receive camera index case Command.SetCamera: ext.setCamera(stream); break; // SetQpos: receive qpos vector case Command.SetQpos: ext.setQpos(stream); break; // SetMocap: receive mocap_pos and mocap_quat vectors case Command.SetMocap: ext.setMocap(stream); break; case Command.ChangeWorld: ReadAll(stream, 4); int strlen = BitConverter.ToInt32(buffer, 0); ReadAll(stream, strlen); string path = System.Text.Encoding.UTF8.GetString(buffer, 0, strlen); Debug.Log("Change World"); GameObject.Find("Importer").GetComponent <MJImport>().Import(path); //Kills MJRemote return; //don't processess more commands until next frame case Command.GetWorldInfo: Debug.Log("Write Info"); ext.writeSettings(stream); break; case Command.RandomizeAppearance: ext.randomizeAppearance(); break; case Command.GetDepthImage: ext.writeDepthImage(stream); break; } } }
// run importer private unsafe void RunImport() { // adjust global settings Time.fixedDeltaTime = 0.005f; PlayerSettings.runInBackground = true; if (enableRemote) { QualitySettings.vSyncCount = (noVSync ? 0 : 1); } else { QualitySettings.vSyncCount = 1; } // disable active cameras Camera[] activecam = FindObjectsOfType <Camera>(); foreach (Camera ac in activecam) { ac.gameObject.SetActive(false); } // get filename only (not path or extension) int i1 = modelFile.LastIndexOf('/'); int i2 = modelFile.LastIndexOf('.'); if (i1 >= 0 && i2 > i1) { fileName = modelFile.Substring(i1 + 1, i2 - i1 - 1); } else { throw new System.Exception("Unexpected model file format"); } // initialize plugin and load model MJP.Initialize(); MJP.LoadModel(modelFile); // get model sizes MJP.TSize size; MJP.GetSize(&size); // save binary model MakeDirectory("Assets", "StreamingAssets"); MJP.SaveMJB("Assets/StreamingAssets/" + fileName + ".mjb"); // import textures if (size.ntexture > 0 && importTexture) { MakeDirectory("Assets", "Textures"); ImportTextures(size.ntexture); } // import materials if (size.nmaterial > 0) { MakeDirectory("Assets", "Materials"); ImportMaterials(size.nmaterial); } // create root, destroy old if present root = GameObject.Find("MuJoCo"); if (root != null) { DestroyImmediate(root); } root = new GameObject("MuJoCo"); if (root == null) { throw new System.Exception("Could not create root MuJoCo object"); } // add camera to root AddCamera(); // import renderable objects under root ImportObjects(size.nobject); // attach script to root if (enableRemote) { // add remote MJRemote rem = root.GetComponent <MJRemote>(); if (rem == null) { rem = root.AddComponent <MJRemote>(); } rem.modelFile = fileName + ".mjb"; rem.tcpAddress = tcpAddress; rem.tcpPort = tcpPort; // destroy simulate if present if (root.GetComponent <MJSimulate>()) { DestroyImmediate(root.GetComponent <MJSimulate>()); } } else { // add simulate MJSimulate sim = root.GetComponent <MJSimulate>(); if (sim == null) { sim = root.AddComponent <MJSimulate>(); } sim.modelFile = fileName + ".mjb"; // destroy remote if present if (root.GetComponent <MJRemote>()) { DestroyImmediate(root.GetComponent <MJRemote>()); } } // close plugin MJP.Close(); }