Skip to content

Commit

Permalink
Added shader for reflections from newest commit to fix problem with f…
Browse files Browse the repository at this point in the history
…reeze
  • Loading branch information
ItsOKayCZ committed Oct 23, 2024
1 parent b8d9fb9 commit 4fd39a7
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 19 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 python_trainer/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion unity_env/Assets/Materials/Deepracer - terrain.mat
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
Expand Down Expand Up @@ -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: []
5 changes: 5 additions & 0 deletions unity_env/Assets/Materials/Road/Amazon road.mat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: []
23 changes: 17 additions & 6 deletions unity_env/Assets/Materials/Road/Reflection.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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"
}
}
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,13 +73,29 @@ void Start()
);
}

SetMaterial();

AddAreas();

carCameras = GameObject.FindGameObjectsWithTag("CarCamera");

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)
Expand Down

0 comments on commit 4fd39a7

Please sign in to comment.