protected virtual void AddRandomSimulationObject()
    {
        int templateIndex = 0;


        if (controllerState.noObjects - noObjects < controllerState.initialBigObjects)
        {
            templateIndex = Random.Range(0, numberOfDistinctObjectUsed / 2);
            templateIndex = templateIndex * 2 + 1;
        }
        else
        {
            templateIndex = Random.Range(0, numberOfDistinctObjectUsed);
        }

        /*
         * if (controllerState.noObjects - noObjects < controllerState.initialBigObjects)
         * {
         *  templateIndex = 3;
         * }
         * else
         * {
         *  templateIndex = Random.Range(2, numberOfDistinctObjectUsed);
         * }
         */

        GameObject refObject = gameObjectTemplates[templateIndex];
        GameObject obj       = Object.Instantiate(refObject);

        obj.SetActive(true);

        float x_pos = Random.Range(controllerState.throwMinX, controllerState.throwMaxX);
        float y_pos = Random.Range(controllerState.throwMinY, controllerState.throwMaxY);
        float z_pos = Random.Range(controllerState.throwMinZ, controllerState.throwMaxZ);

        obj.GetComponent <Rigidbody>().position = new Vector3(x_pos, y_pos, z_pos);

        SimulationObjectState objState = new SimulationObjectState();

        objState.material = (templateIndex < 2) ? 0 : 1;
        objState.size     = (templateIndex % 2 == 0) ? 0 : 1;

        int             colorIndex = Random.Range(0, numberOfDistinctColorsUsed);
        SimulationColor col        = new SimulationColor((SimulationColor.TYPE)colorIndex);
        Material        newMat     = Instantiate(refObject.GetComponent <Renderer>().material);

        newMat.SetColor("_BaseColor", col.GetColor((SimulationMaterial.TYPE)objState.material));
        obj.GetComponent <Renderer>().material = newMat;


        objState.SetGameObject(obj);
        objState.color         = colorIndex;
        objState.templateIndex = templateIndex;

        prevObjectState   = objState;
        prevCreatedObject = obj;
    }
    static public string toJSON(List <SimulationObjectState> objStates)
    {
        if (objStates != null)
        {
            SimulationSceneState sceneState = new SimulationSceneState();
            foreach (SimulationObjectState obj in objStates)
            {
                SimulationColor    col = new SimulationColor((SimulationColor.TYPE)obj.color);
                SimulationMaterial mat = new SimulationMaterial((SimulationMaterial.TYPE)obj.material);
                sceneState.objectStates.Add(SimulationObjectState.createJSONFromObject(obj.GetGameObject(), mat, col, obj.size, obj.templateIndex));
            }

            return(JsonUtility.ToJson(sceneState));
        }

        return("");
    }
    static public List <SimulationObjectState> fromJSON(string sceneStateStr, GameObject[] gameObjectTemplates, int removedObjectIndex)
    {
        List <SimulationObjectState> ret        = new List <SimulationObjectState>();
        SimulationSceneState         sceneState = JsonUtility.FromJson <SimulationSceneState>(sceneStateStr);

        if (sceneState != null)
        {
            for (int i = 0; i < sceneState.objectStates.Count; i++)
            {
                bool active = i != removedObjectIndex;

                SimulationObjectState objState = SimulationObjectState.createObjectFromJSON(sceneState.objectStates[i], gameObjectTemplates, active);
                if (objState != null)
                {
                    ret.Add(objState);
                }
            }
        }
        return(ret);
    }
Exemplo n.º 4
0
    static public string createJSONFromObject(GameObject obj, SimulationMaterial mat, SimulationColor col, int size, int templateIndex)
    {
        if (obj != null)
        {
            SimulationObjectState state = new SimulationObjectState();
            state.position              = obj.transform.position;
            state.rotation              = obj.transform.rotation;
            state.velocity              = obj.GetComponent <Rigidbody>().velocity;
            state.inertiaTensor         = obj.GetComponent <Rigidbody>().inertiaTensor;
            state.inertiaTensorRotation = obj.GetComponent <Rigidbody>().inertiaTensorRotation;
            state.angularVelocity       = obj.GetComponent <Rigidbody>().angularVelocity;
            state.material              = (int)mat.type;
            state.color = (int)col.type;
            state.size  = size;
            //state.sceneIndex = sceneIndex;
            state.templateIndex = templateIndex;
            state.active        = obj.activeSelf ? 1 : 0;

            return(JsonUtility.ToJson(state));
        }

        return("");
    }
    void Update()
    {
        if ((SimulationState)controllerState.simulationState == SimulationState.CREATE_SCENE)
        {
            if (createSceneState == CreateSceneState.THROW)
            {
                if (prevCreatedObject == null)
                {
                    AddRandomSimulationObject();
                }
                else
                {
                    if (IsObjectStable(prevCreatedObject))
                    {
                        if (IsCreatedObjectValid(prevCreatedObject))
                        {
                            createdSimulationObjects.Add(prevObjectState);
                            noObjects--;
                            prevCreatedObject = null;
                            prevObjectState   = null;
                        }
                        else
                        {
                            prevCreatedObject.SetActive(false);
                            Destroy(prevCreatedObject);
                            prevCreatedObject = null;
                            prevObjectState   = null;
                        }
                    }
                }

                if (noObjects <= 0)
                {
                    createSceneState = CreateSceneState.WAIT_WITH_PIPE;
                }
            }
            else if (createSceneState == CreateSceneState.WAIT_WITH_PIPE)
            {
                if (IsSceneStable())
                {
                    bool changeState = true;
                    foreach (GameObject obj in pipeObjects)
                    {
                        if (obj.activeSelf == true)
                        {
                            changeState = false;
                            obj.SetActive(false);
                            break;
                        }
                    }
                    if (changeState)
                    {
                        createSceneState = CreateSceneState.WAIT_WITHOUT_PIPE;
                    }
                }
            }
            else if (createSceneState == CreateSceneState.WAIT_WITHOUT_PIPE)
            {
                if (IsSceneStable())
                {
                    // Append filename to folder name (format is '0005 shot.png"')
                    WriteGroundTruthInfo("InitialStable");
                    createSceneState = CreateSceneState.STOP;
                }
            }
            else if (createSceneState == CreateSceneState.STOP)
            {
                if (controllerState.stopWaitFrame <= 0)
                {
                    stop();
                }
                controllerState.stopWaitFrame--;
            }
        }

        else if ((SimulationState)controllerState.simulationState == SimulationState.CREATE_FINAL_STABLE)
        {
            if (removeObjectState == RemoveObjectState.WAIT)
            {
                if (IsSceneStable())
                {
                    WriteGroundTruthInfo(string.Format("FinalStable_{0:D04}", controllerState.removedObjectIndex));
                    removeObjectState = RemoveObjectState.STOP;
                }
            }
            else if (removeObjectState == RemoveObjectState.STOP)
            {
                if (controllerState.stopWaitFrame <= 0)
                {
                    stop();
                }
                controllerState.stopWaitFrame--;
            }
        }

        if ((maxSimulationFrames != 0 && Time.frameCount >= maxSimulationFrames) || (Time.frameCount >= hardCodedMaxSimulationFrames))
        {
            //Reset simulation by deleting the directory
            System.IO.Directory.Delete(simulationFolder);
            stop();
        }
    }