Exemple #1
0
    static public SimulationObjectState createObjectFromJSON(string stateStr, GameObject[] gameObjectTemplates, bool active)
    {
        if (gameObjectTemplates != null)
        {
            var state = JsonUtility.FromJson <SimulationObjectState>(stateStr);

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

            SimulationMaterial mat = new SimulationMaterial((SimulationMaterial.TYPE)state.material);
            obj.GetComponent <Rigidbody>().SetDensity(mat.GetDensity());

            SimulationColor col    = new SimulationColor((SimulationColor.TYPE)state.color);
            Material        newMat = Object.Instantiate(refObject.GetComponent <Renderer>().material);
            newMat.SetColor("_BaseColor", col.GetColor(mat.type));
            obj.GetComponent <Renderer>().material = newMat;

            obj.transform.position = state.position;
            obj.transform.rotation = state.rotation;
            //obj.GetComponent<Rigidbody>().velocity = state.velocity;
            //obj.GetComponent<Rigidbody>().inertiaTensor = state.inertiaTensor;
            //obj.GetComponent<Rigidbody>().inertiaTensorRotation = state.inertiaTensorRotation;
            //obj.GetComponent<Rigidbody>().angularVelocity = state.angularVelocity;

            if (active && state.active == 1)
            {
                obj.SetActive(true);
            }

            state.SetGameObject(obj);
            return(state);
        }
        return(null);
    }
    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("");
    }
Exemple #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("");
    }