Пример #1
0
    // initialize
    unsafe void Start()
    {
        // For Furniture Assembly Environment: record geom positions
        modifiedObjects = new Dictionary <string, Vector3>();
        backgrounds     = new List <GameObject>();
        foreach (GameObject child in SceneManager.GetActiveScene().GetRootGameObjects())
        {
            if (child.name.StartsWith("Background_"))
            {
                backgrounds.Add(child);
                child.SetActive(false);
            }
        }

        //material_random_parameters["Plastic (Instance)"] = new RandomColor(0.0f, 1.0f, 0.0f, 1.0f, 0.2f, 1.0f);

        // set selection color
        selcolor = new Color(0.5f, 0.5f, 0.5f, 1);

        // preallocate buffer with maximum possible message size
        buffersize = 2048; // Math.Max(4, Math.Max(4*nqpos, 28*nmocap));
        buffer     = new byte[buffersize];

        // initialize plugin
        //   MJP.Initialize();
        //  MJP.LoadModel(Application.streamingAssetsPath + "/" + modelFile);

        // get number of renderable objects, allocate map
        MJP.TSize size;
        MJP.GetSize(&size);
        nqpos   = size.nqpos;
        nmocap  = size.nmocap;
        ncamera = size.ncamera;
        nobject = size.nobject;
        objects = new GameObject[nobject];

        // get root
        //root = GameObject.Find("MuJoCo");
        if (root == null)
        {
            throw new System.Exception("MuJoCo root object not found");
        }

        root.transform.localPosition = transform.localPosition;
        root.transform.localRotation = transform.localRotation;
        root.transform.localScale    = transform.localScale;

        // get camera under root
        int nchild = root.transform.childCount;

        for (int i = 0; i < nchild; i++)
        {
            thecamera = root.transform.GetChild(i).gameObject.GetComponent <Camera>();
            if (thecamera != null)
            {
                // thecamera.enabled = false;
                break;
            }
        }
        if (thecamera == null)
        {
            throw new System.Exception("No camera found under MuJoCo root object");
        }

        // make map of renderable objects
        for (int i = 0; i < nobject; i++)
        {
            // get object name
            StringBuilder name = new StringBuilder(100);
            MJP.GetObjectName(i, name, 100);

            // find corresponding GameObject
            for (int j = 0; j < nchild; j++)
            {
                if (root.transform.GetChild(j).name == name.ToString())
                {
                    objects[i] = root.transform.GetChild(j).gameObject;
                    break;
                }
            }

            // set initial state
            if (objects[i])
            {
                MJP.TTransform transform;
                int            visible;
                int            selected;
                MJP.GetObjectState(i, &transform, &visible, &selected);
                SetTransform(objects[i], transform);
                objects[i].SetActive(visible > 0);
            }
        }

        // get camera fov and offscreen resolution
        camfov = new float[ncamera + 1];
        int offwidth  = 1280;
        int offheight = 720;

        for (int i = -1; i < ncamera; i++)
        {
            MJP.TCamera cam;
            MJP.GetCamera(i, &cam);
            camfov[i + 1] = cam.fov;

            // plugin returns offscreen width and height for all cameras
            offwidth  = cam.width;
            offheight = cam.height;
        }

        //TODO: The dummy camera and camera added by mjonline import should be merged together
        GameObject camobj = GameObject.Find("DummyCamera");

        if (camobj != null)
        {
            dummycamera         = camobj.GetComponent <Camera>();
            dummycamera.enabled = true;
        }

        // prepare offscreen rendering
        off_render = new OffscreenRenderer(offwidth, offheight);

        // synchronize time
        MJP.SetTime(Time.time);

        //randomizeAppearance();

        Debug.Log("New simulation init'd " + offwidth + "x" + offheight);
    }
Пример #2
0
    // 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();
    }
Пример #3
0
    // 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();
    }
Пример #4
0
    // initialize
    unsafe void Start()
    {
        // set selection color
        selcolor = new Color(0.5f, 0.5f, 0.5f, 1);

        // initialize plugin
        MJP.Initialize();
        MJP.LoadModel(Application.streamingAssetsPath + "/" + modelFile);

        // get number of renderable objects, allocate map
        MJP.TSize size;
        MJP.GetSize(&size);
        nqpos   = size.nqpos;
        nmocap  = size.nmocap;
        ncamera = size.ncamera;
        nobject = size.nobject;
        objects = new GameObject[nobject];

        // get root
        root = GameObject.Find("MuJoCo");
        if (root == null)
        {
            throw new System.Exception("MuJoCo root object not found");
        }

        // get camera under root
        int nchild = root.transform.childCount;

        for (int i = 0; i < nchild; i++)
        {
            thecamera = root.transform.GetChild(i).gameObject.GetComponent <Camera>();
            if (thecamera != null)
            {
                break;
            }
        }
        if (thecamera == null)
        {
            throw new System.Exception("No camera found under MuJoCo root object");
        }

        // make map of renderable objects
        for (int i = 0; i < nobject; i++)
        {
            // get object name
            StringBuilder name = new StringBuilder(100);
            MJP.GetObjectName(i, name, 100);

            // find corresponding GameObject
            for (int j = 0; j < nchild; j++)
            {
                if (root.transform.GetChild(j).name == name.ToString())
                {
                    objects[i] = root.transform.GetChild(j).gameObject;
                    break;
                }
            }

            // set initial state
            if (objects[i])
            {
                MJP.TTransform transform;
                int            visible;
                int            selected;
                MJP.GetObjectState(i, &transform, &visible, &selected);
                SetTransform(objects[i], transform);
                objects[i].SetActive(visible > 0);
            }
        }

        // get camera fov and offscreen resolution
        camfov = new float[ncamera + 1];
        for (int i = -1; i < ncamera; i++)
        {
            MJP.TCamera cam;
            MJP.GetCamera(i, &cam);
            camfov[i + 1] = cam.fov;

            // plugin returns offscreen width and height for all cameras
            offwidth  = cam.width;
            offheight = cam.height;
        }

        // prepare offscreen rendering
        offtex       = new Texture2D(offwidth, offheight, TextureFormat.RGB24, false);
        offrt        = new RenderTexture(offwidth, offheight, 24);
        offrt.width  = offwidth;
        offrt.height = offheight;
        offrt.Create();

        // synchronize time
        MJP.SetTime(Time.time);

        // preallocate buffer with maximum possible message size
        buffersize = Math.Max(4, Math.Max(4 * nqpos, 28 * nmocap));
        buffer     = new byte[buffersize];

        // start listening for connections
        listener = new TcpListener(System.Net.IPAddress.Parse(tcpAddress), tcpPort);
        listener.Start();
    }
Пример #5
0
    // initialize
    unsafe void Start()
    {
        // set selection color
        selcolor = new Color(0.5f, 0.5f, 0.5f, 1);

        // initialize plugin
        ////  MJP.Initialize();
        //  MJP.LoadModel(Application.streamingAssetsPath + "/" + modelFile);

        // get number of renderable objects, allocate map
        MJP.TSize size;
        MJP.GetSize(&size);
        ncamera = size.ncamera;
        nobject = size.nobject;
        objects = new GameObject[nobject];

        // get root
        // root = GameObject.Find("MuJoCo");
        if (root == null)
        {
            throw new System.Exception("MuJoCo root object not found");
        }

        // get camera under root
        int nchild = root.transform.childCount;

        for (int i = 0; i < nchild; i++)
        {
            thecamera = root.transform.GetChild(i).gameObject.GetComponent <Camera>();
            if (thecamera != null)
            {
                break;
            }
        }
        if (thecamera == null)
        {
            throw new System.Exception("No camera found under MuJoCo root object");
        }

        // make map of renderable objects
        for (int i = 0; i < nobject; i++)
        {
            // get object name
            StringBuilder name = new StringBuilder(100);
            MJP.GetObjectName(i, name, 100);

            // find corresponding GameObject
            for (int j = 0; j < nchild; j++)
            {
                if (root.transform.GetChild(j).name == name.ToString())
                {
                    objects[i] = root.transform.GetChild(j).gameObject;
                    break;
                }
            }

            // set initial state
            if (objects[i])
            {
                MJP.TTransform transform;
                int            visible;
                int            selected;
                MJP.GetObjectState(i, &transform, &visible, &selected);
                SetTransform(objects[i], transform);
                objects[i].SetActive(visible > 0);
            }
        }

        int offwidth  = 1280;
        int offheight = 720;

        // get camera fov and offscreen resolution
        camfov = new float[ncamera + 1];
        for (int i = -1; i < ncamera; i++)
        {
            MJP.TCamera cam;
            MJP.GetCamera(i, &cam);
            camfov[i + 1] = cam.fov;

            // plugin returns offscreen width and height for all cameras
            offwidth  = cam.width;
            offheight = cam.height;
        }

        off_render = new OffscreenRenderer(offwidth, offheight);

        // synchronize time
        MJP.SetTime(Time.time);
        videotime = Time.time;
    }