Beispiel #1
0
    //same as PlaceObjectReceptacle but instead only succeeds if final placed object is within viewport

    public bool PlaceObjectReceptacleInViewport(List <ReceptacleSpawnPoint> rsps, SimObjPhysics sop, bool PlaceStationary, int maxPlacementAttempts, int degreeIncrement, bool AlwaysPlaceUpright)
    {
        if (rsps == null)
        {
            #if UNITY_EDITOR
            Debug.Log("Null list of points to check, please pass in populated list of <ReceptacleSpawnPoint>?");
            #endif
            return(false); //uh, there was nothing in the List for some reason, so failed to spawn
        }

        if (rsps.Count == 0)
        {
            return(false);
        }

        List <ReceptacleSpawnPoint> goodRsps = new List <ReceptacleSpawnPoint>();
        foreach (ReceptacleSpawnPoint p in rsps)
        {
            if (!p.ParentSimObjPhys.GetComponent <SimObjPhysics>().DoesThisObjectHaveThisSecondaryProperty
                    (SimObjSecondaryProperty.ObjectSpecificReceptacle))
            {
                goodRsps.Add(p);
            }
        }

        int tries = 0;
        foreach (ReceptacleSpawnPoint p in goodRsps)
        {
            //if this is an Object Specific Receptacle, stop this check right now! I mean it!
            //Placing objects in/on an Object Specific Receptacle uses different logic to place the
            //object at the Attachemnet point rather than in the spawn area, so stop this right now!

            if (PlaceObject(sop, p, PlaceStationary, degreeIncrement, AlwaysPlaceUpright))
            {
                //check to make sure the placed object is within the viewport
                BaseFPSAgentController primaryAgent = GameObject.Find("PhysicsSceneManager").GetComponent <AgentManager>().ReturnPrimaryAgent();
                if (primaryAgent.GetComponent <PhysicsRemoteFPSAgentController>().objectIsOnScreen(sop))
                {
                    return(true);
                }
            }

            tries += 1;
            if (maxPlacementAttempts > 0 && tries > maxPlacementAttempts)
            {
                break;
            }
        }

        //couldn't find valid places to spawn
        return(false);
    }
Beispiel #2
0
    private void addAgent(ServerAction action)
    {
        Vector3 clonePosition = new Vector3(action.x, action.y, action.z);

        //disable ambient occlusion on primary agetn because it causes issues with multiple main cameras
        primaryAgent.GetComponent <PhysicsRemoteFPSAgentController>().DisableScreenSpaceAmbientOcclusion();

        BaseFPSAgentController clone = UnityEngine.Object.Instantiate(primaryAgent);

        clone.IsVisible      = action.makeAgentsVisible;
        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);
    }
Beispiel #3
0
    public IEnumerator EmitFrame()
    {
        frameCounter += 1;

        bool shouldRender = this.renderImage && serverSideScreenshot;

        if (shouldRender)
        {
            // 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;


        ThirdPartyCameraMetadata[] cameraMetadata = new ThirdPartyCameraMetadata[this.thirdPartyCameras.Count];
        RenderTexture       currentTexture        = null;
        JavaScriptInterface jsInterface           = null;

        if (shouldRender)
        {
            currentTexture = RenderTexture.active;
            for (int i = 0; i < this.thirdPartyCameras.Count; i++)
            {
                ThirdPartyCameraMetadata cMetadata = new ThirdPartyCameraMetadata();
                Camera camera = thirdPartyCameras.ToArray()[i];
                cMetadata.thirdPartyCameraId = i;
                cMetadata.position           = camera.gameObject.transform.position;
                cMetadata.rotation           = camera.gameObject.transform.eulerAngles;
                cameraMetadata[i]            = cMetadata;
                ImageSynthesis imageSynthesis = camera.gameObject.GetComponentInChildren <ImageSynthesis> () as ImageSynthesis;
                addThirdPartyCameraImageForm(form, camera);
                addImageSynthesisImageForm(form, imageSynthesis, this.renderDepthImage, "_depth", "image_thirdParty_depth");
                addImageSynthesisImageForm(form, imageSynthesis, this.renderNormalsImage, "_normals", "image_thirdParty_normals");
                addImageSynthesisImageForm(form, imageSynthesis, this.renderObjectImage, "_id", "image_thirdParty_image_ids");
                addImageSynthesisImageForm(form, imageSynthesis, this.renderClassImage, "_class", "image_thirdParty_classes");
                addImageSynthesisImageForm(form, imageSynthesis, this.renderClassImage, "_flow", "image_thirdParty_flow");//XXX fix this in a bit
            }
        }

        for (int i = 0; i < this.agents.Count; i++)
        {
            BaseFPSAgentController agent = this.agents.ToArray() [i];
            jsInterface = agent.GetComponent <JavaScriptInterface>();
            MetadataWrapper metadata = agent.generateMetadataWrapper();
            metadata.agentId = i;
            // we don't need to render the agent's camera for the first agent
            if (shouldRender)
            {
                addImageForm(form, agent);
                addImageSynthesisImageForm(form, agent.imageSynthesis, this.renderDepthImage, "_depth", "image_depth");
                addImageSynthesisImageForm(form, agent.imageSynthesis, this.renderNormalsImage, "_normals", "image_normals");
                addObjectImageForm(form, agent, ref metadata);
                addImageSynthesisImageForm(form, agent.imageSynthesis, this.renderClassImage, "_class", "image_classes");
                addImageSynthesisImageForm(form, agent.imageSynthesis, this.renderFlowImage, "_flow", "image_flow");

                metadata.thirdPartyCameras = cameraMetadata;
            }
            multiMeta.agents [i] = metadata;
        }

        if (shouldRender)
        {
            RenderTexture.active = currentTexture;
        }

        var serializedMetadata = Newtonsoft.Json.JsonConvert.SerializeObject(multiMeta);

                #if UNITY_WEBGL
        // JavaScriptInterface jsI =  FindObjectOfType<JavaScriptInterface>();
        // jsInterface.SendAction(new ServerAction(){action = "Test"});
        if (jsInterface != null)
        {
            jsInterface.SendActionMetadata(serializedMetadata);
        }
        #endif

        //form.AddField("metadata", JsonUtility.ToJson(multiMeta));
        form.AddField("metadata", serializedMetadata);
        form.AddField("token", robosimsClientToken);

        #if !UNITY_WEBGL
        if (synchronousHttp)
        {
            if (this.sock == null)
            {
                // Debug.Log("connecting to host: " + robosimsHost);
                IPAddress  host   = IPAddress.Parse(robosimsHost);
                IPEndPoint hostep = new IPEndPoint(host, robosimsPort);
                this.sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try {
                    this.sock.Connect(hostep);
                }
                catch (SocketException e) {
                    Debug.Log("Socket exception: " + e.ToString());
                }
            }


            if (this.sock != null && this.sock.Connected)
            {
                byte[] rawData = form.data;

                string request = "POST /train HTTP/1.1\r\n" +
                                 "Content-Length: " + rawData.Length.ToString() + "\r\n";

                foreach (KeyValuePair <string, string> entry in form.headers)
                {
                    request += entry.Key + ": " + entry.Value + "\r\n";
                }
                request += "\r\n";

                int sent = this.sock.Send(Encoding.ASCII.GetBytes(request));
                sent = this.sock.Send(rawData);

                // waiting for a frame here keeps the Unity window in sync visually
                // its not strictly necessary, but allows the interact() command to work properly
                // and does not reduce the overall FPS
                yield return(new WaitForEndOfFrame());

                byte[] headerBuffer      = new byte[1024];
                int    bytesReceived     = 0;
                byte[] bodyBuffer        = null;
                int    bodyBytesReceived = 0;
                int    contentLength     = 0;

                // read header
                while (true)
                {
                    int received = this.sock.Receive(headerBuffer, bytesReceived, headerBuffer.Length - bytesReceived, SocketFlags.None);
                    if (received == 0)
                    {
                        Debug.LogError("0 bytes received attempting to read header - connection closed");
                        break;
                    }

                    bytesReceived += received;;
                    string headerMsg = Encoding.ASCII.GetString(headerBuffer, 0, bytesReceived);
                    int    offset    = headerMsg.IndexOf("\r\n\r\n");
                    if (offset > 0)
                    {
                        contentLength     = parseContentLength(headerMsg.Substring(0, offset));
                        bodyBuffer        = new byte[contentLength];
                        bodyBytesReceived = bytesReceived - (offset + 4);
                        Array.Copy(headerBuffer, offset + 4, bodyBuffer, 0, bodyBytesReceived);
                        break;
                    }
                }

                // read body
                while (bodyBytesReceived < contentLength)
                {
                    // check for 0 bytes received
                    int received = this.sock.Receive(bodyBuffer, bodyBytesReceived, bodyBuffer.Length - bodyBytesReceived, SocketFlags.None);
                    if (received == 0)
                    {
                        Debug.LogError("0 bytes received attempting to read body - connection closed");
                        break;
                    }

                    bodyBytesReceived += received;
                    //Debug.Log("total bytes received: " + bodyBytesReceived);
                }

                string msg = Encoding.ASCII.GetString(bodyBuffer, 0, bodyBytesReceived);
                ProcessControlCommand(msg);
            }
        }
        else
        {
            using (var www = UnityWebRequest.Post("http://" + robosimsHost + ":" + robosimsPort + "/train", form))
            {
                yield return(www.SendWebRequest());

                if (www.isNetworkError || www.isHttpError)
                {
                    Debug.Log("Error: " + www.error);
                    yield break;
                }
                ProcessControlCommand(www.downloadHandler.text);
            }
        }
        #endif
    }