// Initialize parameters from environment variables protected virtual void Awake() { #if UNITY_WEBGL this.jsInterface = this.GetComponent <JavaScriptInterface>(); this.jsInterface.enabled = true; #endif // whether it's in training or test phase // character controller parameters m_CharacterController = GetComponent <CharacterController>(); //float radius = m_CharacterController.radius; m_CharacterController.radius = 0.2f; // using default for now to remain consistent with generated points //m_CharacterController.height = LoadFloatVariable (height, "AGENT_HEIGHT"); this.m_WalkSpeed = 2; this.m_RunSpeed = 10; this.m_GravityMultiplier = 2; //this.m_UseFovKick = true; //this.m_StepInterval = 5; }
public string JsonCall(string extID, string methodName, string[] jsonArgs) { ClientInfo client; lock (_clients) client = _clients.FirstOrDefault(c => c.ExtensionID == extID); if (client == null || client.Client.State != CommunicationState.Opened) { return(JavaScriptInterface.JsonErrorResponse("The specified extension (" + extID + ") is not currently connected - it may have crashed or be restarting")); } try { return(client.Client.JsonCall(methodName, jsonArgs)); } catch (Exception ex) { return(JavaScriptInterface.JsonErrorResponse("There was an error communicating with the extension: " + ex.Message)); } }
public ScriptRunner(ServiceManager svc, ExtensionClientManager clientManager) { _svc = svc; _clientManager = clientManager; _engine.DisableSecurity(); _engine.SetFunction("__svc_jsonCall", new Func <string, string, string>(ServiceManagerJsonCall)); _engine.SetFunction("__ext_jsonCall", new Func <string, string, string, string>(ExtensionJsonCall)); _preScript = string.Concat( Resources.json2, Resources.scriptrunner, JavaScriptInterface.GenerateJavaScriptWrapper(svc), clientManager.GetJavaScriptWrappers() ); try { _engine.Run(_preScript); } catch (Exception ex) { LastException = ex; } }
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 }
internal string JsonCall(string name, string[] jsonArgs) { return(JavaScriptInterface.JsonCall(this, name, jsonArgs, JavaScriptInterface.ExcludedServiceManagerJsMethodNames)); }