Skip to content

Commit

Permalink
Added option to change appearance of relfection from python
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsOKayCZ committed Oct 22, 2024
1 parent bb784c8 commit 7910686
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
32 changes: 32 additions & 0 deletions python_trainer/environment_parameters.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 3 additions & 11 deletions python_trainer/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion unity_env/Assets/Materials/Road/Amazon road.mat
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Material:
- _NoiseScaleY: 0.05
- _OcclusionStrength: 1
- _Parallax: 0.02
- _ReflectionStrength: 1
- _ReflectionStrength: 0
- _SetTexture: 1
- _Smoothness: 0
- _SmoothnessTextureChannel: 1
Expand Down
3 changes: 3 additions & 0 deletions unity_env/Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions unity_env/Assets/Scripts/TrainingReplicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public enum RoadColor
private float changingColorSpeed;
private bool displayRandomColorInMain = false;

public Material[] roadMaterials;

struct HSV
{
public float hue;
Expand Down Expand Up @@ -71,6 +73,8 @@ void Start()
);
}

SetMaterial();

AddAreas();

carCameras = GameObject.FindGameObjectsWithTag("CarCamera");
Expand Down Expand Up @@ -105,6 +109,20 @@ private void ChangeCameraBackgroundColor()
GameObject.Find("Camera").GetComponent<Camera>().backgroundColor = backgroundColor;
}

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));
}
}

private void AddAreas()
{
for (int i = 0; i < numAreas; i++)
Expand Down

0 comments on commit 7910686

Please sign in to comment.