Browse Source

Cleaned up raycast code in Decals sample.

Lasse Öörni 12 years ago
parent
commit
f3eff662d0
2 changed files with 17 additions and 18 deletions
  1. 15 15
      Bin/Data/LuaScripts/08_Decals.lua
  2. 2 3
      Bin/Data/LuaScripts/15_Navigation.lua

+ 15 - 15
Bin/Data/LuaScripts/08_Decals.lua

@@ -205,11 +205,10 @@ function MoveCamera(timeStep)
 end
 
 function PaintDecal()
-    local hit = {}
-
-    if Raycast(250.0, hit) then
+    local result, hitPos, hitDrawable = Raycast(250.0)
+    if result then
         -- Check if target scene node already has a DecalSet component. If not, create now
-        local targetNode = hit[2]:GetNode()
+        local targetNode = hitDrawable:GetNode()
         local decal = targetNode:GetComponent("DecalSet")
         if decal == nil then
             decal = targetNode:CreateComponent("DecalSet")
@@ -219,32 +218,33 @@ function PaintDecal()
         -- use full texture UV's (0,0) to (1,1). Note that if we create several decals to a large object (such as the ground
         -- plane) over a large area using just one DecalSet component, the decals will all be culled as one unit. If that is
         -- undesirable, it may be necessary to create more than one DecalSet based on the distance
-        decal:AddDecal(hit[2], hit[1], cameraNode.rotation, 0.5, 1.0, 1.0, Vector2(0.0, 0.0), Vector2(1.0, 1.0))
+        decal:AddDecal(hitDrawable, hitPos, cameraNode.rotation, 0.5, 1.0, 1.0, Vector2(0.0, 0.0), Vector2(1.0, 1.0))
     end
 end
 
-function Raycast(maxDistance, hit)
-    hit[2] = nil
+function Raycast(maxDistance)
+    local hitPos = nil
+    local hitDrawable = nil
 
     local pos = ui.cursorPosition
     -- Check the cursor is visible and there is no UI element in front of the cursor
-    if not ui.cursor.visible or ui:GetElementAt(pos, true) ~= nil then
-        return false
+    if (not ui.cursor.visible) or (ui:GetElementAt(pos, true) ~= nil) then
+        return false, nil, nil
     end
 
     local camera = cameraNode:GetComponent("Camera")
     local cameraRay = camera:GetScreenRay(pos.x / graphics.width, pos.y / graphics.height)
     -- Pick only geometry objects, not eg. zones or lights, only get the first (closest) hit
-    local result = scene_:GetComponent("Octree"):RaycastSingle(cameraRay, RAY_TRIANGLE, maxDistance, DRAWABLE_GEOMETRY)
+    local octree = scene_:GetComponent("Octree")
+    local result = octree:RaycastSingle(cameraRay, RAY_TRIANGLE, maxDistance, DRAWABLE_GEOMETRY)
     if result.drawable ~= nil then
         -- Calculate hit position in world space
-        hit[1] = cameraRay.origin + cameraRay.direction * result.distance
-        hit[2] = result.drawable
-        print("Has hit, drawable is " .. result.drawable.typeName)
-        return true
+        hitPos = cameraRay.origin + cameraRay.direction * result.distance
+        hitDrawable = result.drawable
+        return true, hitPos, hitDrawable
     end
 
-    return false
+    return false, nil, nil
 end
 
 function HandleUpdate(eventType, eventData)

+ 2 - 3
Bin/Data/LuaScripts/15_Navigation.lua

@@ -216,7 +216,7 @@ function MoveCamera(timeStep)
 end
 
 function SetPathPoint()
-    local result, hitPos, hitDrawable = Raycast(250.0, hitPos, hitDrawable)
+    local result, hitPos, hitDrawable = Raycast(250.0)
     if result then
         local setStart = input:GetQualifierDown(QUAL_SHIFT)
         if setStart then
@@ -235,7 +235,7 @@ end
 
 function AddOrRemoveObject()
     -- Raycast and check if we hit a mushroom node. If yes, remove it, if no, create a new one
-    local result, hitPos, hitDrawable = Raycast(250.0, hitPos, hitDrawable)
+    local result, hitPos, hitDrawable = Raycast(250.0)
     if result then
         -- The part of the navigation mesh we must update, which is the world bounding box of the associated
         -- drawable component
@@ -288,7 +288,6 @@ function Raycast(maxDistance)
     local camera = cameraNode:GetComponent("Camera")
     local cameraRay = camera:GetScreenRay(pos.x / graphics.width, pos.y / graphics.height)
     -- Pick only geometry objects, not eg. zones or lights, only get the first (closest) hit
-    -- Note the convenience accessor to scene's Octree component
     local octree = scene_:GetComponent("Octree")
     local result = octree:RaycastSingle(cameraRay, RAY_TRIANGLE, maxDistance, DRAWABLE_GEOMETRY)
     if result.drawable ~= nil then