I’m trying to match a GUI node position and width to a gameworld model using Rendercam.
How would I go about achieving this?
This is my result so far:
Script:
function init(self)
timer.delay(0.1, false, function() -- Wait at least 10 frames so Rendercam has is updated
-- Set background colour
msg.post("@render:", "clear_color", { color = vmath.vector4(1, 1, 1, 1) } )
-- Create cube
local MODEL_SIZE = vmath.vector3(1,1,1)
local scale = 200
local scaled_model_size = MODEL_SIZE * scale
local pos = vmath.vector3(0,-200,0)
local cube = factory.create("go#cube_factory", pos, nil, nil, vmath.vector3(scale))
local model = msg.url(nil, cube, "model")
local light_pos = vmath.vector4( pos.x,pos.y-1000,pos.z+1500,1)
go.set(model,"light", light_pos)
-- Create GUI node to match
local screen_pos_x, screen_pos_y = rendercam.world_to_screen(pos, gui.ADJUST_FIT, true)
local gui_pos_x,gui_pos_y = rendercam.screen_to_gui(screen_pos_x,screen_pos_y, gui.ADJUST_FIT, false)
local gui_size_x, gui_size_y = rendercam.screen_to_gui(scaled_model_size.x, scaled_model_size.y, gui.ADJUST_FIT, true)
local gui_pos = vmath.vector3(gui_pos_x,gui_pos_y,0)
local gui_size = vmath.vector3(gui_size_x,10,0)
local node_color = vmath.vector4(0.9,0.2,0.1,1)
msg.post("go#gui", "new_box_node", {pos=gui_pos, size=gui_size, color=node_color} )
end)
end
Gui script:
function on_message(self, message_id, message, sender)
if message_id == hash("new_box_node") then
local node = gui.new_box_node(message.pos, message.size)
gui.set_color(node, message.color)
end
end
Project zipped up: Archive.zip (123.3 KB)