diff --git a/lib/std.lua b/lib/std.lua index 2554cf9..6565578 100644 --- a/lib/std.lua +++ b/lib/std.lua @@ -63,3 +63,31 @@ std.pcty = function(percent) local width, height = ag.getScreenSize() return height * percent end + +-- Load an object from a model file and figure out what scale +-- to use based on a maximum specified bounding box the model must fit in. +-- Input: +-- model_name: the string containing the name of the model to load +-- max_x, max_y, max_z: +std.loadModelBounds = function(model_name, max_x, max_y, max_z) + local tmp_model = ag.loadModel(model_name) + local sx, sy, sz = tmp_model:getSize() + tmp_model:destroy() + local scale = 1.0 + if (max_x > 0 and sx > 0) then + scale = max_x / sx + end + if (max_y > 0 and sy > 0) then + local s = max_y / sy + if (s < scale) then + scale = s + end + end + if (max_z > 0 and sz > 0) then + local s = max_z / sz + if (s < scale) then + scale = s + end + end + return ag.loadModel(model_name, scale) +end