Asking where the drawn world is along a ray
Asking where the drawn world is along a ray
renderer.raycast(origin, direction, maxDistance?, exclude?) reports the
nearest surface the renderer DRAWS along a ray, and renderer.raycastAll every
surface along it, nearest first. Both answer for a mesh whether or not anything
gave it a rigid body, so a terrain, a procedurally generated mesh, or any plain
Model reports the surface under a point.
The answer is the nearest triangle of the mesh, so a sloped or terraced surface reports its height where it was asked rather than the extent of its bounding box:
local hit = renderer.raycast({ x, 100, z }, { 0, -1, 0 }, 200)
if hit then
print(hit.entityId, hit.point.y, hit.normal.y, hit.distance)
end
A hit carries entityId, the world-space point, a unit normal turned to
face back along the ray, the distance from the origin, the meshGuid,
castsShadows — whether the surface casts a shadow — and exact — true where
the answer came from a triangle, false where a mesh's vertices live on the GPU
alone and the object's bounds are the whole answer.
distance is measured in world units — the direction is normalised, at any
length you hand it in — so it is directly comparable to a physics.raycast
distance along the same ray. The triangles walked are the mesh's own, placed by
the entity's transform and by the mesh's bind pose, so a surface a skinning or
morph pass deforms on the GPU answers as the geometry the mesh holds.
Everything drawn is in scope — the ground you meant, and equally a character
standing on it, a prop, or a placeholder floor left in the scene. Read
entityId before trusting a height, step over what you did not mean with
exclude (an entity's id steps over the instanced copies standing in it too),
or take the whole column with raycastAll and pick the surface yourself:
-- the ground under a camera station, ignoring whoever is standing there
local hit = renderer.raycast({ x, 100, z }, { 0, -1, 0 }, 200, { playerId })
-- or: every surface in the column, choose the one you are after
for _, h in renderer.raycastAll({ x, 100, z }, { 0, -1, 0 }, 200) do
if h.entityId == groundId then print(h.point.y) end
end
Which object the ray hit, instanced copies included
A forest, a scatter, a Generator's instanced output or a quick-geometry model
(:asModel) draws many copies of one mesh in one hardware-instanced draw, and
the ray answers each copy from its own triangles at the matrix it is drawn at.
A copy is no entity of its own, so the hit says which copy it is:
instancedDrawis the draw, theInstancedDrawhandle everyrenderer.mesh.instance*call takes (absent for an entity's own mesh);instanceis which copy of it, counted from 1 in the order of the draw's transform buffer;entityIdis the entity the copies stand in: the Generator, for a Generator's output, and absent for copies standing in world space.
renderer.mesh.instanceTransforms(hit.instancedDraw) reads every copy's
matrix in the space of the entity the copies stand in (16 floats each,
column-major), so copy instance stands at floats (instance - 1) * 16 + 13
to + 15 of it; renderer.mesh.instanceWorlds reads the same matrices in
world space, and renderer.mesh.instanceInfo names the copy's mesh and
material. That place is where the item the copy draws stands: a tree of a
quick-geometry forest is one copy in the trunk draw and one in the crown draw,
both at the tree's place, so an effect, a marker or a fire put there sits on
the tree the ray met. Where the Generator's items carry components, each stands
as a child entity of the Generator at that place too, and the nearest child is
the item:
local hit = renderer.raycast(strikeFrom, { 0, -1, 0 }, 500)
if hit and hit.instancedDraw then
local pending = renderer.mesh.instanceWorlds(hit.instancedDraw)
while not pending:ready() do task.wait() end
local m, at = pending:result(), (hit.instance - 1) * 16
local place = { m[at + 13], m[at + 14], m[at + 15] }
print("copy", hit.instance, "of draw", hit.instancedDraw.id, "in", hit.entityId, "stands at", place[1], place[2], place[3])
-- items with components stand as the Generator's children: the nearest one is the item
local nearest, best = nil, math.huge
for _, child in entity.find(hit.entityId):getChildren() do
local p = child.position
local d = (p.x - place[1]) ^ 2 + (p.z - place[3]) ^ 2
if d < best then nearest, best = child, d end
end
end
physics.raycast answers from colliders instead, which is the query for what a
body would collide with; this one is the query for what a camera would see.