Exemple #1
0
    private void SpawnNPCMock(string vehicleId, NPCS npcData, int npcControllerSeed, Color color, Vector3 position, Quaternion rotation)  // TODO can this use SpawnNPC method?
    {
        var go = new GameObject("NPC " + vehicleId);

        go.SetActive(false);
        go.transform.SetParent(transform);
        go.layer = LayerMask.NameToLayer("NPC");
        go.tag   = "Car";
        var rb = go.AddComponent <Rigidbody>();

        rb.mass                   = 2000;
        rb.interpolation          = RigidbodyInterpolation.Interpolate;
        rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
        rb.position               = position;
        rb.rotation               = rotation;
        go.transform.SetPositionAndRotation(position, rotation);
        go.AddComponent <NPCController>();
        var npc_name = Instantiate(npcData.Prefab, go.transform).name;

        go.name = npc_name + vehicleId;
        var NPCController = go.GetComponent <NPCController>();

        NPCController.Size     = npcData.NPCType;
        NPCController.NPCColor = color;
        NPCController.NPCLabel = GetNPCLabel(npc_name);
        NPCController.id       = vehicleId;
        NPCController.Init(NPCSeedGenerator.Next());
        CurrentPooledNPCs.Add(NPCController);

        SimulatorManager.Instance.UpdateSegmentationColors(go);

        //Add required components for cluster simulation
        ClusterSimulationUtilities.AddDistributedComponents(go);
    }
Exemple #2
0
    public NPCController SpawnNPC(NPCSpawnData spawnData)
    {
        var go = new GameObject();

        go.SetActive(spawnData.Active);
        go.transform.SetParent(transform);
        go.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
        go.layer = LayerMask.NameToLayer("NPC");
        go.tag   = "Car";
        var rb = go.AddComponent <Rigidbody>();

        rb.mass                   = 2000;
        rb.interpolation          = RigidbodyInterpolation.Interpolate;
        rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
        var NPCController = go.AddComponent <NPCController>();
        var npc_name      = Instantiate(spawnData.Template.Prefab, go.transform).name;

        go.name                = npc_name + spawnData.GenId;
        NPCController.Size     = spawnData.Template.NPCType;
        NPCController.NPCColor = spawnData.Color;
        NPCController.NPCLabel = GetNPCLabel(npc_name);
        NPCController.id       = spawnData.GenId;
        NPCController.GTID     = ++SimulatorManager.Instance.GTIDs;
        NPCController.Init(spawnData.Seed);
        go.transform.SetPositionAndRotation(spawnData.Position, spawnData.Rotation);
        NPCController.SetLastPosRot(spawnData.Position, spawnData.Rotation);
        NPCController.SetBehaviour(DefaultBehaviour);
        CurrentPooledNPCs.Add(NPCController);

        SimulatorManager.Instance.UpdateSegmentationColors(NPCController.gameObject, NPCController.GTID);

        //Add required components for distributing rigidbody from master to clients
        if (Loader.Instance.Network.IsClusterSimulation)
        {
            ClusterSimulationUtilities.AddDistributedComponents(go);
        }

        // Add components for auto light layers change
        var triggerCollider = go.AddComponent <SphereCollider>();

        if (triggerCollider != null)
        {
            triggerCollider.radius    = NPCController.Bounds.size.z * 0.25f;
            triggerCollider.isTrigger = true;
        }
        go.AddComponent <AgentZoneController>();

        foreach (var callback in SpawnCallbacks)
        {
            callback(NPCController);
        }

        return(NPCController);
    }
Exemple #3
0
    public GameObject SpawnVehicle(string name, string genId, Vector3 position, Quaternion rotation, Color color)
    {
        var template = NPCVehicles.Find(obj => obj.Prefab.name == name);

        if (template.Prefab == null)
        {
            return(null);
        }

        var go = new GameObject("NPC " + genId);

        go.transform.SetParent(transform);
        go.layer = LayerMask.NameToLayer("NPC");
        go.tag   = "Car";
        var rb = go.AddComponent <Rigidbody>();

        rb.mass                   = 2000;
        rb.interpolation          = RigidbodyInterpolation.Interpolate;
        rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
        var npcC     = go.AddComponent <NPCController>();
        var npc_name = Instantiate(template.Prefab, go.transform).name;

        go.name = npc_name + genId;

        //Add required components for cluster simulation
        if (Loader.Instance.Network.IsClusterSimulation)
        {
            ClusterSimulationUtilities.AddDistributedComponents(go);
        }

        var NPCController = go.GetComponent <NPCController>();

        NPCController.NPCLabel = GetNPCLabel(npc_name);
        APINPCs.Add(NPCController);
        NPCController.id       = genId;
        NPCController.GTID     = ++SimulatorManager.Instance.GTIDs;
        NPCController.NPCColor = color == Color.clear ? GetWeightedRandomColor(template.NPCType) : color;
        var s = NPCSeedGenerator.Next();

        NPCController.Init(s);
        SimulatorManager.Instance.UpdateSegmentationColors(go);
        go.transform.SetPositionAndRotation(position, rotation); // TODO check for incorrect calc speed
        npcC.SetLastPosRot(position, rotation);

        return(go);
    }
Exemple #4
0
        public IControllable SpawnControllable(GameObject prefab, string uid, Vector3 pos, Quaternion rot, Vector3 velocity, Vector3 angVelocity)
        {
            var api = ApiManager.Instance;
            var obj = Instantiate(prefab, pos, rot, transform);

            var rb = obj.gameObject.GetComponent <Rigidbody>();

            if (rb != null)
            {
                rb.velocity        = velocity;
                rb.angularVelocity = angVelocity;
            }

            IControllable controllable = obj.GetComponent <IControllable>();

            if (controllable == null)
            {
                Destroy(obj);
                Debug.LogWarning($"Prefab missing IControllable component, spawning {prefab.name} aborted");
                return(null);
            }

            controllable.UID     = uid;
            controllable.Spawned = true;

            // Add components for auto light layers change
            var triggerCollider = obj.AddComponent <SphereCollider>();

            if (triggerCollider != null)
            {
                triggerCollider.radius    = 0.3f;
                triggerCollider.isTrigger = true;
            }
            obj.AddComponent <AgentZoneController>();

            RegisterControllable(controllable);

            if (Loader.Instance.Network.IsClusterSimulation)
            {
                //Add required components for cluster simulation
                ClusterSimulationUtilities.AddDistributedComponents(obj);
            }

            return(controllable);
        }
        public IControllable SpawnControllable(GameObject prefab, Vector3 pos, Quaternion rot, Vector3 velocity, Vector3 angVelocity)
        {
            var api = ApiManager.Instance;
            var obj = Instantiate(prefab, pos, rot);

            obj.transform.SetParent(transform);
            obj.transform.position = pos;
            obj.transform.rotation = rot;

            var rb = obj.gameObject.GetComponent <Rigidbody>();

            if (rb != null)
            {
                rb.velocity        = velocity;
                rb.angularVelocity = angVelocity;
            }

            IControllable controllable = obj.GetComponent <IControllable>();

            if (controllable == null)
            {
                Destroy(obj);
                Debug.LogError($"Prefab missing IControllable component, spawning {prefab.name} aborted");
                return(null);
            }

            var uid = System.Guid.NewGuid().ToString();

            controllable.UID     = uid;
            controllable.Spawned = true;

            api.Controllables.Add(uid, controllable);
            api.ControllablesUID.Add(controllable, uid);

            if (Loader.Instance.Network.IsClusterSimulation)
            {
                //Add required components for cluster simulation
                ClusterSimulationUtilities.AddDistributedComponents(obj);
            }

            return(controllable);
        }
Exemple #6
0
    private void SpawnNPC(string vehicleId, NPCS npcData, Color color, int npcControllerSeed)
    {
        var genId = vehicleId;
        var go    = new GameObject("NPC " + genId);

        go.SetActive(false);
        go.transform.SetParent(transform);
        go.layer = LayerMask.NameToLayer("NPC");
        go.tag   = "Car";
        var rb = go.AddComponent <Rigidbody>();

        rb.mass                   = 2000;
        rb.interpolation          = RigidbodyInterpolation.Interpolate;
        rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
        go.AddComponent <NPCController>();
        var npc_name = Instantiate(npcData.Prefab, go.transform).name;

        go.name = npc_name + genId;
        var NPCController = go.GetComponent <NPCController>();

        NPCController.Size     = npcData.NPCType;
        NPCController.NPCColor = color;
        NPCController.NPCLabel = GetNPCLabel(npc_name);
        NPCController.id       = genId;
        NPCController.Init(NPCSeedGenerator.Next());
        CurrentPooledNPCs.Add(NPCController);

        SimulatorManager.Instance.UpdateSegmentationColors(go);

        //Add required components for distributing rigidbody from master to clients
        if (Loader.Instance.Network.IsClusterSimulation)
        {
            //Add required components for cluster simulation
            ClusterSimulationUtilities.AddDistributedComponents(go);
            if (Loader.Instance.Network.IsMaster)
            {
                BroadcastMessage(GetSpawnMessage(genId, npcData, npcControllerSeed, color, go.transform.position,
                                                 go.transform.rotation));
            }
        }
    }
Exemple #7
0
    public NPCController SpawnNPC(NPCSpawnData spawnData)
    {
        var go = new GameObject();

        go.SetActive(spawnData.Active);
        go.transform.SetParent(transform);
        go.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
        go.layer = LayerMask.NameToLayer("NPC");
        go.tag   = "Car";
        var rb = go.AddComponent <Rigidbody>();

        rb.mass                   = 2000;
        rb.interpolation          = RigidbodyInterpolation.Interpolate;
        rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
        var NPCController = go.AddComponent <NPCController>();
        var npc_name      = Instantiate(spawnData.Template.Prefab, go.transform).name;

        go.name                = npc_name + spawnData.GenId;
        NPCController.Size     = spawnData.Template.NPCType;
        NPCController.NPCColor = spawnData.Color;
        NPCController.NPCLabel = GetNPCLabel(npc_name);
        NPCController.id       = spawnData.GenId;
        NPCController.GTID     = ++SimulatorManager.Instance.GTIDs;
        NPCController.Init(spawnData.Seed);
        go.transform.SetPositionAndRotation(spawnData.Position, spawnData.Rotation);
        NPCController.SetLastPosRot(spawnData.Position, spawnData.Rotation);
        NPCController.SetBehaviour <NPCLaneFollowBehaviour>();
        CurrentPooledNPCs.Add(NPCController);

        SimulatorManager.Instance.UpdateSegmentationColors(go);

        //Add required components for distributing rigidbody from master to clients
        if (Loader.Instance.Network.IsClusterSimulation)
        {
            ClusterSimulationUtilities.AddDistributedComponents(go);
        }

        return(NPCController);
    }
Exemple #8
0
    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);
    }
Exemple #9
0
    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);
    }
Exemple #10
0
    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);
    }