MikaFil commited on
Commit
b04ec53
·
verified ·
1 Parent(s): c84d811

Update viewer_ar.js

Browse files
Files changed (1) hide show
  1. viewer_ar.js +52 -26
viewer_ar.js CHANGED
@@ -1,11 +1,12 @@
1
  /* script_ar.js — AR PlayCanvas + GLB HuggingFace
2
- - Hit-test AR (horizontaux uniquement) + placement + drag
3
- - Slider custom (360° en haut → 0° en bas), knob centré, rail plein
4
- - Tap sur la box grise capturé en phase CAPTURE => l’objet ne se déplace jamais
 
5
  */
6
 
7
  (() => {
8
- const GLB_URL = "https://huggingface.co/datasets/MikaFil/viewer_gs/resolve/main/AR/tests/danae.glb";
9
 
10
  // ===== PlayCanvas version fixée =====
11
  const PC_VERSION = "2.11.7";
@@ -31,13 +32,13 @@
31
 
32
  .ar-ui .label{font-size:12px;text-align:center;opacity:.95}
33
 
34
- /* Grande zone tactile */
35
  .rotY-wrap{position:relative;width:48px;height:200px;display:flex;align-items:center;justify-content:center;touch-action:none;overflow:visible;pointer-events:auto}
36
 
37
- /* Rail PLEIN (haut->bas), centré */
38
  .rotY-rail{position:absolute;left:50%;transform:translateX(-50%);width:4px;height:100%;background:rgba(255,255,255,.35);border-radius:2px;pointer-events:none}
39
 
40
- /* Knob custom centré */
41
  .rotY-knob{position:absolute;left:50%;width:22px;height:22px;border-radius:50%;background:#fff;box-shadow:0 2px 8px rgba(0,0,0,.35);transform:translate(-50%,-50%);top:50%;will-change:top;touch-action:none;pointer-events:none}
42
 
43
  /* Input range caché (pour accessibilité/valeur) */
@@ -68,8 +69,10 @@
68
  return p;
69
  }
70
 
 
71
  (async()=>{ try{ await loadPlayCanvasRobust({esmFirst:true,loadTimeoutMs:15000}); }catch(e){ console.error("Chargement PlayCanvas échoué ->",e); message("Impossible de charger PlayCanvas (réseau/CDN). Réessaie plus tard."); return; } initARApp(); })();
72
 
 
73
  function initARApp(){
74
  const pc = window.pc;
75
  const canvas = ensureCanvas();
@@ -93,11 +96,21 @@
93
  const onResize = () => app.resizeCanvas(); window.addEventListener("resize", onResize); app.on("destroy",()=>window.removeEventListener("resize",onResize));
94
  app.start();
95
 
96
- // Camera + lumière
 
 
 
 
 
 
97
  const camera = new pc.Entity("Camera");
98
- camera.addComponent("camera", { clearColor: new pc.Color(0,0,0,0), farClip: 10000 }); app.root.addChild(camera);
 
 
99
  const light = new pc.Entity("Light");
100
- light.addComponent("light", { type: "spot", range: 30, intensity: 10.1, castShadows: true }); light.setLocalPosition(0,10,0); app.root.addChild(light);
 
 
101
 
102
  // Réticule
103
  const reticleMat = new pc.StandardMaterial(); reticleMat.diffuse=new pc.Color(0.2,0.8,1.0); reticleMat.opacity=0.85; reticleMat.blendType=pc.BLEND_NORMAL; reticleMat.update();
@@ -127,12 +140,26 @@
127
  updateKnobFromY(y);
128
  }
129
 
130
- // Chargement GLB
131
  app.assets.loadFromUrl(GLB_URL, "container", (err, asset) => {
132
  if (err){ console.error(err); message("Échec du chargement du modèle GLB."); return; }
133
  const instance = asset.resource.instantiateRenderEntity({ castShadows:false, receiveShadows:false });
134
  modelRoot.addChild(instance);
135
- modelRoot.setLocalScale(1,1,1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  const initE = modelRoot.getEulerAngles();
138
  baseEulerX = initE.x; baseEulerZ = initE.z;
@@ -143,40 +170,36 @@
143
 
144
  if (!app.xr.supported){ message("WebXR n’est pas supporté sur cet appareil."); return; }
145
 
146
- // ===== Gestion fiable du slider : Pointer Events en CAPTURE =====
147
  let uiInteracting=false;
148
  let draggingWrap=false;
149
  let activePointerId=null;
150
 
151
  const insideWrap = (target) => rotWrap.contains(target);
152
-
153
- const getDegFromPointer = (e) => {
154
  const rect = rotWrap.getBoundingClientRect();
155
- const y = e.clientY ?? (e.touches && e.touches[0] && e.touches[0].clientY) ?? 0;
156
  const ratio = (y - rect.top) / rect.height; // 0 en haut -> 1 en bas
157
  const t = Math.max(0, Math.min(1, ratio));
158
- return (1 - t) * 360; // inversion : haut=360, bas=0
159
  };
160
 
161
- // pointerdown CAPTURE → on “prend la main” AVANT PlayCanvas
162
  const onPointerDownCapture = (e) => {
163
  if (!insideWrap(e.target)) return;
164
  uiInteracting = true;
165
  draggingWrap = true;
166
  activePointerId = e.pointerId ?? 1;
167
  try { rotWrap.setPointerCapture?.(activePointerId); } catch {}
168
- applyRotationY(getDegFromPointer(e));
169
  e.preventDefault();
170
  e.stopPropagation();
171
  };
172
-
173
  const onPointerMoveCapture = (e) => {
174
  if (!draggingWrap || (e.pointerId ?? 1) !== activePointerId) return;
175
- applyRotationY(getDegFromPointer(e));
176
  e.preventDefault();
177
  e.stopPropagation();
178
  };
179
-
180
  const endDrag = (e) => {
181
  if (!draggingWrap || (e.pointerId ?? 1) !== activePointerId) return;
182
  draggingWrap = false;
@@ -192,12 +215,13 @@
192
  document.addEventListener("pointerup", endDrag, { capture: true, passive: false });
193
  document.addEventListener("pointercancel", endDrag, { capture: true, passive: false });
194
 
195
- // --- Démarrage AR
196
  const activateAR = () => {
197
  if (!app.xr.isAvailable(pc.XRTYPE_AR)){ message("AR immersive indisponible sur cet appareil."); return; }
198
  app.xr.domOverlay.root = document.getElementById("xr-overlay-root");
199
  camera.camera.startXr(pc.XRTYPE_AR, pc.XRSPACE_LOCALFLOOR, {
200
  requiredFeatures: ["hit-test","dom-overlay"],
 
201
  domOverlay: { root: app.xr.domOverlay.root },
202
  callback: (err) => { if (err){ console.error("Échec du démarrage AR :", err); message(`Échec du démarrage AR : ${err.message||err}`); } }
203
  });
@@ -212,14 +236,14 @@
212
  const TMP_IN = new pc.Vec3(0,1,0), TMP_OUT = new pc.Vec3();
213
  function isHorizontalUpFacing(rot, minDot = 0.75){ rot.transformVector(TMP_IN, TMP_OUT); return TMP_OUT.y >= minDot; }
214
 
215
- // Hit Test
216
  app.xr.hitTest.on("available", () => {
217
  app.xr.hitTest.start({
218
  entityTypes: [pc.XRTRACKABLE_POINT, pc.XRTRACKABLE_PLANE],
219
  callback: (err, hitSource) => {
220
  if (err){ message("Le AR hit test n’a pas pu démarrer."); return; }
221
  hitSource.on("result", (pos, rot) => {
222
- if (!isHorizontalUpFacing(rot)) return;
223
 
224
  reticle.enabled = true;
225
  reticle.setPosition(pos);
@@ -230,7 +254,9 @@
230
  modelRoot.setPosition(pos);
231
 
232
  const e = new pc.Vec3(); rot.getEulerAngles(e);
233
- applyRotationY(((e.y % 360)+360)%360);
 
 
234
 
235
  placedOnce = true;
236
  rotYInput.disabled = false;
 
1
  /* script_ar.js — AR PlayCanvas + GLB HuggingFace
2
+ - Hit-test AR (HORIZONTAUX uniquement) + placement + drag (XR)
3
+ - Slider custom Yaw (360° en haut → 0° en bas), knob centré, rail plein
4
+ - Blocage total des interactions scène quand on touche le slider
5
+ - Éclairage PBR par défaut (sans WebXR light estimation)
6
  */
7
 
8
  (() => {
9
+ const GLB_URL = "https://huggingface.co/datasets/MikaFil/viewer_gs/resolve/main/AR/tests/fraisier.glb";
10
 
11
  // ===== PlayCanvas version fixée =====
12
  const PC_VERSION = "2.11.7";
 
32
 
33
  .ar-ui .label{font-size:12px;text-align:center;opacity:.95}
34
 
35
+ /* Grande zone tactile (box grise) */
36
  .rotY-wrap{position:relative;width:48px;height:200px;display:flex;align-items:center;justify-content:center;touch-action:none;overflow:visible;pointer-events:auto}
37
 
38
+ /* Rail PLEIN (de haut en bas), centré */
39
  .rotY-rail{position:absolute;left:50%;transform:translateX(-50%);width:4px;height:100%;background:rgba(255,255,255,.35);border-radius:2px;pointer-events:none}
40
 
41
+ /* Knob custom bien centré */
42
  .rotY-knob{position:absolute;left:50%;width:22px;height:22px;border-radius:50%;background:#fff;box-shadow:0 2px 8px rgba(0,0,0,.35);transform:translate(-50%,-50%);top:50%;will-change:top;touch-action:none;pointer-events:none}
43
 
44
  /* Input range caché (pour accessibilité/valeur) */
 
69
  return p;
70
  }
71
 
72
+ // ===== Boot =====
73
  (async()=>{ try{ await loadPlayCanvasRobust({esmFirst:true,loadTimeoutMs:15000}); }catch(e){ console.error("Chargement PlayCanvas échoué ->",e); message("Impossible de charger PlayCanvas (réseau/CDN). Réessaie plus tard."); return; } initARApp(); })();
74
 
75
+ // ===== App =====
76
  function initARApp(){
77
  const pc = window.pc;
78
  const canvas = ensureCanvas();
 
96
  const onResize = () => app.resizeCanvas(); window.addEventListener("resize", onResize); app.on("destroy",()=>window.removeEventListener("resize",onResize));
97
  app.start();
98
 
99
+ // ===== Rendu / PBR defaults (évite l'objet noir) =====
100
+ app.scene.gammaCorrection = pc.GAMMA_SRGB;
101
+ app.scene.toneMapping = pc.TONEMAP_ACES;
102
+ app.scene.exposure = 1.25; // ajuste 0.9–1.8 si besoin
103
+ app.scene.ambientLight = new pc.Color(0.35, 0.35, 0.35);
104
+
105
+ // Camera + lumière (directionnelle par défaut)
106
  const camera = new pc.Entity("Camera");
107
+ camera.addComponent("camera", { clearColor: new pc.Color(0,0,0,0), farClip: 10000 });
108
+ app.root.addChild(camera);
109
+
110
  const light = new pc.Entity("Light");
111
+ light.addComponent("light", { type: "directional", intensity: 2.0, castShadows: false, color: new pc.Color(1,1,1) });
112
+ light.setLocalEulerAngles(45, 30, 0); // direction lisible par défaut
113
+ app.root.addChild(light);
114
 
115
  // Réticule
116
  const reticleMat = new pc.StandardMaterial(); reticleMat.diffuse=new pc.Color(0.2,0.8,1.0); reticleMat.opacity=0.85; reticleMat.blendType=pc.BLEND_NORMAL; reticleMat.update();
 
140
  updateKnobFromY(y);
141
  }
142
 
143
+ // Chargement GLB + "fix matériaux"
144
  app.assets.loadFromUrl(GLB_URL, "container", (err, asset) => {
145
  if (err){ console.error(err); message("Échec du chargement du modèle GLB."); return; }
146
  const instance = asset.resource.instantiateRenderEntity({ castShadows:false, receiveShadows:false });
147
  modelRoot.addChild(instance);
148
+ modelRoot.setLocalScale(0.2,0.2,0.2);
149
+
150
+ // Fix matériaux (évite l'objet noir si diffuse ≠ 1 / IBL off)
151
+ const renders = instance.findComponents('render');
152
+ for (const r of renders) {
153
+ for (const mi of r.meshInstances) {
154
+ const m = mi.material;
155
+ if (!m) continue;
156
+ if (m.diffuse && (m.diffuse.r !== 1 || m.diffuse.g !== 1 || m.diffuse.b !== 1)) {
157
+ m.diffuse.set(1,1,1);
158
+ }
159
+ if ('useSkybox' in m) m.useSkybox = true; // exploite l'ambiant/scene lighting
160
+ m.update();
161
+ }
162
+ }
163
 
164
  const initE = modelRoot.getEulerAngles();
165
  baseEulerX = initE.x; baseEulerZ = initE.z;
 
170
 
171
  if (!app.xr.supported){ message("WebXR n’est pas supporté sur cet appareil."); return; }
172
 
173
+ // ===== Slider fiable : Pointer Events en CAPTURE =====
174
  let uiInteracting=false;
175
  let draggingWrap=false;
176
  let activePointerId=null;
177
 
178
  const insideWrap = (target) => rotWrap.contains(target);
179
+ const degFromPointer = (e) => {
 
180
  const rect = rotWrap.getBoundingClientRect();
181
+ const y = (e.clientY != null) ? e.clientY : (e.touches && e.touches[0] && e.touches[0].clientY) || 0;
182
  const ratio = (y - rect.top) / rect.height; // 0 en haut -> 1 en bas
183
  const t = Math.max(0, Math.min(1, ratio));
184
+ return (1 - t) * 360; // 360 en haut, 0 en bas
185
  };
186
 
 
187
  const onPointerDownCapture = (e) => {
188
  if (!insideWrap(e.target)) return;
189
  uiInteracting = true;
190
  draggingWrap = true;
191
  activePointerId = e.pointerId ?? 1;
192
  try { rotWrap.setPointerCapture?.(activePointerId); } catch {}
193
+ applyRotationY(degFromPointer(e));
194
  e.preventDefault();
195
  e.stopPropagation();
196
  };
 
197
  const onPointerMoveCapture = (e) => {
198
  if (!draggingWrap || (e.pointerId ?? 1) !== activePointerId) return;
199
+ applyRotationY(degFromPointer(e));
200
  e.preventDefault();
201
  e.stopPropagation();
202
  };
 
203
  const endDrag = (e) => {
204
  if (!draggingWrap || (e.pointerId ?? 1) !== activePointerId) return;
205
  draggingWrap = false;
 
215
  document.addEventListener("pointerup", endDrag, { capture: true, passive: false });
216
  document.addEventListener("pointercancel", endDrag, { capture: true, passive: false });
217
 
218
+ // --- Démarrage AR (sans light-estimation)
219
  const activateAR = () => {
220
  if (!app.xr.isAvailable(pc.XRTYPE_AR)){ message("AR immersive indisponible sur cet appareil."); return; }
221
  app.xr.domOverlay.root = document.getElementById("xr-overlay-root");
222
  camera.camera.startXr(pc.XRTYPE_AR, pc.XRSPACE_LOCALFLOOR, {
223
  requiredFeatures: ["hit-test","dom-overlay"],
224
+ // PAS de "light-estimation" ici (demandé)
225
  domOverlay: { root: app.xr.domOverlay.root },
226
  callback: (err) => { if (err){ console.error("Échec du démarrage AR :", err); message(`Échec du démarrage AR : ${err.message||err}`); } }
227
  });
 
236
  const TMP_IN = new pc.Vec3(0,1,0), TMP_OUT = new pc.Vec3();
237
  function isHorizontalUpFacing(rot, minDot = 0.75){ rot.transformVector(TMP_IN, TMP_OUT); return TMP_OUT.y >= minDot; }
238
 
239
+ // Hit Test global (réticule + 1er placement)
240
  app.xr.hitTest.on("available", () => {
241
  app.xr.hitTest.start({
242
  entityTypes: [pc.XRTRACKABLE_POINT, pc.XRTRACKABLE_PLANE],
243
  callback: (err, hitSource) => {
244
  if (err){ message("Le AR hit test n’a pas pu démarrer."); return; }
245
  hitSource.on("result", (pos, rot) => {
246
+ if (!isHorizontalUpFacing(rot)) return; // horizontaux uniquement
247
 
248
  reticle.enabled = true;
249
  reticle.setPosition(pos);
 
254
  modelRoot.setPosition(pos);
255
 
256
  const e = new pc.Vec3(); rot.getEulerAngles(e);
257
+ // normalise yaw initiale 0..360
258
+ const y0 = ((e.y % 360)+360)%360;
259
+ applyRotationY(y0);
260
 
261
  placedOnce = true;
262
  rotYInput.disabled = false;