@davi-ai/retorik-framework 4.0.0 → 4.0.1

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.
@@ -0,0 +1,666 @@
1
+ var $4gYsi$reactjsxruntime = require("react/jsx-runtime");
2
+ var $4gYsi$react = require("react");
3
+ var $4gYsi$daviaireactbodyenginethree = require("@davi-ai/react-bodyengine-three");
4
+ var $4gYsi$reactredux = require("react-redux");
5
+ var $4gYsi$mediapipetasksvision = require("@mediapipe/tasks-vision");
6
+
7
+
8
+ function $parcel$interopDefault(a) {
9
+ return a && a.__esModule ? a.default : a;
10
+ }
11
+
12
+ function $parcel$defineInteropFlag(a) {
13
+ Object.defineProperty(a, '__esModule', {value: true, configurable: true});
14
+ }
15
+
16
+ function $parcel$export(e, n, v, s) {
17
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
18
+ }
19
+
20
+ var $parcel$global =
21
+ typeof globalThis !== 'undefined'
22
+ ? globalThis
23
+ : typeof self !== 'undefined'
24
+ ? self
25
+ : typeof window !== 'undefined'
26
+ ? window
27
+ : typeof global !== 'undefined'
28
+ ? global
29
+ : {};
30
+ var parcelRequire = $parcel$global["parcelRequirea39d"];
31
+ var parcelRegister = parcelRequire.register;
32
+ parcelRegister("3QHCU", function(module, exports) {
33
+
34
+ $parcel$defineInteropFlag(module.exports);
35
+
36
+ $parcel$export(module.exports, "default", () => $929a5fce6d871a46$export$2e2bcd8739ae039);
37
+
38
+
39
+
40
+
41
+
42
+ var $kpW3P = parcelRequire("kpW3P");
43
+
44
+ var $7WHA7 = parcelRequire("7WHA7");
45
+
46
+ var $gp19G = parcelRequire("gp19G");
47
+
48
+ var $dZyZK = parcelRequire("dZyZK");
49
+ const $929a5fce6d871a46$var$ThreeJsWrapper = ({ agentData: agentData, visible: visible, height: height, size: size, cameraPreset: cameraPreset, calibration: calibration, centered: centered })=>{
50
+ var _calibration_static, _calibration_dynamic;
51
+ const dispatch = (0, $4gYsi$reactredux.useDispatch)();
52
+ const gpuData = (0, $4gYsi$reactredux.useSelector)((state)=>state.retorikReducer.gpuData);
53
+ const speaking = (0, $4gYsi$reactredux.useSelector)((state)=>state.speechReducer.speaking);
54
+ const loaderClosed = (0, $4gYsi$reactredux.useSelector)((state)=>state.retorikReducer.loaderClosed);
55
+ const recognitionState = (0, $4gYsi$reactredux.useSelector)((state)=>state.speechReducer.activeRecognitionState);
56
+ const lastRecognitionInterim = (0, $4gYsi$reactredux.useSelector)((state)=>state.speechReducer.lastRecognitionInterim);
57
+ const singleViseme = (0, $4gYsi$reactredux.useSelector)((state)=>state.speechReducer.singleVisemeAdded);
58
+ const multiVisemes = (0, $4gYsi$reactredux.useSelector)((state)=>state.speechReducer.multiVisemesAdded);
59
+ const streamingQueue = (0, $4gYsi$reactredux.useSelector)((state)=>state.speechReducer.streamingQueue);
60
+ const characterLoaded = (0, $4gYsi$reactredux.useSelector)((state)=>state.viewReducer.characterLoaded);
61
+ const isMobile = (0, $4gYsi$reactredux.useSelector)((state)=>state.viewReducer.isMobile);
62
+ const [targets, setTargets] = (0, $4gYsi$react.useState)([]);
63
+ const [dynamicData, setDynamicData] = (0, $4gYsi$react.useState)(undefined);
64
+ const [openCalibration, setOpenCalibration] = (0, $4gYsi$react.useState)(false);
65
+ /**
66
+ * Use model's source to get the right loader. Default to avaturn loader
67
+ */ const type = (0, $4gYsi$react.useMemo)(()=>{
68
+ if (agentData.source) switch(agentData.source.toLowerCase()){
69
+ case (0, $7WHA7.Source3DEngine).avatarsdk:
70
+ case (0, $7WHA7.Source3DEngine).avatarsdkbis:
71
+ return (0, $4gYsi$daviaireactbodyenginethree.CreationTool).avatarsdk;
72
+ case (0, $7WHA7.Source3DEngine).cc3:
73
+ case (0, $7WHA7.Source3DEngine).cc4:
74
+ return (0, $4gYsi$daviaireactbodyenginethree.CreationTool).cc3;
75
+ case (0, $7WHA7.Source3DEngine).rpm:
76
+ case (0, $7WHA7.Source3DEngine).readyplayerme:
77
+ return (0, $4gYsi$daviaireactbodyenginethree.CreationTool).rpm;
78
+ case (0, $7WHA7.Source3DEngine).avaturn:
79
+ default:
80
+ return (0, $4gYsi$daviaireactbodyenginethree.CreationTool).avaturn;
81
+ }
82
+ return (0, $4gYsi$daviaireactbodyenginethree.CreationTool).avaturn;
83
+ }, [
84
+ agentData
85
+ ]);
86
+ (0, $4gYsi$react.useEffect)(()=>{
87
+ var _calibration_static, _calibration_dynamic;
88
+ if (!(calibration === null || calibration === void 0 ? void 0 : (_calibration_static = calibration.static) === null || _calibration_static === void 0 ? void 0 : _calibration_static.enabled) && !(calibration === null || calibration === void 0 ? void 0 : (_calibration_dynamic = calibration.dynamic) === null || _calibration_dynamic === void 0 ? void 0 : _calibration_dynamic.enabled) || isMobile) dispatch((0, $kpW3P.storeActions).view.setCalibrationCompleted(true));
89
+ }, [
90
+ calibration,
91
+ isMobile
92
+ ]);
93
+ (0, $4gYsi$react.useEffect)(()=>{
94
+ var _calibration_static, _calibration_dynamic;
95
+ // Check if we should open the calibration screen or not
96
+ if (((calibration === null || calibration === void 0 ? void 0 : (_calibration_static = calibration.static) === null || _calibration_static === void 0 ? void 0 : _calibration_static.enabled) || (calibration === null || calibration === void 0 ? void 0 : (_calibration_dynamic = calibration.dynamic) === null || _calibration_dynamic === void 0 ? void 0 : _calibration_dynamic.enabled)) && !isMobile) {
97
+ var _calibration_static1, _calibration_static2, _calibration_dynamic1, _calibration_dynamic2;
98
+ // If there are some static data in the props or the local storage, we can directly set the targets without opening the calibration screen
99
+ if ((calibration === null || calibration === void 0 ? void 0 : (_calibration_static1 = calibration.static) === null || _calibration_static1 === void 0 ? void 0 : _calibration_static1.enabled) && calibration.static.staticTarget) {
100
+ const o = new (0, $4gYsi$daviaireactbodyenginethree.Object3D)();
101
+ o.position.set(calibration.static.staticTarget.x, calibration.static.staticTarget.y, calibration.static.staticTarget.z);
102
+ setTargets([
103
+ o
104
+ ]);
105
+ setOpenCalibration(false);
106
+ dispatch((0, $kpW3P.storeActions).view.setCalibrationCompleted(true));
107
+ } else if ((calibration === null || calibration === void 0 ? void 0 : (_calibration_static2 = calibration.static) === null || _calibration_static2 === void 0 ? void 0 : _calibration_static2.enabled) && calibration.static.loadStaticTargetFromLocalStorage && localStorage.getItem('Retorik.Framework.Static.Target')) {
108
+ const calibrationData = JSON.parse(localStorage.getItem('Retorik.Framework.Static.Target') || '{}');
109
+ const o = new (0, $4gYsi$daviaireactbodyenginethree.Object3D)();
110
+ o.position.set(calibrationData.x, calibrationData.y, calibrationData.z);
111
+ setTargets([
112
+ o
113
+ ]);
114
+ setOpenCalibration(false);
115
+ dispatch((0, $kpW3P.storeActions).view.setCalibrationCompleted(true));
116
+ } else if (((_calibration_dynamic1 = calibration.dynamic) === null || _calibration_dynamic1 === void 0 ? void 0 : _calibration_dynamic1.enabled) && calibration.dynamic.dynamicData) {
117
+ // Check if all the mandatory dynamic data are in the props
118
+ if (calibration.dynamic.dynamicData.webcamHeight && calibration.dynamic.dynamicData.ratioPixelToCentimeter && calibration.dynamic.dynamicData.deltaAgentHeightAgentWorldHeightInCentimeters && calibration.dynamic.dynamicData.distanceFromAgentToCameraInCentimeters) setDynamicData({
119
+ ...calibration.dynamic.dynamicData,
120
+ cameraRotationInDegrees: calibration.dynamic.dynamicData.cameraRotationInDegrees || 0
121
+ });
122
+ setOpenCalibration(true);
123
+ } else if (((_calibration_dynamic2 = calibration.dynamic) === null || _calibration_dynamic2 === void 0 ? void 0 : _calibration_dynamic2.enabled) && calibration.dynamic.loadDynamicDataFromLocalStorage && localStorage.getItem('Retorik.Framework.Dynamic.Data')) {
124
+ // Check if the local storage data is complete
125
+ const calibrationData = JSON.parse(localStorage.getItem('Retorik.Framework.Dynamic.Data') || '{}');
126
+ if (calibrationData.webcamHeight && calibrationData.ratioPixelToCentimeter && calibrationData.deltaAgentHeightAgentWorldHeightInCentimeters && calibrationData.distanceFromAgentToCameraInCentimeters) setDynamicData({
127
+ ...calibrationData,
128
+ cameraRotationInDegrees: calibrationData.cameraRotationInDegrees || 0
129
+ });
130
+ setOpenCalibration(true);
131
+ } else setOpenCalibration(true);
132
+ } else {
133
+ setOpenCalibration(false);
134
+ dispatch((0, $kpW3P.storeActions).view.setCalibrationCompleted(true));
135
+ }
136
+ }, [
137
+ calibration,
138
+ isMobile
139
+ ]);
140
+ (0, $4gYsi$react.useEffect)(()=>{
141
+ singleViseme && (0, $4gYsi$daviaireactbodyenginethree.addViseme)(singleViseme);
142
+ }, [
143
+ singleViseme
144
+ ]);
145
+ (0, $4gYsi$react.useEffect)(()=>{
146
+ multiVisemes.length && (0, $4gYsi$daviaireactbodyenginethree.setVisemes)(multiVisemes);
147
+ }, [
148
+ multiVisemes
149
+ ]);
150
+ (0, $4gYsi$react.useEffect)(()=>{
151
+ if (speaking || streamingQueue.length) (0, $4gYsi$daviaireactbodyenginethree.setCharacterState)((0, $4gYsi$daviaireactbodyenginethree.CharacterState).speaking);
152
+ else if (recognitionState === (0, $7WHA7.RecognitionState).Closed) (0, $4gYsi$daviaireactbodyenginethree.setCharacterState)((0, $4gYsi$daviaireactbodyenginethree.CharacterState).idle);
153
+ else if (lastRecognitionInterim) (0, $4gYsi$daviaireactbodyenginethree.setCharacterState)((0, $4gYsi$daviaireactbodyenginethree.CharacterState).listening);
154
+ else (0, $4gYsi$daviaireactbodyenginethree.setCharacterState)((0, $4gYsi$daviaireactbodyenginethree.CharacterState).waiting);
155
+ }, [
156
+ speaking,
157
+ recognitionState,
158
+ lastRecognitionInterim,
159
+ streamingQueue
160
+ ]);
161
+ (0, $4gYsi$react.useEffect)(()=>{
162
+ !speaking && (0, $4gYsi$daviaireactbodyenginethree.flushLipSyncData)();
163
+ }, [
164
+ speaking
165
+ ]);
166
+ return /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)((0, ($parcel$interopDefault($4gYsi$react))).Fragment, {
167
+ children: [
168
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
169
+ style: {
170
+ height: size || '100%',
171
+ paddingTop: height || 0,
172
+ visibility: characterLoaded && visible ? 'visible' : 'hidden'
173
+ },
174
+ children: type && /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)((0, $4gYsi$daviaireactbodyenginethree.Character), {
175
+ url: agentData.url,
176
+ type: type,
177
+ animationsUrl: (agentData === null || agentData === void 0 ? void 0 : agentData.animationsUrl) || 'https://cdn.retorik.ai/bodyengine-three/animations/cc4/female/standing/',
178
+ gender: (agentData === null || agentData === void 0 ? void 0 : agentData.gender) === 'male' ? 'male' : 'female',
179
+ onLoadingCompleted: ()=>dispatch((0, $kpW3P.storeActions).view.setCharacterLoaded(true)),
180
+ camera: (0, $4gYsi$daviaireactbodyenginethree.presets)[cameraPreset || 'default'],
181
+ targets: targets,
182
+ autoLookAt: true,
183
+ detectGpuData: gpuData,
184
+ license: "fake-license"
185
+ })
186
+ }),
187
+ !characterLoaded && /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
188
+ className: "rf-absolute rf-top-0 rf-left-0 rf-w-full rf-h-full rf-flex rf-justify-center rf-items-center",
189
+ children: /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
190
+ className: "rf-w-1/4 rf-aspect-square",
191
+ children: /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)((0, $gp19G.default), {})
192
+ })
193
+ }),
194
+ openCalibration && loaderClosed && /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)((0, $dZyZK.default), {
195
+ centered: centered,
196
+ staticCalibrationEnabled: !!(calibration === null || calibration === void 0 ? void 0 : (_calibration_static = calibration.static) === null || _calibration_static === void 0 ? void 0 : _calibration_static.enabled),
197
+ dynamicCalibrationEnabled: !!(calibration === null || calibration === void 0 ? void 0 : (_calibration_dynamic = calibration.dynamic) === null || _calibration_dynamic === void 0 ? void 0 : _calibration_dynamic.enabled),
198
+ setTargets: (targets)=>setTargets(targets),
199
+ dynamicData: dynamicData
200
+ })
201
+ ]
202
+ });
203
+ };
204
+ var $929a5fce6d871a46$export$2e2bcd8739ae039 = $929a5fce6d871a46$var$ThreeJsWrapper;
205
+
206
+ });
207
+ parcelRegister("dZyZK", function(module, exports) {
208
+
209
+ $parcel$export(module.exports, "default", () => $5422cff32f1fe2dc$export$2e2bcd8739ae039);
210
+
211
+
212
+
213
+
214
+
215
+ var $kpW3P = parcelRequire("kpW3P");
216
+
217
+ var $8sMtK = parcelRequire("8sMtK");
218
+ var $5422cff32f1fe2dc$var$CalibrationMode = /*#__PURE__*/ function(CalibrationMode) {
219
+ CalibrationMode[CalibrationMode["none"] = 1] = "none";
220
+ CalibrationMode[CalibrationMode["static"] = 2] = "static";
221
+ CalibrationMode[CalibrationMode["dynamic"] = 3] = "dynamic";
222
+ return CalibrationMode;
223
+ }($5422cff32f1fe2dc$var$CalibrationMode || {});
224
+ const $5422cff32f1fe2dc$var$CalibrationScreen = ({ centered: centered, staticCalibrationEnabled: staticCalibrationEnabled, dynamicCalibrationEnabled: dynamicCalibrationEnabled, dynamicData: dynamicData, setTargets: setTargets })=>{
225
+ const dispatch = (0, $4gYsi$reactredux.useDispatch)();
226
+ const cameraPosition = (0, $4gYsi$daviaireactbodyenginethree.getCameraPosition)();
227
+ const characterHeight = (0, $4gYsi$daviaireactbodyenginethree.getCharacterHeight)();
228
+ const characterLoaded = (0, $4gYsi$reactredux.useSelector)((state)=>state.viewReducer.characterLoaded);
229
+ const calibrationCompleted = (0, $4gYsi$reactredux.useSelector)((state)=>state.viewReducer.calibrationCompleted);
230
+ const [calibrationMode, setCalibrationMode] = (0, $4gYsi$react.useState)(dynamicData ? 3 : 1);
231
+ const [distanceFromDevice, setDistanceFromDevice] = (0, $4gYsi$react.useState)((dynamicData === null || dynamicData === void 0 ? void 0 : dynamicData.distanceFromAgentToCameraInCentimeters) || 0);
232
+ const [webcamRotation, setWebcamRotation] = (0, $4gYsi$react.useState)(0);
233
+ const [agentEyesHeightOnScreen, setAgentEyesHeightOnScreen] = (0, $4gYsi$react.useState)(0);
234
+ const [worldDistancePerDegree, setWorldDistancePerDegree] = (0, $4gYsi$react.useState)(0);
235
+ const [currentAngleInDegrees, setCurrentAngleInDegrees] = (0, $4gYsi$react.useState)(0);
236
+ const [cameraX, setCameraX] = (0, $4gYsi$react.useState)(0);
237
+ const [cameraY, setCameraY] = (0, $4gYsi$react.useState)(0);
238
+ const [cameraZ, setCameraZ] = (0, $4gYsi$react.useState)(0);
239
+ const cameraXRef = (0, $4gYsi$react.useRef)(0);
240
+ const cameraYRef = (0, $4gYsi$react.useRef)(0);
241
+ const cameraZRef = (0, $4gYsi$react.useRef)(0);
242
+ const currentAngleInDegreesRef = (0, $4gYsi$react.useRef)(0);
243
+ const webcamHeightRef = (0, $4gYsi$react.useRef)((dynamicData === null || dynamicData === void 0 ? void 0 : dynamicData.webcamHeight) || 0);
244
+ const userEyesHeightRef = (0, $4gYsi$react.useRef)(0);
245
+ const agentEyesHeightRef = (0, $4gYsi$react.useRef)(0);
246
+ const distanceFromDeviceRef = (0, $4gYsi$react.useRef)(0);
247
+ const webcamRotatedRef = (0, $4gYsi$react.useRef)((dynamicData === null || dynamicData === void 0 ? void 0 : dynamicData.cameraRotationInDegrees) || 0);
248
+ const deltaAgentHeightAgentWorldHeightInCentimetersRef = (0, $4gYsi$react.useRef)((dynamicData === null || dynamicData === void 0 ? void 0 : dynamicData.deltaAgentHeightAgentWorldHeightInCentimeters) || 0);
249
+ const ratioPixelToCentimeterRef = (0, $4gYsi$react.useRef)((dynamicData === null || dynamicData === void 0 ? void 0 : dynamicData.ratioPixelToCentimeter) || 0);
250
+ const calibrationCompletedRef = (0, $4gYsi$react.useRef)(calibrationCompleted);
251
+ const counterRef = (0, $4gYsi$react.useRef)(0);
252
+ (0, $4gYsi$react.useEffect)(()=>{
253
+ // If dynamic data are given, let's validate immediately to set calibratoin as completed
254
+ dynamicData && handleValidate();
255
+ }, []);
256
+ (0, $4gYsi$react.useEffect)(()=>{
257
+ characterLoaded && !dynamicData && (0, $4gYsi$daviaireactbodyenginethree.setLookAtCamera)(true);
258
+ return ()=>{
259
+ (0, $4gYsi$daviaireactbodyenginethree.setLookAtCamera)(false);
260
+ };
261
+ }, [
262
+ characterLoaded
263
+ ]);
264
+ (0, $4gYsi$react.useEffect)(()=>{
265
+ if (worldDistancePerDegree && !dynamicData) {
266
+ const o = new (0, $4gYsi$daviaireactbodyenginethree.Object3D)();
267
+ o.position.set(cameraX, cameraY + currentAngleInDegrees * worldDistancePerDegree, Math.max(cameraZ / 5, 0.3));
268
+ setTargets([
269
+ o
270
+ ]);
271
+ }
272
+ }, [
273
+ currentAngleInDegrees,
274
+ worldDistancePerDegree,
275
+ distanceFromDevice,
276
+ cameraX,
277
+ cameraY,
278
+ cameraZ
279
+ ]);
280
+ (0, $4gYsi$react.useEffect)(()=>{
281
+ if (cameraPosition && !(cameraPosition.x === 0 && cameraPosition.y === 0)) {
282
+ const zDistanceCameraPlusUser = cameraPosition.z + distanceFromDevice / 100;
283
+ setCameraX(cameraPosition.x);
284
+ setCameraY(cameraPosition.y);
285
+ setCameraZ(zDistanceCameraPlusUser);
286
+ cameraXRef.current = cameraPosition.x;
287
+ cameraYRef.current = cameraPosition.y;
288
+ cameraZRef.current = zDistanceCameraPlusUser;
289
+ // Get Y world distance for one degree using tangent. The z distance is divided by 5 with 0.3 at the lowest to show a visible height bar when calibrating
290
+ setWorldDistancePerDegree(Math.tan(Math.PI / 180) * Math.max(zDistanceCameraPlusUser / 5, 0.3));
291
+ }
292
+ }, [
293
+ cameraPosition,
294
+ distanceFromDevice
295
+ ]);
296
+ (0, $4gYsi$react.useEffect)(()=>{
297
+ if (!dynamicData) characterHeight && agentEyesHeightOnScreen && (deltaAgentHeightAgentWorldHeightInCentimetersRef.current = (characterHeight - characterHeight / 16) * 100 - agentEyesHeightOnScreen);
298
+ }, [
299
+ characterHeight,
300
+ agentEyesHeightOnScreen
301
+ ]);
302
+ (0, $4gYsi$react.useEffect)(()=>{
303
+ !dynamicData && (webcamRotatedRef.current = webcamRotation);
304
+ }, [
305
+ webcamRotation
306
+ ]);
307
+ (0, $4gYsi$react.useEffect)(()=>{
308
+ calibrationCompletedRef.current = calibrationCompleted;
309
+ }, [
310
+ calibrationCompleted
311
+ ]);
312
+ (0, $4gYsi$react.useEffect)(()=>{
313
+ if (!dynamicData && calibrationMode === 2) (0, $4gYsi$daviaireactbodyenginethree.setShowFirstTarget)(!calibrationCompleted);
314
+ }, [
315
+ calibrationMode,
316
+ calibrationCompleted
317
+ ]);
318
+ const handleReset = ()=>{
319
+ setCurrentAngleInDegrees(0);
320
+ currentAngleInDegreesRef.current = 0;
321
+ };
322
+ const handleValidate = ()=>{
323
+ (0, $4gYsi$daviaireactbodyenginethree.setLookAtCamera)(false);
324
+ if (calibrationMode === 2) // In static mode, store the target in local storage to be able to reuse it later and avoid asking the user to calibrate again if the data are valid
325
+ localStorage.setItem('Retorik.Framework.Static.Target', JSON.stringify({
326
+ x: cameraX,
327
+ y: cameraY + currentAngleInDegrees * worldDistancePerDegree,
328
+ z: cameraZ
329
+ }));
330
+ else if (calibrationMode === 3 && ratioPixelToCentimeterRef.current) {
331
+ localStorage.setItem('Retorik.Framework.Dynamic.Data', JSON.stringify({
332
+ webcamHeight: webcamHeightRef.current,
333
+ ratioPixelToCentimeter: ratioPixelToCentimeterRef.current,
334
+ deltaAgentHeightAgentWorldHeightInCentimeters: deltaAgentHeightAgentWorldHeightInCentimetersRef.current,
335
+ cameraRotationInDegrees: webcamRotatedRef.current,
336
+ distanceFromAgentToCameraInCentimeters: distanceFromDeviceRef.current
337
+ }));
338
+ setTargets([]);
339
+ }
340
+ dispatch((0, $kpW3P.storeActions).view.setCalibrationCompleted(true));
341
+ };
342
+ const handleFaceDetected = (detections, width, height)=>{
343
+ counterRef.current++;
344
+ if ((detections === null || detections === void 0 ? void 0 : detections.length) && width && height) {
345
+ // The detections are inverted in X and Y axis (the origin is the top right corner of the image)
346
+ const webcamPositionY = webcamRotatedRef.current === 0 ? height / 2 : width / 2;
347
+ const userPositionY = webcamRotatedRef.current === 0 ? height * (1 - detections[0].keypoints[0].y) : width * (1 - detections[0].keypoints[0].x);
348
+ if (calibrationCompletedRef.current) // Check if the mandatory data have been filled (webcam / agent / user heights + distance user <-> device)
349
+ {
350
+ if (webcamHeightRef.current && ratioPixelToCentimeterRef.current && deltaAgentHeightAgentWorldHeightInCentimetersRef.current !== 0) {
351
+ const deltaHeightInPixels = webcamPositionY - userPositionY;
352
+ const userEyesHeightInCentimeters = webcamHeightRef.current - deltaHeightInPixels * ratioPixelToCentimeterRef.current;
353
+ const targetYInWorldCoordinates = (userEyesHeightInCentimeters + deltaAgentHeightAgentWorldHeightInCentimetersRef.current) / 100;
354
+ const o = new (0, $4gYsi$daviaireactbodyenginethree.Object3D)();
355
+ o.position.set(cameraXRef.current, targetYInWorldCoordinates, cameraZRef.current);
356
+ setTargets([
357
+ o
358
+ ]);
359
+ }
360
+ } else // The detections are inverted in X and Y axis (the origin is the top right corner of the image)
361
+ if (webcamHeightRef.current && userEyesHeightRef.current && distanceFromDeviceRef.current) {
362
+ // Get the difference in height between the webcam and the user's eyes in cm, then use the detection to get the same difference in pixels on the webcam's image
363
+ const deltaHeightInCentimeters = Math.abs(webcamHeightRef.current - userEyesHeightRef.current);
364
+ const deltaHeightInPixels = Math.abs(webcamPositionY - userPositionY);
365
+ deltaHeightInPixels && (ratioPixelToCentimeterRef.current = deltaHeightInCentimeters / deltaHeightInPixels);
366
+ }
367
+ }
368
+ };
369
+ return /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
370
+ id: "retorik-framework-animation-container-calibration",
371
+ className: `rf-absolute rf-top-0 rf-h-full rf-w-full rf-grid rf-grid-cols-1 rf-grid-rows-1 ${!centered && 'landscape-retorik:rf-translate-x-1/4'}`,
372
+ children: [
373
+ calibrationMode === 3 && /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
374
+ className: "rf-relative rf-z-1 rf-h-full rf-w-full rf-col-start-1 rf-col-span-1 rf-row-start-1 rf-row-span-1 rf-flex rf-justify-start rf-items-start",
375
+ children: /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)((0, $8sMtK.default), {
376
+ detectionThreshold: 0.8,
377
+ onFaceDetected: handleFaceDetected,
378
+ showThumbnail: !calibrationCompleted,
379
+ rotation: webcamRotation
380
+ })
381
+ }),
382
+ !calibrationCompleted && /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)((0, ($parcel$interopDefault($4gYsi$react))).Fragment, {
383
+ children: calibrationMode === 1 ? /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
384
+ className: "rf-h-full rf-w-full rf-col-start-1 rf-col-span-1 rf-row-start-1 rf-row-span-1 rf-flex rf-flex-col rf-justify-center rf-items-center rf-gap-4 rf-bg-white rf-bg-opacity-50",
385
+ children: [
386
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
387
+ className: "rf-text-medium-size-auto",
388
+ children: "Choisissez un mode de calibrage :"
389
+ }),
390
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
391
+ className: "rf-flex rf-flex-row rf-gap-4",
392
+ children: [
393
+ staticCalibrationEnabled && /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
394
+ className: "rf-px-4 rf-py-2 rf-border rf-border-primary rf-rounded-lg rf-bg-primary rf-bg-opacity-50 rf-cursor-pointer",
395
+ onClick: ()=>setCalibrationMode(2),
396
+ children: "STATIQUE"
397
+ }),
398
+ dynamicCalibrationEnabled && /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
399
+ className: "rf-px-4 rf-py-2 rf-border rf-border-primary rf-rounded-lg rf-bg-primary rf-bg-opacity-50 rf-cursor-pointer",
400
+ onClick: ()=>setCalibrationMode(3),
401
+ children: "DYNAMIQUE"
402
+ })
403
+ ]
404
+ })
405
+ ]
406
+ }) : /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
407
+ className: "rf-relative rf-h-full rf-w-full rf-col-start-1 rf-col-span-1 rf-row-start-1 rf-row-span-1 rf-flex rf-flex-col rf-justify-center rf-gap-4",
408
+ children: calibrationMode === 3 ? /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
409
+ className: "rf-absolute rf-left-32 rf-flex rf-flex-col rf-justify-center rf-items-center rf-gap-4",
410
+ children: [
411
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
412
+ className: "rf-grid rf-grid-cols-2 rf-grid-rows-3 rf-gap-4",
413
+ children: [
414
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
415
+ className: "rf-font-bold",
416
+ children: "Hauteur webcam (en cm)"
417
+ }),
418
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("input", {
419
+ type: "number",
420
+ min: 0,
421
+ max: 300,
422
+ onChange: (e)=>{
423
+ webcamHeightRef.current = parseInt(e.target.value);
424
+ }
425
+ }),
426
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
427
+ className: "rf-font-bold",
428
+ children: "Votre taille (en cm)"
429
+ }),
430
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("input", {
431
+ type: "number",
432
+ min: 50,
433
+ max: 300,
434
+ onChange: (e)=>{
435
+ // We estimate the height of the user's eyes is 12 cm less than their total height
436
+ userEyesHeightRef.current = parseInt(e.target.value) - 12;
437
+ }
438
+ }),
439
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
440
+ className: "rf-font-bold",
441
+ children: "Hauteur des yeux de l'agent (en cm)"
442
+ }),
443
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("input", {
444
+ type: "number",
445
+ min: 50,
446
+ max: 300,
447
+ onChange: (e)=>{
448
+ setAgentEyesHeightOnScreen(parseInt(e.target.value));
449
+ agentEyesHeightRef.current = parseInt(e.target.value);
450
+ }
451
+ }),
452
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
453
+ className: "rf-font-bold",
454
+ children: "Distance utilisateur - borne (en cm)"
455
+ }),
456
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("input", {
457
+ type: "number",
458
+ min: 20,
459
+ max: 100,
460
+ onChange: (e)=>{
461
+ setDistanceFromDevice(parseInt(e.target.value));
462
+ distanceFromDeviceRef.current = parseInt(e.target.value);
463
+ }
464
+ }),
465
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
466
+ className: "rf-font-bold",
467
+ children: "Rotation de la webcam (en degr\xe9s)"
468
+ }),
469
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
470
+ className: "rf-flex rf-flex-row rf-gap-2",
471
+ children: [
472
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("button", {
473
+ className: `rf-px-2 rf-py-1 rf-border rf-border-primary rf-rounded-lg rf-bg-primary rf-bg-opacity-50 rf-cursor-pointer ${webcamRotation === 0 && 'rf-bg-opacity-100'}`,
474
+ onClick: ()=>setWebcamRotation(0),
475
+ children: "0"
476
+ }),
477
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("button", {
478
+ className: `rf-px-2 rf-py-1 rf-border rf-border-primary rf-rounded-lg rf-bg-primary rf-bg-opacity-50 rf-cursor-pointer ${webcamRotation === 90 && 'rf-bg-opacity-100'}`,
479
+ onClick: ()=>setWebcamRotation(90),
480
+ children: "90"
481
+ }),
482
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("button", {
483
+ className: `rf-px-2 rf-py-1 rf-border rf-border-primary rf-rounded-lg rf-bg-primary rf-bg-opacity-50 rf-cursor-pointer ${webcamRotation === -90 && 'rf-bg-opacity-100'}`,
484
+ onClick: ()=>setWebcamRotation(-90),
485
+ children: "-90"
486
+ })
487
+ ]
488
+ })
489
+ ]
490
+ }),
491
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
492
+ className: "rf-h-16 rf-w-16 rf-flex rf-justify-center rf-items-center rf-bg-white rf-border rf-border-black rf-rounded-lg hover: rf-cursor-pointer",
493
+ onClick: handleValidate,
494
+ children: "Done"
495
+ })
496
+ ]
497
+ }) : /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
498
+ className: "rf-ml-4 rf-flex rf-flex-col rf-gap-4",
499
+ children: [
500
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
501
+ className: "rf-flex rf-flex-row rf-items-center rf-gap-4",
502
+ children: [
503
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("input", {
504
+ type: "range",
505
+ min: -25,
506
+ max: 25,
507
+ step: 1,
508
+ value: currentAngleInDegrees,
509
+ onChange: (e)=>{
510
+ const val = parseInt(e.target.value);
511
+ setCurrentAngleInDegrees(val);
512
+ currentAngleInDegreesRef.current = val;
513
+ },
514
+ className: "rf-range-vertical rf-w-8 rf-h-80 rf-appearance-none rf-border rf-border-black rf-rounded-lg rf-cursor-pointer",
515
+ style: {
516
+ writingMode: 'vertical-lr',
517
+ direction: 'rtl'
518
+ }
519
+ }),
520
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
521
+ className: "rf-w-fit rf-p-4 rf-flex rf-flex-col rf-justify-center rf-gap-2 rf-bg-white rf-border rf-border-black rf-rounded-lg",
522
+ children: [
523
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
524
+ children: "Position actuelle de la cible : "
525
+ }),
526
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
527
+ children: `x: ${cameraX}`
528
+ }),
529
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
530
+ children: `y: ${cameraY + (currentAngleInDegrees ? currentAngleInDegrees * worldDistancePerDegree : 0)}`
531
+ }),
532
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
533
+ children: `z: ${Math.max(cameraZ / 5, 0.3)}`
534
+ }),
535
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("p", {
536
+ children: `angle: ${currentAngleInDegrees}\xb0`
537
+ })
538
+ ]
539
+ })
540
+ ]
541
+ }),
542
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsxs)("div", {
543
+ className: "rf-flex rf-flex-row rf-gap-2",
544
+ children: [
545
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
546
+ className: "rf-h-10 rf-px-4 rf-flex rf-justify-center rf-items-center rf-bg-white rf-border rf-border-black rf-rounded-lg rf-cursor-pointer",
547
+ onClick: ()=>handleReset(),
548
+ children: "Reset"
549
+ }),
550
+ /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
551
+ className: "rf-h-10 rf-px-4 rf-flex rf-justify-center rf-items-center rf-bg-white rf-border rf-border-black rf-rounded-lg rf-cursor-pointer",
552
+ onClick: handleValidate,
553
+ children: "Done"
554
+ })
555
+ ]
556
+ })
557
+ ]
558
+ })
559
+ })
560
+ })
561
+ ]
562
+ });
563
+ };
564
+ var $5422cff32f1fe2dc$export$2e2bcd8739ae039 = $5422cff32f1fe2dc$var$CalibrationScreen;
565
+
566
+ });
567
+ parcelRegister("8sMtK", function(module, exports) {
568
+
569
+ $parcel$export(module.exports, "default", () => $8d9a75d3aaaf531b$export$2e2bcd8739ae039);
570
+
571
+
572
+
573
+ const $8d9a75d3aaaf531b$var$runningMode = 'VIDEO';
574
+ let $8d9a75d3aaaf531b$var$lastVideoTime = -1;
575
+ const $8d9a75d3aaaf531b$var$defaultConstraints = {
576
+ video: {
577
+ facingMode: 'user'
578
+ }
579
+ };
580
+ const $8d9a75d3aaaf531b$var$FaceDetector = ({ detectionThreshold: detectionThreshold, onFaceDetected: onFaceDetected, showThumbnail: showThumbnail, rotation: rotation })=>{
581
+ const [webcamWidth, setWebcamWidth] = (0, $4gYsi$react.useState)(0);
582
+ const [webcamHeight, setWebcamHeight] = (0, $4gYsi$react.useState)(0);
583
+ const [faceDetector, setFaceDetector] = (0, $4gYsi$react.useState)();
584
+ const videoRef = (0, $4gYsi$react.useRef)(null);
585
+ const rotationRef = (0, $4gYsi$react.useRef)(rotation || 0);
586
+ (0, $4gYsi$react.useEffect)(()=>{
587
+ rotationRef.current = rotation || 0;
588
+ }, [
589
+ rotation
590
+ ]);
591
+ (0, $4gYsi$react.useEffect)(()=>{
592
+ const createfaceDetector = async ()=>{
593
+ const vision = await (0, $4gYsi$mediapipetasksvision.FilesetResolver).forVisionTasks('https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm');
594
+ const newFaceDetector = await (0, $4gYsi$mediapipetasksvision.FaceDetector).createFromOptions(vision, {
595
+ baseOptions: {
596
+ modelAssetPath: `https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float16/1/blaze_face_short_range.tflite`,
597
+ delegate: 'GPU'
598
+ },
599
+ runningMode: $8d9a75d3aaaf531b$var$runningMode
600
+ });
601
+ newFaceDetector && setFaceDetector(newFaceDetector);
602
+ };
603
+ const initializefaceDetector = async ()=>{
604
+ navigator.mediaDevices.getUserMedia($8d9a75d3aaaf531b$var$defaultConstraints).then((display)=>{
605
+ const settings = display.getVideoTracks()[0].getSettings();
606
+ setWebcamWidth(settings.width || 0);
607
+ setWebcamHeight(settings.height || 0);
608
+ createfaceDetector();
609
+ }).catch((error)=>console.warn('Retorik Framework > an error occured when trying to use the webcam : ', error));
610
+ };
611
+ initializefaceDetector();
612
+ }, []);
613
+ const predictWebcam = async ()=>{
614
+ if (faceDetector && videoRef.current) {
615
+ let startTimeMs = performance.now();
616
+ // Detect faces using detectForVideo
617
+ if (videoRef.current.currentTime !== $8d9a75d3aaaf531b$var$lastVideoTime) {
618
+ $8d9a75d3aaaf531b$var$lastVideoTime = videoRef.current.currentTime;
619
+ const detections = faceDetector.detectForVideo(videoRef.current, startTimeMs, {
620
+ rotationDegrees: rotationRef.current
621
+ }).detections;
622
+ if (detections.length) {
623
+ const score = detections[0].categories[0].score;
624
+ score > detectionThreshold && onFaceDetected(detections, videoRef.current.videoWidth, videoRef.current.videoHeight);
625
+ }
626
+ }
627
+ }
628
+ // Call this function again to keep predicting when the browser is ready
629
+ window.requestAnimationFrame(predictWebcam);
630
+ };
631
+ (0, $4gYsi$react.useEffect)(()=>{
632
+ if (faceDetector && videoRef.current) // Activate the webcam stream
633
+ navigator.mediaDevices.getUserMedia($8d9a75d3aaaf531b$var$defaultConstraints).then((stream)=>{
634
+ if (videoRef.current) {
635
+ videoRef.current.srcObject = stream;
636
+ videoRef.current.addEventListener('loadeddata', predictWebcam);
637
+ }
638
+ }).catch((err)=>{
639
+ console.error(err);
640
+ });
641
+ }, [
642
+ faceDetector
643
+ ]);
644
+ return /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("div", {
645
+ id: "retorik-framework-face-detector",
646
+ className: `rf-absolute rf-top-0 rf-left-0 ${!showThumbnail && 'rf-invisible'}`,
647
+ children: /*#__PURE__*/ (0, $4gYsi$reactjsxruntime.jsx)("video", {
648
+ ref: videoRef,
649
+ src: "",
650
+ autoPlay: true,
651
+ style: {
652
+ rotate: `${rotation}deg`,
653
+ width: showThumbnail ? webcamWidth / 2 : webcamWidth,
654
+ height: showThumbnail ? webcamHeight / 2 : webcamHeight
655
+ }
656
+ })
657
+ });
658
+ };
659
+ var $8d9a75d3aaaf531b$export$2e2bcd8739ae039 = $8d9a75d3aaaf531b$var$FaceDetector;
660
+
661
+ });
662
+
663
+
664
+
665
+
666
+ //# sourceMappingURL=ThreeJsWrapper.4302165c.js.map