diff --git a/python_trainer/environment_parameters.py b/python_trainer/environment_parameters.py new file mode 100644 index 00000000..94a57b0e --- /dev/null +++ b/python_trainer/environment_parameters.py @@ -0,0 +1,32 @@ +def set_parameters(data_channel) -> None: + # Wide - 15 + # Slim - 10 + data_channel.set_int_parameter("roadSize", 15) + + # 0 -> Amazon road + # 1 -> Black & white road + data_channel.set_int_parameter("roadColor", 0) + + data_channel.set_bool_parameter("randomBackgroundColor", True) + + data_channel.set_float_parameter("changingBackgroundColorSpeed", 0.75) + + # When the parameter 'backgroundColor' is not set, it is going to generate random colors + # Values of the channels are <0, 255> + # data_channel.set_color_parameter('backgroundColor', (255, 0, 0)) + + # How well you can see the reflection + # Default: 1.0 + data_channel.set_float_parameter("reflectionStrength", 1.0) + + # How scaled are the reflections in the X and Y directions + # Scaling both the numbers up will make the reflections smaller + # Default: 0.4 + data_channel.set_float_parameter("noiseScaleX", 0.4) + # Default: 0.05 + data_channel.set_float_parameter("noiseScaleY", 0.05) + + # The animation speed of the reflection + # Range <0, 1> + # Default: 0.4 + data_channel.set_float_parameter("noiseSpeed", 0.4) diff --git a/python_trainer/train.py b/python_trainer/train.py index cd6c51b3..03016c9c 100644 --- a/python_trainer/train.py +++ b/python_trainer/train.py @@ -5,6 +5,7 @@ import torch from data_channel import DataChannel +from environment_parameters import set_parameters from keyboard_listener import KeyboardListener from mlagents_envs.environment import UnityEnvironment from mlagents_envs.side_channel.engine_configuration_channel import EngineConfigurationChannel @@ -86,17 +87,8 @@ def print_env_info(env: UnityEnvironment) -> None: side_channels=[engine_channel, data_channel], ) - # Wide - 15 - # Slim - 10 - data_channel.set_int_parameter("roadSize", 15) - # 0 -> Amazon road - # 1 -> Black & white road - data_channel.set_int_parameter("roadColor", 0) - data_channel.set_bool_parameter("randomBackgroundColor", True) - data_channel.set_float_parameter("changingBackgroundColorSpeed", 0.75) - # When the parameter 'backgroundColor' is not set, the default color is the color of the amazon color - # Values of the channels are <0, 255> - # data_channel.set_color_parameter('backgroundColor', (255, 0, 0)) + set_parameters(data_channel) + engine_channel.set_configuration_parameters(time_scale=TIME_SCALE) env.reset() diff --git a/python_trainer/variables.py b/python_trainer/variables.py index cd08a9d4..63e87519 100644 --- a/python_trainer/variables.py +++ b/python_trainer/variables.py @@ -9,7 +9,7 @@ REDUCE_TEMPERATURE = 1 / 25 DISCOUNT = 0.95 # devalues future reward LEARNING_RATE = 0.0005 -NUM_TRAINING_EXAMPLES = 10000 +NUM_TRAINING_EXAMPLES = 5000 MAX_TRAINED_EPOCHS = 500 # Reward diff --git a/unity_env/Assets/Materials/Deepracer - terrain.mat b/unity_env/Assets/Materials/Deepracer - terrain.mat index 820a9aff..6b69d445 100644 --- a/unity_env/Assets/Materials/Deepracer - terrain.mat +++ b/unity_env/Assets/Materials/Deepracer - terrain.mat @@ -8,7 +8,7 @@ Material: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_Name: Deepracer - terrain - m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} + m_Shader: {fileID: 4800000, guid: 7bd43790af56d1df988076f434fb05d3, type: 3} m_Parent: {fileID: 0} m_ModifiedSerializedProperties: 0 m_ValidKeywords: [] @@ -70,14 +70,21 @@ Material: - _GlossyReflections: 1 - _Metallic: 0 - _Mode: 0 + - _NoiseScaleX: 1 + - _NoiseScaleY: 1 - _OcclusionStrength: 1 - _Parallax: 0.02 + - _ReflectionStrength: 1 + - _SetTexture: 0 - _SmoothnessTextureChannel: 0 - _SpecularHighlights: 1 + - _Speed: 0.5 - _SrcBlend: 1 - _UVSec: 0 - _ZWrite: 1 m_Colors: - _Color: {r: 0, g: 0.81960785, b: 0.5294118, a: 1} - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _MainColor: {r: 0, g: 0.81960785, b: 0.5294118, a: 1} + - _ReflectionColor: {r: 1, g: 1, b: 1, a: 1} m_BuildTextureStacks: [] diff --git a/unity_env/Assets/Materials/Road/Amazon road.mat b/unity_env/Assets/Materials/Road/Amazon road.mat index f9a92a04..a4acb91c 100644 --- a/unity_env/Assets/Materials/Road/Amazon road.mat +++ b/unity_env/Assets/Materials/Road/Amazon road.mat @@ -81,9 +81,12 @@ Material: - _Metallic: 0 - _Mode: 0 - _NoiseScale: 0.07 + - _NoiseScaleX: 1 + - _NoiseScaleY: 1 - _OcclusionStrength: 1 - _Parallax: 0.02 - _ReflectionStrength: 4 + - _SetTexture: 1 - _Smoothness: 0 - _SmoothnessTextureChannel: 1 - _SpecularHighlights: 1 @@ -94,5 +97,7 @@ Material: m_Colors: - _Color: {r: 1, g: 1, b: 1, a: 1} - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _MainColor: {r: 1, g: 1, b: 0, a: 1} + - _ReflectionColor: {r: 1, g: 1, b: 1, a: 1} - _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1} m_BuildTextureStacks: [] diff --git a/unity_env/Assets/Materials/Road/Reflection.shader b/unity_env/Assets/Materials/Road/Reflection.shader index e43e5efd..efd17a08 100644 --- a/unity_env/Assets/Materials/Road/Reflection.shader +++ b/unity_env/Assets/Materials/Road/Reflection.shader @@ -3,9 +3,13 @@ Shader "Custom/Reflection" Properties { _MainTex ("Albedo (RGB)", 2D) = "white" {} + [Maincolor] _MainColor ("Main color", Color) = (1., 1., 0., 1.) + _SetTexture ("Texture set", Float) = 0. _ReflectionStrength ("Reflection strength", Float) = 1.0 - _NoiseScale ("Noise scale", Float) = 1.0 + _NoiseScaleX ("Noise scale X", Float) = 1.0 + _NoiseScaleY ("Noise scale Y", Float) = 1.0 _Speed ("Noise speed", Range(0, 1)) = 0.5 + _ReflectionColor ("Reflection color", Color) = (1., 1., 1., 1.) } SubShader { @@ -20,9 +24,13 @@ Shader "Custom/Reflection" #pragma target 3.0 sampler2D _MainTex; + float4 _MainColor; float _ReflectionStrength; - float _NoiseScale; + float _NoiseScaleX; + float _NoiseScaleY; float _Speed; + float4 _ReflectionColor; + float _SetTexture; struct Input { @@ -166,14 +174,17 @@ Shader "Custom/Reflection" void surf (Input IN, inout SurfaceOutputStandard o) { // float2 noiseInput = IN.uv_MainTex * _NoiseScale; - float2 noiseInput = IN.worldPos.xz * _NoiseScale; + float2 noiseInput = IN.worldPos.xz; + noiseInput.x *= _NoiseScaleX; + noiseInput.y *= _NoiseScaleY; float noiseValue = clamp(ClassicNoise(float3(noiseInput.x, noiseInput.y, _Time.y * _Speed)), 0.0, 1.0) * _ReflectionStrength; // float noiseValue = ClassicNoise(noiseInput); - fixed4 albedo = tex2D(_MainTex, IN.uv_MainTex); - o.Albedo = albedo + noiseValue; + fixed4 albedo = tex2D(_MainTex, IN.uv_MainTex) * _SetTexture + _MainColor * (1. - _SetTexture); + + o.Albedo = albedo + noiseValue * _ReflectionColor.rgb; } ENDCG } FallBack "Diffuse" -} +} \ No newline at end of file diff --git a/unity_env/Assets/Scenes/Main.unity b/unity_env/Assets/Scenes/Main.unity index 7d5e0700..2ac02771 100644 --- a/unity_env/Assets/Scenes/Main.unity +++ b/unity_env/Assets/Scenes/Main.unity @@ -723,6 +723,9 @@ MonoBehaviour: roadColor: 1 backgroundColor: {r: 0, g: 0, b: 0, a: 0} randomBackgroundColor: 0 + roadMaterials: + - {fileID: 2100000, guid: d63a402699f7f904c8bad171fdc44dca, type: 2} + - {fileID: 2100000, guid: 39a14f8831164d0b6b9637adb0cdbddf, type: 2} --- !u!1001 &7475695033151031654 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/unity_env/Assets/Scripts/TrainingReplicator.cs b/unity_env/Assets/Scripts/TrainingReplicator.cs index 3272b04c..d7843c7d 100644 --- a/unity_env/Assets/Scripts/TrainingReplicator.cs +++ b/unity_env/Assets/Scripts/TrainingReplicator.cs @@ -28,6 +28,8 @@ public enum RoadColor private float changingColorSpeed; private bool displayRandomColorInMain = false; + public Material[] roadMaterials; + struct HSV { public float hue; @@ -71,6 +73,8 @@ void Start() ); } + SetMaterial(); + AddAreas(); carCameras = GameObject.FindGameObjectsWithTag("CarCamera"); @@ -78,6 +82,20 @@ void Start() ChangeCameraBackgroundColor(); } + void SetMaterial() + { + foreach (var mat in roadMaterials) + { + mat.SetFloat( + "_ReflectionStrength", + DataChannel.getParameter("reflectionStrength", 1f) + ); + mat.SetFloat("_NoiseScaleX", DataChannel.getParameter("noiseScaleX", 0.4f)); + mat.SetFloat("_NoiseScaleY", DataChannel.getParameter("noiseScaleY", 0.05f)); + mat.SetFloat("_Speed", DataChannel.getParameter("noiseSpeed", 0.3f)); + } + } + void Update() { if (randomBackgroundColor)