Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: configuration refactor #24

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
367 changes: 367 additions & 0 deletions Assets/AWSIM/Scenes/Composition/AWSIMSimulation.unity

Large diffs are not rendered by default.

29 changes: 23 additions & 6 deletions Assets/AWSIM/Scenes/Composition/LoaderScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ MonoBehaviour:
m_HandleRect: {fileID: 1911219726}
m_Direction: 0
m_Value: 0
m_Size: 0.9981195
m_Size: 0.9979867
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down Expand Up @@ -2875,9 +2875,6 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
simulationSceneName: AWSIMSimulation
simulationConfiguration:
timeScale: 0
useTraffic: 0
mapTrafficToggle: {fileID: 899049272}
--- !u!1 &692632476
GameObject:
Expand Down Expand Up @@ -6516,9 +6513,29 @@ MonoBehaviour:
m_GameObject: {fileID: 1505419645}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 23c1ce4fb46143f46bc5cb5224c934f6, type: 3}
m_Script: {fileID: 11500000, guid: a79441f348de89743a2939f4d699eac1, type: 3}
m_Name:
m_EditorClassIdentifier:
m_RenderShadows: 1
m_RequiresDepthTextureOption: 2
m_RequiresOpaqueTextureOption: 2
m_CameraType: 0
m_Cameras: []
m_RendererIndex: -1
m_VolumeLayerMask:
serializedVersion: 2
m_Bits: 1
m_VolumeTrigger: {fileID: 0}
m_RenderPostProcessing: 0
m_Antialiasing: 0
m_AntialiasingQuality: 2
m_StopNaN: 0
m_Dithering: 0
m_ClearDepth: 1
m_AllowXRRendering: 1
m_RequiresDepthTexture: 0
m_RequiresColorTexture: 0
m_Version: 2
--- !u!20 &1505419648
Camera:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -6840,7 +6857,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0.00030517578, y: -0.0007372491}
m_AnchoredPosition: {x: 0.00061035156, y: -0.0007372491}
m_SizeDelta: {x: -12.2252, y: 0}
m_Pivot: {x: 0.5, y: 1}
--- !u!114 &1558658280
Expand Down
1 change: 1 addition & 0 deletions Assets/AWSIM/Scripts/Loader/AWSIMConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace AWSIM.Loader
public class MapConfiguration
{
public string mapName;
public bool useShadows;
}

[Serializable]
Expand Down
17 changes: 5 additions & 12 deletions Assets/AWSIM/Scripts/Loader/EgosManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class EgosManager : MonoBehaviour, IConfigurableManager

private Vector3 egoEulerAngles;

private EgoConfiguration egoConfiguration;
public EgoConfiguration egoConfiguration {private set; get; }

private GameObject egoModel;

Expand Down Expand Up @@ -103,7 +103,7 @@ public Vector3 GetEulersFromUI()
};
}

private void SpawnEgo(Transform hierarchyParent)
public GameObject SpawnEgo(Transform hierarchyParent)
{
if (egoModel)
{
Expand All @@ -117,21 +117,14 @@ private void SpawnEgo(Transform hierarchyParent)
var egoInstance = Instantiate(egoModel, position, orientation);

egoInstance.gameObject.transform.SetParent(hierarchyParent);

return egoInstance;
} else {
Log(LogLevel.LOG_ERROR, $"No ego prefab set.");
return null;
}
}


/// <summary>
/// Configure scene
/// Spawns an Ego.
/// </summary>
public void ConfigureScene(Transform rootTransform = null)
{
SpawnEgo(rootTransform);
}

/// <summary>
/// Get available Ego vehicle names.
/// </summary>
Expand Down
5 changes: 0 additions & 5 deletions Assets/AWSIM/Scripts/Loader/IConfigurableManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public interface IConfigurableManager
/// </summary>
public void LoadUI();

/// <summary>
/// Configure the scene.
/// </summary>
public void ConfigureScene(Transform rootTransform);

/// <summary>
/// Log callback.
/// </summary>
Expand Down
22 changes: 6 additions & 16 deletions Assets/AWSIM/Scripts/Loader/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ IEnumerator LoadCoroutine()

// Then load simulation components
AsyncOperation simulationLoad = simulationManager.LoadSimulation();
simulationLoad.allowSceneActivation = true;
yield return new WaitUntil(() => simulationLoad.isDone);

// Finally configure the scene
ConfigureScenes();
SimConfiguration.Configure(egoManager, mapManager, simulationManager);

// Hide loading screen and gui
loadingScreen.SetActive(false);
Expand Down Expand Up @@ -181,7 +182,7 @@ public void StartLoader()
Log(LogLevel.LOG_INFO, "Configuration file loaded.");

// Configure managers with configuration file.
if (ConfigureManagers(configuration))
if (LoadManagersConfig(configuration))
{
// Configuration went well. Load all scenes.
Load();
Expand All @@ -208,7 +209,7 @@ public void OnLoadButtonPressed()
{
var configuration = LoadConfigFromGUI();

if (ConfigureManagers(configuration))
if (LoadManagersConfig(configuration))
{
Load();
}
Expand All @@ -220,6 +221,7 @@ AWSIMConfiguration LoadConfigFromGUI()
{
AWSIMConfiguration simulationConfig = new AWSIMConfiguration();
simulationConfig.mapConfiguration.mapName = mapManager.mapUISelecor.options[mapManager.mapUISelecor.value].text;
simulationConfig.mapConfiguration.useShadows = false; // Set shadows default to false.
simulationConfig.simulationConfiguration.useTraffic = simulationManager.mapTrafficToggle.isOn;
simulationConfig.simulationConfiguration.timeScale = 1.0f; //TODO: Time scale is not yet implemented on the GUI.
simulationConfig.egoConfiguration.egoVehicleName = egoManager.egoUISelecor.options[egoManager.egoUISelecor.value].text;
Expand Down Expand Up @@ -253,7 +255,7 @@ AWSIMConfiguration LoadConfigFromFile(string path)
}
}

bool ConfigureManagers(AWSIMConfiguration awsimConfig)
bool LoadManagersConfig(AWSIMConfiguration awsimConfig)
{
if (usingConfigFile && !configFileLoaded)
{
Expand Down Expand Up @@ -318,18 +320,6 @@ void Load()
StartCoroutine(LoadCoroutine());
}

void ConfigureScenes()
{
Scene simulationScene = SceneManager.GetSceneByName(simulationManager.simulationSceneName);
Scene mapScene = SceneManager.GetSceneByName(mapManager.spawnedMapName);

egoManager.ConfigureScene(simulationScene.GetRootGameObjects()[0].transform);
mapManager.ConfigureScene(mapScene.GetRootGameObjects()[0].transform);
simulationManager.ConfigureScene();

SceneManager.SetActiveScene(mapScene);
}

void Log(LogLevel level, string message)
{
switch (level)
Expand Down
9 changes: 1 addition & 8 deletions Assets/AWSIM/Scripts/Loader/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MapManager : MonoBehaviour, IConfigurableManager
[HideInInspector]
public string loaderSceneName = "LoaderScene";

private MapConfiguration mapConfiguration;
public MapConfiguration mapConfiguration {private set; get; }

/// <summary>
/// Map configuration.
Expand Down Expand Up @@ -56,13 +56,6 @@ public AsyncOperation LoadMap()
return SceneManager.LoadSceneAsync(mapConfiguration.mapName, LoadSceneMode.Additive);
}

/// <summary>
/// Configure the scene
/// </summary>
public void ConfigureScene(Transform rootTransform = null)
{
}

/// <summary>
/// Set up the UI for map configuration
/// </summary>
Expand Down
79 changes: 79 additions & 0 deletions Assets/AWSIM/Scripts/Loader/SimConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Linq;

namespace AWSIM.Loader
{
static class SimConfiguration
{
static public void Configure(EgosManager egoManager, MapManager mapManager, SimulationManager simulationManager)
{
//////////////////////
// Levels load order:
// LoaderScene -> MapScene -> SimulationScene
//
// Code in this section activates after all "Start()" and "Awake()" from map and simulation scenes.

//////////////////////
// Configuration resources

// Get our scene handles
Scene simulationScene = SceneManager.GetSceneByName(simulationManager.simulationSceneName);
Scene mapScene = SceneManager.GetSceneByName(mapManager.spawnedMapName);

// We set the map scene to active to get its environment parameters (skybox and ambient lighting)
SceneManager.SetActiveScene(mapScene);

// Get configurations
var simulationConfiguration = simulationManager.simulationConfiguration;
var mapConfiguration = mapManager.mapConfiguration;
var egoConfiguration = egoManager.egoConfiguration;

//////////////////////
// Ego Spawn

// We want our ego to spawn under the Simulation scene root object
var ego = egoManager.SpawnEgo(egoManager.gameObject.transform);

//////////////////////
// Core initialization

// Set camera GUI
FollowCamera followCamera = GameObject.FindObjectOfType<FollowCamera>();
GameObject.FindObjectOfType<MainCameraViewUI>().SetFollowCamera(followCamera);

// Set Ego position manager
Scripts.UI.EgoVehiclePositionManager positionManager = GameObject.FindObjectOfType<Scripts.UI.EgoVehiclePositionManager>();
positionManager.InitializeEgoTransform(ego.transform);

// Set scene time scale
DemoUI demoUi = GameObject.FindObjectOfType<DemoUI>();
demoUi.SetTimeScale(simulationConfiguration.timeScale);
demoUi.TimeScaleSlider.value = simulationConfiguration.timeScale;

// Set traffic on/off
var trafficSims = GameObject.FindObjectsOfType<TrafficSimulation.TrafficManager>();
foreach (var trafficSim in trafficSims)
{
trafficSim.gameObject.SetActive(simulationConfiguration.useTraffic);
}

// Turn shadows for directional light
if (mapConfiguration.useShadows)
{
var lights = GameObject.FindObjectsOfType<Light>();
foreach (Light light in lights)
{
if(light.type == LightType.Directional)
{
light.shadows = LightShadows.Hard;
}
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/AWSIM/Scripts/Loader/SimConfiguration.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 1 addition & 20 deletions Assets/AWSIM/Scripts/Loader/SimulationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SimulationManager : MonoBehaviour, IConfigurableManager
{
public string simulationSceneName = "AWSIMSimulation";
[HideInInspector]
public SimulationConfiguration simulationConfiguration;
public SimulationConfiguration simulationConfiguration {private set; get; }
public Toggle mapTrafficToggle;
public Action<LogLevel, string> Log { get; set; }

Expand All @@ -24,25 +24,6 @@ public AsyncOperation LoadSimulation()
return SceneManager.LoadSceneAsync(simulationSceneName, LoadSceneMode.Additive);
}

public void ConfigureScene(Transform rootTransform = null)
{
// Set traffic on/off
var trafficSims = GameObject.FindObjectsOfType<TrafficSimulation.TrafficManager>();
foreach (var trafficSim in trafficSims)
{
trafficSim.gameObject.SetActive(simulationConfiguration.useTraffic);
}

// Set Ego position manager
Scripts.UI.EgoVehiclePositionManager positionManager = GameObject.FindObjectOfType<Scripts.UI.EgoVehiclePositionManager>();
positionManager.InitializeEgoTransform(GameObject.FindObjectOfType<Vehicle>().transform);

// Set scene time scale
DemoUI demoUi = GameObject.FindObjectOfType<DemoUI>();
demoUi.SetTimeScale(simulationConfiguration.timeScale);
demoUi.TimeScaleSlider.value = simulationConfiguration.timeScale;
}

public void LoadUI()
{
}
Expand Down
6 changes: 6 additions & 0 deletions Assets/AWSIM/Scripts/UI/MainCameraViewUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ private void Awake()
canvasGroup.alpha = 0.0f;
}

public void SetFollowCamera(FollowCamera camera)
{
followCamera = camera;
OnEnable();
}

void OnEnable()
{
if(followCamera != null)
Expand Down
3 changes: 2 additions & 1 deletion Assets/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"mapConfiguration": {
"mapName": "Shinjuku"
"mapName": "Shinjuku",
"useShadows": false
},
"simulationConfiguration": {
"useTraffic": true,
Expand Down
Loading