Skip to content

Commit

Permalink
add walkable area editor
Browse files Browse the repository at this point in the history
  • Loading branch information
maajor committed Nov 11, 2018
1 parent d0c4d20 commit 91dc1d8
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 18 deletions.
1 change: 0 additions & 1 deletion Assets/Code/Pedestrian/Editor/PedestrianAreaEditor.cs
Expand Up @@ -29,7 +29,6 @@ public override void OnInspectorGUI()
{
myTarget.BuildWalkableArea();
}
myTarget.startPos = myTarget.GetComponent<Transform>().position;
if (changed)
{
serializedObject.ApplyModifiedProperties();
Expand Down
35 changes: 27 additions & 8 deletions Assets/Code/Pedestrian/PedestrianArea.cs
Expand Up @@ -108,6 +108,23 @@ private bool IsWalkable(int2 gridId)

public void BuildWalkableArea()
{
WalkableArea[] areas = gameObject.GetComponentsInChildren<WalkableArea>();
if (areas.Length == 0) return;
Bounds bound = new Bounds();
bool empty = true;
foreach (var area in areas)
{
area.OnBuild();
if (empty){
bound = area.Col.bounds;
empty = false;
}
bound.Encapsulate(area.Col.bounds);
}

startPos = bound.center;
Size = bound.size;

WalkableArea.Clear();
float div_x = Size.x / PatchResolution;
float div_z = Size.z / PatchResolution;
Expand All @@ -123,6 +140,10 @@ public void BuildWalkableArea()
WalkableArea.Add(BuildPatch(patch_min, patch_max));
}
}
foreach (var area in areas)
{
area.OnFinish();
}
EditorUtility.ClearProgressBar();
}

Expand All @@ -140,8 +161,9 @@ private WalkablePatch BuildPatch(Vector3 min, Vector3 max)
Vector3 texelCenter = min + new Vector3((i) * div_x, 0, (j) * div_z);
Ray castRay = new Ray(new Vector3(texelCenter.x, 1000, texelCenter.z), new Vector3(0, -1, 0));
int id = i * 9 + j;
RaycastHit[] castHits = Physics.RaycastAll(castRay, 2000.0f, 1 << 11 | 1 << 12);
if (castHits.Length == 0)
RaycastHit[] castHitObstacles = Physics.RaycastAll(castRay, 2000.0f, 1 << 11 | 1 << 12);
RaycastHit[] castHitAreas = Physics.RaycastAll(castRay, 2000.0f, 1 << 31);
if (castHitObstacles.Length == 0 && castHitAreas.Length > 0)
{
conserveGrid[id] = true;
}
Expand All @@ -167,16 +189,13 @@ private WalkablePatch BuildPatch(Vector3 min, Vector3 max)

void OnDrawGizmos()
{
if (!bDebug)
if (bDebug)
{
Color boxcolor = new Color(0.5f, 0, 0, 0.2f);
Gizmos.color = boxcolor;
Gizmos.DrawCube(startPos, Size);
}
Gizmos.color = Color.white;
Gizmos.DrawWireCube(startPos, Size);
if (bDebug)
{
Gizmos.color = Color.white;
Gizmos.DrawWireCube(startPos, Size);
DebugDrawWalkableArea();
}
}
Expand Down
7 changes: 5 additions & 2 deletions Assets/Code/Pedestrian/PedestrianFactory.cs
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Collections;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
Expand All @@ -19,9 +20,9 @@ public class PedestrianFactory
public static void Init(EntityManager manager)
{
_pedestrianArchetype = manager.CreateArchetype(typeof(PedestrianData), typeof(Position), typeof(Rotation),
typeof(MeshInstanceRenderer));
typeof(MeshInstanceRenderer)/*, typeof(MeshLODComponent), typeof(MeshLODGroupComponent)*/);
//_pedestrianCount = 0;

//FixedArrayArray<InstanceRendererProperty>
PedestrianArea.Instance.InitRandom();
}

Expand All @@ -46,6 +47,8 @@ public static Entity AddPedestrian(EntityManager manager)
receiveShadows = false,
subMesh = 0
});
//manager.SetComponentData(pedestrian, new MeshLODComponent(){ Group = pedestrian, LODMask = 1});
//manager.SetComponentData(pedestrian, new MeshLODGroupComponent() {});

//_pedestrianCount++;
return pedestrian;
Expand Down
3 changes: 2 additions & 1 deletion Assets/Code/Pedestrian/PedestrianSystem.cs
Expand Up @@ -6,6 +6,7 @@
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine;
Expand Down Expand Up @@ -70,7 +71,7 @@ protected override JobHandle OnUpdate(JobHandle deps)
};

deps = senseJob.Schedule(_capacity, 64, deps);

var stateJob = new PedestrianStateMachineJob()
{
};
Expand Down
37 changes: 37 additions & 0 deletions Assets/Code/Pedestrian/WalkableArea.cs
@@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WalkableArea : MonoBehaviour
{

public Vector3 Extent;

[HideInInspector] public BoxCollider Col;

void OnDrawGizmos()
{
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
Gizmos.DrawWireCube(Vector3.zero, Extent);
Gizmos.matrix = Matrix4x4.identity;
}

public void OnBuild()
{
Col = gameObject.AddComponent<BoxCollider>();
Col.size = Extent;
gameObject.layer = 31;
}

public Bounds GetBounds()
{
var bound = Col.bounds;
return bound;
}

public void OnFinish()
{
Col = gameObject.GetComponent<BoxCollider>();
if(Col != null) DestroyImmediate(Col);
}
}
11 changes: 11 additions & 0 deletions Assets/Code/Pedestrian/WalkableArea.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Code/Traffic/VehicleSystem.cs
Expand Up @@ -4,6 +4,7 @@
using Unity.Jobs;
using UnityEngine;
using OSMTrafficSim.BVH;
using Unity.Rendering;

namespace OSMTrafficSim
{
Expand Down
96 changes: 91 additions & 5 deletions Assets/Scenes/SampleScene.unity

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Packages/manifest.json
@@ -1,6 +1,7 @@
{
"dependencies": {
"com.unity.burst": "0.2.4-preview.34",
"com.unity.burst": "0.2.4-preview.37",
"com.unity.collections": "0.0.9-preview.9",
"com.unity.entities": "0.0.12-preview.19",
"com.unity.jobs": "0.0.7-preview.5",
"com.unity.mathematics": "0.0.12-preview.19",
Expand Down
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -23,3 +23,12 @@ System requirements
-------------------

- Unity 2018.2 or later

Feature Plan
-------------------

- Crossing Simulation
- Pedestrian Animation State Machine
- DrawMeshInstance with Custom MaterialProperty
- Visibility-Driven Update

0 comments on commit 91dc1d8

Please sign in to comment.