Exemplo n.º 1
0
    private Vector3 agentStartPosition(DiscreteRemoteFPSAgentController agent)
    {
        Transform t = agent.transform;

        Vector3[] castDirections = new Vector3[] { t.forward, t.forward * -1, t.right, t.right * -1 };

        RaycastHit maxHit       = new RaycastHit();
        Vector3    maxDirection = Vector3.zero;

        RaycastHit          hit;
        CharacterController charContr = agent.m_CharacterController;
        Vector3             p1        = t.position + charContr.center + Vector3.up * -charContr.height * 0.5f;
        Vector3             p2        = p1 + Vector3.up * charContr.height;

        foreach (Vector3 d in castDirections)
        {
            if (Physics.CapsuleCast(p1, p2, charContr.radius, d, out hit))
            {
                if (hit.distance > maxHit.distance)
                {
                    maxHit       = hit;
                    maxDirection = d;
                }
            }
        }

        if (maxHit.distance > (charContr.radius * 5))
        {
            return(t.position + (maxDirection * (charContr.radius * 4)));
        }

        return(Vector3.zero);
    }
Exemplo n.º 2
0
    void Start()
    {
        primaryAgent = GameObject.Find("FPSController").GetComponent <DiscreteRemoteFPSAgentController>();
        primaryAgent.actionDuration = this.actionDuration;
        readyToEmit = true;

        this.agents.Add(primaryAgent);
    }
Exemplo n.º 3
0
 private void updateAgentColor(DiscreteRemoteFPSAgentController agent, Color color)
 {
     foreach (MeshRenderer r in agent.gameObject.GetComponentsInChildren <MeshRenderer> () as MeshRenderer[])
     {
         foreach (Material m in r.materials)
         {
             m.color = color;
         }
     }
 }
Exemplo n.º 4
0
 private void addClassImageForm(WWWForm form, DiscreteRemoteFPSAgentController agent)
 {
     if (this.renderClassImage)
     {
         if (!agent.imageSynthesis.hasCapturePass("_class"))
         {
             Debug.LogError("class image not available - sending empty image");
         }
         byte[] bytes = agent.imageSynthesis.Encode("_class");
         form.AddBinaryData("image_classes", bytes);
     }
 }
Exemplo n.º 5
0
 private void addImageForm(WWWForm form, DiscreteRemoteFPSAgentController agent, bool renderCamera)
 {
     if (this.renderImage)
     {
         if (renderCamera)
         {
             RenderTexture.active = agent.m_Camera.activeTexture;
             agent.m_Camera.Render();
         }
         form.AddBinaryData("image", captureScreen());
     }
 }
Exemplo n.º 6
0
    private void addDepthImageForm(WWWForm form, DiscreteRemoteFPSAgentController agent)
    {
        if (this.renderDepthImage)
        {
            if (!agent.imageSynthesis.hasCapturePass("_depth"))
            {
                Debug.LogError("Depth image not available - returning empty image");
            }

            byte[] bytes = agent.imageSynthesis.Encode("_depth");
            form.AddBinaryData("image_depth", bytes);
        }
    }
Exemplo n.º 7
0
    private IEnumerator EmitFrame()
    {
        frameCounter += 1;

        // we should only read the screen buffer after rendering is complete
        yield return(new WaitForEndOfFrame());


        WWWForm form = new WWWForm();

        MultiAgentMetadata multiMeta = new MultiAgentMetadata();

        multiMeta.agents        = new MetadataWrapper[this.agents.Count];
        multiMeta.activeAgentId = this.activeAgentId;
        multiMeta.sequenceId    = this.currentSequenceId;
        RenderTexture currentTexture = RenderTexture.active;


        for (int i = 0; i < this.agents.Count; i++)
        {
            DiscreteRemoteFPSAgentController agent = this.agents.ToArray() [i];
            MetadataWrapper metadata = agent.generateMetadataWrapper();
            metadata.agentId = i;
            // we don't need to render the agent's camera for the first agent
            addImageForm(form, agent, i > 0);
            addDepthImageForm(form, agent);
            addObjectImageForm(form, agent, ref metadata);
            addClassImageForm(form, agent);
            multiMeta.agents [i] = metadata;
        }

        RenderTexture.active = currentTexture;

        form.AddField("metadata", JsonUtility.ToJson(multiMeta));
        form.AddField("token", robosimsClientToken);
        WWW w = new WWW("http://" + robosimsHost + ":" + robosimsPort + "/train", form);

        yield return(w);

        if (!string.IsNullOrEmpty(w.error))
        {
            Debug.Log("Error: " + w.error);
            yield break;
        }
        else
        {
            ProcessControlCommand(w.text);
        }
    }
Exemplo n.º 8
0
    private void addAgent(ServerAction action)
    {
        Vector3 clonePosition = agentStartPosition(primaryAgent);

        if (clonePosition.magnitude > 0)
        {
            GameObject visCapsule = primaryAgent.transform.Find("VisibilityCapsule").gameObject;
            visCapsule.SetActive(true);


            DiscreteRemoteFPSAgentController clone = UnityEngine.Object.Instantiate(primaryAgent);
            clone.actionDuration         = this.actionDuration;
            clone.m_Camera.targetDisplay = this.agents.Count;
            clone.transform.position     = clonePosition;
            updateAgentColor(clone, agentColors[this.agents.Count]);
            clone.ProcessControlCommand(action);
            this.agents.Add(clone);
        }
        else
        {
            Debug.LogError("couldn't find a valid start position for a new agent");
        }
    }
Exemplo n.º 9
0
    private void addObjectImageForm(WWWForm form, DiscreteRemoteFPSAgentController agent, ref MetadataWrapper metadata)
    {
        if (this.renderObjectImage)
        {
            if (!agent.imageSynthesis.hasCapturePass("_id"))
            {
                Debug.LogError("Object Image not available in imagesynthesis - returning empty image");
            }
            byte[] bytes = agent.imageSynthesis.Encode("_id");
            form.AddBinaryData("image_ids", bytes);

            Color[] id_image = agent.imageSynthesis.tex.GetPixels();
            Dictionary <Color, int[]> colorBounds = new Dictionary <Color, int[]> ();
            for (int yy = 0; yy < tex.height; yy++)
            {
                for (int xx = 0; xx < tex.width; xx++)
                {
                    Color colorOn = id_image [yy * tex.width + xx];
                    if (!colorBounds.ContainsKey(colorOn))
                    {
                        colorBounds [colorOn] = new int[] { xx, yy, xx, yy };
                    }
                    else
                    {
                        int[] oldPoint = colorBounds [colorOn];
                        if (xx < oldPoint [0])
                        {
                            oldPoint [0] = xx;
                        }
                        if (yy < oldPoint [1])
                        {
                            oldPoint [1] = yy;
                        }
                        if (xx > oldPoint [2])
                        {
                            oldPoint [2] = xx;
                        }
                        if (yy > oldPoint [3])
                        {
                            oldPoint [3] = yy;
                        }
                    }
                }
            }
            List <ColorBounds> boundsList = new List <ColorBounds> ();
            foreach (Color key in colorBounds.Keys)
            {
                ColorBounds bounds = new ColorBounds();
                bounds.color = new byte[] {
                    (byte)Math.Round(key.r * 255),
                    (byte)Math.Round(key.g * 255),
                    (byte)Math.Round(key.b * 255)
                };
                bounds.bounds = colorBounds [key];
                boundsList.Add(bounds);
            }
            metadata.colorBounds = boundsList.ToArray();

            List <ColorId> colors = new List <ColorId> ();
            foreach (Color key in agent.imageSynthesis.colorIds.Keys)
            {
                ColorId cid = new ColorId();
                cid.color = new byte[] {
                    (byte)Math.Round(key.r * 255),
                    (byte)Math.Round(key.g * 255),
                    (byte)Math.Round(key.b * 255)
                };

                cid.name = agent.imageSynthesis.colorIds [key];
                colors.Add(cid);
            }
            metadata.colors = colors.ToArray();
        }
    }