From 22a06916f3d1c93429840cb1f05cdded7c9b833f Mon Sep 17 00:00:00 2001 From: Leon van Kammen Date: Thu, 11 Jun 2026 14:01:26 +0200 Subject: [PATCH] working version of janusxr src-equivalent --- src/ecs.lua | 2 +- src/ext/3DFile/main.lua | 10 ++--- src/ext/URI/main.lua | 5 ++- src/ext/janusxr/main.lua | 87 ++++++++++++++++++++++++++-------------- src/lovr/main.lua | 6 +-- src/main.lua | 8 +++- src/util.lua | 22 ++++++---- 7 files changed, 89 insertions(+), 51 deletions(-) diff --git a/src/ecs.lua b/src/ecs.lua index 8acd3f8..865a47e 100644 --- a/src/ecs.lua +++ b/src/ecs.lua @@ -6,7 +6,7 @@ ecs.init = function() baseEntify.filter = ecs.rejectAll('commit') baseEntify.updatethread = true function baseEntify:onAdd(obj) - obj.commit = api.util.commit( api.world, api.ecs.requireAll('allowcommit') ) + obj.commit = api.util.commit( api.world, obj, api ) end ecs.addSystem( api.world, baseEntify ) end diff --git a/src/ext/3DFile/main.lua b/src/ext/3DFile/main.lua index 3ba9de4..ae5667c 100644 --- a/src/ext/3DFile/main.lua +++ b/src/ext/3DFile/main.lua @@ -5,7 +5,7 @@ return { name = "3DFile", enabled = true, - loadasset = function(obj) + onURI = function(obj) if( obj.URL ~= nil and obj.URLResponse ~= nil and (obj.URL.extension == 'GLB' or obj.URL.extension == 'GLTF' or @@ -13,10 +13,10 @@ return { if obj.URLResponse.ok then obj.model = api.graphics.newModel( api.data.newBlob( obj.URLResponse.data) ) obj.root = (obj.URI.target == '_top') - obj.x = 0 - obj.y = 0 - obj.z = 0 - obj.commit() -- notify systems + if obj.x == nil then obj.x = 0 end + if obj.y == nil then obj.y = 0 end + if obj.z == nil then obj.z = 0 end + obj.commit('on3DFile') -- notify systems else print("[3DFile] error: could not load " .. obj.URL.string ) end diff --git a/src/ext/URI/main.lua b/src/ext/URI/main.lua index d86a363..722953f 100644 --- a/src/ext/URI/main.lua +++ b/src/ext/URI/main.lua @@ -19,7 +19,8 @@ return { to = function(URI,refererer, obj) obj = obj or {} - print("surfing to " .. URI.url) + local msg = "[i] loading" + print("[i] loading " .. URI.url) obj.URL = api.url.parse(URI.url) obj.URLResponse = {} foreach( api.protocol, function(name, p) @@ -35,7 +36,7 @@ return { data = data, headers = headers } - api.exec( api.ext, 'loadasset', obj) + obj.commit('onURI') end end diff --git a/src/ext/janusxr/main.lua b/src/ext/janusxr/main.lua index 5798ad1..98311ce 100644 --- a/src/ext/janusxr/main.lua +++ b/src/ext/janusxr/main.lua @@ -6,38 +6,63 @@ return { name = "janusxr", enabled = true, - init = function() - JMLLoader = ecs.processingSystem() - JMLLoader.updatethread = true - JMLLoader.filter = ecs.requireAll('URL', 'method', 'data', 'ok') - function JMLLoader:process(req, dt) - if req.ok and req.xml == nil then - - -- JML heuristic - if req.data.find("") or req.data.match("]") then - - local JML0 - local JML - req.xml = api.parser.xml.newParser() - local xmlstr = req.data - -- cleanup JML0 - local JML0 = xmlstr:gsub("[=%w].*","","") - util.traverseXML( req.xml:ParseXmlText(JML0), function(node,raw) - print_r(node) - end) - os.exit() - --api.world.add({ - -- x = 0, - -- y = 0, - -- z = 0, - -- model = req.model, - -- req = req - --}) - end + onURI = function(obj) + if obj.URL ~= nil and obj.URLResponse ~= nil and obj.URLResponse.ok then + local res = obj.URLResponse + -- JML heuristic + if res.data:lower():match("") or res.data:lower():match("]") then + local JML0 + local JML + res.xml = api.parser.xml.newParser() + local xmlstr = res.data + -- cleanup JML0 + local JML0 = xmlstr:gsub(".*<[Rr]oom",".*","") + api.ext.janusxr.loadXML( JML0, res, obj ) end end - ecs.addSystem(api.world,JMLLoader) - end, + + loadXML = function(xml,res,obj) + print("[i] janusxr: loading JML:\n\n" .. xml .. "\n\n") + util.traverseXML( res.xml:ParseXmlText(xml), function(node,raw) + if node.tag == 'object' and node.prop.id ~= nil then + local protocol = nil + foreach( api.protocol, function(name, p) + if node.prop.id:match("^"..name) then + protocol = p + end + end) + if protocol ~= nil then + local newobj = { + URI = { url = node.prop.id, method = 'GET' }, + janusxr = node + } + api.ext.janusxr.parseProps(node, newobj) + api.ecs.add( api.world, newobj ) + end + end + end) + end, + + parseProps = function(node,obj) + local me = api.ext.janusxr + foreach( node.prop, function(k,v) + if k == 'pos' then + local xyz = me.parseFloats(v) + obj.x = xyz[1] + obj.y = xyz[2] + obj.z = xyz[3] + end + end) + end, + + parseFloats = function(s) + local t = {} + for num in s:gmatch("%S+") do + t[#t+1] = tonumber(num) + end + return t + end + } diff --git a/src/lovr/main.lua b/src/lovr/main.lua index 1785121..352625c 100644 --- a/src/lovr/main.lua +++ b/src/lovr/main.lua @@ -163,9 +163,9 @@ local initECS = function(ecs) pass:setCullMode('none') pass:draw( obj['model'], - obj['x'] or 0, - obj['y'] or 0, - obj['z'] or 0, + obj['x'], + obj['y'], + obj['z'], 1, 0, 1, 0, 0, 1) end end diff --git a/src/main.lua b/src/main.lua index f8ab809..daba727 100644 --- a/src/main.lua +++ b/src/main.lua @@ -30,10 +30,16 @@ util.loaddir( "media", api, api.media ) ecs.init() api.exec( api.ext, 'init') -local url='https://coderofsalvation.codeberg.page/xrfragment-haxe/example/assets/example.glb?bar=1&f=2#foo' +-- GLB URLS +--local url='https://coderofsalvation.codeberg.page/xrfragment-haxe/example/assets/example.glb?bar=1&f=2#foo' --local url = 'https://codeberg.org/coderofsalvation/xrfragment/raw/branch/main/assets/template/website/website.glb' --local url = 'https://codeberg.org/coderofsalvation/xrfragment/raw/branch/main/assets/simple-a.glb' +--local url = 'https://xrforge.isvery.ninja/models/zzswz4qlqw8w/model_files/test.xrf.glb' +-- +-- JANUS JML URLS --local url = 'https://janusxr.org/index.html' +--local url = 'https://snips.sh/f/rHFLg-cewi?r=1' -- cube +local url = 'https://snips.sh/f/_U5-XctEVE?r=1' -- cube, monkey, scene api.ecs.add( api.world, { URI = { url = url, method = 'GET', target = '_top' } }) diff --git a/src/util.lua b/src/util.lua index 7ee6bf5..fa14cd4 100644 --- a/src/util.lua +++ b/src/util.lua @@ -88,14 +88,14 @@ function util.count(self) end end -function util.traverse(arr, cb, key) +function util.traverse(arr, cb, key, parent) if key == nil then key = 'children' end foreach( arr, function(k,child) if type(child) == 'table' then - cb(child) + cb(child,parent) if type(child[key]) == 'table' then if child[key][1] then - util.traverse( child[key], cb, key ) + util.traverse( child[key], cb, key, child ) end end end @@ -104,16 +104,21 @@ end function util.traverseXML( parsedXml, cb) util.traverse( parsedXml, - function(node) + function(node, parent) local struct = { tag = '?', prop = {} } - if node['name'] ~= nil then struct['tag'] = node:name() end - foreach( node['___props'], function(k,v) + if node['name'] ~= nil then struct['tag'] = node:name():lower() end + foreach( node['___props'], function(k,v) -- move to props array local val = node[ '@' .. v['name'] ] if type(val) == "table" then foreach( val, function(k,v) val = v end) -- pick last end struct['prop'][ v['name'] ] = val end) + if parent ~= nil then + struct['parent'] = (function(parent) + return function() return parent end + end)(parent) + end cb(struct,raw) end, '___children' @@ -162,8 +167,8 @@ end -- recalculate filters for tiny-ecs systems -- usage: entity.commit = util.commit(api.world) -- once -- entity.commit() -- this recalculates the filtercache of systems -util.commit = function(world) - return function() +util.commit = function(world, obj, api) + return function(extCb) for j = 1, #world.entities do local system = world.systems[j] system.entities = {} @@ -174,6 +179,7 @@ util.commit = function(world) end end end + api.exec( api.ext, extCb, obj) end end