Onehot功能完善,修正
OneHot功能完善,更改TaySensor探测到Tag的index表示。与之前不通用! 修正PlayMode中的scenePrefabSet未定位错误 修正Raysensor中TagToInt不包含rayTagResult中的-1
This commit is contained in:
@@ -100,7 +100,7 @@ public class MLAgentsCustomController : Agent
|
||||
myObserve[2] = transform.localPosition.z;
|
||||
myObserve[3] = transform.eulerAngles.y / 36f;
|
||||
rayTagResult = raySensors.rayTagResult;// 探测用RayTag类型结果 float[](raySensorNum,1)
|
||||
rayTagResultOnehot = raySensors.rayTagResultOneHot.ToArray(); // 探测用RayTagonehot结果 List<int>[](raySensorNum*Tags,1)
|
||||
rayTagResultOnehot = raySensors.rayTagResultOneHot; // 探测用RayTagonehot结果 List<int>[](raySensorNum*Tags,1)
|
||||
rayDisResult = raySensors.rayDisResult; // 探测用RayDis距离结果 float[](raySensorNum,1)
|
||||
targetStates = targetController.targetState; // (6) targettype, target x,y,z, firebasesAreaDiameter
|
||||
remainTime = targetController.leftTime;
|
||||
|
||||
@@ -30,7 +30,7 @@ public class RaySensors : MonoBehaviour
|
||||
[Header("RayCastResult")]
|
||||
public float[] rayTagResult;
|
||||
|
||||
public List<float> rayTagResultOneHot;
|
||||
public float[] rayTagResultOneHot;
|
||||
public float[] rayDisResult;
|
||||
|
||||
[System.NonSerialized] public int totalRayNum;
|
||||
@@ -49,6 +49,7 @@ public class RaySensors : MonoBehaviour
|
||||
totalRayNum = halfOuterRayNum * 2 + focusRayNum;
|
||||
rayTagResult = new float[totalRayNum];
|
||||
rayDisResult = new float[totalRayNum];
|
||||
rayTagResultOneHot = new float[totalRayNum*ObjectTags.Tags.Count];
|
||||
linesOBJ = new GameObject[totalRayNum];
|
||||
lineRenderers = new LineRenderer[totalRayNum];
|
||||
rayInfoOBJ = new GameObject[totalRayNum];
|
||||
@@ -70,85 +71,60 @@ public class RaySensors : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public int TagToInt(string tag)
|
||||
{
|
||||
switch (tag)
|
||||
{
|
||||
case "Wall":
|
||||
return 1;
|
||||
|
||||
default:
|
||||
if (tag != myTag)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void SingleRaycastUpdate(Ray ray, LineRenderer lineRenderer, RayInfoUI rayInfoUI, out float rayTagResult, out float rayDisResult)
|
||||
private void SingleRaycastUpdate(Ray ray,int rayIndex)
|
||||
{
|
||||
// get Raycast hit infomation and return Tag and distance
|
||||
RaycastHit nowHit;
|
||||
Color rayColor = Color.cyan;
|
||||
Color rayColor = Color.gray;
|
||||
float lineLength = viewDistance;
|
||||
string rayInfoText = "";
|
||||
Vector3 rayInfoPosition;
|
||||
if (Physics.Raycast(ray, out nowHit, viewDistance)) // 若在viewDistance范围内有碰撞
|
||||
{
|
||||
rayInfoText = nowHit.collider.tag;
|
||||
rayTagResult = TagToInt(rayInfoText);
|
||||
rayTagResultOneHot.AddRange(oneHotTags.Encoder(rayInfoText));
|
||||
rayDisResult = nowHit.distance;
|
||||
lineLength = rayDisResult;
|
||||
rayInfoText += "\n" + Convert.ToString(rayDisResult);
|
||||
// rayDisResult = rayDisResult / viewDistance; // Normalization!
|
||||
// 输出log
|
||||
switch (rayTagResult)
|
||||
// Tag
|
||||
string thisTag = nowHit.collider.tag;
|
||||
// result update
|
||||
rayTagResult[rayIndex] = ObjectTags.TagToInt(thisTag);
|
||||
rayDisResult[rayIndex] = nowHit.distance;
|
||||
int oneHotIndex = rayIndex * ObjectTags.Tags.Count + ObjectTags.TagToInt(thisTag);
|
||||
rayTagResultOneHot[oneHotIndex] = 1f;
|
||||
if (rayTagResult[rayIndex] == ObjectTags.TagToInt("Enemy"))
|
||||
{
|
||||
case 1:// Wall
|
||||
rayColor = Color.white;
|
||||
break;
|
||||
|
||||
case 2: // Enemy
|
||||
rayColor = Color.red;
|
||||
inViewEnemies.Add(nowHit.transform.gameObject);
|
||||
break;
|
||||
|
||||
case -1: // Hit Nothing
|
||||
rayColor = Color.gray;
|
||||
break;
|
||||
|
||||
default: // default,got wrong
|
||||
rayColor = Color.cyan;
|
||||
break;
|
||||
inViewEnemies.Add(nowHit.transform.gameObject);
|
||||
}
|
||||
// ingame info update
|
||||
lineLength = nowHit.distance;
|
||||
rayInfoText = thisTag + "\n" + Convert.ToString(nowHit.distance);
|
||||
rayColor = ObjectTags.TagToCololr(thisTag);
|
||||
|
||||
}
|
||||
else // 若在viewDistance范围无碰撞
|
||||
{
|
||||
rayTagResultOneHot.AddRange(oneHotTags.Encoder());
|
||||
rayTagResult = -1f;
|
||||
rayDisResult = -1f;
|
||||
//输出log
|
||||
//Debug.Log(0);
|
||||
//Debug.Log(0);
|
||||
// Result update
|
||||
// rayTagResultOneHot keep zero
|
||||
rayTagResult[rayIndex] = -1;
|
||||
rayDisResult[rayIndex] = -1;
|
||||
// Info
|
||||
rayInfoText = "Empty";
|
||||
}
|
||||
|
||||
// draw Info In Game
|
||||
rayInfoPosition = ray.origin + (ray.direction * lineLength);
|
||||
if (showInGameRay)
|
||||
{
|
||||
DrawLine(ray, lineLength, lineRenderer, rayColor);
|
||||
DrawLine(ray, lineLength, lineRenderers[rayIndex], rayColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
TurnOffLine(lineRenderer, rayColor);
|
||||
TurnOffLine(lineRenderers[rayIndex], rayColor);
|
||||
}
|
||||
// drawRay in game
|
||||
if (showInGameRayInfo) rayInfoUI.UpdateInfo(rayInfoText, rayInfoPosition, rayColor, TPSCam);
|
||||
if (showInGameRayInfo) rayInfoUIs[rayIndex].UpdateInfo(rayInfoText, rayInfoPosition, rayColor, TPSCam);
|
||||
// Show log
|
||||
if (showDebugRay) Debug.DrawRay(ray.origin, ray.direction * viewDistance, rayColor); // drawRay in debug
|
||||
// Debug.Log(ray.origin + ray.direction);
|
||||
// Debug.Log(rayTagResult);
|
||||
// Debug.Log(tagToInt(nowHit.collider.tag));
|
||||
// Debug.Log(thisRayTagResult);
|
||||
// Debug.Log(tagToInt(nowHit.collider.thisTag));
|
||||
}
|
||||
|
||||
private void DrawLine(Ray ray, float lineLength, LineRenderer lineRenderer, Color lineColor)
|
||||
@@ -177,24 +153,31 @@ public class RaySensors : MonoBehaviour
|
||||
float focusREdge = agentCam.pixelWidth * (1 + focusRange) / 2;
|
||||
float camPixelHeight = agentCam.pixelHeight;
|
||||
inViewEnemies.Clear();
|
||||
rayTagResultOneHot.Clear();
|
||||
ClearResult();
|
||||
for (int i = 0; i < halfOuterRayNum; i++) // create left outside rays; 0 ~ focusLeftEdge
|
||||
{
|
||||
Vector3 point = new Vector3(i * focusLEdge / (halfOuterRayNum - 1), camPixelHeight / 2, 0);
|
||||
Ray nowRay = agentCam.ScreenPointToRay(point);
|
||||
SingleRaycastUpdate(nowRay, lineRenderers[i], rayInfoUIs[i], out rayTagResult[i], out rayDisResult[i]);
|
||||
SingleRaycastUpdate(nowRay,i);
|
||||
}
|
||||
for (int i = 0; i < halfOuterRayNum; i++) // create right outside rays; focusRightEdge ~ MaxPixelHeight
|
||||
{
|
||||
Vector3 point = new Vector3(focusREdge + (i * focusLEdge / (halfOuterRayNum - 1)), camPixelHeight / 2, 0);
|
||||
Ray nowRay = agentCam.ScreenPointToRay(point);
|
||||
SingleRaycastUpdate(nowRay, lineRenderers[halfOuterRayNum + i], rayInfoUIs[halfOuterRayNum + i], out rayTagResult[halfOuterRayNum + i], out rayDisResult[halfOuterRayNum + i]);
|
||||
SingleRaycastUpdate(nowRay, halfOuterRayNum + i);
|
||||
}
|
||||
for (int i = 0; i < focusRayNum; i++) // create center focus rays; focusLeftEdge ~ focusLeftEdge
|
||||
{
|
||||
Vector3 point = new Vector3(focusLEdge + ((i + 1) * (focusREdge - focusLEdge) / (focusRayNum + 1)), camPixelHeight / 2, 0);
|
||||
Ray nowRay = agentCam.ScreenPointToRay(point);
|
||||
SingleRaycastUpdate(nowRay, lineRenderers[halfOuterRayNum * 2 + i], rayInfoUIs[halfOuterRayNum * 2 + i], out rayTagResult[halfOuterRayNum * 2 + i], out rayDisResult[halfOuterRayNum * 2 + i]);
|
||||
SingleRaycastUpdate(nowRay, halfOuterRayNum * 2 + i);
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearResult()
|
||||
{
|
||||
rayTagResult = new float[totalRayNum];
|
||||
rayDisResult = new float[totalRayNum];
|
||||
rayTagResultOneHot = new float[totalRayNum * ObjectTags.Tags.Count];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user