xurfer/xurfer/ext/3DFile/main.lua

47 lines
1.5 KiB
Lua

local api = ...
local ecs = api.ecs
return {
name = "3DFile",
enabled = true,
onURI = function(obj)
if( obj.URL ~= nil and (obj.URLResponse ~= nil or obj.URL.protocol == 'file') and
(obj.URL.extension == 'GLB' or
obj.URL.extension == 'GLTF' or
obj.URL.extension == 'OBJ')) then
if obj.URLResponse.ok or obj.URL.protocol == 'file' then
obj.root = false
if obj.URI.target == '_top' then
obj.root = true
if obj.URI.protocol ~= 'xrf' then -- xrf:// should not update toplevel URI
api.ext.URI.current = obj.URL -- set current url
end
end
if type(obj.URI.target) == 'table' and obj.URI.target.obj ~= nil then
local parent = obj.URI.target.obj
-- TODO: the runtime/lovr/render/model code should calculate parent offset
obj.x = parent.x
obj.y = parent.y
obj.z = parent.z
obj.scale = parent.scale
else
obj.x = obj.x or 0
obj.y = obj.y or 0
obj.z = obj.z or 0
obj.scale = obj.scale or 1
end
if obj.URL.protocol == 'file' then
obj.model = api.graphics.newModel( obj.URL.file )
else
obj.model = api.graphics.newModel( api.data.newBlob( obj.URLResponse.data) )
end
api.ecs.add( api.world, obj )
obj.commit('on3DFile') -- notify systems
else
print("[3DFile] error: could not load " .. obj.URL.string .. ": " .. obj.URLResponse.data )
end
end
end
}