@omnidotdev/rdk 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"FiducialSession.d.ts","sourceRoot":"","sources":["../../src/fiducial/FiducialSession.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAE/C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAEhE,MAAM,WAAW,oBAAqB,SAAQ,iBAAiB;IAC7D,gCAAgC;IAChC,OAAO,CAAC,EAAE,sBAAsB,CAAC;CAClC;AAED;;;GAGG;AACH,QAAA,MAAM,eAAe;4BAAgC,oBAAoB;;CA+CxE,CAAC;AAKF,eAAe,eAAe,CAAC"}
1
+ {"version":3,"file":"FiducialSession.d.ts","sourceRoot":"","sources":["../../src/fiducial/FiducialSession.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAE/C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAEhE,MAAM,WAAW,oBAAqB,SAAQ,iBAAiB;IAC7D,gCAAgC;IAChC,OAAO,CAAC,EAAE,sBAAsB,CAAC;CAClC;AAED;;;GAGG;AACH,QAAA,MAAM,eAAe;4BAAgC,oBAAoB;;CA8CxE,CAAC;AAKF,eAAe,eAAe,CAAC"}
@@ -1,5 +1,6 @@
1
1
  import { PropsWithChildren } from 'react';
2
- export interface GeolocationAnchorProps extends PropsWithChildren {
2
+ import { GeolocationSessionOptions } from './geolocationBackend';
3
+ export interface GeolocationAnchorProps extends Pick<GeolocationSessionOptions, "onGpsUpdate">, PropsWithChildren {
3
4
  /** Physical target latitude (where you want the AR object placed in the real world). */
4
5
  latitude: number;
5
6
  /** Physical target longitude (where you want the AR object placed in the real world). */
@@ -15,13 +16,11 @@ export interface GeolocationAnchorProps extends PropsWithChildren {
15
16
  */
16
17
  isBillboard?: boolean;
17
18
  /** Called once actually attached to LocAR (after first `gpsupdate`). */
18
- onAttached?: () => void;
19
- /** Forward raw GPS updates. */
20
- onGpsUpdate?: (pos: any) => void;
19
+ onAttach?: () => void;
21
20
  }
22
21
  /**
23
22
  * Geolocation anchor.
24
23
  */
25
- declare const GeolocationAnchor: ({ latitude, longitude, altitude, isBillboard, onAttached, onGpsUpdate, children, }: GeolocationAnchorProps) => import("react").JSX.Element;
24
+ declare const GeolocationAnchor: ({ latitude, longitude, altitude, isBillboard, onAttach: _onAttach, onGpsUpdate: _onGpsUpdate, children, }: GeolocationAnchorProps) => import("react").JSX.Element;
26
25
  export default GeolocationAnchor;
27
26
  //# sourceMappingURL=GeolocationAnchor.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"GeolocationAnchor.d.ts","sourceRoot":"","sources":["../../src/geolocation/GeolocationAnchor.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AA6B/C,MAAM,WAAW,sBAAuB,SAAQ,iBAAiB;IAC/D,wFAAwF;IACxF,QAAQ,EAAE,MAAM,CAAC;IACjB,yFAAyF;IACzF,SAAS,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,wEAAwE;IACxE,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;IACxB,+BAA+B;IAE/B,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,CAAC;CAClC;AAED;;GAEG;AACH,QAAA,MAAM,iBAAiB,GAAI,oFAQxB,sBAAsB,gCAqMxB,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
1
+ {"version":3,"file":"GeolocationAnchor.d.ts","sourceRoot":"","sources":["../../src/geolocation/GeolocationAnchor.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAC/C,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AA8BtE,MAAM,WAAW,sBACf,SAAQ,IAAI,CAAC,yBAAyB,EAAE,aAAa,CAAC,EACpD,iBAAiB;IACnB,wFAAwF;IACxF,QAAQ,EAAE,MAAM,CAAC;IACjB,yFAAyF;IACzF,SAAS,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;CACvB;AAED;;GAEG;AACH,QAAA,MAAM,iBAAiB,GAAI,2GAQxB,sBAAsB,gCAwMxB,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
@@ -14,7 +14,7 @@ export interface GeolocationSessionOptions {
14
14
  * @param position Updated GPS position.
15
15
  * @param distanceMoved Distance moved, in meters, since the last update.
16
16
  */
17
- onGpsUpdated?: (position: GeolocationPosition, distanceMoved: number) => void;
17
+ onGpsUpdate?: (position: GeolocationPosition, distanceMoved: number) => void;
18
18
  }
19
19
  /**
20
20
  * Create a location-based AR backend.
@@ -1 +1 @@
1
- {"version":3,"file":"geolocationBackend.d.ts","sourceRoot":"","sources":["../../src/geolocation/geolocationBackend.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAmB,MAAM,kBAAkB,CAAC;AAGjE;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC,2EAA2E;IAC3E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4EAA4E;IAC5E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iCAAiC;IACjC,iBAAiB,CAAC,EAAE,sBAAsB,CAAC;IAC3C;;;;OAIG;IAEH,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,KAAK,IAAI,CAAC;CAC/E;AAED;;GAEG;AACH,QAAA,MAAM,wBAAwB,GAC5B,SAAS,yBAAyB,KACjC,OA+KF,CAAC;AAEF,eAAe,wBAAwB,CAAC"}
1
+ {"version":3,"file":"geolocationBackend.d.ts","sourceRoot":"","sources":["../../src/geolocation/geolocationBackend.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAmB,MAAM,kBAAkB,CAAC;AAGjE;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC,2EAA2E;IAC3E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4EAA4E;IAC5E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iCAAiC;IACjC,iBAAiB,CAAC,EAAE,sBAAsB,CAAC;IAC3C;;;;OAIG;IAEH,WAAW,CAAC,EAAE,CAAC,QAAQ,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9E;AAED;;GAEG;AACH,QAAA,MAAM,wBAAwB,GAC5B,SAAS,yBAAyB,KACjC,OA+KF,CAAC;AAEF,eAAe,wBAAwB,CAAC"}
package/build/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index-Dh1EpLV3.cjs");require("@react-three/fiber");exports.FiducialAnchor=e.FiducialAnchor;exports.FiducialSession=e.FiducialSession;exports.GeolocationAnchor=e.GeolocationAnchor;exports.GeolocationSession=e.GeolocationSession;exports.ImmersiveSession=e.ImmersiveSession;exports.XR=e.XR;exports.createFiducialBackend=e.createFiducialBackend;exports.createGeolocationBackend=e.createGeolocationBackend;exports.getXRStore=e.getXRStore;exports.subscribeToXRStore=e.subscribeToXRStore;exports.useXRStore=e.useXRStore;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("react"),v=require("@react-three/fiber"),L=require("react/jsx-runtime"),O=require("@ar-js-org/ar.js/three.js/build/ar-threex"),N=require("three"),H=require("locar"),G=require("@react-three/xr");var B=typeof document<"u"?document.currentScript:null;function W(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const o=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,o.get?o:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const F=W(H),j=e=>{let t;const n=new Set,o=(a,l)=>{const d=typeof a=="function"?a(t):a;if(!Object.is(d,t)){const c=t;t=l??(typeof d!="object"||d===null)?d:Object.assign({},t,d),n.forEach(m=>m(t,c))}},A=()=>t,r={setState:o,getState:A,getInitialState:()=>u,subscribe:a=>(n.add(a),()=>n.delete(a))},u=t=e(o,A,r);return r},Q=(e=>e?j(e):j),V=e=>e;function Y(e,t=V){const n=f.useSyncExternalStore(e.subscribe,f.useCallback(()=>t(e.getState()),[e,t]),f.useCallback(()=>t(e.getInitialState()),[e,t]));return f.useDebugValue(n),n}const K=e=>{const t=Q(e),n=o=>Y(t,o);return Object.assign(n,t),n},$=(e=>K),J=e=>(t,n,o)=>{const A=o.subscribe;return o.subscribe=((i,r,u)=>{let a=i;if(r){const l=u?.equalityFn||Object.is;let d=i(o.getState());a=c=>{const m=i(c);if(!l(d,m)){const g=d;r(d=m,g)}},u?.fireImmediately&&r(d,d)}return A(a)}),e(t,n,o)},Z=J,p={FIDUCIAL:"FiducialSession",GEOLOCATION:"GeolocationSession",IMMERSIVE:"ImmersiveSession"},M=$()(Z((e,t)=>({video:null,backends:[],sessionTypes:new Set,immersiveStore:null,registerBackend:async(n,o,A)=>{try{if(A){const s=t(),i=new Set([...s.sessionTypes,A]),r=i.has(p.FIDUCIAL),u=i.has(p.GEOLOCATION);if(r&&u){const a=`❌ [RDK] INCOMPATIBLE SESSIONS: ${p.FIDUCIAL} and ${p.GEOLOCATION} cannot be used together due to camera/video conflicts between AR.js and LocAR.js libraries. Use only one session type per app.`;throw console.error(a),new Error(a)}e(()=>({sessionTypes:i}))}await n.init({scene:o.scene,camera:o.camera,renderer:o.renderer}),e(s=>({backends:[...s.backends,n]}))}catch(s){throw console.error("[XRStore] Failed to register backend:",s),s}},unregisterBackend:(n,o)=>{e(A=>{const s=new Set(A.sessionTypes);o&&s.delete(o);const i=A.backends.filter(r=>r!==n);try{n.dispose?.()}catch(r){console.error("[XRStore] Error disposing backend:",r)}return{backends:i,sessionTypes:s}})},setVideo:n=>{e({video:n})},updateBackends:n=>{const{backends:o}=t();o.forEach(A=>{try{A.update?.(n)}catch(s){console.error("[XRStore] Backend update error:",s)}})},setImmersiveStore:n=>{e({immersiveStore:n})}}))),P=()=>M.getState(),ee=M.subscribe,E=e=>{const t=M(),n={...t,isImmersive:t.sessionTypes.has(p.IMMERSIVE),immersive:t.immersiveStore||null};return e?e(n):n},te=({children:e})=>{const{updateBackends:t}=E();return v.useFrame((n,o)=>{t(o)}),e},ne=({patternUrl:e,barcodeValue:t,params:n,onMarkerFound:o,onMarkerLost:A,children:s})=>{const i=f.useRef(null),r=E(d=>d.backends),u=f.useRef(!1),a=f.useRef(!1),l=f.useRef(null);return f.useEffect(()=>{let d=!1;return(async()=>{try{const m=r.find(w=>w.getInternal?.()?.arContext);if(!m||d)return;const k=m.getInternal?.()?.arContext,y=i.current;if(!k||!y)return;if(y.visible=!1,a.current=!1,!k.arController){requestAnimationFrame(()=>{if(!i.current||d)return;const b=m.getInternal?.()?.arContext;if(!b?.arController)return;const h=new O.ArMarkerControls(b,i.current,{type:e?"pattern":t?"barcode":"unknown",patternUrl:e,barcodeValue:t,...n});l.current=h,a.current=!0});return}const I=new O.ArMarkerControls(k,y,{type:e?"pattern":t?"barcode":"unknown",patternUrl:e,barcodeValue:t,...n});l.current=I,a.current=!0}catch(m){console.error("Failed to initialize fiducial backend:",m)}})(),()=>{d=!0;const m=l.current;typeof m?.dispose=="function"&&m.dispose(),a.current=!1}},[r,e,t,n]),v.useFrame(()=>{const d=i.current;if(!d||!a.current){d&&(d.visible=!1);return}const c=d.visible;c&&!u.current?(u.current=!0,o?.()):!c&&u.current&&(u.current=!1,A?.())}),L.jsx("group",{ref:i,visible:!1,children:s})},U=({options:e={},children:t})=>{const{scene:n,camera:o,gl:A}=v.useThree(),{registerBackend:s,unregisterBackend:i}=E(),r=f.useRef(null);return f.useEffect(()=>{if(r.current)return;let u=!1;return(async()=>{try{const l=q(e);if(u)return;await s(l,{scene:n,camera:o,renderer:A},p.FIDUCIAL),r.current=l}catch(l){console.error("[FiducialSession] Failed to initialize:",l)}})(),()=>{u=!0,r.current&&(i(r.current,p.FIDUCIAL),r.current=null)}},[n,o,A,s,i,e]),t};U.sessionType="FiducialSession";const q=e=>{let t,n,o;return{async init({camera:s,renderer:i}){t=new O.ArToolkitSource({sourceType:e.sourceType??"webcam"}),await new Promise((a,l)=>{const d=setTimeout(()=>{l(new Error("AR source initialization timeout"))},1e4);t.init(()=>{clearTimeout(d);const c=t.domElement;c&&(c.style.position="fixed",c.style.top="0",c.style.left="0",c.style.width="100vw",c.style.height="100vh",c.style.objectFit="cover",c.style.zIndex="-1",c.style.pointerEvents="none"),a()})});const r={cameraParametersUrl:e.cameraParametersUrl??new URL("data:application/octet-stream;base64,AAACgAAAAeBAgwrsW6bUSwAAAAAAAAAAQHQ3KqAAAAAAAAAAAAAAAAAAAAAAAAAAQIL0K3dHyf9AbbNowAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/wAAAAAAAAAAAAAAAAAAA/uWNa4AAAAL+3lTLAAAAAv17YFWAAAAA/VYLXIAAAAECCe0YgAAAAQIJlMOAAAABAdDcqoAAAAEBts2jAAAAAP+8OmzqkDy4=",typeof document>"u"?require("url").pathToFileURL(__filename).href:B&&B.tagName.toUpperCase()==="SCRIPT"&&B.src||new URL("index.cjs",document.baseURI).href).toString(),detectionMode:e.detectionMode??"mono",patternRatio:e.patternRatio??.5,matrixCodeType:e.matrixCodeType??"3x3"};n=new O.ArToolkitContext(r);const u=()=>{t.onResizeElement();const a=window.innerWidth,l=window.innerHeight;i.setSize(a,l,!1),t.copyElementSizeTo(i.domElement),n?.arController&&t.copyElementSizeTo(n.arController.canvas)};await new Promise((a,l)=>{const d=setTimeout(()=>{l(new Error("AR context initialization timeout"))},1e4);n.init(()=>{clearTimeout(d);try{s.projectionMatrix.copy(n.getProjectionMatrix()),u(),a()}catch(c){l(c)}})}),this._arSource=t,this._arContext=n,this._camera=s,window.addEventListener("resize",u),o=u},update(){const s=this._arSource,i=this._arContext,r=this._camera;if(!(!s||!i)&&s.ready!==!1&&s.domElement)try{i.update(s.domElement),r&&i.getProjectionMatrix&&r.projectionMatrix.copy(i.getProjectionMatrix())}catch(u){console.warn("AR.js update error:",u)}},dispose(){o&&(window.removeEventListener("resize",o),o=void 0);const s=this._arSource,i=this._arContext;if(i&&typeof i.dispose=="function"&&i.dispose(),s?.domElement){const r=s.domElement.parentNode;r&&r.removeChild(s.domElement)}this._arSource=void 0,this._arContext=void 0},getInternal(){return{arSource:this._arSource,arContext:this._arContext}}}},R=new Map;let z=!1,x=null,C=null;const re=({latitude:e,longitude:t,altitude:n=0,isBillboard:o=!0,onAttach:A,onGpsUpdate:s,children:i})=>{const r=E(g=>g.backends),{camera:u}=v.useThree(),[a]=f.useState(()=>new N.Group),[l]=f.useState(()=>Math.random().toString(36).slice(2,11)),d=f.useRef(!1),c=f.useCallback(()=>{A?.()},[A]),m=f.useCallback((g,k)=>{s?.(g,k)},[s]);return f.useEffect(()=>{let g=!1;return(async()=>{try{const y=r.find(S=>S.getInternal?.()?.locar);if(!y||g)return;const w=y.getInternal?.()?.locar;if(!w)return;const b=S=>{try{w.add(S.anchor,S.longitude,S.latitude,S.altitude),S.isAttached=!0,S.onAttach?.()}catch(T){console.error("❌ Failed to attach anchor:",T)}},h={anchor:a,isAttached:!1,latitude:e,longitude:t,altitude:n||0,onAttach:c,onGpsUpdate:m};R.set(l,h),z||(C=({position:S,distMoved:T})=>{x=S,R.forEach(_=>{_.isAttached||b(_),_.onGpsUpdate?.(S,T)})},x!==null&&!h.isAttached&&b(h),w.on?.("gpsupdate",C),z=!0),x!==null&&(h.isAttached||b(h),h.onGpsUpdate?.(x,0)),d.current=!1}catch(y){console.error("Failed to initialize geolocation backend:",y)}})(),()=>{if(g=!0,R.get(l)?.isAttached)try{const b=r.find(h=>h.getInternal?.()?.locar)?.getInternal?.()?.locar;typeof b?.remove=="function"?b.remove(a):a.removeFromParent()}catch(I){console.error(`⚠️ Error removing anchor ${l}:`,I)}R.delete(l),R.size===0&&C&&(r.find(h=>h.getInternal?.()?.locar)?.getInternal?.()?.locar?.off?.("gpsupdate",C),C=null,z=!1)}},[r,a,l,e,t,n,c,m]),v.useFrame(()=>{!o||!u||!R.get(l)?.isAttached||a.lookAt(u.position)}),v.createPortal(L.jsx("group",{children:i}),a)},X=({options:e={},children:t})=>{const{scene:n,camera:o,gl:A}=v.useThree(),{registerBackend:s,unregisterBackend:i}=E(),r=f.useRef(null);return f.useEffect(()=>{if(r.current)return;let u=!1;return(async()=>{try{const l=D(e);if(u)return;await s(l,{scene:n,camera:o,renderer:A},p.GEOLOCATION),r.current=l}catch(l){console.error("[GeolocationSession] Failed to initialize:",l)}})(),()=>{u=!0,r.current&&(i(r.current,p.GEOLOCATION),r.current=null)}},[n,o,A,s,i,e]),t};X.sessionType="GeolocationSession";const D=e=>{let t,n,o,A,s=null,i=null;return{async init(r){const{camera:u,renderer:a,scene:l}=r;if(!l)throw new Error("[geolocationBackend] A THREE.Scene is required for location-based AR");s=u,i=a,t=new F.LocationBased(l,u),n=new F.Webcam(e.webcamConstraints??{video:{facingMode:"environment"}},null),t.on("gpsupdate",c=>{e.onGpsUpdate?.(c.position,c.distMoved)}),n.on("webcamstarted",c=>{l.background=c.texture}),n.on("webcamerror",c=>{console.error("[geolocationBackend] webcam error:",c)}),o=new F.DeviceOrientationControls(u),o.on("deviceorientationgranted",c=>{c.target?.connect?.()}),o.on("deviceorientationerror",c=>{console.error("[geolocationBackend] Device orientation error:",c)}),o.init?.(),t.on("gpserror",c=>{console.error("[geolocationBackend] gps error:",c)}),t.startGps(),typeof e.fakeLat=="number"&&typeof e.fakeLon=="number"&&t.fakeGps(e.fakeLon,e.fakeLat);const d=()=>{if(!i||!s)return;const c=window.innerWidth,m=window.innerHeight;i.setSize(c,m,!1),s.aspect=c/m,s.updateProjectionMatrix?.()};d(),window.addEventListener("resize",d),A=d,this._locar=t,this._webcam=n,this._deviceOrientation=o,this._scene=l,this._camera=u},update(){this._deviceOrientation?.update?.()},dispose(){const r=this._locar,u=this._webcam,a=this._deviceOrientation;e?.onGpsUpdate&&r?.off("gpsupdate",e.onGpsUpdate),r?.stopGps?.(),u?.stop?.(),a?.dispose?.(),A&&(window.removeEventListener("resize",A),A=void 0),this._locar=null,this._webcam=null,this._deviceOrientation=null,this._scene=null,this._camera=null},getInternal(){return{locar:this._locar,webcam:this._webcam,deviceOrientation:this._deviceOrientation,scene:this._scene,camera:this._camera}}}},oe=({children:e})=>{const t=f.useMemo(()=>G.createXRStore(),[]);return f.useEffect(()=>{const n=P();return n.sessionTypes.add(p.IMMERSIVE),n.setImmersiveStore(t),()=>{n.sessionTypes.delete(p.IMMERSIVE),n.setImmersiveStore(null)}},[t]),L.jsx(G.XR,{store:t,children:e})};exports.FiducialAnchor=ne;exports.FiducialSession=U;exports.GeolocationAnchor=re;exports.GeolocationSession=X;exports.ImmersiveSession=oe;exports.XR=te;exports.createFiducialBackend=q;exports.createGeolocationBackend=D;exports.getXRStore=P;exports.subscribeToXRStore=ee;exports.useXRStore=E;
package/build/index.js CHANGED
@@ -1,15 +1,468 @@
1
- import { F as o, f as i, G as c, i as r, I as t, X as n, h as l, k as u, e as S, s as d, u as X } from "./index-BsJaHBDm.js";
2
- import "@react-three/fiber";
1
+ import T, { useRef as y, useEffect as C, useState as j, useCallback as P, useMemo as X } from "react";
2
+ import { useFrame as F, useThree as L, createPortal as N } from "@react-three/fiber";
3
+ import { jsx as M } from "react/jsx-runtime";
4
+ import { ArMarkerControls as D, ArToolkitSource as H, ArToolkitContext as W } from "@ar-js-org/ar.js/three.js/build/ar-threex";
5
+ import { Group as q } from "three";
6
+ import * as B from "locar";
7
+ import { createXRStore as Q, XR as V } from "@react-three/xr";
8
+ const U = (e) => {
9
+ let t;
10
+ const n = /* @__PURE__ */ new Set(), o = (a, l) => {
11
+ const d = typeof a == "function" ? a(t) : a;
12
+ if (!Object.is(d, t)) {
13
+ const c = t;
14
+ t = l ?? (typeof d != "object" || d === null) ? d : Object.assign({}, t, d), n.forEach((f) => f(t, c));
15
+ }
16
+ }, A = () => t, r = { setState: o, getState: A, getInitialState: () => u, subscribe: (a) => (n.add(a), () => n.delete(a)) }, u = t = e(o, A, r);
17
+ return r;
18
+ }, Y = ((e) => e ? U(e) : U), $ = (e) => e;
19
+ function K(e, t = $) {
20
+ const n = T.useSyncExternalStore(
21
+ e.subscribe,
22
+ T.useCallback(() => t(e.getState()), [e, t]),
23
+ T.useCallback(() => t(e.getInitialState()), [e, t])
24
+ );
25
+ return T.useDebugValue(n), n;
26
+ }
27
+ const J = (e) => {
28
+ const t = Y(e), n = (o) => K(t, o);
29
+ return Object.assign(n, t), n;
30
+ }, Z = ((e) => J), ee = (e) => (t, n, o) => {
31
+ const A = o.subscribe;
32
+ return o.subscribe = ((s, r, u) => {
33
+ let a = s;
34
+ if (r) {
35
+ const l = u?.equalityFn || Object.is;
36
+ let d = s(o.getState());
37
+ a = (c) => {
38
+ const f = s(c);
39
+ if (!l(d, f)) {
40
+ const g = d;
41
+ r(d = f, g);
42
+ }
43
+ }, u?.fireImmediately && r(d, d);
44
+ }
45
+ return A(a);
46
+ }), e(t, n, o);
47
+ }, te = ee, p = {
48
+ FIDUCIAL: "FiducialSession",
49
+ GEOLOCATION: "GeolocationSession",
50
+ IMMERSIVE: "ImmersiveSession"
51
+ }, G = Z()(
52
+ te((e, t) => ({
53
+ // initial state
54
+ video: null,
55
+ backends: [],
56
+ sessionTypes: /* @__PURE__ */ new Set(),
57
+ immersiveStore: null,
58
+ // actions
59
+ registerBackend: async (n, o, A) => {
60
+ try {
61
+ if (A) {
62
+ const i = t(), s = /* @__PURE__ */ new Set([...i.sessionTypes, A]), r = s.has(p.FIDUCIAL), u = s.has(p.GEOLOCATION);
63
+ if (r && u) {
64
+ const a = `❌ [RDK] INCOMPATIBLE SESSIONS: ${p.FIDUCIAL} and ${p.GEOLOCATION} cannot be used together due to camera/video conflicts between AR.js and LocAR.js libraries. Use only one session type per app.`;
65
+ throw console.error(a), new Error(a);
66
+ }
67
+ e(() => ({
68
+ sessionTypes: s
69
+ }));
70
+ }
71
+ await n.init({
72
+ scene: o.scene,
73
+ camera: o.camera,
74
+ renderer: o.renderer
75
+ }), e((i) => ({
76
+ backends: [...i.backends, n]
77
+ }));
78
+ } catch (i) {
79
+ throw console.error("[XRStore] Failed to register backend:", i), i;
80
+ }
81
+ },
82
+ unregisterBackend: (n, o) => {
83
+ e((A) => {
84
+ const i = new Set(A.sessionTypes);
85
+ o && i.delete(o);
86
+ const s = A.backends.filter((r) => r !== n);
87
+ try {
88
+ n.dispose?.();
89
+ } catch (r) {
90
+ console.error("[XRStore] Error disposing backend:", r);
91
+ }
92
+ return {
93
+ backends: s,
94
+ sessionTypes: i
95
+ };
96
+ });
97
+ },
98
+ setVideo: (n) => {
99
+ e({ video: n });
100
+ },
101
+ updateBackends: (n) => {
102
+ const { backends: o } = t();
103
+ o.forEach((A) => {
104
+ try {
105
+ A.update?.(n);
106
+ } catch (i) {
107
+ console.error("[XRStore] Backend update error:", i);
108
+ }
109
+ });
110
+ },
111
+ setImmersiveStore: (n) => {
112
+ e({ immersiveStore: n });
113
+ }
114
+ }))
115
+ ), ne = () => G.getState(), fe = G.subscribe, R = (e) => {
116
+ const t = G(), n = {
117
+ ...t,
118
+ isImmersive: t.sessionTypes.has(p.IMMERSIVE),
119
+ immersive: t.immersiveStore || null
120
+ };
121
+ return e ? e(n) : n;
122
+ }, me = ({ children: e }) => {
123
+ const { updateBackends: t } = R();
124
+ return F((n, o) => {
125
+ t(o);
126
+ }), e;
127
+ }, Se = ({
128
+ patternUrl: e,
129
+ barcodeValue: t,
130
+ params: n,
131
+ onMarkerFound: o,
132
+ onMarkerLost: A,
133
+ children: i
134
+ }) => {
135
+ const s = y(null), r = R((d) => d.backends), u = y(!1), a = y(!1), l = y(null);
136
+ return C(() => {
137
+ let d = !1;
138
+ return (async () => {
139
+ try {
140
+ const f = r.find((w) => w.getInternal?.()?.arContext);
141
+ if (!f || d) return;
142
+ const v = f.getInternal?.()?.arContext, b = s.current;
143
+ if (!v || !b) return;
144
+ if (b.visible = !1, a.current = !1, !v.arController) {
145
+ requestAnimationFrame(() => {
146
+ if (!s.current || d) return;
147
+ const h = f.getInternal?.()?.arContext;
148
+ if (!h?.arController) return;
149
+ const S = new D(h, s.current, {
150
+ type: e ? "pattern" : t ? "barcode" : "unknown",
151
+ patternUrl: e,
152
+ barcodeValue: t,
153
+ ...n
154
+ });
155
+ l.current = S, a.current = !0;
156
+ });
157
+ return;
158
+ }
159
+ const I = new D(v, b, {
160
+ type: e ? "pattern" : t ? "barcode" : "unknown",
161
+ patternUrl: e,
162
+ barcodeValue: t,
163
+ ...n
164
+ });
165
+ l.current = I, a.current = !0;
166
+ } catch (f) {
167
+ console.error("Failed to initialize fiducial backend:", f);
168
+ }
169
+ })(), () => {
170
+ d = !0;
171
+ const f = l.current;
172
+ typeof f?.dispose == "function" && f.dispose(), a.current = !1;
173
+ };
174
+ }, [r, e, t, n]), F(() => {
175
+ const d = s.current;
176
+ if (!d || !a.current) {
177
+ d && (d.visible = !1);
178
+ return;
179
+ }
180
+ const c = d.visible;
181
+ c && !u.current ? (u.current = !0, o?.()) : !c && u.current && (u.current = !1, A?.());
182
+ }), /* @__PURE__ */ M(
183
+ "group",
184
+ {
185
+ ref: s,
186
+ visible: !1,
187
+ children: i
188
+ }
189
+ );
190
+ }, re = ({ options: e = {}, children: t }) => {
191
+ const { scene: n, camera: o, gl: A } = L(), { registerBackend: i, unregisterBackend: s } = R(), r = y(null);
192
+ return C(() => {
193
+ if (r.current) return;
194
+ let u = !1;
195
+ return (async () => {
196
+ try {
197
+ const l = oe(e);
198
+ if (u) return;
199
+ await i(
200
+ l,
201
+ { scene: n, camera: o, renderer: A },
202
+ p.FIDUCIAL
203
+ ), r.current = l;
204
+ } catch (l) {
205
+ console.error("[FiducialSession] Failed to initialize:", l);
206
+ }
207
+ })(), () => {
208
+ u = !0, r.current && (s(r.current, p.FIDUCIAL), r.current = null);
209
+ };
210
+ }, [n, o, A, i, s, e]), t;
211
+ };
212
+ re.sessionType = "FiducialSession";
213
+ const oe = (e) => {
214
+ let t, n, o;
215
+ return {
216
+ async init({ camera: i, renderer: s }) {
217
+ t = new H({
218
+ sourceType: e.sourceType ?? "webcam"
219
+ }), await new Promise((a, l) => {
220
+ const d = setTimeout(() => {
221
+ l(new Error("AR source initialization timeout"));
222
+ }, 1e4);
223
+ t.init(() => {
224
+ clearTimeout(d);
225
+ const c = t.domElement;
226
+ c && (c.style.position = "fixed", c.style.top = "0", c.style.left = "0", c.style.width = "100vw", c.style.height = "100vh", c.style.objectFit = "cover", c.style.zIndex = "-1", c.style.pointerEvents = "none"), a();
227
+ });
228
+ });
229
+ const r = {
230
+ cameraParametersUrl: e.cameraParametersUrl ?? // default to internal camera parameters
231
+ new URL("data:application/octet-stream;base64,AAACgAAAAeBAgwrsW6bUSwAAAAAAAAAAQHQ3KqAAAAAAAAAAAAAAAAAAAAAAAAAAQIL0K3dHyf9AbbNowAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/wAAAAAAAAAAAAAAAAAAA/uWNa4AAAAL+3lTLAAAAAv17YFWAAAAA/VYLXIAAAAECCe0YgAAAAQIJlMOAAAABAdDcqoAAAAEBts2jAAAAAP+8OmzqkDy4=", import.meta.url).toString(),
232
+ detectionMode: e.detectionMode ?? "mono",
233
+ patternRatio: e.patternRatio ?? 0.5,
234
+ matrixCodeType: e.matrixCodeType ?? "3x3"
235
+ };
236
+ n = new W(r);
237
+ const u = () => {
238
+ t.onResizeElement();
239
+ const a = window.innerWidth, l = window.innerHeight;
240
+ s.setSize(a, l, !1), t.copyElementSizeTo(s.domElement), n?.arController && t.copyElementSizeTo(n.arController.canvas);
241
+ };
242
+ await new Promise((a, l) => {
243
+ const d = setTimeout(() => {
244
+ l(new Error("AR context initialization timeout"));
245
+ }, 1e4);
246
+ n.init(() => {
247
+ clearTimeout(d);
248
+ try {
249
+ i.projectionMatrix.copy(n.getProjectionMatrix()), u(), a();
250
+ } catch (c) {
251
+ l(c);
252
+ }
253
+ });
254
+ }), this._arSource = t, this._arContext = n, this._camera = i, window.addEventListener("resize", u), o = u;
255
+ },
256
+ update() {
257
+ const i = this._arSource, s = this._arContext, r = this._camera;
258
+ if (!(!i || !s) && i.ready !== !1 && i.domElement)
259
+ try {
260
+ s.update(i.domElement), r && s.getProjectionMatrix && r.projectionMatrix.copy(s.getProjectionMatrix());
261
+ } catch (u) {
262
+ console.warn("AR.js update error:", u);
263
+ }
264
+ },
265
+ dispose() {
266
+ o && (window.removeEventListener("resize", o), o = void 0);
267
+ const i = this._arSource, s = this._arContext;
268
+ if (s && typeof s.dispose == "function" && s.dispose(), i?.domElement) {
269
+ const r = i.domElement.parentNode;
270
+ r && r.removeChild(i.domElement);
271
+ }
272
+ this._arSource = void 0, this._arContext = void 0;
273
+ },
274
+ getInternal() {
275
+ return {
276
+ arSource: this._arSource,
277
+ arContext: this._arContext
278
+ };
279
+ }
280
+ };
281
+ }, k = /* @__PURE__ */ new Map();
282
+ let z = !1, O = null, E = null;
283
+ const he = ({
284
+ latitude: e,
285
+ longitude: t,
286
+ altitude: n = 0,
287
+ isBillboard: o = !0,
288
+ onAttach: A,
289
+ onGpsUpdate: i,
290
+ children: s
291
+ }) => {
292
+ const r = R((g) => g.backends), { camera: u } = L(), [a] = j(() => new q()), [l] = j(() => Math.random().toString(36).slice(2, 11)), d = y(!1), c = P(() => {
293
+ A?.();
294
+ }, [A]), f = P(
295
+ (g, v) => {
296
+ i?.(g, v);
297
+ },
298
+ [i]
299
+ );
300
+ return C(() => {
301
+ let g = !1;
302
+ return (async () => {
303
+ try {
304
+ const b = r.find((m) => m.getInternal?.()?.locar);
305
+ if (!b || g) return;
306
+ const w = b.getInternal?.()?.locar;
307
+ if (!w) return;
308
+ const h = (m) => {
309
+ try {
310
+ w.add(
311
+ m.anchor,
312
+ m.longitude,
313
+ m.latitude,
314
+ m.altitude
315
+ ), m.isAttached = !0, m.onAttach?.();
316
+ } catch (x) {
317
+ console.error("❌ Failed to attach anchor:", x);
318
+ }
319
+ }, S = {
320
+ anchor: a,
321
+ isAttached: !1,
322
+ latitude: e,
323
+ longitude: t,
324
+ altitude: n || 0,
325
+ onAttach: c,
326
+ onGpsUpdate: f
327
+ };
328
+ k.set(l, S), z || (E = ({
329
+ position: m,
330
+ distMoved: x
331
+ }) => {
332
+ O = m, k.forEach((_) => {
333
+ _.isAttached || h(_), _.onGpsUpdate?.(m, x);
334
+ });
335
+ }, O !== null && !S.isAttached && h(S), w.on?.("gpsupdate", E), z = !0), O !== null && (S.isAttached || h(S), S.onGpsUpdate?.(O, 0)), d.current = !1;
336
+ } catch (b) {
337
+ console.error("Failed to initialize geolocation backend:", b);
338
+ }
339
+ })(), () => {
340
+ if (g = !0, k.get(l)?.isAttached)
341
+ try {
342
+ const h = r.find((S) => S.getInternal?.()?.locar)?.getInternal?.()?.locar;
343
+ typeof h?.remove == "function" ? h.remove(a) : a.removeFromParent();
344
+ } catch (I) {
345
+ console.error(`⚠️ Error removing anchor ${l}:`, I);
346
+ }
347
+ k.delete(l), k.size === 0 && E && (r.find((S) => S.getInternal?.()?.locar)?.getInternal?.()?.locar?.off?.("gpsupdate", E), E = null, z = !1);
348
+ };
349
+ }, [
350
+ r,
351
+ a,
352
+ l,
353
+ e,
354
+ t,
355
+ n,
356
+ c,
357
+ f
358
+ ]), F(() => {
359
+ !o || !u || !k.get(l)?.isAttached || a.lookAt(u.position);
360
+ }), N(/* @__PURE__ */ M("group", { children: s }), a);
361
+ }, ie = ({
362
+ options: e = {},
363
+ children: t
364
+ }) => {
365
+ const { scene: n, camera: o, gl: A } = L(), { registerBackend: i, unregisterBackend: s } = R(), r = y(null);
366
+ return C(() => {
367
+ if (r.current) return;
368
+ let u = !1;
369
+ return (async () => {
370
+ try {
371
+ const l = se(e);
372
+ if (u) return;
373
+ await i(
374
+ l,
375
+ { scene: n, camera: o, renderer: A },
376
+ p.GEOLOCATION
377
+ ), r.current = l;
378
+ } catch (l) {
379
+ console.error("[GeolocationSession] Failed to initialize:", l);
380
+ }
381
+ })(), () => {
382
+ u = !0, r.current && (s(r.current, p.GEOLOCATION), r.current = null);
383
+ };
384
+ }, [n, o, A, i, s, e]), t;
385
+ };
386
+ ie.sessionType = "GeolocationSession";
387
+ const se = (e) => {
388
+ let t, n, o, A, i = null, s = null;
389
+ return {
390
+ async init(r) {
391
+ const { camera: u, renderer: a, scene: l } = r;
392
+ if (!l)
393
+ throw new Error(
394
+ "[geolocationBackend] A THREE.Scene is required for location-based AR"
395
+ );
396
+ i = u, s = a, t = new B.LocationBased(l, u), n = new B.Webcam(
397
+ e.webcamConstraints ?? {
398
+ video: { facingMode: "environment" }
399
+ },
400
+ null
401
+ ), t.on(
402
+ "gpsupdate",
403
+ // TODO remove explicit type annotation once LocAR.js converted to TS (https://github.com/AR-js-org/locar.js/pull/27#issuecomment-3487422995)
404
+ (c) => {
405
+ e.onGpsUpdate?.(c.position, c.distMoved);
406
+ }
407
+ ), n.on("webcamstarted", (c) => {
408
+ l.background = c.texture;
409
+ }), n.on("webcamerror", (c) => {
410
+ console.error("[geolocationBackend] webcam error:", c);
411
+ }), o = new B.DeviceOrientationControls(u), o.on("deviceorientationgranted", (c) => {
412
+ c.target?.connect?.();
413
+ }), o.on("deviceorientationerror", (c) => {
414
+ console.error("[geolocationBackend] Device orientation error:", c);
415
+ }), o.init?.(), t.on("gpserror", (c) => {
416
+ console.error("[geolocationBackend] gps error:", c);
417
+ }), t.startGps(), typeof e.fakeLat == "number" && typeof e.fakeLon == "number" && t.fakeGps(e.fakeLon, e.fakeLat);
418
+ const d = () => {
419
+ if (!s || !i) return;
420
+ const c = window.innerWidth, f = window.innerHeight;
421
+ s.setSize(c, f, !1), i.aspect = c / f, i.updateProjectionMatrix?.();
422
+ };
423
+ d(), window.addEventListener("resize", d), A = d, this._locar = t, this._webcam = n, this._deviceOrientation = o, this._scene = l, this._camera = u;
424
+ },
425
+ update() {
426
+ this._deviceOrientation?.update?.();
427
+ },
428
+ dispose() {
429
+ const r = this._locar, u = this._webcam, a = this._deviceOrientation;
430
+ e?.onGpsUpdate && r?.off("gpsupdate", e.onGpsUpdate), r?.stopGps?.(), u?.stop?.(), a?.dispose?.(), A && (window.removeEventListener("resize", A), A = void 0), this._locar = null, this._webcam = null, this._deviceOrientation = null, this._scene = null, this._camera = null;
431
+ },
432
+ getInternal() {
433
+ return {
434
+ // biome-ignore lint/suspicious/noExplicitAny: TODO solve once LocAR.js converted to TS (https://github.com/AR-js-org/locar.js/pull/27#issuecomment-3487422995)
435
+ locar: this._locar,
436
+ // biome-ignore lint/suspicious/noExplicitAny: TODO solve once LocAR.js converted to TS (https://github.com/AR-js-org/locar.js/pull/27#issuecomment-3487422995)
437
+ webcam: this._webcam,
438
+ // biome-ignore lint/suspicious/noExplicitAny: TODO solve once LocAR.js converted to TS (https://github.com/AR-js-org/locar.js/pull/27#issuecomment-3487422995)
439
+ deviceOrientation: this._deviceOrientation,
440
+ // biome-ignore lint/suspicious/noExplicitAny: TODO solve once LocAR.js converted to TS (https://github.com/AR-js-org/locar.js/pull/27#issuecomment-3487422995)
441
+ scene: this._scene,
442
+ // biome-ignore lint/suspicious/noExplicitAny: TODO solve once LocAR.js converted to TS (https://github.com/AR-js-org/locar.js/pull/27#issuecomment-3487422995)
443
+ camera: this._camera
444
+ };
445
+ }
446
+ };
447
+ }, pe = ({ children: e }) => {
448
+ const t = X(() => Q(), []);
449
+ return C(() => {
450
+ const n = ne();
451
+ return n.sessionTypes.add(p.IMMERSIVE), n.setImmersiveStore(t), () => {
452
+ n.sessionTypes.delete(p.IMMERSIVE), n.setImmersiveStore(null);
453
+ };
454
+ }, [t]), /* @__PURE__ */ M(V, { store: t, children: e });
455
+ };
3
456
  export {
4
- o as FiducialAnchor,
5
- i as FiducialSession,
6
- c as GeolocationAnchor,
7
- r as GeolocationSession,
8
- t as ImmersiveSession,
9
- n as XR,
10
- l as createFiducialBackend,
11
- u as createGeolocationBackend,
12
- S as getXRStore,
13
- d as subscribeToXRStore,
14
- X as useXRStore
457
+ Se as FiducialAnchor,
458
+ re as FiducialSession,
459
+ he as GeolocationAnchor,
460
+ ie as GeolocationSession,
461
+ pe as ImmersiveSession,
462
+ me as XR,
463
+ oe as createFiducialBackend,
464
+ se as createGeolocationBackend,
465
+ ne as getXRStore,
466
+ fe as subscribeToXRStore,
467
+ R as useXRStore
15
468
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@omnidotdev/rdk",
3
3
  "description": "AR.js with react-three-fiber",
4
- "version": "0.4.2",
4
+ "version": "0.5.0",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "main": "build/index.js",
@@ -46,11 +46,23 @@
46
46
  "peerDependencies": {
47
47
  "@ar-js-org/ar.js": "^3.4.7",
48
48
  "@react-three/fiber": "^9.4.0",
49
+ "@react-three/xr": "^6.6.28",
49
50
  "locar": "0.1.5",
50
51
  "react": "^19.2.0",
51
52
  "react-dom": "^19.2.0",
52
53
  "three": "^0.180.0"
53
54
  },
55
+ "peerDependenciesMeta": {
56
+ "@ar-js-org/ar.js": {
57
+ "optional": true
58
+ },
59
+ "@react-three/xr": {
60
+ "optional": true
61
+ },
62
+ "locar": {
63
+ "optional": true
64
+ }
65
+ },
54
66
  "devDependencies": {
55
67
  "@ar-js-org/ar.js": "^3.4.7",
56
68
  "@biomejs/biome": "^2.3.8",
@@ -67,7 +79,6 @@
67
79
  "react": "^19.2.0",
68
80
  "react-dom": "^19.2.0",
69
81
  "three": "^0.181.2",
70
- "ts-pattern": "^5.9.0",
71
82
  "typescript": "^5.9.3",
72
83
  "vite": "^7.2.4",
73
84
  "vite-plugin-dts": "^4.5.4",
@@ -75,7 +86,7 @@
75
86
  "vitest": "^4.0.14"
76
87
  },
77
88
  "dependencies": {
78
- "@react-three/xr": "^6.6.28",
89
+ "ts-pattern": "^5.9.0",
79
90
  "zustand": "^5.0.8"
80
91
  }
81
92
  }