ForEachVisibleEntityDo

This command calls the specified function for all entities in the world that are visible to the specified camera. The optional extra parameter will be passed to the callback function each time it is called.

Syntax

Parameters

Remarks

The callback function will only be called for the top-level entities who's recursive bounding boxes intersect the specified camera's frustum volume. Your callback function should be written so that it is called recursively with the top-level entity's children.

Example

function Callback(camera,entity, extra)
--We only want to execute this on models.
if (entity:GetClass()==Object.ModelClass) then
local name = entity:GetKeyValue("name")
if (name ~= "") then
local context = extra
local position = entity:GetPosition()
position = camera:Project(position)
local font = context:GetFont()
context:DrawText(name,position.x - font:GetTextWidth(name)/2, position.y - font:GetHeight()/2)
end
end
end

--Create a window
window = Window:Create()
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
camera:Move(0,0,-6)
local light = DirectionalLight:Create()
light:SetRotation(35,35,0)

--Create some entities
local model = Model:Box()
model:SetKeyValue("name","Box1")
model:SetColor(0.0,0.0,1.0)
model:SetPosition(-5,0,0)

model = Model:Box()
model:SetKeyValue("name","Box2")
model:SetColor(0.0,0.0,1.0)
model:SetPosition(-2,1,0)

model = Model:Box()
model:SetKeyValue("name","Box3")
model:SetColor(0.0,0.0,1.0)
model:SetPosition(5,0,0)

while true do

if window:Closed() or window:KeyHit(Key.Escape) then return false end

Time:Update()
world:Update()
world:Render()

context:SetBlendMode(Blend.Alpha)

--Execute the callback for all entities that intersect the bounding box.
world:ForEachVisibleEntityDo(camera,"Callback",context)

context:SetBlendMode(Blend.Solid)

context:Sync(false)

end