Add streaming mode to Lua samples 15_Navigation and 39_CrowdNavigation. Minor changes.

This commit is contained in:
Eugene Kozlov 2017-08-08 01:29:19 +03:00
parent e110998f70
commit ef3b6efd96
6 changed files with 157 additions and 3 deletions

View File

@ -448,6 +448,7 @@ void Navigation::SaveNavigationData()
{
NavigationMesh* navMesh = scene_->GetComponent<NavigationMesh>();
tileData_.Clear();
addedTiles_.Clear();
const IntVector2 numTiles = navMesh->GetNumTiles();
for (int z = 0; z < numTiles.y_; ++z)
for (int x = 0; x <= numTiles.x_; ++x)

View File

@ -540,6 +540,7 @@ void CrowdNavigation::SaveNavigationData()
{
DynamicNavigationMesh* navMesh = scene_->GetComponent<DynamicNavigationMesh>();
tileData_.Clear();
addedTiles_.Clear();
const IntVector2 numTiles = navMesh->GetNumTiles();
for (int z = 0; z < numTiles.y_; ++z)
for (int x = 0; x <= numTiles.x_; ++x)

View File

@ -12,6 +12,11 @@ require "LuaScripts/Utilities/Sample"
local endPos = nil
local currentPath = {}
local useStreaming = false
local streamingDistance = 2
local navigationTiles = {}
local addedTiles = {}
function Start()
-- Execute the common startup for samples
SampleStart()
@ -98,6 +103,8 @@ function CreateScene()
-- Create a NavigationMesh component to the scene root
local navMesh = scene_:CreateComponent("NavigationMesh")
-- Set small tiles to show navigation mesh streaming
navMesh.tileSize = 32
-- Create a Navigable component to the scene root. This tags all of the geometry in the scene as being part of the
-- navigation mesh. By default this is recursive, but the recursion could be turned off from Navigable
scene_:CreateComponent("Navigable")
@ -135,6 +142,7 @@ function CreateUI()
instructionText.text = "Use WASD keys to move, RMB to rotate view\n"..
"LMB to set destination, SHIFT+LMB to teleport\n"..
"MMB or O key to add or remove obstacles\n"..
"Tab to toggle navigation mesh streaming\n"..
"Space to toggle debug geometry"
instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
-- The text has multiple rows. Center them in relation to each other
@ -242,7 +250,7 @@ end
function AddOrRemoveObject()
-- Raycast and check if we hit a mushroom node. If yes, remove it, if no, create a new one
local hitPos, hitDrawable = Raycast(250.0)
if hitDrawable then
if not useStreaming and hitDrawable then
-- The part of the navigation mesh we must update, which is the world bounding box of the associated
-- drawable component
local updateBox = nil
@ -298,6 +306,61 @@ function Raycast(maxDistance)
return nil, nil
end
function ToggleStreaming(enabled)
local navMesh = scene_:GetComponent("NavigationMesh")
if enabled then
local maxTiles = (2 * streamingDistance + 1) * (2 * streamingDistance + 1)
local boundingBox = BoundingBox(navMesh.boundingBox)
SaveNavigationData()
navMesh:Allocate(boundingBox, maxTiles);
else
navMesh:Build();
end
end
function UpdateStreaming()
local navMesh = scene_:GetComponent("NavigationMesh")
-- Center the navigation mesh at the jack
local jackTile = navMesh:GetTileIndex(jackNode.worldPosition)
local beginTile = VectorMax(IntVector2(0, 0), jackTile - IntVector2(1, 1) * streamingDistance)
local endTile = VectorMin(jackTile + IntVector2(1, 1) * streamingDistance, navMesh.numTiles - IntVector2(1, 1))
-- Remove tiles
local numTiles = navMesh.numTiles
for i,tileIdx in pairs(addedTiles) do
if not (beginTile.x <= tileIdx.x and tileIdx.x <= endTile.x and beginTile.y <= tileIdx.y and tileIdx.y <= endTile.y) then
addedTiles[i] = nil
navMesh:RemoveTile(tileIdx)
end
end
-- Add tiles
for z = beginTile.y, endTile.y do
for x = beginTile.x, endTile.x do
local i = z * numTiles.x + x
if not navMesh:HasTile(IntVector2(x, z)) and navigationTiles[i] then
addedTiles[i] = IntVector2(x, z)
navMesh:AddTile(navigationTiles[i])
end
end
end
end
function SaveNavigationData()
local navMesh = scene_:GetComponent("NavigationMesh")
navigationTiles = {}
addedTiles = {}
local numTiles = navMesh.numTiles
for z = 0, numTiles.y - 1 do
for x = 0, numTiles.x - 1 do
local i = z * numTiles.x + x
navigationTiles[i] = navMesh:GetTileData(IntVector2(x, z))
end
end
end
function HandleUpdate(eventType, eventData)
-- Take the frame time step, which is stored as a float
local timeStep = eventData["TimeStep"]:GetFloat()
@ -307,6 +370,15 @@ function HandleUpdate(eventType, eventData)
-- Make Jack follow the Detour path
FollowPath(timeStep)
-- Update streaming
if input:GetKeyPress(KEY_TAB) then
useStreaming = not useStreaming
ToggleStreaming(useStreaming)
end
if useStreaming then
UpdateStreaming()
end
end
function FollowPath(timeStep)

View File

@ -13,6 +13,11 @@ require "LuaScripts/Utilities/Sample"
local INSTRUCTION = "instructionText"
local useStreaming = false
local streamingDistance = 2
local navigationTiles = {}
local addedTiles = {}
function Start()
-- Execute the common startup for samples
SampleStart()
@ -85,6 +90,8 @@ function CreateScene()
-- Create a DynamicNavigationMesh component to the scene root
local navMesh = scene_:CreateComponent("DynamicNavigationMesh")
-- Set small tiles to show navigation mesh streaming
navMesh.tileSize = 32
-- Enable drawing debug geometry for obstacles and off-mesh connections
navMesh.drawObstacles = true
navMesh.drawOffMeshConnections = true
@ -157,6 +164,7 @@ function CreateUI()
"LMB to set destination, SHIFT+LMB to spawn a Jack\n"..
"MMB or O key to add obstacles or remove obstacles/agents\n"..
"F5 to save scene, F7 to load\n"..
"Tab to toggle navigation mesh streaming\n"..
"Space to toggle debug geometry\n"..
"F12 to toggle this instruction text"
instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
@ -385,6 +393,69 @@ function MoveCamera(timeStep)
instruction.visible = not instruction.visible
end
end
function ToggleStreaming(enabled)
local navMesh = scene_:GetComponent("DynamicNavigationMesh")
if enabled then
local maxTiles = (2 * streamingDistance + 1) * (2 * streamingDistance + 1)
local boundingBox = BoundingBox(navMesh.boundingBox)
SaveNavigationData()
navMesh:Allocate(boundingBox, maxTiles);
else
navMesh:Build();
end
end
function UpdateStreaming()
local navMesh = scene_:GetComponent("DynamicNavigationMesh")
-- Center the navigation mesh at the jacks crowd
local averageJackPosition = Vector3(0, 0, 0)
local jackGroup = scene_:GetChild("Jacks")
if jackGroup then
for i = 0,jackGroup:GetNumChildren()-1 do
averageJackPosition = averageJackPosition + jackGroup:GetChild(i).worldPosition
end
averageJackPosition = averageJackPosition / jackGroup:GetNumChildren()
end
local jackTile = navMesh:GetTileIndex(averageJackPosition)
local beginTile = VectorMax(IntVector2(0, 0), jackTile - IntVector2(1, 1) * streamingDistance)
local endTile = VectorMin(jackTile + IntVector2(1, 1) * streamingDistance, navMesh.numTiles - IntVector2(1, 1))
-- Remove tiles
local numTiles = navMesh.numTiles
for i,tileIdx in pairs(addedTiles) do
if not (beginTile.x <= tileIdx.x and tileIdx.x <= endTile.x and beginTile.y <= tileIdx.y and tileIdx.y <= endTile.y) then
addedTiles[i] = nil
navMesh:RemoveTile(tileIdx)
end
end
-- Add tiles
for z = beginTile.y, endTile.y do
for x = beginTile.x, endTile.x do
local i = z * numTiles.x + x
if not navMesh:HasTile(IntVector2(x, z)) and navigationTiles[i] then
addedTiles[i] = IntVector2(x, z)
navMesh:AddTile(navigationTiles[i])
end
end
end
end
function SaveNavigationData()
local navMesh = scene_:GetComponent("DynamicNavigationMesh")
navigationTiles = {}
addedTiles = {}
local numTiles = navMesh.numTiles
for z = 0, numTiles.y - 1 do
for x = 0, numTiles.x - 1 do
local i = z * numTiles.x + x
navigationTiles[i] = navMesh:GetTileData(IntVector2(x, z))
end
end
end
function HandleUpdate(eventType, eventData)
-- Take the frame time step, which is stored as a float
@ -392,6 +463,15 @@ function HandleUpdate(eventType, eventData)
-- Move the camera, scale movement with time step
MoveCamera(timeStep)
-- Update streaming
if input:GetKeyPress(KEY_TAB) then
useStreaming = not useStreaming
ToggleStreaming(useStreaming)
end
if useStreaming then
UpdateStreaming()
end
end
function HandlePostRenderUpdate(eventType, eventData)

View File

@ -396,7 +396,6 @@ void UpdateStreaming()
{
addedTiles.Push(tileIdx);
navMesh.AddTile(navigationTilesData[tileDataIdx]);
Print("Add tile " + tileIdx.ToString());
}
}
}
@ -406,6 +405,7 @@ void SaveNavigationData()
NavigationMesh@ navMesh = scene_.GetComponent("NavigationMesh");
navigationTilesData.Clear();
navigationTilesIdx.Clear();
addedTiles.Clear();
IntVector2 numTiles = navMesh.numTiles;
for (int z = 0; z < numTiles.y; ++z)
for (int x = 0; x < numTiles.x; ++x)

View File

@ -480,7 +480,6 @@ void UpdateStreaming()
{
addedTiles.Push(tileIdx);
navMesh.AddTile(navigationTilesData[tileDataIdx]);
Print("Add tile " + tileIdx.ToString());
}
}
}
@ -490,6 +489,7 @@ void SaveNavigationData()
DynamicNavigationMesh@ navMesh = scene_.GetComponent("DynamicNavigationMesh");
navigationTilesData.Clear();
navigationTilesIdx.Clear();
addedTiles.Clear();
IntVector2 numTiles = navMesh.numTiles;
for (int z = 0; z < numTiles.y; ++z)
for (int x = 0; x < numTiles.x; ++x)