@vsllabs/webgl-react
Version:
VSL-Labs Webgl implementation for translating text into 3D sign language
695 lines (694 loc) • 23.4 kB
JavaScript
import _, { useState as I, useCallback as C, useEffect as T } from "react";
var b = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}, H = {}, $ = {}, P = {};
Object.defineProperty(P, "__esModule", { value: !0 });
P.useUnityCanvasId = void 0;
var oe = _, ue = 0, le = "react-unity-webgl-canvas", ce = function(n) {
if (n.id !== void 0)
return n.id;
var o = (0, oe.useMemo)(function() {
return [le, ++ue].join("-");
}, []);
return o;
};
P.useUnityCanvasId = ce;
var F = {}, N = {};
Object.defineProperty(N, "__esModule", { value: !0 });
N.UnityLoaderStatus = void 0;
var Z;
(function(n) {
n.Idle = "Idle", n.Loading = "Loading", n.Loaded = "Loaded", n.Error = "Error";
})(Z || (N.UnityLoaderStatus = Z = {}));
var E = {};
Object.defineProperty(E, "__esModule", { value: !0 });
E.isBrowserEnvironment = void 0;
E.isBrowserEnvironment = typeof window < "u" && typeof document < "u";
var de = b && b.__awaiter || function(n, o, t, s) {
function a(e) {
return e instanceof t ? e : new t(function(i) {
i(e);
});
}
return new (t || (t = Promise))(function(e, i) {
function l(u) {
try {
r(s.next(u));
} catch (f) {
i(f);
}
}
function c(u) {
try {
r(s.throw(u));
} catch (f) {
i(f);
}
}
function r(u) {
u.done ? e(u.value) : a(u.value).then(l, c);
}
r((s = s.apply(n, o || [])).next());
});
}, ve = b && b.__generator || function(n, o) {
var t = { label: 0, sent: function() {
if (e[0] & 1) throw e[1];
return e[1];
}, trys: [], ops: [] }, s, a, e, i = Object.create((typeof Iterator == "function" ? Iterator : Object).prototype);
return i.next = l(0), i.throw = l(1), i.return = l(2), typeof Symbol == "function" && (i[Symbol.iterator] = function() {
return this;
}), i;
function l(r) {
return function(u) {
return c([r, u]);
};
}
function c(r) {
if (s) throw new TypeError("Generator is already executing.");
for (; i && (i = 0, r[0] && (t = 0)), t; ) try {
if (s = 1, a && (e = r[0] & 2 ? a.return : r[0] ? a.throw || ((e = a.return) && e.call(a), 0) : a.next) && !(e = e.call(a, r[1])).done) return e;
switch (a = 0, e && (r = [r[0] & 2, e.value]), r[0]) {
case 0:
case 1:
e = r;
break;
case 4:
return t.label++, { value: r[1], done: !1 };
case 5:
t.label++, a = r[1], r = [0];
continue;
case 7:
r = t.ops.pop(), t.trys.pop();
continue;
default:
if (e = t.trys, !(e = e.length > 0 && e[e.length - 1]) && (r[0] === 6 || r[0] === 2)) {
t = 0;
continue;
}
if (r[0] === 3 && (!e || r[1] > e[0] && r[1] < e[3])) {
t.label = r[1];
break;
}
if (r[0] === 6 && t.label < e[1]) {
t.label = e[1], e = r;
break;
}
if (e && t.label < e[2]) {
t.label = e[2], t.ops.push(r);
break;
}
e[2] && t.ops.pop(), t.trys.pop();
continue;
}
r = o.call(n, t);
} catch (u) {
r = [6, u], a = 0;
} finally {
s = e = 0;
}
if (r[0] & 5) throw r[1];
return { value: r[0] ? r[1] : void 0, done: !0 };
}
};
Object.defineProperty(F, "__esModule", { value: !0 });
F.useUnityInstance = void 0;
var fe = _, ye = N, be = E, me = function(n, o, t, s) {
(0, fe.useEffect)(function() {
(function() {
return de(void 0, void 0, void 0, function() {
var a, e;
return ve(this, function(i) {
switch (i.label) {
case 0:
if (be.isBrowserEnvironment === !1)
return [
2
/*return*/
];
if (n !== ye.UnityLoaderStatus.Loaded || o === null)
return s.setUnityInstance(null), s.setInitialisationError(null), [
2
/*return*/
];
i.label = 1;
case 1:
return i.trys.push([1, 3, , 4]), [4, window.createUnityInstance(o, t, s.setLoadingProgression)];
case 2:
return a = i.sent(), s.setUnityInstance(a), s.setInitialisationError(null), [3, 4];
case 3:
return e = i.sent(), s.setUnityInstance(null), s.setInitialisationError(e), [3, 4];
case 4:
return [
2
/*return*/
];
}
});
});
})();
}, [n, o, t, s]);
};
F.useUnityInstance = me;
var G = {};
Object.defineProperty(G, "__esModule", { value: !0 });
G.useUnityArguments = void 0;
var _e = _, Ue = function(n) {
return (0, _e.useMemo)(function() {
return {
// Assigns the data URL, framework URL, and code URL to the Unity
// arguments object.
dataUrl: n.unityProvider.unityConfig.dataUrl,
frameworkUrl: n.unityProvider.unityConfig.frameworkUrl,
codeUrl: n.unityProvider.unityConfig.codeUrl,
// Assigns the optional streaming assets URL, memory URL, and symbols URL
// to the Unity arguments object.
streamingAssetsUrl: n.unityProvider.unityConfig.streamingAssetsUrl,
memoryUrl: n.unityProvider.unityConfig.memoryUrl,
symbolsUrl: n.unityProvider.unityConfig.symbolsUrl,
// Assigns the optional company name, product name, and product version to
// the Unity arguments object.
companyName: n.unityProvider.unityConfig.companyName,
productName: n.unityProvider.unityConfig.productName,
productVersion: n.unityProvider.unityConfig.productVersion,
// Assigns the webgl context attributes to the Unity arguments object.
// If the webgl context attributes are not defined via the Unity Props,
// the default value of an empty object will be used.
webglContextAttributes: n.unityProvider.unityConfig.webglContextAttributes || {},
// Assigns the cache control value to the Unity arguments object. If the
// cache control value is not defined via the Unity Props, the default
// value of always `must-revalidate` will be used.
cacheControl: n.unityProvider.unityConfig.cacheControl || function() {
return "must-revalidate";
},
// Assigns the device pixel ratio to the Unity arguments object. If the
// device pixel ratio is not defined via the Unity Props, the default
// value of `1` will be used.
devicePixelRatio: n.devicePixelRatio || 1,
// Assigns the match WebGL to canvas size value to the Unity arguments
// object. If the match WebGL to canvas size value is not defined via the
// Unity Props, the default value of `true` will be used.
matchWebGLToCanvasSize: typeof n.matchWebGLToCanvasSize == "boolean" ? n.matchWebGLToCanvasSize : !0,
// Assigns the disabled canvas events to the Unity arguments object. If
// the disabled canvas events are not defined via the Unity Props, the
// default value of `contextmenu` and `dragstart` will be used.
disabledCanvasEvents: n.disabledCanvasEvents || [
"contextmenu",
"dragstart"
],
// Assigns the print hook to the Unity arguments object. This hook will
// be called whenever the Unity instance prints a message.
print: (
/**
* Intercept print events in order to catch messages and send them to
* the unity context instead.
* @param message The message to be printed.
*/
function(o) {
}
),
// Assigns the print error hook to the Unity arguments object. This hook
// will be called whenever the Unity instance prints an error.
printErr: (
/**
* Intercept print error events in order to catch messages and send them
* to the unity context instead.
* @param error The error to be printed.
*/
function(o) {
}
)
};
}, []);
};
G.useUnityArguments = Ue;
var q = {};
Object.defineProperty(q, "__esModule", { value: !0 });
q.useUnityLoader = void 0;
var Y = _, ge = E, p = N, he = function(n) {
var o = (0, Y.useState)(p.UnityLoaderStatus.Loading), t = o[0], s = o[1];
return (0, Y.useEffect)(function() {
if (ge.isBrowserEnvironment !== !1) {
if (n.loaderUrl === null) {
s(p.UnityLoaderStatus.Idle);
return;
}
var a = window.document.querySelector('script[src="'.concat(n.loaderUrl, '"]'));
a === null ? (a = window.document.createElement("script"), a.type = "text/javascript", a.src = n.loaderUrl, a.async = !0, a.setAttribute("data-status", "loading"), window.document.body.appendChild(a), a.addEventListener("load", function() {
return a == null ? void 0 : a.setAttribute("data-status", "loaded");
}), a.addEventListener("error", function() {
return a == null ? void 0 : a.setAttribute("data-status", "error");
})) : s(a.getAttribute("data-status") === "loaded" ? p.UnityLoaderStatus.Loaded : p.UnityLoaderStatus.Error);
var e = function(i) {
return s(i.type === "load" ? p.UnityLoaderStatus.Loaded : p.UnityLoaderStatus.Error);
};
return a.addEventListener("load", e), a.addEventListener("error", e), function() {
a !== null && (a.removeEventListener("load", e), a.removeEventListener("error", e), window.document.body.removeChild(a));
};
}
}, [n.loaderUrl]), t;
};
q.useUnityLoader = he;
Object.defineProperty($, "__esModule", { value: !0 });
$.Unity = void 0;
var ee = _, te = _, we = P, Ie = F, Ce = G, pe = q, Le = (0, te.forwardRef)(
/**
* @param unityProps The Unity props provided the the Unity component.
* @param forwardedRef The forwarded ref to the Unity component.
* @returns The Unity canvas renderer.
*/
function(n, o) {
var t = (0, te.useRef)(null), s = (0, we.useUnityCanvasId)(n), a = (0, Ce.useUnityArguments)(n), e = (0, pe.useUnityLoader)(n.unityProvider.unityConfig);
return (0, Ie.useUnityInstance)(e, t.current, a, n.unityProvider), (0, ee.useImperativeHandle)(o, function() {
return t.current;
}), (0, ee.createElement)("canvas", {
ref: t,
id: s,
style: n.style,
className: n.className,
tabIndex: n.tabIndex
});
}
);
$.Unity = Le;
var R = {}, A = {};
Object.defineProperty(A, "__esModule", { value: !0 });
A.errorMessages = void 0;
var Ee = {
/**
* A generic error message when the Unity Instance is not available.
*/
genericNoUnityInstance: "No Unity Instance found.",
/**
* The error message for when no Unity Instance was found while trying to set
* the fullscreen mode.
*/
requestFullscreenNoUnityInstance: "Unable to Set Fullscreen while Unity is not Instantiated.",
/**
* The error message for when no Unity Instance was found while trying to
* request the pointer lock.
*/
requestPointerLockNoUnityInstanceOrCanvas: "Unable to Request Pointer Lock while Unity is not Instantiated or the Canvas is not found.",
/**
* The error message for when no Unity Instance was found while trying to send
* a message.
*/
sendMessageNoUnityInstance: "Unable to Send Message while Unity is not Instantiated.",
/**
* The error message for when no Unity Instance was found while trying to quit
* the Unity Instance.
*/
quitNoUnityInstance: "Unable to Quit Unity while Unity is not Instantiated.",
/**
* The error message for when no Unity Instance or Canvas was found while
* trying to take a screenshot.
*/
screenshotNoUnityInstanceOrCanvas: "Unable to Take Screenshot while Unity is not Instantiated or Canvas is not available.",
/**
* The error message for when no event listener was found in the event
* system.
*/
noEventListener: "Unable to find Event Listener in Event System for Event"
};
A.errorMessages = Ee;
var W = {}, J = b && b.__spreadArray || function(n, o, t) {
if (t || arguments.length === 2) for (var s = 0, a = o.length, e; s < a; s++)
(e || !(s in o)) && (e || (e = Array.prototype.slice.call(o, 0, s)), e[s] = o[s]);
return n.concat(e || Array.prototype.slice.call(o));
};
Object.defineProperty(W, "__esModule", { value: !0 });
W.useEventSystem = void 0;
var k = _, Se = A, Me = E, O = [], xe = function(n) {
for (var o = [], t = 1; t < arguments.length; t++)
o[t - 1] = arguments[t];
var s = void 0;
return O.forEach(function(a) {
s = a.apply(void 0, J([n], o, !1));
}), s;
};
Me.isBrowserEnvironment === !0 && (window.dispatchReactUnityEvent = xe);
var ke = function() {
var n = (0, k.useRef)([]), o = (0, k.useCallback)(
/**
* @param eventName The name of the event to listen to.
* @param callback The callback to invoke when the event is fired.
*/
function(a, e) {
n.current = J(J([], n.current, !0), [
{ eventName: a, callback: e }
], !1);
},
[n]
), t = (0, k.useCallback)(
/**
* @param eventName The name of the event to remove.
* @param callback The callback to remove.
*/
function(a, e) {
n.current = n.current.filter(function(i) {
return i.eventName !== a && i.callback !== e;
});
},
[n]
), s = (0, k.useCallback)(
/**
* @param eventName The name of the event to dispatch.
* @param parameters The parameters to pass to the event listener.
*/
function(a) {
for (var e = [], i = 1; i < arguments.length; i++)
e[i - 1] = arguments[i];
var l = n.current.find(function(c) {
return c.eventName === a;
});
if (typeof l > "u") {
console.warn(Se.errorMessages.noEventListener, { eventName: a });
return;
}
return l.callback.apply(l, e);
},
[n]
);
return (0, k.useEffect)(function() {
return O.push(s), function() {
O.splice(O.indexOf(s), 1);
};
}, [s]), {
addEventListener: o,
removeEventListener: t
};
};
W.useEventSystem = ke;
var B = {};
Object.defineProperty(B, "__esModule", { value: !0 });
B.useNullableState = void 0;
var Ne = _, Ae = function(n) {
return (0, Ne.useState)(n || null);
};
B.useNullableState = Ae;
var je = b && b.__awaiter || function(n, o, t, s) {
function a(e) {
return e instanceof t ? e : new t(function(i) {
i(e);
});
}
return new (t || (t = Promise))(function(e, i) {
function l(u) {
try {
r(s.next(u));
} catch (f) {
i(f);
}
}
function c(u) {
try {
r(s.throw(u));
} catch (f) {
i(f);
}
}
function r(u) {
u.done ? e(u.value) : a(u.value).then(l, c);
}
r((s = s.apply(n, o || [])).next());
});
}, Te = b && b.__generator || function(n, o) {
var t = { label: 0, sent: function() {
if (e[0] & 1) throw e[1];
return e[1];
}, trys: [], ops: [] }, s, a, e, i = Object.create((typeof Iterator == "function" ? Iterator : Object).prototype);
return i.next = l(0), i.throw = l(1), i.return = l(2), typeof Symbol == "function" && (i[Symbol.iterator] = function() {
return this;
}), i;
function l(r) {
return function(u) {
return c([r, u]);
};
}
function c(r) {
if (s) throw new TypeError("Generator is already executing.");
for (; i && (i = 0, r[0] && (t = 0)), t; ) try {
if (s = 1, a && (e = r[0] & 2 ? a.return : r[0] ? a.throw || ((e = a.return) && e.call(a), 0) : a.next) && !(e = e.call(a, r[1])).done) return e;
switch (a = 0, e && (r = [r[0] & 2, e.value]), r[0]) {
case 0:
case 1:
e = r;
break;
case 4:
return t.label++, { value: r[1], done: !1 };
case 5:
t.label++, a = r[1], r = [0];
continue;
case 7:
r = t.ops.pop(), t.trys.pop();
continue;
default:
if (e = t.trys, !(e = e.length > 0 && e[e.length - 1]) && (r[0] === 6 || r[0] === 2)) {
t = 0;
continue;
}
if (r[0] === 3 && (!e || r[1] > e[0] && r[1] < e[3])) {
t.label = r[1];
break;
}
if (r[0] === 6 && t.label < e[1]) {
t.label = e[1], e = r;
break;
}
if (e && t.label < e[2]) {
t.label = e[2], t.ops.push(r);
break;
}
e[2] && t.ops.pop(), t.trys.pop();
continue;
}
r = o.call(n, t);
} catch (u) {
r = [6, u], a = 0;
} finally {
s = e = 0;
}
if (r[0] & 5) throw r[1];
return { value: r[0] ? r[1] : void 0, done: !0 };
}
};
Object.defineProperty(R, "__esModule", { value: !0 });
R.useUnityContext = void 0;
var y = _, L = A, Oe = W, ne = B, $e = function(n) {
var o = (0, ne.useNullableState)(), t = o[0], s = o[1], a = (0, y.useState)(0), e = a[0], i = a[1], l = (0, y.useState)(!1), c = l[0], r = l[1], u = (0, ne.useNullableState)(), f = u[0], S = u[1], M = (0, Oe.useEventSystem)(), j = (0, y.useRef)({
setLoadingProgression: i,
setInitialisationError: S,
setUnityInstance: s,
setIsLoaded: r,
unityConfig: n
}), V = (0, y.useCallback)(
/**
* @param enabled Defines whether Unity should be in fullscreen.
*/
function(v) {
if (t === null) {
console.warn(L.errorMessages.requestFullscreenNoUnityInstance);
return;
}
t.SetFullscreen(v === !0 ? 1 : 0);
},
[t]
), Q = (0, y.useCallback)(function() {
if (t === null || typeof t.Module.canvas > "u") {
console.warn(L.errorMessages.requestPointerLockNoUnityInstanceOrCanvas);
return;
}
return t.Module.canvas.requestPointerLock();
}, [t]), g = (0, y.useCallback)(
/**
* @param gameObjectName the name of the game object in your Unity scene.
* @param methodName the name of the public method on the game object.
* @param parameter an optional parameter to pass along to the method.
*/
function(v, U, D) {
if (t === null) {
console.warn(L.errorMessages.sendMessageNoUnityInstance);
return;
}
t.SendMessage(v, U, D);
},
[t]
), z = (0, y.useCallback)(
/**
* @param dataType Defines the type of screenshot to take.
* @param quality Defines the quality of the screenshot.
* @returns A base 64 encoded string of the screenshot.
*/
function(v, U) {
if (t === null || typeof t.Module.canvas > "u") {
console.warn(L.errorMessages.screenshotNoUnityInstanceOrCanvas);
return;
}
return t.Module.canvas.toDataURL(v, U);
},
[t]
), m = (0, y.useCallback)(
/**
* @returns A promise that resolves when the UnityInstance has been unloaded.
*/
function() {
return t === null ? (console.warn(L.errorMessages.quitNoUnityInstance), Promise.reject()) : t.Quit();
},
[t]
), x = (0, y.useCallback)(
/**
* @returns A promise that resolves when the UnityInstance has been unloaded.
*/
function() {
return je(void 0, void 0, void 0, function() {
var v;
return Te(this, function(U) {
switch (U.label) {
case 0:
return t === null || typeof t.Module.canvas > "u" ? (console.warn(L.errorMessages.genericNoUnityInstance), [2, Promise.reject()]) : (v = t.Module.canvas, document.body.appendChild(v), v.style.display = "none", [4, m()]);
case 1:
return U.sent(), v.remove(), [
2
/*return*/
];
}
});
});
},
[t]
);
return (0, y.useEffect)(function() {
r(e === 1);
}, [e]), {
unityProvider: j.current,
loadingProgression: e,
initialisationError: f,
isLoaded: c,
UNSAFE__unityInstance: t,
requestFullscreen: V,
requestPointerLock: Q,
sendMessage: g,
takeScreenshot: z,
unload: m,
UNSAFE__detachAndUnloadImmediate: x,
addEventListener: M.addEventListener,
removeEventListener: M.removeEventListener
};
};
R.useUnityContext = $e;
(function(n) {
Object.defineProperty(n, "__esModule", { value: !0 }), n.useUnityContext = n.Unity = void 0;
var o = $;
Object.defineProperty(n, "Unity", { enumerable: !0, get: function() {
return o.Unity;
} });
var t = R;
Object.defineProperty(n, "useUnityContext", { enumerable: !0, get: function() {
return t.useUnityContext;
} });
})(H);
const re = async (n, o) => await (await fetch(
"https://api.vsllabs.com/api/v2/models/clerc/0",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": n
},
body: JSON.stringify({
user_input: o,
settings_nlp: {
synonyms: !1,
details: !1,
ner: !1,
numbers: !1,
nmm: !1,
emotions: !1,
translation_model: {
model_type: "SEQ2SEQ",
model_id: ""
}
}
})
}
)).json();
function Pe(n) {
return n.endsWith(".loader.js") || n.endsWith(".framework.js.br") ? "no-store" : "must-revalidate";
}
const Ge = ({
API_KEY: n,
loaderUrl: o,
dataUrl: t,
frameworkUrl: s,
codeUrl: a,
streaming_mode: e = !1
}) => {
const [i, l] = I(""), [c, r] = I(""), [u, f] = I([]), [S, M] = I(!1), [j, V] = I(!1), [Q, g] = I(""), { unityProvider: z, sendMessage: m, isLoaded: x } = H.useUnityContext({
loaderUrl: o,
dataUrl: t,
frameworkUrl: s,
codeUrl: a,
cacheControl: Pe
}), v = C(
async (d, w) => {
var K, X;
try {
!w && l(""), M(!0);
const h = await re(n, d);
w ? r(((K = h == null ? void 0 : h.NLP) == null ? void 0 : K.unity_3d_input) || "") : l(((X = h == null ? void 0 : h.NLP) == null ? void 0 : X.unity_3d_input) || ""), M(!1), g("");
} catch {
g("Invalid API Key!"), console.error("Invalid API Key!");
}
},
[n]
), U = C(
async (d) => {
x && j && d && (e ? f((w) => [...w, d]) : await v(d, !1));
},
[x, n, j, e]
), D = C(() => {
i && !e ? m("Gabi", "GetWordsFromUser", i) : g(
"Cannot reply, either no text provided or streaming mode is enabled."
);
}, [i, e]), ae = C((d) => {
d && m("BGcontroller", "ChangeColorFromUserInput", d);
}, []), se = C((d) => {
m(
"Main Camera",
"ToggleRotationFromStringInput",
d ? "true" : "false"
);
}, []), ie = C((d) => {
m("speedController", "SetSpeedFromUserInput", d);
}, []);
return T(() => {
n ? (async () => {
try {
await re(n, "test"), V(!0);
} catch {
g("Invalid API Key!"), console.error("Invalid API Key!");
}
})() : (g("Missing API_KEY!"), console.error("Missing API_KEY!"));
}, [n]), T(() => {
i && m("Gabi", "GetWordsFromUser", i);
}, [i]), T(() => {
c && m("Gabi", "GetWordsFromUserStream", c);
}, [c]), T(() => {
!S && u.length && (async () => {
const d = u[0];
f((w) => w.slice(1)), await v(d, !0);
})();
}, [u.length, S]), {
VSLWebGl: H.Unity,
translateTextToASL: U,
isUnityLoaded: x,
isTranslating: S,
unityProvider: z,
replay: D,
error: Q,
changeBgColor: ae,
toggleCameraRotation: se,
setAnimationSpeed: ie
};
};
export {
Ge as useVslWebGL
};
//# sourceMappingURL=index.es.js.map