public GameObject SpawnAgent(AgentConfig config) { var go = Instantiate(config.Prefab); go.name = config.Name; var agentController = go.GetComponent <AgentController>(); agentController.Config = config; SIM.LogSimulation(SIM.Simulation.VehicleStart, config.Name); ActiveAgents.Add(go); agentController.GTID = ++SimulatorManager.Instance.GTIDs; BridgeClient bridgeClient = null; if (config.Bridge != null) { bridgeClient = go.AddComponent <BridgeClient>(); bridgeClient.Init(config.Bridge); if (config.Connection != null) { var split = config.Connection.Split(':'); if (split.Length != 2) { throw new Exception("Incorrect bridge connection string, expected HOSTNAME:PORT"); } bridgeClient.Connect(split[0], int.Parse(split[1])); } } SIM.LogSimulation(SIM.Simulation.BridgeTypeStart, config.Bridge != null ? config.Bridge.Name : "None"); if (!string.IsNullOrEmpty(config.Sensors)) { SetupSensors(go, config.Sensors, bridgeClient); } agentController.AgentSensors.AddRange(agentController.GetComponentsInChildren <SensorBase>(true)); go.transform.position = config.Position; go.transform.rotation = config.Rotation; agentController.Init(); return(go); }
public GameObject SpawnAgent(AgentConfig config) { var go = Instantiate(config.Prefab, transform); go.name = config.Name; var agentController = go.GetComponent <AgentController>(); agentController.SensorsChanged += AgentControllerOnSensorsChanged; agentController.Config = config; agentController.Config.AgentGO = go; SIM.LogSimulation(SIM.Simulation.VehicleStart, config.Name); ActiveAgents.Add(agentController.Config); agentController.GTID = ++SimulatorManager.Instance.GTIDs; agentController.Config.GTID = agentController.GTID; BridgeClient bridgeClient = null; if (config.Bridge != null) { bridgeClient = go.AddComponent <BridgeClient>(); bridgeClient.Init(config.Bridge); if (config.Connection != null) { var split = config.Connection.Split(':'); if (split.Length != 2) { throw new Exception("Incorrect bridge connection string, expected HOSTNAME:PORT"); } bridgeClient.Connect(split[0], int.Parse(split[1])); } } SIM.LogSimulation(SIM.Simulation.BridgeTypeStart, config.Bridge != null ? config.Bridge.Name : "None"); var sensorsController = go.AddComponent <SensorsController>(); agentController.AgentSensorsController = sensorsController; sensorsController.SetupSensors(config.Sensors); //Add required components for distributing rigidbody from master to clients var network = Loader.Instance.Network; if (network.IsClusterSimulation) { HierarchyUtilities.ChangeToUniqueName(go); if (network.IsClient) { //Disable controller and dynamics on clients so it will not interfere mocked components agentController.enabled = false; var vehicleDynamics = agentController.GetComponent <IVehicleDynamics>() as MonoBehaviour; if (vehicleDynamics != null) { vehicleDynamics.enabled = false; } } //Change the simulation type only if it's not set in the prefab var distributedRigidbody = go.GetComponent <DistributedRigidbody>(); if (distributedRigidbody == null) { distributedRigidbody = go.AddComponent <DistributedRigidbody>(); distributedRigidbody.SimulationType = DistributedRigidbody.MockingSimulationType.ExtrapolateVelocities; } //Add the rest required components for cluster simulation ClusterSimulationUtilities.AddDistributedComponents(go); } go.transform.position = config.Position; go.transform.rotation = config.Rotation; agentController.Init(); #if UNITY_EDITOR // TODO remove hack for editor opaque with alpha clipping 2019.3.3 Array.ForEach(go.GetComponentsInChildren <Renderer>(), renderer => { foreach (var m in renderer.materials) { m.shader = Shader.Find(m.shader.name); } }); Array.ForEach(go.GetComponentsInChildren <DecalProjector>(), decal => { decal.material.shader = Shader.Find(decal.material.shader.name); }); #endif return(go); }
public GameObject SpawnAgent(AgentConfig config) { var go = Instantiate(config.Prefab, transform); go.name = config.Name; // set it inactive until we can be sure setting up sensors etc worked without exceptions and it AgentController was initialized go.SetActive(false); var agentController = go.GetComponent <AgentController>(); agentController.Config = config; agentController.Config.AgentGO = go; var lane = go.AddComponent <VehicleLane>(); var baseLink = go.GetComponentInChildren <BaseLink>(); if (baseLink == null) { baseLink = new GameObject("BaseLink").AddComponent <BaseLink>(); baseLink.transform.SetParent(go.transform, false); } ActiveAgents.Add(agentController.Config); agentController.GTID = ++SimulatorManager.Instance.GTIDs; agentController.Config.GTID = agentController.GTID; BridgeClient bridgeClient = null; if (config.Bridge != null) { bridgeClient = go.AddComponent <BridgeClient>(); bridgeClient.Init(config.Bridge); if (!String.IsNullOrEmpty(config.Connection)) { bridgeClient.Connect(config.Connection); } } var sensorsController = go.AddComponent <SensorsController>(); agentController.AgentSensorsController = sensorsController; //Add required components for distributing rigidbody from master to clients var network = Loader.Instance.Network; if (network.IsClusterSimulation) { HierarchyUtilities.ChangeToUniqueName(go); if (network.IsClient) { //Disable controller and dynamics on clients so it will not interfere mocked components agentController.enabled = false; var vehicleDynamics = agentController.GetComponent <IVehicleDynamics>() as MonoBehaviour; if (vehicleDynamics != null) { vehicleDynamics.enabled = false; } } //Change the simulation type only if it's not set in the prefab var distributedRigidbody = go.GetComponent <DistributedRigidbody>(); if (distributedRigidbody == null) { distributedRigidbody = go.AddComponent <DistributedRigidbody>(); distributedRigidbody.SimulationType = DistributedRigidbody.MockingSimulationType.ExtrapolateVelocities; } //Add the rest required components for cluster simulation ClusterSimulationUtilities.AddDistributedComponents(go); } go.transform.position = config.Position; go.transform.rotation = config.Rotation; sensorsController.SetupSensors(config.Sensors); agentController.Init(); go.SetActive(true); return(go); }
public GameObject SpawnAgent(AgentConfig config) { var go = Instantiate(config.Prefab, transform); go.name = config.Name; // set it inactive until we can be sure setting up sensors etc worked without exceptions and it AgentController was initialized go.SetActive(false); var controller = go.GetComponent <IAgentController>(); if (controller == null) { Debug.LogWarning($"{nameof(IAgentController)} implementation not found on the {config.Name} vehicle. This vehicle can't be used as an ego vehicle."); } else { controller.Config = config; controller.Config.AgentGO = go; ActiveAgents.Add(controller.Config); controller.GTID = ++SimulatorManager.Instance.GTIDs; controller.Config.GTID = controller.GTID; } var lane = go.AddComponent <VehicleLane>(); var baseLink = go.GetComponentInChildren <BaseLink>(); if (baseLink == null) { baseLink = new GameObject("BaseLink").AddComponent <BaseLink>(); baseLink.transform.SetParent(go.transform, false); } var sensorsController = go.GetComponent <ISensorsController>() ?? go.AddComponent <SensorsController>(); if (controller != null) { controller.AgentSensorsController = sensorsController; } //Add required components for distributing rigidbody from master to clients var network = Loader.Instance.Network; if (network.IsClusterSimulation) { HierarchyUtilities.ChangeToUniqueName(go); //Add the rest required components for cluster simulation ClusterSimulationUtilities.AddDistributedComponents(go); if (network.IsClient) { controller?.DisableControl(); } } BridgeClient bridgeClient = null; if (config.BridgeData != null) { var dir = Path.Combine(Simulator.Web.Config.PersistentDataPath, "Bridges"); var vfs = VfsEntry.makeRoot(dir); Simulator.Web.Config.CheckDir(vfs.GetChild(config.BridgeData.AssetGuid), Simulator.Web.Config.LoadBridgePlugin); bridgeClient = go.AddComponent <BridgeClient>(); config.Bridge = BridgePlugins.Get(config.BridgeData.Type); bridgeClient.Init(config.Bridge); if (!String.IsNullOrEmpty(config.Connection)) { bridgeClient.Connect(config.Connection); } } go.transform.position = config.Position; go.transform.rotation = config.Rotation; sensorsController.SetupSensors(config.Sensors); controller?.Init(); if (SimulatorManager.Instance.IsAPI) { SimulatorManager.Instance.EnvironmentEffectsManager.InitRainVFX(go.transform); } go.SetActive(true); return(go); }
public GameObject SpawnAgent(AgentConfig config) { var go = Instantiate(config.Prefab, transform); go.name = config.Name; var agentController = go.GetComponent <AgentController>(); agentController.SensorsChanged += AgentControllerOnSensorsChanged; agentController.Config = config; SIM.LogSimulation(SIM.Simulation.VehicleStart, config.Name); ActiveAgents.Add(go); agentController.GTID = ++SimulatorManager.Instance.GTIDs; BridgeClient bridgeClient = null; if (config.Bridge != null) { bridgeClient = go.AddComponent <BridgeClient>(); bridgeClient.Init(config.Bridge); if (config.Connection != null) { var split = config.Connection.Split(':'); if (split.Length != 2) { throw new Exception("Incorrect bridge connection string, expected HOSTNAME:PORT"); } bridgeClient.Connect(split[0], int.Parse(split[1])); } } SIM.LogSimulation(SIM.Simulation.BridgeTypeStart, config.Bridge != null ? config.Bridge.Name : "None"); var sensorsController = go.AddComponent <SensorsController>(); agentController.AgentSensorsController = sensorsController; sensorsController.SetupSensors(config.Sensors); //Add required components for distributing rigidbody from master to clients var network = SimulatorManager.Instance.Network; if (network.IsMaster) { if (go.GetComponent <DistributedObject>() == null) { go.AddComponent <DistributedObject>(); } var distributedRigidbody = go.GetComponent <DistributedRigidbody>(); if (distributedRigidbody == null) { distributedRigidbody = go.AddComponent <DistributedRigidbody>(); } distributedRigidbody.SimulationType = MockedRigidbody.MockingSimulationType.ExtrapolateVelocities; } else if (network.IsClient) { //Disable controller and dynamics on clients so it will not interfere mocked components agentController.enabled = false; var vehicleDynamics = agentController.GetComponent <VehicleDynamics>(); if (vehicleDynamics != null) { vehicleDynamics.enabled = false; } //Add mocked components if (go.GetComponent <MockedObject>() == null) { go.AddComponent <MockedObject>(); } var mockedRigidbody = go.GetComponent <MockedRigidbody>(); if (mockedRigidbody == null) { mockedRigidbody = go.AddComponent <MockedRigidbody>(); } mockedRigidbody.SimulationType = MockedRigidbody.MockingSimulationType.ExtrapolateVelocities; } go.transform.position = config.Position; go.transform.rotation = config.Rotation; agentController.Init(); return(go); }