From c01b72af87e8fdcc87c3aebd599a8b8cf6fa15af Mon Sep 17 00:00:00 2001 From: colourfulspring Date: Fri, 23 Feb 2024 22:33:39 +0800 Subject: [PATCH] CarCatching Environment LineRenderer --- .../Prefabs/CarAgentGridCollab.prefab | 126 +++++++++++++++++- .../Prefabs/CarCatchingArea.prefab | 14 +- .../Examples/CarCatching/Scripts/CarAgent.cs | 19 +-- .../Scripts/CarCatchingEnvController.cs | 3 + .../Examples/NavMeshTest/Scenes/Player.prefab | 10 +- .../NavMeshTest/Scripts/LineRender.cs | 2 +- .../SharedAssets/Materials/SpiritBlue.mat | 2 +- .../SharedAssets/Materials/SpiritPurple.mat | 91 +++++++++++++ .../Materials/SpiritPurple.mat.meta | 8 ++ 9 files changed, 252 insertions(+), 23 deletions(-) create mode 100644 Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritPurple.mat create mode 100644 Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritPurple.mat.meta diff --git a/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarAgentGridCollab.prefab b/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarAgentGridCollab.prefab index 67cc2842..9f25f85f 100644 --- a/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarAgentGridCollab.prefab +++ b/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarAgentGridCollab.prefab @@ -329,6 +329,8 @@ GameObject: - component: {fileID: 5601152702967375707} - component: {fileID: 3822330893343168228} - component: {fileID: 5628194071647130095} + - component: {fileID: 6387908156284689739} + - component: {fileID: 2645550221542489290} m_Layer: 2 m_Name: CarAgentGridCollab m_TagString: blueAgent @@ -351,7 +353,7 @@ Transform: - {fileID: 2704888262008647423} - {fileID: 2709359580712052713} m_Father: {fileID: 0} - m_RootOrder: 0 + m_RootOrder: -1 m_LocalEulerAnglesHint: {x: 0, y: 180, z: 0} --- !u!114 &8110157416270102484 MonoBehaviour: @@ -366,7 +368,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_BrainParameters: - VectorObservationSize: 0 + VectorObservationSize: 8 NumStackedVectorObservations: 1 m_ActionSpec: m_NumContinuousActions: 2 @@ -375,7 +377,7 @@ MonoBehaviour: VectorActionDescriptions: [] VectorActionSpaceType: 1 hasUpgradedBrainParametersWithActionSpec: 1 - m_Model: {fileID: 0} + m_Model: {fileID: 5022602860645237092, guid: 3c50cb9026aba7e34955f04c12fa789a, type: 3} m_InferenceDevice: 0 m_BehaviorType: 0 m_BehaviorName: Catching @@ -463,6 +465,124 @@ BoxCollider: serializedVersion: 3 m_Size: {x: 45, y: 44, z: 60} m_Center: {x: 0, y: 0, z: 0} +--- !u!120 &6387908156284689739 +LineRenderer: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2710286047221272849} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 0 + m_LightProbeUsage: 0 + m_ReflectionProbeUsage: 0 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: af274938e390bf241a2ec6dff21f7b4a, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Positions: + - {x: 0, y: 0, z: 0} + - {x: 0, y: 0, z: 1} + m_Parameters: + serializedVersion: 3 + widthMultiplier: 1 + widthCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + colorGradient: + serializedVersion: 2 + key0: {r: 1, g: 1, b: 1, a: 1} + key1: {r: 1, g: 1, b: 1, a: 1} + key2: {r: 0, g: 0, b: 0, a: 0} + key3: {r: 0, g: 0, b: 0, a: 0} + key4: {r: 0, g: 0, b: 0, a: 0} + key5: {r: 0, g: 0, b: 0, a: 0} + key6: {r: 0, g: 0, b: 0, a: 0} + key7: {r: 0, g: 0, b: 0, a: 0} + ctime0: 0 + ctime1: 65535 + ctime2: 0 + ctime3: 0 + ctime4: 0 + ctime5: 0 + ctime6: 0 + ctime7: 0 + atime0: 0 + atime1: 65535 + atime2: 0 + atime3: 0 + atime4: 0 + atime5: 0 + atime6: 0 + atime7: 0 + m_Mode: 0 + m_ColorSpace: -1 + m_NumColorKeys: 2 + m_NumAlphaKeys: 2 + numCornerVertices: 0 + numCapVertices: 0 + alignment: 0 + textureMode: 0 + textureScale: {x: 1, y: 1} + shadowBias: 0.5 + generateLightingData: 0 + m_MaskInteraction: 0 + m_UseWorldSpace: 1 + m_Loop: 0 + m_ApplyActiveColorSpace: 1 +--- !u!114 &2645550221542489290 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2710286047221272849} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: de79dcaa17358444fa44202c1787e270, type: 3} + m_Name: + m_EditorClassIdentifier: + minDistance: 0.1 + width: 5 --- !u!1 &2710572731397926669 GameObject: m_ObjectHideFlags: 0 diff --git a/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarCatchingArea.prefab b/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarCatchingArea.prefab index 82368f3b..00e1f000 100644 --- a/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarCatchingArea.prefab +++ b/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarCatchingArea.prefab @@ -61,24 +61,28 @@ MonoBehaviour: StartingScale: {x: 0, y: 0, z: 0} DecisionRequester: {fileID: 0} NavMeshAgent: {fileID: 0} + LineRenderer: {fileID: 0} - Agent: {fileID: 7246486209573452180} StartingPos: {x: 0, y: 0, z: 0} StartingRot: {x: 0, y: 0, z: 0, w: 0} StartingScale: {x: 0, y: 0, z: 0} DecisionRequester: {fileID: 0} NavMeshAgent: {fileID: 0} + LineRenderer: {fileID: 0} - Agent: {fileID: 3964309580746521657} StartingPos: {x: 0, y: 0, z: 0} StartingRot: {x: 0, y: 0, z: 0, w: 0} StartingScale: {x: 0, y: 0, z: 0} DecisionRequester: {fileID: 0} NavMeshAgent: {fileID: 0} + LineRenderer: {fileID: 0} - Agent: {fileID: 4005891969102443327} StartingPos: {x: 0, y: 0, z: 0} StartingRot: {x: 0, y: 0, z: 0, w: 0} StartingScale: {x: 0, y: 0, z: 0} DecisionRequester: {fileID: 0} NavMeshAgent: {fileID: 0} + LineRenderer: {fileID: 0} UseRandomAgentRotation: 1 UseRandomAgentPosition: 1 ResetTimer: 0 @@ -406,11 +410,6 @@ PrefabInstance: serializedVersion: 3 m_TransformParent: {fileID: 1581870063183487095} m_Modifications: - - target: {fileID: 1548337883655231979, guid: a431228058383324287911e110279f5a, - type: 3} - propertyPath: m_ShowGizmos - value: 1 - objectReference: {fileID: 0} - target: {fileID: 2708762399863795223, guid: a431228058383324287911e110279f5a, type: 3} propertyPath: m_RootOrder @@ -602,6 +601,11 @@ PrefabInstance: propertyPath: m_Enabled value: 1 objectReference: {fileID: 0} + - target: {fileID: 6387908156284689739, guid: a431228058383324287911e110279f5a, + type: 3} + propertyPath: m_Materials.Array.data[0] + value: + objectReference: {fileID: 2100000, guid: 524edbe6e93be4be3bcce154cad79235, type: 2} - target: {fileID: 8110157416270102484, guid: a431228058383324287911e110279f5a, type: 3} propertyPath: m_Model diff --git a/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarAgent.cs b/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarAgent.cs index e0c9fe7a..3ad1a7b9 100644 --- a/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarAgent.cs +++ b/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarAgent.cs @@ -4,6 +4,7 @@ using Unity.MLAgents.Actuators; using Unity.MLAgents.Sensors; using UnityEngine.AI; +using Random = UnityEngine.Random; public class CarAgent : Agent { @@ -58,12 +59,12 @@ public override void CollectObservations(VectorSensor sensor) Debug.Log(this.transform.parent.gameObject.name + ", " + this.name + " CollectObservations: " + carCatchingEnvController.ResetTimer); Vector2[] obs = carCatchingEnvController.GetAgentPosObs(this); - // foreach (var myobs in obs) - // { - // Debug.Log(this.transform.parent.gameObject.name + - // ", " + this.name + " Observations: " + myobs); - // sensor.AddObservation(myobs); - // } + foreach (var myobs in obs) + { + Debug.Log(this.transform.parent.gameObject.name + + ", " + this.name + " Observations: " + myobs); + sensor.AddObservation(myobs); + } // sensor.AddOneHotObservation(2, 8); } @@ -209,8 +210,10 @@ public override void Heuristic(in ActionBuffers actionsOut) // discreteActionsOut[0] = 2; // } var continuousActionsOut = actionsOut.ContinuousActions; - continuousActionsOut[0] = 0f; - continuousActionsOut[1] = 0f; + // continuousActionsOut[0] = 0f; + // continuousActionsOut[1] = 0f; + continuousActionsOut[0] = Random.Range(-1f,1f); + continuousActionsOut[1] = Random.Range(-1f, 1f); } public void FixedUpdate() diff --git a/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarCatchingEnvController.cs b/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarCatchingEnvController.cs index 4069176a..91373044 100644 --- a/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarCatchingEnvController.cs +++ b/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarCatchingEnvController.cs @@ -23,6 +23,7 @@ public class CarInfo [HideInInspector] public Vector3 StartingScale; [HideInInspector] public DecisionRequester DecisionRequester; [HideInInspector] public NavMeshAgent NavMeshAgent; + [HideInInspector] public LineRenderer LineRenderer; } /// @@ -79,6 +80,7 @@ void Start() item.StartingScale = itemTrans.localScale; item.DecisionRequester = item.Agent.GetComponent(); item.NavMeshAgent = item.Agent.GetComponent(); + item.LineRenderer = item.Agent.GetComponent(); } for (; RunningNum < AgentsList.Count && AgentsList[RunningNum].Agent.isRunning; ++RunningNum) ; @@ -142,6 +144,7 @@ public void ResetScene() var pos = UseRandomAgentPosition ? GetRandomSpawnPos(rot) : item.StartingPos; item.Agent.transform.SetPositionAndRotation(pos, rot); + item.LineRenderer.positionCount = 0; } } diff --git a/Project/Assets/ML-Agents/Examples/NavMeshTest/Scenes/Player.prefab b/Project/Assets/ML-Agents/Examples/NavMeshTest/Scenes/Player.prefab index 69ddd183..ba3ec882 100644 --- a/Project/Assets/ML-Agents/Examples/NavMeshTest/Scenes/Player.prefab +++ b/Project/Assets/ML-Agents/Examples/NavMeshTest/Scenes/Player.prefab @@ -211,9 +211,9 @@ LineRenderer: m_RotationOrder: 4 colorGradient: serializedVersion: 2 - key0: {r: 0, g: 0, b: 0, a: 1} - key1: {r: 0, g: 0, b: 0, a: 1} - key2: {r: 1, g: 0.10272626, b: 0, a: 0} + key0: {r: 1, g: 1, b: 1, a: 1} + key1: {r: 1, g: 1, b: 1, a: 1} + key2: {r: 0, g: 0, b: 0, a: 0} key3: {r: 0, g: 0, b: 0, a: 0} key4: {r: 0, g: 0, b: 0, a: 0} key5: {r: 0, g: 0, b: 0, a: 0} @@ -221,7 +221,7 @@ LineRenderer: key7: {r: 0, g: 0, b: 0, a: 0} ctime0: 0 ctime1: 65535 - ctime2: 1542 + ctime2: 65535 ctime3: 0 ctime4: 0 ctime5: 0 @@ -263,4 +263,4 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: minDistance: 0.1 - width: 2 + width: 5 diff --git a/Project/Assets/ML-Agents/Examples/NavMeshTest/Scripts/LineRender.cs b/Project/Assets/ML-Agents/Examples/NavMeshTest/Scripts/LineRender.cs index 584972e8..6c69cb1d 100644 --- a/Project/Assets/ML-Agents/Examples/NavMeshTest/Scripts/LineRender.cs +++ b/Project/Assets/ML-Agents/Examples/NavMeshTest/Scripts/LineRender.cs @@ -9,7 +9,7 @@ public class LineRender : MonoBehaviour private LineRenderer line; private Vector3 previousPosition; [SerializeField] private float minDistance = 0.1f; - [SerializeField, Range(0f, 20f)] private float width = 2f; + [SerializeField, Range(0f, 20f)] private float width = 5f; private void Start() { diff --git a/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritBlue.mat b/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritBlue.mat index 4edfc182..50812a08 100644 --- a/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritBlue.mat +++ b/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritBlue.mat @@ -84,7 +84,7 @@ Material: - _UVSec: 0 - _ZWrite: 1 m_Colors: - - _Color: {r: 0, g: 0, b: 1, a: 1} + - _Color: {r: 0.12941177, g: 0.5882353, b: 0.9529412, a: 1} - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} - _Flip: {r: 1, g: 1, b: 1, a: 1} - _RendererColor: {r: 1, g: 1, b: 1, a: 1} diff --git a/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritPurple.mat b/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritPurple.mat new file mode 100644 index 00000000..396f46ce --- /dev/null +++ b/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritPurple.mat @@ -0,0 +1,91 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 8 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: SpiritPurple + m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0} + m_Parent: {fileID: 0} + m_ModifiedSerializedProperties: 0 + m_ValidKeywords: [] + m_InvalidKeywords: [] + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_LockedProperties: + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _AlphaTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Ints: [] + m_Floats: + - PixelSnap: 0 + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _EnableExternalAlpha: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 0.5529412, g: 0.42745098, b: 0.78431374, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _Flip: {r: 1, g: 1, b: 1, a: 1} + - _RendererColor: {r: 1, g: 1, b: 1, a: 1} + m_BuildTextureStacks: [] diff --git a/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritPurple.mat.meta b/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritPurple.mat.meta new file mode 100644 index 00000000..4e518aa3 --- /dev/null +++ b/Project/Assets/ML-Agents/Examples/SharedAssets/Materials/SpiritPurple.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 524edbe6e93be4be3bcce154cad79235 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: