murphylmf commited on
Commit
a33deb9
·
1 Parent(s): 8a80622
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +1 -0
  2. README.md +1 -1
  3. app.py +371 -431
  4. examples/example1.mp4 +3 -0
  5. examples/example2.mp4 +3 -0
  6. examples/example3.mp4 +3 -0
  7. examples/example4.mp4 +3 -0
  8. index.html +582 -0
  9. requirements.txt +10 -6
  10. static/BufferGeometryUtils.js +1375 -0
  11. static/GLTFLoader.js +0 -0
  12. static/OrbitControls.js +1407 -0
  13. static/teaser_video_final.mp4 +3 -0
  14. static/three.module.js +0 -0
  15. static/three.module.min.js +0 -0
  16. unish/__pycache__/pipeline.cpython-310.pyc +0 -0
  17. unish/heads/__pycache__/align_net.cpython-310.pyc +0 -0
  18. unish/heads/__pycache__/dpt_head.cpython-310.pyc +0 -0
  19. unish/heads/__pycache__/head_act.cpython-310.pyc +0 -0
  20. unish/heads/__pycache__/human_head_cliff.cpython-310.pyc +0 -0
  21. unish/heads/__pycache__/pose_transformer.cpython-310.pyc +0 -0
  22. unish/heads/__pycache__/t_cond_mlp.cpython-310.pyc +0 -0
  23. unish/heads/__pycache__/utils.cpython-310.pyc +0 -0
  24. unish/heads/__pycache__/vit.cpython-310.pyc +0 -0
  25. unish/heads/align_net.py +3 -3
  26. unish/pi3/models/__pycache__/pi3.cpython-310.pyc +0 -0
  27. unish/pi3/models/dinov2/__pycache__/__init__.cpython-310.pyc +0 -0
  28. unish/pi3/models/dinov2/hub/__pycache__/__init__.cpython-310.pyc +0 -0
  29. unish/pi3/models/dinov2/hub/__pycache__/backbones.cpython-310.pyc +0 -0
  30. unish/pi3/models/dinov2/hub/__pycache__/utils.cpython-310.pyc +0 -0
  31. unish/pi3/models/dinov2/layers/__pycache__/__init__.cpython-310.pyc +0 -0
  32. unish/pi3/models/dinov2/layers/__pycache__/attention.cpython-310.pyc +0 -0
  33. unish/pi3/models/dinov2/layers/__pycache__/block.cpython-310.pyc +0 -0
  34. unish/pi3/models/dinov2/layers/__pycache__/dino_head.cpython-310.pyc +0 -0
  35. unish/pi3/models/dinov2/layers/__pycache__/drop_path.cpython-310.pyc +0 -0
  36. unish/pi3/models/dinov2/layers/__pycache__/layer_scale.cpython-310.pyc +0 -0
  37. unish/pi3/models/dinov2/layers/__pycache__/mlp.cpython-310.pyc +0 -0
  38. unish/pi3/models/dinov2/layers/__pycache__/patch_embed.cpython-310.pyc +0 -0
  39. unish/pi3/models/dinov2/layers/__pycache__/swiglu_ffn.cpython-310.pyc +0 -0
  40. unish/pi3/models/dinov2/models/__pycache__/__init__.cpython-310.pyc +0 -0
  41. unish/pi3/models/dinov2/models/__pycache__/vision_transformer.cpython-310.pyc +0 -0
  42. unish/pi3/models/layers/__pycache__/attention.cpython-310.pyc +0 -0
  43. unish/pi3/models/layers/__pycache__/block.cpython-310.pyc +0 -0
  44. unish/pi3/models/layers/__pycache__/camera_head.cpython-310.pyc +0 -0
  45. unish/pi3/models/layers/__pycache__/pos_embed.cpython-310.pyc +0 -0
  46. unish/pi3/models/layers/__pycache__/transformer_head.cpython-310.pyc +0 -0
  47. unish/pi3/models/layers/attention.py +2 -2
  48. unish/pi3/utils/__pycache__/geometry.cpython-310.pyc +0 -0
  49. unish/utils/__pycache__/constants.cpython-310.pyc +0 -0
  50. unish/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.mp4 filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🏃‍♂️
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.0.0
8
  app_file: app.py
9
  pinned: false
10
  license: cc-by-nc-4.0
 
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 5.1.0
8
  app_file: app.py
9
  pinned: false
10
  license: cc-by-nc-4.0
app.py CHANGED
@@ -1,19 +1,8 @@
1
- import huggingface_hub
2
-
3
- if not hasattr(huggingface_hub, "HfFolder"):
4
- class HfFolder:
5
- @staticmethod
6
- def save_token(token):
7
- pass
8
- @staticmethod
9
- def get_token():
10
- return os.environ.get("HF_TOKEN")
11
-
12
- huggingface_hub.HfFolder = HfFolder
13
-
14
  import gradio as gr
15
- import spaces
16
  import os
 
 
 
17
  import sys
18
  import shutil
19
  import tempfile
@@ -22,525 +11,476 @@ import cv2
22
  import subprocess
23
  import numpy as np
24
  import trimesh
 
25
  from huggingface_hub import hf_hub_download
 
 
 
26
 
27
- # --- Patch SAM 2 Installation ---
28
- # Since we can't use Docker, we run the patch logic at runtime before imports that might need it.
29
- # However, for a persistent install, we usually need to do this at build time.
30
- # In Hugging Face Spaces (Gradio SDK), we can use a pre-start script or run it here if it's not too late.
31
- # But `requirements.txt` is installed BEFORE app.py runs.
32
- #
33
- # Strategy:
34
- # 1. We removed sam-2 from requirements.txt to pass the build.
35
- # 2. We install it manually here on first run.
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  def install_sam2():
38
  try:
39
  import sam2
40
- print("SAM 2 already installed.")
41
  except ImportError:
42
  print("Installing SAM 2 with patch...")
43
- # Clone, Patch, Install
44
  subprocess.run(["git", "clone", "https://github.com/facebookresearch/segment-anything-2.git", "_tmp_sam2"], check=True)
45
-
46
  setup_path = "_tmp_sam2/setup.py"
47
  with open(setup_path, "r") as f:
48
  content = f.read()
49
-
50
- # Patch the requirement
51
  content = content.replace("torch>=2.5.1", "torch>=2.4.1")
52
-
53
  with open(setup_path, "w") as f:
54
  f.write(content)
55
-
56
- subprocess.run(["pip", "install", "--no-build-isolation", "-v", "."], cwd="_tmp_sam2", check=True)
57
- # Cleanup
58
  shutil.rmtree("_tmp_sam2")
59
- print("SAM 2 installed successfully.")
60
 
61
- # Execute installation
62
  install_sam2()
63
- # --------------------------------
64
 
65
- # Add current directory to path
66
- sys.path.append(os.path.dirname(os.path.abspath(__file__)))
67
 
 
68
  from unish.utils.inference_utils import (
69
- load_model,
70
- process_video,
71
- run_inference,
72
  generate_mixed_geometries_in_memory,
73
- save_smpl_meshes_per_frame,
74
- save_scene_only_point_clouds,
75
- save_human_point_clouds,
76
- save_camera_parameters_per_frame
77
  )
78
 
79
  MODEL = None
80
  BODY_MODELS_PATH = "body_models/"
81
 
 
 
 
82
  def download_smpl_assets(body_models_path):
83
- """
84
- Download SMPL models from private repository if they don't exist.
85
- The path logic mimics SMPLWrapper's expectation:
86
- 1. SMPLWrapper appends 'smpl' if not present in body_models_path.
87
- 2. smplx library expects another 'smpl' folder inside that (or appends it).
88
- Based on existing structure 'body_models/smpl/smpl/SMPL_*.pkl', the target dir is constructed below.
89
- """
90
  if 'smpl' not in body_models_path:
91
  model_path = os.path.join(body_models_path, 'smpl')
92
  else:
93
  model_path = body_models_path
94
-
95
- # smplx looks for a 'smpl' folder inside the given model_path
96
  target_dir = os.path.join(model_path, 'smpl')
97
-
98
  os.makedirs(target_dir, exist_ok=True)
99
-
100
  files = ["SMPL_NEUTRAL.pkl", "SMPL_MALE.pkl", "SMPL_FEMALE.pkl"]
101
  token = os.environ.get("SMPL_DOWNLOAD_TOKEN")
102
 
103
- if not token:
104
- # Check if files exist locally (e.g. uploaded in repo)
105
- missing = [f for f in files if not os.path.exists(os.path.join(target_dir, f))]
106
- if missing:
107
- print(f"Warning: SMPL models missing: {missing} and SMPL_DOWNLOAD_TOKEN not set.")
108
- return
109
 
110
  repo_id = "erik0/SMPL_Body_Models"
111
-
112
  for filename in files:
113
  file_path = os.path.join(target_dir, filename)
114
  if not os.path.exists(file_path):
115
  try:
116
- print(f"Downloading {filename}...")
117
- downloaded_path = hf_hub_download(
118
- repo_id=repo_id,
119
- filename=f"smpl/{filename}",
120
- token=token,
121
- local_dir=target_dir,
122
- local_dir_use_symlinks=False
123
- )
124
- # Move if structure is slightly off (hf_hub_download maintains path in repo)
125
- # The repo structure is likely smpl/SMPL_*.pkl, so local_dir/smpl/SMPL_*.pkl
126
- # We want it exactly at target_dir/SMPL_*.pkl
127
- # Adjusting based on actual download behavior
128
-
129
- except Exception as e:
130
- print(f"Failed to download {filename}: {e}")
131
 
132
  def pack_sequence_to_glb(base_dir, output_path, start_frame, end_frame, scene_rate=1.0):
133
- """
134
- Pack a sequence of meshes/pointclouds into a single GLB file for visualization.
135
- Uses a specific naming convention 'frame_{i}' for each mesh to allow
136
- 'stop motion' style visualization in the frontend.
137
- """
138
- # Create a scene
139
  scene = trimesh.Scene()
140
 
141
- # 1. Add Accumulated Scene Point Cloud (Static Background)
142
- # The frontend looks for 'scene' in the name to adjust point size.
143
- scene_ply = os.path.join(os.path.dirname(output_path), f"{os.path.basename(base_dir)}_scene.ply")
144
- if os.path.exists(scene_ply):
145
- scene_pc = trimesh.load(scene_ply)
146
- # Add to scene
147
- scene.add_geometry(scene_pc, node_name='scene_cloud', geom_name='scene_cloud')
148
-
149
- # 2. Iterate over frames and add Human Meshes
150
- # The frontend looks for 'frame_{i}' to toggle visibility.
151
  for i in range(start_frame, end_frame):
152
- human_mesh_path = os.path.join(base_dir, f"smpl_{i:06d}.ply")
153
- if os.path.exists(human_mesh_path):
154
- human_mesh = trimesh.load(human_mesh_path)
 
 
 
 
 
 
155
 
156
- # Color it if needed (default logic seemed to want colors)
157
- # But usually ply has colors. If not, maybe set a default.
158
- # user previous logic: human_mesh.visual.vertex_colors = [200, 100, 100, 255]
159
-
160
- # Naming is CRITICAL for the frontend logic
161
- node_name = f"frame_{i}"
162
- scene.add_geometry(human_mesh, node_name=node_name, geom_name=node_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
  scene.export(output_path)
 
 
165
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
- def get_player_html(glb_path):
168
- """
169
- Generate HTML to display the GLB file using Three.js with 'Stop Motion' logic.
170
- Extracts logic from index.html provided by user.
 
 
 
 
 
 
 
 
 
 
 
 
171
  """
172
- import base64
173
- with open(glb_path, "rb") as f:
174
- data = f.read()
175
- b64_data = base64.b64encode(data).decode('utf-8')
176
-
177
- html = f"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  <!DOCTYPE html>
179
  <html>
180
  <head>
181
  <meta charset="utf-8">
182
  <style>
183
- #canvas-container {{
184
- width: 100%;
185
- height: 600px; /* Increased height */
186
- background: #f5f5f5;
187
- border-radius: 8px;
188
- position: relative;
189
- overflow: hidden;
190
- box-shadow: inset 0 0 20px rgba(0,0,0,0.05);
191
- }}
192
-
193
- #loading-overlay {{
194
- position: absolute; top:0; left:0; width:100%; height:100%;
195
- background: rgba(0,0,0,0.7); color: white;
196
- display: flex; flex-direction: column; justify-content: center; align-items: center;
197
- z-index: 10;
198
- }}
199
-
200
- .player-controls {{
201
- margin-top: 10px;
202
- padding: 10px;
203
- background: #fff;
204
- border-radius: 8px;
205
- box-shadow: 0 2px 10px rgba(0,0,0,0.1);
206
- display: flex;
207
- align-items: center;
208
- gap: 15px;
209
- }}
210
-
211
- .button {{
212
- padding: 8px 16px;
213
- border: none;
214
- border-radius: 4px;
215
- background: #363636;
216
- color: white;
217
- cursor: pointer;
218
- font-weight: bold;
219
- }}
220
- .button:hover {{ background: #4a4a4a; }}
221
-
222
- input[type=range] {{
223
- flex-grow: 1;
224
- cursor: pointer;
225
- }}
226
-
227
- .tag {{
228
- background: #f5f5f5;
229
- padding: 5px 10px;
230
- border-radius: 4px;
231
- font-family: monospace;
232
- white-space: nowrap;
233
- }}
234
  </style>
235
-
236
- <!-- Import Map for Three.js -->
237
  <script type="importmap">
238
- {{
239
- "imports": {{
240
- "three": "https://unpkg.com/three@0.158.0/build/three.module.js",
241
- "three/addons/": "https://unpkg.com/three@0.158.0/examples/jsm/"
242
- }}
243
- }}
244
  </script>
245
  </head>
246
  <body>
247
- <div id="player-wrapper">
248
- <div id="canvas-container">
249
- <div id="loading-overlay">
250
- <p>Loading 3D Sequence...</p>
251
- </div>
252
- </div>
253
-
254
- <div class="player-controls">
255
- <button id="play-btn" class="button">Play</button>
256
- <input id="frame-slider" type="range" min="0" max="0" value="0" step="1">
257
- <span id="frame-count" class="tag">Frame: 0</span>
258
- </div>
259
- </div>
260
-
261
  <script type="module">
262
  import * as THREE from 'three';
263
  import {{ OrbitControls }} from 'three/addons/controls/OrbitControls.js';
264
  import {{ GLTFLoader }} from 'three/addons/loaders/GLTFLoader.js';
265
-
266
- // Config
267
- const MODEL_DATA = "data:model/gltf-binary;base64,{b64_data}";
268
- const FPS = 10;
269
-
270
- let scene, camera, renderer, controls;
271
- let frames = [];
272
- let currentFrame = 0;
273
- let isPlaying = false;
274
- let intervalId = null;
275
-
276
- const container = document.getElementById('canvas-container');
277
- const slider = document.getElementById('frame-slider');
278
- const playBtn = document.getElementById('play-btn');
279
- const frameLabel = document.getElementById('frame-count');
280
- const loadingOverlay = document.getElementById('loading-overlay');
281
-
282
- init();
283
-
284
- function init() {{
285
- scene = new THREE.Scene();
286
- scene.background = new THREE.Color(0xf5f5f5);
287
-
288
- // Setup Camera
289
- camera = new THREE.PerspectiveCamera(50, container.clientWidth / container.clientHeight, 0.1, 1000);
290
- // Initial position - will be roughly looking at origin
291
- camera.position.set(0, 0, 5);
292
-
293
- renderer = new THREE.WebGLRenderer({{ antialias: true, alpha: true }});
294
- renderer.setSize(container.clientWidth, container.clientHeight);
295
- renderer.setPixelRatio(window.devicePixelRatio);
296
- renderer.useLegacyLights = false;
297
-
298
- container.appendChild(renderer.domElement);
299
-
300
- // Lights
301
- const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 3.0);
302
- scene.add(hemiLight);
303
-
304
- const dirLight = new THREE.DirectionalLight(0xffffff, 3.0);
305
- dirLight.position.set(5, 10, 7);
306
- scene.add(dirLight);
307
-
308
- const frontLight = new THREE.DirectionalLight(0xffffff, 2.0);
309
- frontLight.position.set(0, 0, 5);
310
- scene.add(frontLight);
311
-
312
- // Controls
313
- controls = new OrbitControls(camera, renderer.domElement);
314
- controls.enableDamping = true;
315
- controls.dampingFactor = 0.05;
316
- controls.target.set(0, 0, 0);
317
-
318
- // Load GLB
319
- const loader = new GLTFLoader();
320
-
321
- loader.load(MODEL_DATA, function (gltf) {{
322
- const root = gltf.scene;
323
- scene.add(root);
324
-
325
- frames = [];
326
-
327
- // Traverse to find frames and fix materials
328
- root.traverse((node) => {{
329
-
330
- // Material adjustments for visual appeal
331
- if (node.isMesh) {{
332
- node.geometry.computeVertexNormals();
333
- // Reset colors if needed to avoid black meshes
334
- if (node.geometry.attributes.color) {{
335
- // Optionally keep vertex colors if they are good
336
- // node.geometry.deleteAttribute('color');
337
- }}
338
-
339
- // Simple material override for consistency
340
- node.material = new THREE.MeshStandardMaterial({{
341
- color: 0xff9966,
342
- roughness: 0.4,
343
- metalness: 0.0,
344
- side: THREE.DoubleSide
345
- }});
346
- }}
347
-
348
- // Point cloud adjustments
349
- if (node.isPoints) {{
350
- if (node.name && node.name.toLowerCase().includes('scene')) {{
351
- node.material.size = 0.05;
352
- node.material.sizeAttenuation = true;
353
- }}
354
- if (node.name && node.name.toLowerCase().includes('human')) {{
355
- node.material.size = 0.005;
356
- }}
357
- }}
358
-
359
- // Collect Frames based on naming convention
360
- if (node.name && node.name.startsWith('frame_')) {{
361
- const parts = node.name.split('_');
362
- // Expect frame_0, frame_1, etc.
363
- if (parts.length >= 2 && !isNaN(parseInt(parts[1]))) {{
364
- const idx = parseInt(parts[1]);
365
- frames[idx] = node;
366
- node.visible = false;
367
- }}
368
- }}
369
  }});
370
-
371
- // Filter out undefined slots in case of non-sequential or gaps (though logic should be sequential)
372
- // Note: If using frames[idx], gaps might leave undefined.
373
- // Better to compact if we just want a sequence, but index matching is good.
374
- // Let's rely on filter to clean up.
375
- frames = frames.filter(n => n !== undefined);
376
-
377
- console.log(`Loaded ${{frames.length}} frames.`);
378
-
379
- if (frames.length > 0) {{
380
- slider.max = frames.length - 1;
381
- loadingOverlay.style.display = 'none';
382
- showFrame(0);
383
-
384
- // Auto-center camera on the first frame if possible
385
- // const box = new THREE.Box3().setFromObject(frames[0]);
386
- // const center = box.getCenter(new THREE.Vector3());
387
- // controls.target.copy(center);
388
- // camera.position.add(center); // Adjust relative to new center
389
-
390
- }} else {{
391
- loadingOverlay.innerHTML = "<p>No frames found (looking for nodes named 'frame_N').</p>";
392
- }}
393
-
394
- }}, undefined, function (error) {{
395
- console.error(error);
396
- loadingOverlay.innerHTML = "<p>Error loading model.</p>";
397
- }});
398
-
399
- window.addEventListener('resize', onWindowResize);
400
- animate();
401
- }}
402
-
403
  function showFrame(idx) {{
404
- if (!frames[idx]) return;
405
- if (frames[currentFrame]) frames[currentFrame].visible = false;
406
- frames[idx].visible = true;
407
- currentFrame = idx;
408
- slider.value = idx;
409
- frameLabel.innerText = `Frame: ${{idx}}`;
410
- }}
411
-
412
- function togglePlay() {{
413
- if (frames.length === 0) return;
414
- isPlaying = !isPlaying;
415
-
416
- playBtn.innerText = isPlaying ? "Pause" : "Play";
417
-
418
- if (isPlaying) {{
419
- intervalId = setInterval(() => {{
420
- let next = currentFrame + 1;
421
- if (next >= frames.length) next = 0;
422
- showFrame(next);
423
- }}, 1000 / FPS);
424
- }} else {{
425
- clearInterval(intervalId);
426
- }}
427
- }}
428
-
429
- slider.addEventListener('input', (e) => {{
430
- if (isPlaying) togglePlay();
431
- showFrame(parseInt(e.target.value));
432
- }});
433
-
434
- playBtn.addEventListener('click', togglePlay);
435
-
436
- function onWindowResize() {{
437
- camera.aspect = container.clientWidth / container.clientHeight;
438
- camera.updateProjectionMatrix();
439
- renderer.setSize(container.clientWidth, container.clientHeight);
440
- }}
441
-
442
- function animate() {{
443
- requestAnimationFrame(animate);
444
- controls.update();
445
- renderer.render(scene, camera);
446
  }}
 
 
 
 
 
 
 
 
 
 
447
  </script>
448
  </body>
449
  </html>
450
  """
451
- return html
452
 
453
- @spaces.GPU(duration=120)
454
- def predict(video_path, duration):
455
- # Create a temporary directory for outputs
456
- output_dir = tempfile.mkdtemp()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457
 
458
- # 1. Preprocess Video (Trim)
459
- # Trim to specified duration
460
  trimmed_video_path = os.path.join(output_dir, "input_trim.mp4")
 
461
 
462
- # Use ffmpeg to trim
463
- cmd = [
464
- "ffmpeg", "-y",
465
- "-i", video_path,
466
- "-t", str(duration),
467
- "-c:v", "libx264", "-c:a", "aac",
468
- trimmed_video_path
469
- ]
470
  subprocess.run(cmd, check=True)
471
 
472
- # 2. Load Model
473
  global MODEL
474
- if MODEL is None:
475
- MODEL = load_model()
476
-
477
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
478
  MODEL.to(device)
479
  MODEL.eval()
480
 
481
- # 3. Process Video
482
- fps = 6.0
483
- target_size = 518
484
- human_idx = 0
485
- bbox_scale = 1.0
486
-
487
- # Check and download SMPL assets
488
  download_smpl_assets(BODY_MODELS_PATH)
489
 
490
- data_dict = process_video(
491
- trimmed_video_path, fps, human_idx, target_size,
492
- bbox_scale=bbox_scale
493
- )
494
-
495
- # 4. Run Inference
496
- results = run_inference(MODEL, data_dict, device, chunk_size=30)
497
 
498
- # 5. Generate Geometries & Save
499
  seq_name = results['seq_name']
500
-
501
- viz_scene_point_clouds, viz_smpl_meshes, viz_scene_only_point_clouds, smpl_points_for_camera = generate_mixed_geometries_in_memory(
502
- results, BODY_MODELS_PATH, fps=fps, conf_thres=0.1
503
  )
504
 
505
- # Save to disk
506
  save_smpl_meshes_per_frame(results, output_dir, BODY_MODELS_PATH)
507
- save_scene_only_point_clouds(viz_scene_only_point_clouds, output_dir, seq_name)
508
- save_human_point_clouds(viz_scene_point_clouds, viz_scene_only_point_clouds, output_dir, seq_name, results)
509
 
510
- # 6. Pack to GLB
511
  base_dir = os.path.join(output_dir, seq_name)
512
- output_glb_path = os.path.join(output_dir, "output.glb")
 
 
 
 
513
 
514
- num_frames = len(viz_scene_point_clouds)
515
-
516
- pack_sequence_to_glb(
517
- base_dir,
518
- output_glb_path,
519
- start_frame=0,
520
- end_frame=num_frames,
521
- scene_rate=0.5
522
- )
523
-
524
- return get_player_html(output_glb_path)
 
525
 
526
- with gr.Blocks() as demo:
527
- gr.Markdown("# UniSH Demo")
528
- gr.Markdown("Upload a video to reconstruct scene and human in 3D.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529
 
530
- with gr.Row():
531
- with gr.Column():
532
- input_video = gr.Video(label="Input Video")
533
- duration_slider = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Duration to Process (seconds)")
534
- submit_btn = gr.Button("Run Inference", variant="primary")
535
 
536
- with gr.Column():
537
- output_html = gr.HTML(label="3D Result", min_height=600)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
539
  submit_btn.click(
540
- predict,
541
- inputs=[input_video, duration_slider],
542
  outputs=[output_html]
543
  )
544
 
545
  demo.queue()
546
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
  import os
3
+ import time
4
+ import requests
5
+ import spaces
6
  import sys
7
  import shutil
8
  import tempfile
 
11
  import subprocess
12
  import numpy as np
13
  import trimesh
14
+ import open3d as o3d
15
  from huggingface_hub import hf_hub_download
16
+ import html
17
+ import base64
18
+ import inspect
19
 
20
+ # ==========================================
21
+ # 1. 核心路径与环境配置
22
+ # ==========================================
23
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
24
+ STATIC_DIR = os.path.join(BASE_DIR, "static")
25
+ EXAMPLES_DIR = os.path.join(BASE_DIR, "examples")
 
 
 
26
 
27
+ # ==========================================
28
+ # 2. 静态资源准备
29
+ # ==========================================
30
+ def prepare_local_assets():
31
+ os.makedirs(STATIC_DIR, exist_ok=True)
32
+ base_url = "https://registry.npmmirror.com/three/0.160.0/files"
33
+ assets = {
34
+ "three.module.js": f"{base_url}/build/three.module.js",
35
+ "OrbitControls.js": f"{base_url}/examples/jsm/controls/OrbitControls.js",
36
+ "GLTFLoader.js": f"{base_url}/examples/jsm/loaders/GLTFLoader.js",
37
+ "BufferGeometryUtils.js": f"{base_url}/examples/jsm/utils/BufferGeometryUtils.js"
38
+ }
39
+
40
+ for name, url in assets.items():
41
+ path = os.path.join(STATIC_DIR, name)
42
+ if not os.path.exists(path):
43
+ try:
44
+ r = requests.get(url, verify=False, timeout=10)
45
+ if r.status_code == 200:
46
+ with open(path, "wb") as f:
47
+ f.write(r.content)
48
+ except Exception as e:
49
+ print(f"Error downloading {name}: {e}")
50
+
51
+ prepare_local_assets()
52
+
53
+ # ==========================================
54
+ # 3. 环境 Patch (SAM 2)
55
+ # ==========================================
56
  def install_sam2():
57
  try:
58
  import sam2
 
59
  except ImportError:
60
  print("Installing SAM 2 with patch...")
 
61
  subprocess.run(["git", "clone", "https://github.com/facebookresearch/segment-anything-2.git", "_tmp_sam2"], check=True)
 
62
  setup_path = "_tmp_sam2/setup.py"
63
  with open(setup_path, "r") as f:
64
  content = f.read()
 
 
65
  content = content.replace("torch>=2.5.1", "torch>=2.4.1")
 
66
  with open(setup_path, "w") as f:
67
  f.write(content)
68
+ subprocess.run(["pip", "install", "--no-build-isolation", "--no-deps", "-v", "."], cwd="_tmp_sam2", check=True)
 
 
69
  shutil.rmtree("_tmp_sam2")
 
70
 
 
71
  install_sam2()
 
72
 
73
+ sys.path.append(BASE_DIR)
 
74
 
75
+ # 引入核心模型代码
76
  from unish.utils.inference_utils import (
77
+ load_model, process_video, run_inference,
 
 
78
  generate_mixed_geometries_in_memory,
79
+ save_smpl_meshes_per_frame
 
 
 
80
  )
81
 
82
  MODEL = None
83
  BODY_MODELS_PATH = "body_models/"
84
 
85
+ # ==========================================
86
+ # 4. 辅助函数
87
+ # ==========================================
88
  def download_smpl_assets(body_models_path):
 
 
 
 
 
 
 
89
  if 'smpl' not in body_models_path:
90
  model_path = os.path.join(body_models_path, 'smpl')
91
  else:
92
  model_path = body_models_path
 
 
93
  target_dir = os.path.join(model_path, 'smpl')
 
94
  os.makedirs(target_dir, exist_ok=True)
 
95
  files = ["SMPL_NEUTRAL.pkl", "SMPL_MALE.pkl", "SMPL_FEMALE.pkl"]
96
  token = os.environ.get("SMPL_DOWNLOAD_TOKEN")
97
 
98
+ if not token: return
 
 
 
 
 
99
 
100
  repo_id = "erik0/SMPL_Body_Models"
 
101
  for filename in files:
102
  file_path = os.path.join(target_dir, filename)
103
  if not os.path.exists(file_path):
104
  try:
105
+ hf_hub_download(repo_id=repo_id, filename=f"smpl/{filename}", token=token, local_dir=target_dir, local_dir_use_symlinks=False)
106
+ except Exception:
107
+ pass
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  def pack_sequence_to_glb(base_dir, output_path, start_frame, end_frame, scene_rate=1.0):
 
 
 
 
 
 
110
  scene = trimesh.Scene()
111
 
112
+ scene_cloud_dir = os.path.join(base_dir, "scene_clouds_per_frame")
113
+ smpl_mesh_dir = os.path.join(base_dir, "smpl_meshes_per_frame")
114
+
115
+ # 60,000 点/帧
116
+ MAX_POINTS_PER_FRAME = 60000
117
+
 
 
 
 
118
  for i in range(start_frame, end_frame):
119
+ candidates = [
120
+ os.path.join(smpl_mesh_dir, f"combined_smpl_mesh_frame_{i:04d}.ply"),
121
+ os.path.join(smpl_mesh_dir, f"smpl_mesh_frame_{i:04d}.ply")
122
+ ]
123
+ target_human_path = None
124
+ for p in candidates:
125
+ if os.path.exists(p):
126
+ target_human_path = p
127
+ break
128
 
129
+ if target_human_path:
130
+ try:
131
+ human_mesh = trimesh.load(target_human_path)
132
+ node_name = f"frame_{i}_human"
133
+ scene.add_geometry(human_mesh, node_name=node_name, geom_name=node_name)
134
+ except Exception:
135
+ pass
136
+
137
+ scene_pcd_path = os.path.join(scene_cloud_dir, f"scene_frame_{i:04d}.ply")
138
+ if os.path.exists(scene_pcd_path):
139
+ try:
140
+ scene_pc = trimesh.load(scene_pcd_path)
141
+ if hasattr(scene_pc, 'vertices') and len(scene_pc.vertices) > 0:
142
+ num_points = len(scene_pc.vertices)
143
+ if num_points > MAX_POINTS_PER_FRAME:
144
+ choice = np.random.choice(num_points, MAX_POINTS_PER_FRAME, replace=False)
145
+ scene_pc.vertices = scene_pc.vertices[choice]
146
+ if hasattr(scene_pc, 'colors') and len(scene_pc.colors) > 0:
147
+ scene_pc.colors = scene_pc.colors[choice]
148
+ node_name = f"frame_{i}_scene"
149
+ scene.add_geometry(scene_pc, node_name=node_name, geom_name=node_name)
150
+ except Exception:
151
+ pass
152
+
153
+ if len(scene.geometry) == 0:
154
+ dummy = trimesh.creation.box(extents=[0.01, 0.01, 0.01])
155
+ scene.add_geometry(dummy, node_name='dummy')
156
 
157
  scene.export(output_path)
158
+ if not os.path.exists(output_path):
159
+ raise FileNotFoundError(f"Export failed: {output_path}")
160
 
161
+ def get_video_duration(video_path):
162
+ if not video_path: return 10.0
163
+ try:
164
+ cap = cv2.VideoCapture(video_path)
165
+ if not cap.isOpened(): return 10.0
166
+ fps = cap.get(cv2.CAP_PROP_FPS)
167
+ frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
168
+ duration = frame_count / fps if fps > 0 else 10.0
169
+ cap.release()
170
+ return duration
171
+ except:
172
+ return 10.0
173
 
174
+ # ==========================================
175
+ # 5. 前端逻辑: Player & Loading HTML
176
+ # ==========================================
177
+
178
+ def get_loading_html(message="Processing..."):
179
+ return f"""
180
+ <div style="height: 600px; width: 100%; background: #f9fafb; border-radius: 12px; border: 1px solid #e5e7eb; display: flex; flex-direction: column; align-items: center; justify-content: center; font-family: sans-serif; color: #4b5563;">
181
+ <div class="loader-ring"></div>
182
+ <p style="margin-top: 20px; font-weight: 500; font-size: 1.1em; animation: pulse 2s infinite;">{message}</p>
183
+ <style>
184
+ .loader-ring {{ display: inline-block; width: 64px; height: 64px; }}
185
+ .loader-ring:after {{ content: " "; display: block; width: 46px; height: 46px; margin: 8px; border-radius: 50%; border: 5px solid #4f46e5; border-color: #4f46e5 transparent #4f46e5 transparent; animation: ring-spin 1.2s linear infinite; }}
186
+ @keyframes ring-spin {{ 0% {{ transform: rotate(0deg); }} 100% {{ transform: rotate(360deg); }} }}
187
+ @keyframes pulse {{ 0% {{ opacity: 0.6; }} 50% {{ opacity: 1; }} 100% {{ opacity: 0.6; }} }}
188
+ </style>
189
+ </div>
190
  """
191
+
192
+ def get_player_html(abs_glb_path):
193
+ def read_and_patch(filename):
194
+ path = os.path.join(STATIC_DIR, filename)
195
+ if not os.path.exists(path): return ""
196
+ with open(path, "r", encoding="utf-8") as f: content = f.read()
197
+ content = content.replace('../utils/BufferGeometryUtils.js', 'BufferGeometryUtils')
198
+ content = content.replace('./BufferGeometryUtils.js', 'BufferGeometryUtils')
199
+ content = content.replace('../../build/three.module.js', 'three')
200
+ content = content.replace('../build/three.module.js', 'three')
201
+ return content
202
+
203
+ js_three = read_and_patch("three.module.js")
204
+ js_orbit = read_and_patch("OrbitControls.js")
205
+ js_loader = read_and_patch("GLTFLoader.js")
206
+ js_buffer = read_and_patch("BufferGeometryUtils.js")
207
+
208
+ def to_data_url(content, mime="text/javascript"):
209
+ b64 = base64.b64encode(content.encode('utf-8')).decode('utf-8')
210
+ return f"data:{mime};base64,{b64}"
211
+
212
+ blob_three = to_data_url(js_three)
213
+ blob_orbit = to_data_url(js_orbit)
214
+ blob_loader = to_data_url(js_loader)
215
+ blob_buffer = to_data_url(js_buffer)
216
+
217
+ if not os.path.exists(abs_glb_path): return '<div style="color:red; padding:20px;">Error: Output file not found.</div>'
218
+ with open(abs_glb_path, "rb") as f: glb_data = f.read()
219
+ model_data_url = f"data:model/gltf-binary;base64,{base64.b64encode(glb_data).decode('utf-8')}"
220
+
221
+ raw_html = f"""
222
  <!DOCTYPE html>
223
  <html>
224
  <head>
225
  <meta charset="utf-8">
226
  <style>
227
+ body {{ margin: 0; background: transparent; height: 100vh; overflow: hidden; font-family: sans-serif; }}
228
+ #container {{ width: 100%; height: 100%; }}
229
+ .controls {{ position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; gap: 12px; padding: 12px 20px; background: rgba(255, 255, 255, 0.9); border-radius: 30px; z-index: 100; box-shadow: 0 4px 15px rgba(0,0,0,0.15); backdrop-filter: blur(5px); align-items: center; }}
230
+ button {{ padding: 8px 20px; cursor: pointer; background: #4f46e5; color: white; border: none; border-radius: 20px; font-weight: 600; font-size: 14px; transition: background 0.2s; }}
231
+ button:hover {{ background: #4338ca; }}
232
+ input[type=range] {{ width: 200px; cursor: pointer; accent-color: #4f46e5; }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  </style>
 
 
234
  <script type="importmap">
235
+ {{ "imports": {{ "three": "{blob_three}", "three/addons/controls/OrbitControls.js": "{blob_orbit}", "three/addons/loaders/GLTFLoader.js": "{blob_loader}", "BufferGeometryUtils": "{blob_buffer}" }} }}
 
 
 
 
 
236
  </script>
237
  </head>
238
  <body>
239
+ <div id="container"></div>
240
+ <div class="controls"><button id="btn-play">Play</button><input type="range" id="slider" min="0" max="0" value="0" step="1"></div>
 
 
 
 
 
 
 
 
 
 
 
 
241
  <script type="module">
242
  import * as THREE from 'three';
243
  import {{ OrbitControls }} from 'three/addons/controls/OrbitControls.js';
244
  import {{ GLTFLoader }} from 'three/addons/loaders/GLTFLoader.js';
245
+ const container = document.getElementById('container');
246
+ const scene = new THREE.Scene(); scene.background = new THREE.Color(0xf9fafb);
247
+
248
+ const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100);
249
+ camera.position.set(0, 1.5, 4);
250
+
251
+ const renderer = new THREE.WebGLRenderer({{ antialias: true, alpha: true }}); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRatio(window.devicePixelRatio); container.appendChild(renderer.domElement);
252
+ const controls = new OrbitControls(camera, renderer.domElement); controls.enableDamping = true;
253
+ scene.add(new THREE.AmbientLight(0xffffff, 0.8)); const dirLight = new THREE.DirectionalLight(0xffffff, 1.2); dirLight.position.set(5, 10, 7); scene.add(dirLight);
254
+ let frames = []; let currentFrame = 0; let isPlaying = false; let timer = null;
255
+ new GLTFLoader().load("{model_data_url}", (gltf) => {{
256
+ gltf.scene.rotation.x = Math.PI; scene.add(gltf.scene);
257
+ gltf.scene.traverse(n => {{
258
+ if(n.name && n.name.startsWith('frame_')) {{
259
+ let parts = n.name.split('_'); let idx = parseInt(parts[1]);
260
+ if(!isNaN(idx)) {{ if(!frames[idx]) frames[idx] = []; frames[idx].push(n); n.visible = false; }}
261
+ }}
262
+ if(n.isMesh) {{ n.geometry.computeVertexNormals(); n.material = new THREE.MeshStandardMaterial({{ color: 0x6366f1, roughness: 0.4, metalness: 0.1 }}); }}
263
+ if(n.isPoints) {{
264
+ let size = n.name.includes('scene') ? 0.05 : 0.005;
265
+ n.material.size = size;
266
+ }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
  }});
268
+ if(frames.length > 0) {{ document.getElementById('slider').max = frames.length - 1; showFrame(0); animate(); }}
269
+ }}, undefined, (e) => console.error(e));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  function showFrame(idx) {{
271
+ if(frames[currentFrame]) frames[currentFrame].forEach(o => o.visible = false);
272
+ if(frames[idx]) frames[idx].forEach(o => o.visible = true);
273
+ currentFrame = idx;
274
+ const slider = document.getElementById('slider');
275
+ if(slider) slider.value = idx;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  }}
277
+ function animate() {{ requestAnimationFrame(animate); controls.update(); renderer.render(scene, camera); }}
278
+ document.getElementById('btn-play').onclick = () => {{
279
+ isPlaying = !isPlaying; const btn = document.getElementById('btn-play');
280
+ if(isPlaying) {{
281
+ btn.innerText = "Pause"; btn.style.background = "#ef4444";
282
+ timer = setInterval(() => {{ if(frames.length > 0) {{ let next = (currentFrame + 1) % frames.length; showFrame(next); }} }}, 166);
283
+ }} else {{ clearInterval(timer); btn.innerText = "Play"; btn.style.background = "#4f46e5"; }}
284
+ }};
285
+ document.getElementById('slider').oninput = (e) => {{ if(isPlaying) document.getElementById('btn-play').click(); showFrame(parseInt(e.target.value)); }};
286
+ window.onresize = () => {{ camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); }};
287
  </script>
288
  </body>
289
  </html>
290
  """
291
+ return f'<iframe srcdoc="{html.escape(raw_html)}" width="100%" height="600px" style="border:none; border-radius: 12px;"></iframe>'
292
 
293
+ # ==========================================
294
+ # 6. 推理逻辑封装
295
+ # ==========================================
296
+ if os.environ.get("SPACE_ID"):
297
+ from spaces import GPU
298
+ gpu_decorator = GPU(duration=120)
299
+ else:
300
+ def gpu_decorator(func):
301
+ def wrapper(*args, **kwargs):
302
+ if inspect.isgeneratorfunction(func):
303
+ yield from func(*args, **kwargs)
304
+ else:
305
+ return func(*args, **kwargs)
306
+ return wrapper
307
+
308
+ @gpu_decorator
309
+ def predict(video_path, start_time, end_time):
310
+ # 1. 时长限制检测
311
+ duration_input = end_time - start_time
312
+ if duration_input > 10.0:
313
+ raise gr.Error(f"Video limit exceeded ({duration_input:.1f}s). Please keep it under 10 seconds.")
314
+
315
+ if start_time >= end_time:
316
+ raise gr.Error("Error: End time must be greater than Start time.")
317
+
318
+ # 2. 正常流程
319
+ yield get_loading_html("Processing...")
320
 
321
+ output_dir = tempfile.mkdtemp()
 
322
  trimmed_video_path = os.path.join(output_dir, "input_trim.mp4")
323
+ duration = end_time - start_time
324
 
325
+ cmd = ["ffmpeg", "-y", "-ss", str(start_time), "-i", video_path, "-t", str(duration), "-c:v", "libx264", "-c:a", "aac", trimmed_video_path]
 
 
 
 
 
 
 
326
  subprocess.run(cmd, check=True)
327
 
 
328
  global MODEL
329
+ if MODEL is None: MODEL = load_model()
 
 
330
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
331
  MODEL.to(device)
332
  MODEL.eval()
333
 
 
 
 
 
 
 
 
334
  download_smpl_assets(BODY_MODELS_PATH)
335
 
336
+ data_dict = process_video(trimmed_video_path, 6.0, 0, 518, bbox_scale=1.0)
337
+ results = run_inference(MODEL, data_dict, device, chunk_size=300)
 
 
 
 
 
338
 
 
339
  seq_name = results['seq_name']
340
+ viz_scene, viz_smpl, viz_scene_only, _ = generate_mixed_geometries_in_memory(
341
+ results, BODY_MODELS_PATH, fps=6.0, conf_thres=0.1
 
342
  )
343
 
 
344
  save_smpl_meshes_per_frame(results, output_dir, BODY_MODELS_PATH)
 
 
345
 
 
346
  base_dir = os.path.join(output_dir, seq_name)
347
+ scene_cloud_dir = os.path.join(base_dir, "scene_clouds_per_frame")
348
+ os.makedirs(scene_cloud_dir, exist_ok=True)
349
+ for i, pcd in enumerate(viz_scene_only):
350
+ if len(pcd.points) > 0:
351
+ o3d.io.write_point_cloud(os.path.join(scene_cloud_dir, f"scene_frame_{i:04d}.ply"), pcd)
352
 
353
+ tmp_glb_path = os.path.join(output_dir, "output.glb")
354
+ pack_sequence_to_glb(base_dir, tmp_glb_path, 0, len(viz_scene), 0.5)
355
+
356
+ yield get_player_html(tmp_glb_path)
357
+
358
+ # ==========================================
359
+ # 7. UI 构建
360
+ # ==========================================
361
+
362
+ examples_list = []
363
+ if os.path.exists(EXAMPLES_DIR):
364
+ examples_list = [[os.path.join("examples", f)] for f in os.listdir(EXAMPLES_DIR) if f.endswith(".mp4")]
365
 
366
+ js_scrub = """(val) => {
367
+ var video = document.querySelector('#input-video video');
368
+ if (video) {
369
+ video.currentTime = val;
370
+ }
371
+ return val;
372
+ }"""
373
+
374
+ js_reset_video = """() => {
375
+ setTimeout(() => {
376
+ var video = document.querySelector('#input-video video');
377
+ if (video) {
378
+ video.currentTime = 0;
379
+ video.pause();
380
+ }
381
+ }, 200);
382
+ }"""
383
+
384
+ custom_css = """
385
+ footer {visibility: hidden}
386
+ h1.header-title { text-align: center; font-family: 'Segoe UI', sans-serif; font-weight: 700; color: #1f2937; margin-bottom: 2rem; }
387
+ .alert-box { padding: 1rem; border-radius: 0.5rem; margin-bottom: 1rem; font-size: 0.9rem; line-height: 1.5; }
388
+ .info-box { background-color: #eff6ff; border-left: 4px solid #3b82f6; color: #1e40af; }
389
+ .warning-box { background-color: #fefce8; border-left: 4px solid #eab308; color: #854d0e; }
390
+ .tip-box { background-color: #f0fdf4; border-left: 4px solid #22c55e; color: #15803d; }
391
+ .viewer-container { box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); border-radius: 12px; overflow: hidden; border: 1px solid #e5e7eb; background: #f9fafb; }
392
+ #run-btn { background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%); border: none; color: white; font-weight: bold; transition: all 0.2s; }
393
+ #run-btn:hover { transform: translateY(-2px); box-shadow: 0 10px 20px rgba(99, 102, 241, 0.4); }
394
+
395
+ #input-video { margin-bottom: 15px !important; }
396
+ """
397
+
398
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", radius_size="md"), css=custom_css, title="UniSH Demo") as demo:
399
 
400
+ with gr.Column(elem_classes=["header-container"]):
401
+ gr.Markdown("# UniSH: Unifying Scene and Human Reconstruction in a Feed-Forward Pass", elem_classes=["header-title"])
402
+
403
+ with gr.Row(equal_height=False):
 
404
 
405
+ with gr.Column(scale=3, variant="panel"):
406
+ gr.Markdown("### 🛠️ Configuration")
407
+
408
+ with gr.Group():
409
+ input_video = gr.Video(label="Upload Video", format="mp4", height=260, interactive=True, elem_id="input-video")
410
+
411
+ with gr.Row():
412
+ start_time = gr.Slider(minimum=0, maximum=10, value=0, step=0.01, label="Start Time (s)")
413
+ end_time = gr.Slider(minimum=0, maximum=10, value=3, step=0.01, label="End Time (s)")
414
+
415
+ # --- Grid Layout 还原完整文案 ---
416
+ gr.HTML("""
417
+ <div class="alert-box tip-box">
418
+ <strong>💡 Use Tips:</strong>
419
+ <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px; font-size: 0.85em; line-height: 1.4;">
420
+ <div>1. Contain only a <strong>single person</strong>.</div>
421
+ <div>2. <strong>No occlusion</strong> (self-occlusion is fine).</div>
422
+ <div>3. Keep the <strong>full body</strong> mostly visible.</div>
423
+ <div>4. Subject should <strong>not be too small</strong>.</div>
424
+ </div>
425
+ </div>
426
+ """)
427
+
428
+ submit_btn = gr.Button("🚀 Start Reconstruction", variant="primary", elem_id="run-btn", size="lg")
429
+
430
+ if examples_list:
431
+ gr.Markdown("### 🎥 Examples")
432
+ gr.Examples(
433
+ examples=examples_list,
434
+ inputs=[input_video],
435
+ label="Click to try:",
436
+ cache_examples=False
437
+ )
438
+
439
+ with gr.Column(scale=7):
440
+ gr.Markdown("### ▶️ Interactive Results")
441
+ with gr.Group(elem_classes=["viewer-container"]):
442
+ output_html = gr.HTML(
443
+ label="3D Viewer",
444
+ min_height=600,
445
+ value='<div style="height:600px; display:flex; align-items:center; justify-content:center; color:#aaa; font-family:sans-serif; background:#f9fafb;">Upload a video and click Start to view result.</div>'
446
+ )
447
 
448
+ gr.HTML("""
449
+ <div class="alert-box warning-box">
450
+ <strong>⚡ Performance Note:</strong><br>
451
+ Inference (feed-forward) is very fast, but generating visualization assets takes up most of the processing time.
452
+ </div>
453
+ <div class="alert-box info-box">
454
+ <strong>👁️ Visual Quality:</strong><br>
455
+ The displayed results are downsampled for better web rendering performance.
456
+ </div>
457
+ """)
458
+
459
+ def update_slider_range(video_path):
460
+ if not video_path:
461
+ return gr.update(value=0), gr.update(value=0)
462
+ dur = get_video_duration(video_path)
463
+ dur = round(dur, 2)
464
+ return gr.update(maximum=dur, value=0), gr.update(maximum=dur, value=dur)
465
+
466
+ input_video.change(fn=update_slider_range, inputs=[input_video], outputs=[start_time, end_time])
467
+ input_video.upload(fn=update_slider_range, inputs=[input_video], outputs=[start_time, end_time])
468
+
469
+ input_video.change(fn=None, inputs=[], outputs=[], js=js_reset_video)
470
+ input_video.upload(fn=None, inputs=[], outputs=[], js=js_reset_video)
471
+
472
+ start_time.change(fn=None, inputs=[start_time], outputs=None, js=js_scrub)
473
+ end_time.change(fn=None, inputs=[end_time], outputs=None, js=js_scrub)
474
+
475
  submit_btn.click(
476
+ fn=predict,
477
+ inputs=[input_video, start_time, end_time],
478
  outputs=[output_html]
479
  )
480
 
481
  demo.queue()
482
+ demo.launch(
483
+ server_name="0.0.0.0",
484
+ server_port=7860,
485
+ allowed_paths=[BASE_DIR, "/tmp", EXAMPLES_DIR]
486
+ )
examples/example1.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd43192864836407058a17231def0945f428e24b4d8991e388abaa65513da9bd
3
+ size 15514979
examples/example2.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:785dbf176d123c971509d3d438d8f66ec3f4715c6277c81c6712a5cd91a28a7e
3
+ size 6870101
examples/example3.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3f1f781527cc026d6da5a6194516ee7ef11bb14976669b97f5eb660c7bedeec8
3
+ size 22927428
examples/example4.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:339a923007e20e2e6a6f0893fdd1f9a75b98f614f1c556c4f4aaae7939c81661
3
+ size 13952778
index.html ADDED
@@ -0,0 +1,582 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="description" content="UniSH: Unifying Scene and Human Reconstruction in a Feed-Forward Pass">
6
+ <meta name="keywords" content="UniSH, Scene Reconstruction, Human Mesh Recovery, NeRF">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1">
8
+ <title>UniSH: Unifying Scene and Human Reconstruction in a Feed-Forward Pass</title>
9
+
10
+ <link rel="stylesheet" href="./static/css/bulma.min.css">
11
+ <link rel="stylesheet" href="./static/css/bulma-carousel.min.css">
12
+ <link rel="stylesheet" href="./static/css/bulma-slider.min.css">
13
+ <link rel="stylesheet" href="./static/css/index.css">
14
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
15
+ <link href="https://fonts.googleapis.com/css?family=Google+Sans|Noto+Sans|Castoro" rel="stylesheet">
16
+
17
+ <style>
18
+ /* 1. 轮播图容器样式 */
19
+ .carousel .item {
20
+ position: relative;
21
+ border-radius: 12px;
22
+ overflow: hidden;
23
+ margin: 10px 5px;
24
+ border: 1px solid rgba(0,0,0,0.08);
25
+ box-shadow: 0 8px 20px rgba(0,0,0,0.12);
26
+ background: #000;
27
+ transform: translateZ(0);
28
+ }
29
+ .carousel .item video {
30
+ display: block;
31
+ width: 100%;
32
+ height: auto;
33
+ }
34
+
35
+ /* 2. 悬浮计数胶囊样式 */
36
+ .video-overlay-label {
37
+ position: absolute;
38
+ bottom: 15px;
39
+ left: 15px;
40
+ background-color: rgba(0, 0, 0, 0.6);
41
+ color: white;
42
+ padding: 5px 12px;
43
+ border-radius: 20px;
44
+ font-size: 0.8rem;
45
+ font-family: 'Consolas', 'Courier New', monospace;
46
+ font-weight: 600;
47
+ letter-spacing: 0.5px;
48
+ pointer-events: none;
49
+ backdrop-filter: blur(5px);
50
+ border: 1px solid rgba(255,255,255,0.1);
51
+ z-index: 10;
52
+ }
53
+
54
+ /* 3. Player 样式 */
55
+ #canvas-container {
56
+ width: 100%;
57
+ height: 500px;
58
+ background: #f5f5f5;
59
+ border-radius: 8px;
60
+ position: relative;
61
+ overflow: hidden;
62
+ box-shadow: inset 0 0 20px rgba(0,0,0,0.05);
63
+ }
64
+
65
+ /* 4. Teaser 媒体容器样式 */
66
+ .teaser-video-wrapper {
67
+ position: relative;
68
+ border-radius: 8px;
69
+ overflow: hidden;
70
+ border: 1px solid rgba(0,0,0,0.08);
71
+ box-shadow: 0 8px 20px rgba(0,0,0,0.12);
72
+ background: #000;
73
+ width: 100%;
74
+ margin-bottom: 20px; /* 视频和图片之间的间距 */
75
+ }
76
+
77
+ .teaser-video-wrapper video {
78
+ display: block;
79
+ width: 100%;
80
+ height: auto;
81
+ }
82
+
83
+ /* 作者名字样式 */
84
+ .author-block {
85
+ margin-right: 10px;
86
+ white-space: nowrap;
87
+ }
88
+
89
+ /* 自定义 Slider */
90
+ input[type=range].custom-slider {
91
+ -webkit-appearance: none;
92
+ width: 100%;
93
+ background: transparent;
94
+ }
95
+ input[type=range].custom-slider::-webkit-slider-thumb {
96
+ -webkit-appearance: none;
97
+ height: 16px;
98
+ width: 16px;
99
+ border-radius: 50%;
100
+ background: #363636;
101
+ cursor: pointer;
102
+ margin-top: -6px;
103
+ }
104
+ input[type=range].custom-slider::-webkit-slider-runnable-track {
105
+ width: 100%;
106
+ height: 4px;
107
+ cursor: pointer;
108
+ background: #dbdbdb;
109
+ border-radius: 2px;
110
+ }
111
+ </style>
112
+
113
+ <script type="importmap">
114
+ {
115
+ "imports": {
116
+ "three": "https://unpkg.com/three@0.158.0/build/three.module.js",
117
+ "three/addons/": "https://unpkg.com/three@0.158.0/examples/jsm/"
118
+ }
119
+ }
120
+ </script>
121
+ </head>
122
+ <body>
123
+
124
+ <section class="hero">
125
+ <div class="hero-body" style="padding-bottom: 0;">
126
+ <div class="container is-max-desktop">
127
+ <div class="columns is-centered">
128
+ <div class="column has-text-centered">
129
+ <h1 class="title is-1 publication-title">UniSH: Unifying Scene and Human Reconstruction in a Feed-Forward Pass</h1>
130
+
131
+ <div class="is-size-5 publication-authors">
132
+ <span class="author-block">Mengfei Li<sup>1</sup>,</span>
133
+ <span class="author-block">Peng Li<sup>1</sup>,</span>
134
+ <span class="author-block">Zheng Zhang<sup>2</sup>,</span>
135
+ <span class="author-block">Jiahao Lu<sup>1</sup>,</span>
136
+ <span class="author-block">Chengfeng Zhao<sup>1</sup>,</span>
137
+ <span class="author-block">Wei Xue<sup>1</sup>,</span>
138
+ <br>
139
+ <span class="author-block">Qifeng Liu<sup>1</sup>,</span>
140
+ <span class="author-block">Sida Peng<sup>3</sup>,</span>
141
+ <span class="author-block">Wenxiao Zhang<sup>1</sup>,</span>
142
+ <span class="author-block">Wenhan Luo<sup>1</sup>,</span>
143
+ <span class="author-block">Yuan Liu<sup>1&#8224;</sup>,</span>
144
+ <span class="author-block">Yike Guo<sup>1&#8224;</sup></span>
145
+ </div>
146
+
147
+ <div class="is-size-5 publication-authors">
148
+ <span class="author-block"><sup>1</sup>The Hong Kong University of Science and Technology</span>
149
+ <br>
150
+ <span class="author-block"><sup>2</sup>Beijing University of Posts and Telecommunications,</span>
151
+ <span class="author-block"><sup>3</sup>Zhejiang University</span>
152
+ </div>
153
+
154
+ <div class="is-size-6 publication-authors">
155
+ <span class="author-block"><sup>&#8224;</sup>Corresponding authors.</span>
156
+ </div>
157
+
158
+ <div class="column has-text-centered">
159
+ <div class="publication-links">
160
+ <span class="link-block">
161
+ <a href="https://arxiv.org/abs/2601.01222" class="external-link button is-normal is-rounded is-dark">
162
+ <span class="icon"><i class="fas fa-file-pdf"></i></span>
163
+ <span>arXiv</span>
164
+ </a>
165
+ </span>
166
+
167
+ <span class="link-block">
168
+ <a href="https://github.com/murphylmf/UniSH" class="external-link button is-normal is-rounded is-dark">
169
+ <span class="icon"><i class="fab fa-github"></i></span>
170
+ <span>Code</span>
171
+ </a>
172
+ </span>
173
+
174
+ <span class="link-block">
175
+ <a href="#BibTeX" class="external-link button is-normal is-rounded is-dark">
176
+ <span class="icon"><i class="fas fa-book"></i></span>
177
+ <span>BibTeX</span>
178
+ </a>
179
+ </span>
180
+ </div>
181
+ </div>
182
+ </div>
183
+ </div>
184
+ </div>
185
+ </div>
186
+ </section>
187
+
188
+ <section class="section" style="padding-top: 20px; padding-bottom: 0;">
189
+ <div class="container is-max-desktop">
190
+ <div class="hero-body has-text-centered" style="padding-top: 0;">
191
+
192
+ <div class="teaser-video-wrapper">
193
+ <video id="teaser-video" autoplay muted loop playsinline controls style="width: 100%; height: auto;">
194
+ <source src="./static/videos/teaser_video_final.mp4" type="video/mp4">
195
+ </video>
196
+ </div>
197
+
198
+ <div class="teaser-video-wrapper" style="background: #fff;">
199
+ <img src="./static/images/teaser.svg" alt="UniSH Teaser" style="width: 100%; display: block;">
200
+ </div>
201
+
202
+ <div class="content has-text-centered is-size-6" style="margin-top: 20px;">
203
+ Given a monocular video as input, our UniSH is capable of jointly reconstructing scene and human in a single forward pass, enabling effective estimation of scene geometry, camera parameters and SMPL parameters.
204
+ </div>
205
+ </div>
206
+ </div>
207
+ </section>
208
+
209
+ <section class="section" style="padding-top: 20px;">
210
+ <div class="container is-max-desktop">
211
+ <div class="columns is-centered has-text-centered">
212
+ <div class="column is-four-fifths">
213
+ <h2 class="title is-3">Abstract</h2>
214
+ <div class="content has-text-justified">
215
+ <p>
216
+ We present UniSH, a unified, feed-forward framework for joint metric-scale 3D scene and human reconstruction. A key challenge in this domain is the scarcity of large-scale, annotated real-world data, forcing a reliance on synthetic datasets. This reliance introduces a significant sim-to-real domain gap, leading to poor generalization, low-fidelity human geometry, and poor alignment on in-the-wild videos.
217
+ </p>
218
+ <p>
219
+ To address this, we propose an innovative training paradigm that effectively leverages unlabeled in-the-wild data. Our framework bridges strong, disparate priors from scene reconstruction and HMR, and is trained with two core components: (1) a robust distillation strategy to refine human surface details by distilling high-frequency details from an expert depth model, and (2) a two-stage supervision scheme, which first learns coarse localization on synthetic data, then fine-tunes on real data by directly optimizing the geometric correspondence between the SMPL mesh and the human point cloud. This approach enables our feed-forward model to jointly recover high-fidelity scene geometry, human point clouds, camera parameters, and coherent, metric-scale SMPL bodies, all in a single forward pass. Extensive experiments demonstrate that our model achieves state-of-the-art performance on human-centric scene reconstruction and delivers highly competitive results on global human motion estimation, comparing favorably against both optimization-based frameworks and HMR-only methods.
220
+ </p>
221
+ </div>
222
+ </div>
223
+ </div>
224
+ </div>
225
+ </section>
226
+
227
+ <section class="section">
228
+ <div class="container is-max-desktop">
229
+ <div class="columns is-centered has-text-centered">
230
+ <div class="column is-full-width">
231
+ <h2 class="title is-3">Method</h2>
232
+ <div class="content">
233
+ <img src="./static/images/framework.svg" alt="UniSH Framework" style="width: 100%; max-width: 1000px; margin-bottom: 10px;">
234
+ <p class="has-text-justified is-size-6">
235
+ <strong>The network architecture of UniSH.</strong>
236
+ UniSH takes a monocular video as input. The video frames are processed by the <strong>Reconstruction Branch</strong> to predict per-frame camera extrinsics <em>E</em>, confidence maps <em>C</em>, and pointmaps <em>P</em>. Camera intrinsics <em>K</em> are derived from the pointmaps. Human crops from the video are fed into the <strong>Human Body Branch</strong> along with <em>K</em> to estimate global SMPL shape parameters <em>&beta;</em> and per-frame pose parameters <em>&theta;<sub>i</sub></em>. Features from both branches are processed by <strong>AlignNet</strong> to predict the global scene scale <em>s</em> and per-frame SMPL translations <em>t<sub>i</sub></em> for coherent scene and human alignment. The indices (e.g., in <em>&theta;<sub>1</sub>, &theta;<sub>2</sub>, &theta;<sub>3</sub></em> and <em>t<sub>1</sub>, t<sub>2</sub>, t<sub>3</sub></em>) denote frame-specific parameters.
237
+ </p>
238
+ </div>
239
+ </div>
240
+ </div>
241
+ </div>
242
+ </section>
243
+
244
+ <section class="section">
245
+ <div class="container is-max-desktop">
246
+ <h2 class="title is-3 has-text-centered">Interactive Visualization</h2>
247
+ <div class="content has-text-centered">
248
+ <p>
249
+ Interactive 4D Player. <br>
250
+ <strong>Left Click</strong> to Rotate, <strong>Right Click</strong> to Pan, <strong>Scroll</strong> to Zoom. <br>
251
+ <span class="is-size-7" style="color: #666;">* Scene point clouds are downsampled for smoother web performance.</span>
252
+ </p>
253
+ </div>
254
+
255
+ <div class="columns is-centered">
256
+ <div class="column is-full-width">
257
+ <div class="box" style="padding: 10px; background: #f5f5f5;">
258
+ <div id="canvas-container">
259
+ <div id="loading-overlay" style="position: absolute; top:0; left:0; width:100%; height:100%; background: rgba(0,0,0,0.7); color: white; display: flex; flex-direction: column; justify-content: center; align-items: center; z-index: 10;">
260
+ <span class="icon is-large"><i class="fas fa-spinner fa-pulse"></i></span>
261
+ <p style="margin-top: 10px;">Loading 3D Sequence...</p>
262
+ </div>
263
+ </div>
264
+
265
+ <div class="columns is-vcentered is-mobile" style="margin-top: 10px; padding: 0 10px;">
266
+ <div class="column is-narrow">
267
+ <button id="play-btn" class="button is-dark is-rounded is-small">
268
+ <span class="icon is-small"><i class="fas fa-play"></i></span>
269
+ </button>
270
+ </div>
271
+ <div class="column">
272
+ <input id="frame-slider" class="slider is-fullwidth is-circle is-dark" step="1" min="0" max="0" value="0" type="range">
273
+ </div>
274
+ <div class="column is-narrow">
275
+ <span id="frame-count" class="tag is-light" style="width: 80px;">Frame: 0</span>
276
+ </div>
277
+ </div>
278
+ </div>
279
+
280
+ </div>
281
+ </div>
282
+ </div>
283
+ </section>
284
+
285
+ <section class="section">
286
+ <div class="container is-max-desktop">
287
+ <h2 class="title is-3 has-text-centered">More Visualization Results</h2>
288
+
289
+ <div id="results-carousel" class="carousel results-carousel">
290
+ <div class="item item-video1">
291
+ <video poster="" id="video1" autoplay controls muted loop playsinline height="100%">
292
+ <source src="./static/videos/case_1.mp4" type="video/mp4">
293
+ </video>
294
+ <div class="video-overlay-label">SEQ 01 / 12</div>
295
+ </div>
296
+ <div class="item item-video2">
297
+ <video poster="" id="video2" autoplay controls muted loop playsinline height="100%">
298
+ <source src="./static/videos/case_2.mp4" type="video/mp4">
299
+ </video>
300
+ <div class="video-overlay-label">SEQ 02 / 12</div>
301
+ </div>
302
+ <div class="item item-video3">
303
+ <video poster="" id="video3" autoplay controls muted loop playsinline height="100%">
304
+ <source src="./static/videos/case_4.mp4" type="video/mp4">
305
+ </video>
306
+ <div class="video-overlay-label">SEQ 03 / 12</div>
307
+ </div>
308
+ <div class="item item-video4">
309
+ <video poster="" id="video4" autoplay controls muted loop playsinline height="100%">
310
+ <source src="./static/videos/case_11.mp4" type="video/mp4">
311
+ </video>
312
+ <div class="video-overlay-label">SEQ 04 / 12</div>
313
+ </div>
314
+ <div class="item item-video5">
315
+ <video poster="" id="video5" autoplay controls muted loop playsinline height="100%">
316
+ <source src="./static/videos/case_3.mp4" type="video/mp4">
317
+ </video>
318
+ <div class="video-overlay-label">SEQ 05 / 12</div>
319
+ </div>
320
+ <div class="item item-video6">
321
+ <video poster="" id="video6" autoplay controls muted loop playsinline height="100%">
322
+ <source src="./static/videos/case_6.mp4" type="video/mp4">
323
+ </video>
324
+ <div class="video-overlay-label">SEQ 06 / 12</div>
325
+ </div>
326
+ <div class="item item-video7">
327
+ <video poster="" id="video7" autoplay controls muted loop playsinline height="100%">
328
+ <source src="./static/videos/case_7.mp4" type="video/mp4">
329
+ </video>
330
+ <div class="video-overlay-label">SEQ 07 / 12</div>
331
+ </div>
332
+ <div class="item item-video8">
333
+ <video poster="" id="video8" autoplay controls muted loop playsinline height="100%">
334
+ <source src="./static/videos/case_8.mp4" type="video/mp4">
335
+ </video>
336
+ <div class="video-overlay-label">SEQ 08 / 12</div>
337
+ </div>
338
+ <div class="item item-video9">
339
+ <video poster="" id="video9" autoplay controls muted loop playsinline height="100%">
340
+ <source src="./static/videos/case_9.mp4" type="video/mp4">
341
+ </video>
342
+ <div class="video-overlay-label">SEQ 09 / 12</div>
343
+ </div>
344
+ <div class="item item-video10">
345
+ <video poster="" id="video10" autoplay controls muted loop playsinline height="100%">
346
+ <source src="./static/videos/case_10.mp4" type="video/mp4">
347
+ </video>
348
+ <div class="video-overlay-label">SEQ 10 / 12</div>
349
+ </div>
350
+ <div class="item item-video11">
351
+ <video poster="" id="video11" autoplay controls muted loop playsinline height="100%">
352
+ <source src="./static/videos/case_5.mp4" type="video/mp4">
353
+ </video>
354
+ <div class="video-overlay-label">SEQ 11 / 12</div>
355
+ </div>
356
+ <div class="item item-video12">
357
+ <video poster="" id="video12" autoplay controls muted loop playsinline height="100%">
358
+ <source src="./static/videos/case_12.mp4" type="video/mp4">
359
+ </video>
360
+ <div class="video-overlay-label">SEQ 12 / 12</div>
361
+ </div>
362
+ </div>
363
+ </div>
364
+ </section>
365
+
366
+ <section class="section" id="BibTeX">
367
+ <div class="container is-max-desktop content">
368
+ <h2 class="title">BibTeX</h2>
369
+ <pre><code>@misc{li2026unishunifyingscenehuman,
370
+ title={UniSH: Unifying Scene and Human Reconstruction in a Feed-Forward Pass},
371
+ author={Mengfei Li and Peng Li and Zheng Zhang and Jiahao Lu and Chengfeng Zhao and Wei Xue and Qifeng Liu and Sida Peng and Wenxiao Zhang and Wenhan Luo and Yuan Liu and Yike Guo},
372
+ year={2026},
373
+ eprint={2601.01222},
374
+ archivePrefix={arXiv},
375
+ primaryClass={cs.CV},
376
+ url={https://arxiv.org/abs/2601.01222},
377
+ }</code></pre>
378
+ </div>
379
+ </section>
380
+
381
+ <footer class="footer">
382
+ <div class="container">
383
+ <div class="columns is-centered">
384
+ <div class="column is-8">
385
+ <div class="content">
386
+ <p>
387
+ This website is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.
388
+ Template borrowed from <a href="https://github.com/nerfies/nerfies.github.io">Nerfies</a>.
389
+ </p>
390
+ </div>
391
+ </div>
392
+ </div>
393
+ </div>
394
+ </footer>
395
+
396
+ <script src="./static/js/fontawesome.all.min.js"></script>
397
+ <script src="./static/js/bulma-carousel.min.js"></script>
398
+ <script src="./static/js/bulma-slider.min.js"></script>
399
+
400
+ <script>
401
+ document.addEventListener('DOMContentLoaded', () => {
402
+ var options = {
403
+ slidesToScroll: 1,
404
+ slidesToShow: 1,
405
+ loop: true,
406
+ infinite: true,
407
+ autoplay: false,
408
+ pagination: false,
409
+ };
410
+ var carousels = bulmaCarousel.attach('.carousel', options);
411
+ });
412
+ </script>
413
+
414
+ <script type="module">
415
+ import * as THREE from 'three';
416
+ import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
417
+ import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
418
+
419
+ // 配置
420
+ const MODEL_PATH = './static/models/sequence.glb';
421
+ const FPS = 10;
422
+
423
+ let scene, camera, renderer, controls;
424
+ let frames = [];
425
+ let currentFrame = 0;
426
+ let isPlaying = false;
427
+ let intervalId = null;
428
+
429
+ const container = document.getElementById('canvas-container');
430
+ const slider = document.getElementById('frame-slider');
431
+ const playBtn = document.getElementById('play-btn');
432
+ const frameLabel = document.getElementById('frame-count');
433
+ const loadingOverlay = document.getElementById('loading-overlay');
434
+
435
+ init();
436
+
437
+ function init() {
438
+ scene = new THREE.Scene();
439
+ scene.background = new THREE.Color(0xf5f5f5);
440
+
441
+ camera = new THREE.PerspectiveCamera(50, container.clientWidth / container.clientHeight, 0.1, 1000);
442
+ camera.position.set(-0.000, -4.272, 0.000);
443
+
444
+ renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
445
+ renderer.setSize(container.clientWidth, container.clientHeight);
446
+ renderer.setPixelRatio(window.devicePixelRatio);
447
+
448
+ renderer.shadowMap.enabled = false;
449
+ renderer.useLegacyLights = false;
450
+
451
+ container.appendChild(renderer.domElement);
452
+
453
+ const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 3.0);
454
+ scene.add(hemiLight);
455
+
456
+ const dirLight = new THREE.DirectionalLight(0xffffff, 3.0);
457
+ dirLight.position.set(5, 10, 7);
458
+ scene.add(dirLight);
459
+
460
+ const frontLight = new THREE.DirectionalLight(0xffffff, 2.0);
461
+ frontLight.position.set(0, 0, 5);
462
+ scene.add(frontLight);
463
+
464
+ controls = new OrbitControls(camera, renderer.domElement);
465
+ controls.enableDamping = true;
466
+ controls.dampingFactor = 0.05;
467
+
468
+ controls.target.set(0.000, 0.000, 0.000);
469
+
470
+ const loader = new GLTFLoader();
471
+ console.log("Loading:", MODEL_PATH);
472
+
473
+ loader.load(MODEL_PATH, function (gltf) {
474
+ const root = gltf.scene;
475
+ scene.add(root);
476
+
477
+ frames = [];
478
+ root.traverse((node) => {
479
+
480
+ if (node.isMesh) {
481
+ node.geometry.computeVertexNormals();
482
+ if (node.geometry.attributes.color) {
483
+ node.geometry.deleteAttribute('color');
484
+ }
485
+ node.material = new THREE.MeshStandardMaterial({
486
+ color: 0xff9966,
487
+ roughness: 0.4,
488
+ metalness: 0.0,
489
+ side: THREE.DoubleSide
490
+ });
491
+ node.material.vertexColors = false;
492
+ }
493
+
494
+ if (node.isPoints) {
495
+ if (node.name.toLowerCase().includes('scene')) {
496
+ node.material.size = 0.05;
497
+ node.material.sizeAttenuation = true;
498
+ }
499
+ if (node.name.toLowerCase().includes('human')) {
500
+ node.material.size = 0.005;
501
+ }
502
+ }
503
+
504
+ if (node.name && node.name.startsWith('frame_')) {
505
+ const parts = node.name.split('_');
506
+ if (parts.length === 2 && !isNaN(parseInt(parts[1]))) {
507
+ const idx = parseInt(parts[1]);
508
+ frames[idx] = node;
509
+ node.visible = false;
510
+ }
511
+ }
512
+ });
513
+
514
+ frames = frames.filter(n => n !== undefined);
515
+ console.log(`Loaded ${frames.length} frames.`);
516
+
517
+ if (frames.length > 0) {
518
+ slider.max = frames.length - 1;
519
+ loadingOverlay.style.display = 'none';
520
+ showFrame(0);
521
+ } else {
522
+ loadingOverlay.innerHTML = "<p>No frames found.</p>";
523
+ }
524
+
525
+ }, undefined, function (error) {
526
+ console.error(error);
527
+ loadingOverlay.innerHTML = "<p>Error loading model.</p>";
528
+ });
529
+
530
+ window.addEventListener('resize', onWindowResize);
531
+ animate();
532
+ }
533
+
534
+ function showFrame(idx) {
535
+ if (!frames[idx]) return;
536
+ if (frames[currentFrame]) frames[currentFrame].visible = false;
537
+ frames[idx].visible = true;
538
+ currentFrame = idx;
539
+ slider.value = idx;
540
+ frameLabel.innerText = `Frame: ${idx}`;
541
+ }
542
+
543
+ function togglePlay() {
544
+ if (frames.length === 0) return;
545
+ isPlaying = !isPlaying;
546
+
547
+ const icon = playBtn.querySelector('.fa-play, .fa-pause');
548
+
549
+ if (isPlaying) {
550
+ if(icon) { icon.classList.remove('fa-play'); icon.classList.add('fa-pause'); }
551
+ intervalId = setInterval(() => {
552
+ let next = currentFrame + 1;
553
+ if (next >= frames.length) next = 0;
554
+ showFrame(next);
555
+ }, 1000 / FPS);
556
+ } else {
557
+ if(icon) { icon.classList.remove('fa-pause'); icon.classList.add('fa-play'); }
558
+ clearInterval(intervalId);
559
+ }
560
+ }
561
+
562
+ slider.addEventListener('input', (e) => {
563
+ if (isPlaying) togglePlay();
564
+ showFrame(parseInt(e.target.value));
565
+ });
566
+ playBtn.addEventListener('click', togglePlay);
567
+
568
+ function onWindowResize() {
569
+ camera.aspect = container.clientWidth / container.clientHeight;
570
+ camera.updateProjectionMatrix();
571
+ renderer.setSize(container.clientWidth, container.clientHeight);
572
+ }
573
+
574
+ function animate() {
575
+ requestAnimationFrame(animate);
576
+ controls.update();
577
+ renderer.render(scene, camera);
578
+ }
579
+ </script>
580
+
581
+ </body>
582
+ </html>
requirements.txt CHANGED
@@ -1,21 +1,25 @@
1
  torch==2.4.1
2
  torchvision==0.19.1
3
- numpy
4
  scipy
 
 
 
 
 
5
  trimesh
6
  plyfile
7
- open3d==0.19.0
8
  opencv-python-headless
 
9
  pillow
10
  einops
11
  safetensors
12
- spaces
13
- huggingface_hub==0.23.0
14
  tqdm
15
- ninja
16
- ultralytics==8.3.227
17
  timm==1.0.24
 
18
  git+https://github.com/EasternJournalist/utils3d.git@3fab839f0be9931dac7c8488eb0e1600c236e183
19
  mmcv==2.2.0 --find-links https://download.openmmlab.com/mmcv/dist/cu121/torch2.4/index.html
20
  pytorch3d @ https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py310_cu121_pyt241/pytorch3d-0.7.8-cp310-cp310-linux_x86_64.whl
21
  smplx
 
 
1
  torch==2.4.1
2
  torchvision==0.19.1
3
+ numpy<1.24
4
  scipy
5
+ gradio>=5.1.0
6
+ fastapi>=0.112.0
7
+ pydantic>=2.0.0
8
+ huggingface_hub>=0.25.0
9
+ spaces
10
  trimesh
11
  plyfile
 
12
  opencv-python-headless
13
+ open3d==0.19.0
14
  pillow
15
  einops
16
  safetensors
 
 
17
  tqdm
18
+ ultralytics==8.3.27
 
19
  timm==1.0.24
20
+ ninja
21
  git+https://github.com/EasternJournalist/utils3d.git@3fab839f0be9931dac7c8488eb0e1600c236e183
22
  mmcv==2.2.0 --find-links https://download.openmmlab.com/mmcv/dist/cu121/torch2.4/index.html
23
  pytorch3d @ https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py310_cu121_pyt241/pytorch3d-0.7.8-cp310-cp310-linux_x86_64.whl
24
  smplx
25
+ hydra-core
static/BufferGeometryUtils.js ADDED
@@ -0,0 +1,1375 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ BufferAttribute,
3
+ BufferGeometry,
4
+ Float32BufferAttribute,
5
+ InstancedBufferAttribute,
6
+ InterleavedBuffer,
7
+ InterleavedBufferAttribute,
8
+ TriangleFanDrawMode,
9
+ TriangleStripDrawMode,
10
+ TrianglesDrawMode,
11
+ Vector3,
12
+ } from 'three';
13
+
14
+ function computeMikkTSpaceTangents( geometry, MikkTSpace, negateSign = true ) {
15
+
16
+ if ( ! MikkTSpace || ! MikkTSpace.isReady ) {
17
+
18
+ throw new Error( 'BufferGeometryUtils: Initialized MikkTSpace library required.' );
19
+
20
+ }
21
+
22
+ if ( ! geometry.hasAttribute( 'position' ) || ! geometry.hasAttribute( 'normal' ) || ! geometry.hasAttribute( 'uv' ) ) {
23
+
24
+ throw new Error( 'BufferGeometryUtils: Tangents require "position", "normal", and "uv" attributes.' );
25
+
26
+ }
27
+
28
+ function getAttributeArray( attribute ) {
29
+
30
+ if ( attribute.normalized || attribute.isInterleavedBufferAttribute ) {
31
+
32
+ const dstArray = new Float32Array( attribute.count * attribute.itemSize );
33
+
34
+ for ( let i = 0, j = 0; i < attribute.count; i ++ ) {
35
+
36
+ dstArray[ j ++ ] = attribute.getX( i );
37
+ dstArray[ j ++ ] = attribute.getY( i );
38
+
39
+ if ( attribute.itemSize > 2 ) {
40
+
41
+ dstArray[ j ++ ] = attribute.getZ( i );
42
+
43
+ }
44
+
45
+ }
46
+
47
+ return dstArray;
48
+
49
+ }
50
+
51
+ if ( attribute.array instanceof Float32Array ) {
52
+
53
+ return attribute.array;
54
+
55
+ }
56
+
57
+ return new Float32Array( attribute.array );
58
+
59
+ }
60
+
61
+ // MikkTSpace algorithm requires non-indexed input.
62
+
63
+ const _geometry = geometry.index ? geometry.toNonIndexed() : geometry;
64
+
65
+ // Compute vertex tangents.
66
+
67
+ const tangents = MikkTSpace.generateTangents(
68
+
69
+ getAttributeArray( _geometry.attributes.position ),
70
+ getAttributeArray( _geometry.attributes.normal ),
71
+ getAttributeArray( _geometry.attributes.uv )
72
+
73
+ );
74
+
75
+ // Texture coordinate convention of glTF differs from the apparent
76
+ // default of the MikkTSpace library; .w component must be flipped.
77
+
78
+ if ( negateSign ) {
79
+
80
+ for ( let i = 3; i < tangents.length; i += 4 ) {
81
+
82
+ tangents[ i ] *= - 1;
83
+
84
+ }
85
+
86
+ }
87
+
88
+ //
89
+
90
+ _geometry.setAttribute( 'tangent', new BufferAttribute( tangents, 4 ) );
91
+
92
+ if ( geometry !== _geometry ) {
93
+
94
+ geometry.copy( _geometry );
95
+
96
+ }
97
+
98
+ return geometry;
99
+
100
+ }
101
+
102
+ /**
103
+ * @param {Array<BufferGeometry>} geometries
104
+ * @param {Boolean} useGroups
105
+ * @return {BufferGeometry}
106
+ */
107
+ function mergeGeometries( geometries, useGroups = false ) {
108
+
109
+ const isIndexed = geometries[ 0 ].index !== null;
110
+
111
+ const attributesUsed = new Set( Object.keys( geometries[ 0 ].attributes ) );
112
+ const morphAttributesUsed = new Set( Object.keys( geometries[ 0 ].morphAttributes ) );
113
+
114
+ const attributes = {};
115
+ const morphAttributes = {};
116
+
117
+ const morphTargetsRelative = geometries[ 0 ].morphTargetsRelative;
118
+
119
+ const mergedGeometry = new BufferGeometry();
120
+
121
+ let offset = 0;
122
+
123
+ for ( let i = 0; i < geometries.length; ++ i ) {
124
+
125
+ const geometry = geometries[ i ];
126
+ let attributesCount = 0;
127
+
128
+ // ensure that all geometries are indexed, or none
129
+
130
+ if ( isIndexed !== ( geometry.index !== null ) ) {
131
+
132
+ console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.' );
133
+ return null;
134
+
135
+ }
136
+
137
+ // gather attributes, exit early if they're different
138
+
139
+ for ( const name in geometry.attributes ) {
140
+
141
+ if ( ! attributesUsed.has( name ) ) {
142
+
143
+ console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure "' + name + '" attribute exists among all geometries, or in none of them.' );
144
+ return null;
145
+
146
+ }
147
+
148
+ if ( attributes[ name ] === undefined ) attributes[ name ] = [];
149
+
150
+ attributes[ name ].push( geometry.attributes[ name ] );
151
+
152
+ attributesCount ++;
153
+
154
+ }
155
+
156
+ // ensure geometries have the same number of attributes
157
+
158
+ if ( attributesCount !== attributesUsed.size ) {
159
+
160
+ console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. Make sure all geometries have the same number of attributes.' );
161
+ return null;
162
+
163
+ }
164
+
165
+ // gather morph attributes, exit early if they're different
166
+
167
+ if ( morphTargetsRelative !== geometry.morphTargetsRelative ) {
168
+
169
+ console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. .morphTargetsRelative must be consistent throughout all geometries.' );
170
+ return null;
171
+
172
+ }
173
+
174
+ for ( const name in geometry.morphAttributes ) {
175
+
176
+ if ( ! morphAttributesUsed.has( name ) ) {
177
+
178
+ console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. .morphAttributes must be consistent throughout all geometries.' );
179
+ return null;
180
+
181
+ }
182
+
183
+ if ( morphAttributes[ name ] === undefined ) morphAttributes[ name ] = [];
184
+
185
+ morphAttributes[ name ].push( geometry.morphAttributes[ name ] );
186
+
187
+ }
188
+
189
+ if ( useGroups ) {
190
+
191
+ let count;
192
+
193
+ if ( isIndexed ) {
194
+
195
+ count = geometry.index.count;
196
+
197
+ } else if ( geometry.attributes.position !== undefined ) {
198
+
199
+ count = geometry.attributes.position.count;
200
+
201
+ } else {
202
+
203
+ console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. The geometry must have either an index or a position attribute' );
204
+ return null;
205
+
206
+ }
207
+
208
+ mergedGeometry.addGroup( offset, count, i );
209
+
210
+ offset += count;
211
+
212
+ }
213
+
214
+ }
215
+
216
+ // merge indices
217
+
218
+ if ( isIndexed ) {
219
+
220
+ let indexOffset = 0;
221
+ const mergedIndex = [];
222
+
223
+ for ( let i = 0; i < geometries.length; ++ i ) {
224
+
225
+ const index = geometries[ i ].index;
226
+
227
+ for ( let j = 0; j < index.count; ++ j ) {
228
+
229
+ mergedIndex.push( index.getX( j ) + indexOffset );
230
+
231
+ }
232
+
233
+ indexOffset += geometries[ i ].attributes.position.count;
234
+
235
+ }
236
+
237
+ mergedGeometry.setIndex( mergedIndex );
238
+
239
+ }
240
+
241
+ // merge attributes
242
+
243
+ for ( const name in attributes ) {
244
+
245
+ const mergedAttribute = mergeAttributes( attributes[ name ] );
246
+
247
+ if ( ! mergedAttribute ) {
248
+
249
+ console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the ' + name + ' attribute.' );
250
+ return null;
251
+
252
+ }
253
+
254
+ mergedGeometry.setAttribute( name, mergedAttribute );
255
+
256
+ }
257
+
258
+ // merge morph attributes
259
+
260
+ for ( const name in morphAttributes ) {
261
+
262
+ const numMorphTargets = morphAttributes[ name ][ 0 ].length;
263
+
264
+ if ( numMorphTargets === 0 ) break;
265
+
266
+ mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {};
267
+ mergedGeometry.morphAttributes[ name ] = [];
268
+
269
+ for ( let i = 0; i < numMorphTargets; ++ i ) {
270
+
271
+ const morphAttributesToMerge = [];
272
+
273
+ for ( let j = 0; j < morphAttributes[ name ].length; ++ j ) {
274
+
275
+ morphAttributesToMerge.push( morphAttributes[ name ][ j ][ i ] );
276
+
277
+ }
278
+
279
+ const mergedMorphAttribute = mergeAttributes( morphAttributesToMerge );
280
+
281
+ if ( ! mergedMorphAttribute ) {
282
+
283
+ console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the ' + name + ' morphAttribute.' );
284
+ return null;
285
+
286
+ }
287
+
288
+ mergedGeometry.morphAttributes[ name ].push( mergedMorphAttribute );
289
+
290
+ }
291
+
292
+ }
293
+
294
+ return mergedGeometry;
295
+
296
+ }
297
+
298
+ /**
299
+ * @param {Array<BufferAttribute>} attributes
300
+ * @return {BufferAttribute}
301
+ */
302
+ function mergeAttributes( attributes ) {
303
+
304
+ let TypedArray;
305
+ let itemSize;
306
+ let normalized;
307
+ let gpuType = - 1;
308
+ let arrayLength = 0;
309
+
310
+ for ( let i = 0; i < attributes.length; ++ i ) {
311
+
312
+ const attribute = attributes[ i ];
313
+
314
+ if ( attribute.isInterleavedBufferAttribute ) {
315
+
316
+ console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. InterleavedBufferAttributes are not supported.' );
317
+ return null;
318
+
319
+ }
320
+
321
+ if ( TypedArray === undefined ) TypedArray = attribute.array.constructor;
322
+ if ( TypedArray !== attribute.array.constructor ) {
323
+
324
+ console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.array must be of consistent array types across matching attributes.' );
325
+ return null;
326
+
327
+ }
328
+
329
+ if ( itemSize === undefined ) itemSize = attribute.itemSize;
330
+ if ( itemSize !== attribute.itemSize ) {
331
+
332
+ console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.itemSize must be consistent across matching attributes.' );
333
+ return null;
334
+
335
+ }
336
+
337
+ if ( normalized === undefined ) normalized = attribute.normalized;
338
+ if ( normalized !== attribute.normalized ) {
339
+
340
+ console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.normalized must be consistent across matching attributes.' );
341
+ return null;
342
+
343
+ }
344
+
345
+ if ( gpuType === - 1 ) gpuType = attribute.gpuType;
346
+ if ( gpuType !== attribute.gpuType ) {
347
+
348
+ console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.gpuType must be consistent across matching attributes.' );
349
+ return null;
350
+
351
+ }
352
+
353
+ arrayLength += attribute.array.length;
354
+
355
+ }
356
+
357
+ const array = new TypedArray( arrayLength );
358
+ let offset = 0;
359
+
360
+ for ( let i = 0; i < attributes.length; ++ i ) {
361
+
362
+ array.set( attributes[ i ].array, offset );
363
+
364
+ offset += attributes[ i ].array.length;
365
+
366
+ }
367
+
368
+ const result = new BufferAttribute( array, itemSize, normalized );
369
+ if ( gpuType !== undefined ) {
370
+
371
+ result.gpuType = gpuType;
372
+
373
+ }
374
+
375
+ return result;
376
+
377
+ }
378
+
379
+ /**
380
+ * @param {BufferAttribute}
381
+ * @return {BufferAttribute}
382
+ */
383
+ export function deepCloneAttribute( attribute ) {
384
+
385
+ if ( attribute.isInstancedInterleavedBufferAttribute || attribute.isInterleavedBufferAttribute ) {
386
+
387
+ return deinterleaveAttribute( attribute );
388
+
389
+ }
390
+
391
+ if ( attribute.isInstancedBufferAttribute ) {
392
+
393
+ return new InstancedBufferAttribute().copy( attribute );
394
+
395
+ }
396
+
397
+ return new BufferAttribute().copy( attribute );
398
+
399
+ }
400
+
401
+ /**
402
+ * @param {Array<BufferAttribute>} attributes
403
+ * @return {Array<InterleavedBufferAttribute>}
404
+ */
405
+ function interleaveAttributes( attributes ) {
406
+
407
+ // Interleaves the provided attributes into an InterleavedBuffer and returns
408
+ // a set of InterleavedBufferAttributes for each attribute
409
+ let TypedArray;
410
+ let arrayLength = 0;
411
+ let stride = 0;
412
+
413
+ // calculate the length and type of the interleavedBuffer
414
+ for ( let i = 0, l = attributes.length; i < l; ++ i ) {
415
+
416
+ const attribute = attributes[ i ];
417
+
418
+ if ( TypedArray === undefined ) TypedArray = attribute.array.constructor;
419
+ if ( TypedArray !== attribute.array.constructor ) {
420
+
421
+ console.error( 'AttributeBuffers of different types cannot be interleaved' );
422
+ return null;
423
+
424
+ }
425
+
426
+ arrayLength += attribute.array.length;
427
+ stride += attribute.itemSize;
428
+
429
+ }
430
+
431
+ // Create the set of buffer attributes
432
+ const interleavedBuffer = new InterleavedBuffer( new TypedArray( arrayLength ), stride );
433
+ let offset = 0;
434
+ const res = [];
435
+ const getters = [ 'getX', 'getY', 'getZ', 'getW' ];
436
+ const setters = [ 'setX', 'setY', 'setZ', 'setW' ];
437
+
438
+ for ( let j = 0, l = attributes.length; j < l; j ++ ) {
439
+
440
+ const attribute = attributes[ j ];
441
+ const itemSize = attribute.itemSize;
442
+ const count = attribute.count;
443
+ const iba = new InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, attribute.normalized );
444
+ res.push( iba );
445
+
446
+ offset += itemSize;
447
+
448
+ // Move the data for each attribute into the new interleavedBuffer
449
+ // at the appropriate offset
450
+ for ( let c = 0; c < count; c ++ ) {
451
+
452
+ for ( let k = 0; k < itemSize; k ++ ) {
453
+
454
+ iba[ setters[ k ] ]( c, attribute[ getters[ k ] ]( c ) );
455
+
456
+ }
457
+
458
+ }
459
+
460
+ }
461
+
462
+ return res;
463
+
464
+ }
465
+
466
+ // returns a new, non-interleaved version of the provided attribute
467
+ export function deinterleaveAttribute( attribute ) {
468
+
469
+ const cons = attribute.data.array.constructor;
470
+ const count = attribute.count;
471
+ const itemSize = attribute.itemSize;
472
+ const normalized = attribute.normalized;
473
+
474
+ const array = new cons( count * itemSize );
475
+ let newAttribute;
476
+ if ( attribute.isInstancedInterleavedBufferAttribute ) {
477
+
478
+ newAttribute = new InstancedBufferAttribute( array, itemSize, normalized, attribute.meshPerAttribute );
479
+
480
+ } else {
481
+
482
+ newAttribute = new BufferAttribute( array, itemSize, normalized );
483
+
484
+ }
485
+
486
+ for ( let i = 0; i < count; i ++ ) {
487
+
488
+ newAttribute.setX( i, attribute.getX( i ) );
489
+
490
+ if ( itemSize >= 2 ) {
491
+
492
+ newAttribute.setY( i, attribute.getY( i ) );
493
+
494
+ }
495
+
496
+ if ( itemSize >= 3 ) {
497
+
498
+ newAttribute.setZ( i, attribute.getZ( i ) );
499
+
500
+ }
501
+
502
+ if ( itemSize >= 4 ) {
503
+
504
+ newAttribute.setW( i, attribute.getW( i ) );
505
+
506
+ }
507
+
508
+ }
509
+
510
+ return newAttribute;
511
+
512
+ }
513
+
514
+ // deinterleaves all attributes on the geometry
515
+ export function deinterleaveGeometry( geometry ) {
516
+
517
+ const attributes = geometry.attributes;
518
+ const morphTargets = geometry.morphTargets;
519
+ const attrMap = new Map();
520
+
521
+ for ( const key in attributes ) {
522
+
523
+ const attr = attributes[ key ];
524
+ if ( attr.isInterleavedBufferAttribute ) {
525
+
526
+ if ( ! attrMap.has( attr ) ) {
527
+
528
+ attrMap.set( attr, deinterleaveAttribute( attr ) );
529
+
530
+ }
531
+
532
+ attributes[ key ] = attrMap.get( attr );
533
+
534
+ }
535
+
536
+ }
537
+
538
+ for ( const key in morphTargets ) {
539
+
540
+ const attr = morphTargets[ key ];
541
+ if ( attr.isInterleavedBufferAttribute ) {
542
+
543
+ if ( ! attrMap.has( attr ) ) {
544
+
545
+ attrMap.set( attr, deinterleaveAttribute( attr ) );
546
+
547
+ }
548
+
549
+ morphTargets[ key ] = attrMap.get( attr );
550
+
551
+ }
552
+
553
+ }
554
+
555
+ }
556
+
557
+ /**
558
+ * @param {BufferGeometry} geometry
559
+ * @return {number}
560
+ */
561
+ function estimateBytesUsed( geometry ) {
562
+
563
+ // Return the estimated memory used by this geometry in bytes
564
+ // Calculate using itemSize, count, and BYTES_PER_ELEMENT to account
565
+ // for InterleavedBufferAttributes.
566
+ let mem = 0;
567
+ for ( const name in geometry.attributes ) {
568
+
569
+ const attr = geometry.getAttribute( name );
570
+ mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;
571
+
572
+ }
573
+
574
+ const indices = geometry.getIndex();
575
+ mem += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0;
576
+ return mem;
577
+
578
+ }
579
+
580
+ /**
581
+ * @param {BufferGeometry} geometry
582
+ * @param {number} tolerance
583
+ * @return {BufferGeometry}
584
+ */
585
+ function mergeVertices( geometry, tolerance = 1e-4 ) {
586
+
587
+ tolerance = Math.max( tolerance, Number.EPSILON );
588
+
589
+ // Generate an index buffer if the geometry doesn't have one, or optimize it
590
+ // if it's already available.
591
+ const hashToIndex = {};
592
+ const indices = geometry.getIndex();
593
+ const positions = geometry.getAttribute( 'position' );
594
+ const vertexCount = indices ? indices.count : positions.count;
595
+
596
+ // next value for triangle indices
597
+ let nextIndex = 0;
598
+
599
+ // attributes and new attribute arrays
600
+ const attributeNames = Object.keys( geometry.attributes );
601
+ const tmpAttributes = {};
602
+ const tmpMorphAttributes = {};
603
+ const newIndices = [];
604
+ const getters = [ 'getX', 'getY', 'getZ', 'getW' ];
605
+ const setters = [ 'setX', 'setY', 'setZ', 'setW' ];
606
+
607
+ // Initialize the arrays, allocating space conservatively. Extra
608
+ // space will be trimmed in the last step.
609
+ for ( let i = 0, l = attributeNames.length; i < l; i ++ ) {
610
+
611
+ const name = attributeNames[ i ];
612
+ const attr = geometry.attributes[ name ];
613
+
614
+ tmpAttributes[ name ] = new BufferAttribute(
615
+ new attr.array.constructor( attr.count * attr.itemSize ),
616
+ attr.itemSize,
617
+ attr.normalized
618
+ );
619
+
620
+ const morphAttr = geometry.morphAttributes[ name ];
621
+ if ( morphAttr ) {
622
+
623
+ tmpMorphAttributes[ name ] = new BufferAttribute(
624
+ new morphAttr.array.constructor( morphAttr.count * morphAttr.itemSize ),
625
+ morphAttr.itemSize,
626
+ morphAttr.normalized
627
+ );
628
+
629
+ }
630
+
631
+ }
632
+
633
+ // convert the error tolerance to an amount of decimal places to truncate to
634
+ const halfTolerance = tolerance * 0.5;
635
+ const exponent = Math.log10( 1 / tolerance );
636
+ const hashMultiplier = Math.pow( 10, exponent );
637
+ const hashAdditive = halfTolerance * hashMultiplier;
638
+ for ( let i = 0; i < vertexCount; i ++ ) {
639
+
640
+ const index = indices ? indices.getX( i ) : i;
641
+
642
+ // Generate a hash for the vertex attributes at the current index 'i'
643
+ let hash = '';
644
+ for ( let j = 0, l = attributeNames.length; j < l; j ++ ) {
645
+
646
+ const name = attributeNames[ j ];
647
+ const attribute = geometry.getAttribute( name );
648
+ const itemSize = attribute.itemSize;
649
+
650
+ for ( let k = 0; k < itemSize; k ++ ) {
651
+
652
+ // double tilde truncates the decimal value
653
+ hash += `${ ~ ~ ( attribute[ getters[ k ] ]( index ) * hashMultiplier + hashAdditive ) },`;
654
+
655
+ }
656
+
657
+ }
658
+
659
+ // Add another reference to the vertex if it's already
660
+ // used by another index
661
+ if ( hash in hashToIndex ) {
662
+
663
+ newIndices.push( hashToIndex[ hash ] );
664
+
665
+ } else {
666
+
667
+ // copy data to the new index in the temporary attributes
668
+ for ( let j = 0, l = attributeNames.length; j < l; j ++ ) {
669
+
670
+ const name = attributeNames[ j ];
671
+ const attribute = geometry.getAttribute( name );
672
+ const morphAttr = geometry.morphAttributes[ name ];
673
+ const itemSize = attribute.itemSize;
674
+ const newarray = tmpAttributes[ name ];
675
+ const newMorphArrays = tmpMorphAttributes[ name ];
676
+
677
+ for ( let k = 0; k < itemSize; k ++ ) {
678
+
679
+ const getterFunc = getters[ k ];
680
+ const setterFunc = setters[ k ];
681
+ newarray[ setterFunc ]( nextIndex, attribute[ getterFunc ]( index ) );
682
+
683
+ if ( morphAttr ) {
684
+
685
+ for ( let m = 0, ml = morphAttr.length; m < ml; m ++ ) {
686
+
687
+ newMorphArrays[ m ][ setterFunc ]( nextIndex, morphAttr[ m ][ getterFunc ]( index ) );
688
+
689
+ }
690
+
691
+ }
692
+
693
+ }
694
+
695
+ }
696
+
697
+ hashToIndex[ hash ] = nextIndex;
698
+ newIndices.push( nextIndex );
699
+ nextIndex ++;
700
+
701
+ }
702
+
703
+ }
704
+
705
+ // generate result BufferGeometry
706
+ const result = geometry.clone();
707
+ for ( const name in geometry.attributes ) {
708
+
709
+ const tmpAttribute = tmpAttributes[ name ];
710
+
711
+ result.setAttribute( name, new BufferAttribute(
712
+ tmpAttribute.array.slice( 0, nextIndex * tmpAttribute.itemSize ),
713
+ tmpAttribute.itemSize,
714
+ tmpAttribute.normalized,
715
+ ) );
716
+
717
+ if ( ! ( name in tmpMorphAttributes ) ) continue;
718
+
719
+ for ( let j = 0; j < tmpMorphAttributes[ name ].length; j ++ ) {
720
+
721
+ const tmpMorphAttribute = tmpMorphAttributes[ name ][ j ];
722
+
723
+ result.morphAttributes[ name ][ j ] = new BufferAttribute(
724
+ tmpMorphAttribute.array.slice( 0, nextIndex * tmpMorphAttribute.itemSize ),
725
+ tmpMorphAttribute.itemSize,
726
+ tmpMorphAttribute.normalized,
727
+ );
728
+
729
+ }
730
+
731
+ }
732
+
733
+ // indices
734
+
735
+ result.setIndex( newIndices );
736
+
737
+ return result;
738
+
739
+ }
740
+
741
+ /**
742
+ * @param {BufferGeometry} geometry
743
+ * @param {number} drawMode
744
+ * @return {BufferGeometry}
745
+ */
746
+ function toTrianglesDrawMode( geometry, drawMode ) {
747
+
748
+ if ( drawMode === TrianglesDrawMode ) {
749
+
750
+ console.warn( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Geometry already defined as triangles.' );
751
+ return geometry;
752
+
753
+ }
754
+
755
+ if ( drawMode === TriangleFanDrawMode || drawMode === TriangleStripDrawMode ) {
756
+
757
+ let index = geometry.getIndex();
758
+
759
+ // generate index if not present
760
+
761
+ if ( index === null ) {
762
+
763
+ const indices = [];
764
+
765
+ const position = geometry.getAttribute( 'position' );
766
+
767
+ if ( position !== undefined ) {
768
+
769
+ for ( let i = 0; i < position.count; i ++ ) {
770
+
771
+ indices.push( i );
772
+
773
+ }
774
+
775
+ geometry.setIndex( indices );
776
+ index = geometry.getIndex();
777
+
778
+ } else {
779
+
780
+ console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.' );
781
+ return geometry;
782
+
783
+ }
784
+
785
+ }
786
+
787
+ //
788
+
789
+ const numberOfTriangles = index.count - 2;
790
+ const newIndices = [];
791
+
792
+ if ( drawMode === TriangleFanDrawMode ) {
793
+
794
+ // gl.TRIANGLE_FAN
795
+
796
+ for ( let i = 1; i <= numberOfTriangles; i ++ ) {
797
+
798
+ newIndices.push( index.getX( 0 ) );
799
+ newIndices.push( index.getX( i ) );
800
+ newIndices.push( index.getX( i + 1 ) );
801
+
802
+ }
803
+
804
+ } else {
805
+
806
+ // gl.TRIANGLE_STRIP
807
+
808
+ for ( let i = 0; i < numberOfTriangles; i ++ ) {
809
+
810
+ if ( i % 2 === 0 ) {
811
+
812
+ newIndices.push( index.getX( i ) );
813
+ newIndices.push( index.getX( i + 1 ) );
814
+ newIndices.push( index.getX( i + 2 ) );
815
+
816
+ } else {
817
+
818
+ newIndices.push( index.getX( i + 2 ) );
819
+ newIndices.push( index.getX( i + 1 ) );
820
+ newIndices.push( index.getX( i ) );
821
+
822
+ }
823
+
824
+ }
825
+
826
+ }
827
+
828
+ if ( ( newIndices.length / 3 ) !== numberOfTriangles ) {
829
+
830
+ console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unable to generate correct amount of triangles.' );
831
+
832
+ }
833
+
834
+ // build final geometry
835
+
836
+ const newGeometry = geometry.clone();
837
+ newGeometry.setIndex( newIndices );
838
+ newGeometry.clearGroups();
839
+
840
+ return newGeometry;
841
+
842
+ } else {
843
+
844
+ console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unknown draw mode:', drawMode );
845
+ return geometry;
846
+
847
+ }
848
+
849
+ }
850
+
851
+ /**
852
+ * Calculates the morphed attributes of a morphed/skinned BufferGeometry.
853
+ * Helpful for Raytracing or Decals.
854
+ * @param {Mesh | Line | Points} object An instance of Mesh, Line or Points.
855
+ * @return {Object} An Object with original position/normal attributes and morphed ones.
856
+ */
857
+ function computeMorphedAttributes( object ) {
858
+
859
+ const _vA = new Vector3();
860
+ const _vB = new Vector3();
861
+ const _vC = new Vector3();
862
+
863
+ const _tempA = new Vector3();
864
+ const _tempB = new Vector3();
865
+ const _tempC = new Vector3();
866
+
867
+ const _morphA = new Vector3();
868
+ const _morphB = new Vector3();
869
+ const _morphC = new Vector3();
870
+
871
+ function _calculateMorphedAttributeData(
872
+ object,
873
+ attribute,
874
+ morphAttribute,
875
+ morphTargetsRelative,
876
+ a,
877
+ b,
878
+ c,
879
+ modifiedAttributeArray
880
+ ) {
881
+
882
+ _vA.fromBufferAttribute( attribute, a );
883
+ _vB.fromBufferAttribute( attribute, b );
884
+ _vC.fromBufferAttribute( attribute, c );
885
+
886
+ const morphInfluences = object.morphTargetInfluences;
887
+
888
+ if ( morphAttribute && morphInfluences ) {
889
+
890
+ _morphA.set( 0, 0, 0 );
891
+ _morphB.set( 0, 0, 0 );
892
+ _morphC.set( 0, 0, 0 );
893
+
894
+ for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
895
+
896
+ const influence = morphInfluences[ i ];
897
+ const morph = morphAttribute[ i ];
898
+
899
+ if ( influence === 0 ) continue;
900
+
901
+ _tempA.fromBufferAttribute( morph, a );
902
+ _tempB.fromBufferAttribute( morph, b );
903
+ _tempC.fromBufferAttribute( morph, c );
904
+
905
+ if ( morphTargetsRelative ) {
906
+
907
+ _morphA.addScaledVector( _tempA, influence );
908
+ _morphB.addScaledVector( _tempB, influence );
909
+ _morphC.addScaledVector( _tempC, influence );
910
+
911
+ } else {
912
+
913
+ _morphA.addScaledVector( _tempA.sub( _vA ), influence );
914
+ _morphB.addScaledVector( _tempB.sub( _vB ), influence );
915
+ _morphC.addScaledVector( _tempC.sub( _vC ), influence );
916
+
917
+ }
918
+
919
+ }
920
+
921
+ _vA.add( _morphA );
922
+ _vB.add( _morphB );
923
+ _vC.add( _morphC );
924
+
925
+ }
926
+
927
+ if ( object.isSkinnedMesh ) {
928
+
929
+ object.applyBoneTransform( a, _vA );
930
+ object.applyBoneTransform( b, _vB );
931
+ object.applyBoneTransform( c, _vC );
932
+
933
+ }
934
+
935
+ modifiedAttributeArray[ a * 3 + 0 ] = _vA.x;
936
+ modifiedAttributeArray[ a * 3 + 1 ] = _vA.y;
937
+ modifiedAttributeArray[ a * 3 + 2 ] = _vA.z;
938
+ modifiedAttributeArray[ b * 3 + 0 ] = _vB.x;
939
+ modifiedAttributeArray[ b * 3 + 1 ] = _vB.y;
940
+ modifiedAttributeArray[ b * 3 + 2 ] = _vB.z;
941
+ modifiedAttributeArray[ c * 3 + 0 ] = _vC.x;
942
+ modifiedAttributeArray[ c * 3 + 1 ] = _vC.y;
943
+ modifiedAttributeArray[ c * 3 + 2 ] = _vC.z;
944
+
945
+ }
946
+
947
+ const geometry = object.geometry;
948
+ const material = object.material;
949
+
950
+ let a, b, c;
951
+ const index = geometry.index;
952
+ const positionAttribute = geometry.attributes.position;
953
+ const morphPosition = geometry.morphAttributes.position;
954
+ const morphTargetsRelative = geometry.morphTargetsRelative;
955
+ const normalAttribute = geometry.attributes.normal;
956
+ const morphNormal = geometry.morphAttributes.position;
957
+
958
+ const groups = geometry.groups;
959
+ const drawRange = geometry.drawRange;
960
+ let i, j, il, jl;
961
+ let group;
962
+ let start, end;
963
+
964
+ const modifiedPosition = new Float32Array( positionAttribute.count * positionAttribute.itemSize );
965
+ const modifiedNormal = new Float32Array( normalAttribute.count * normalAttribute.itemSize );
966
+
967
+ if ( index !== null ) {
968
+
969
+ // indexed buffer geometry
970
+
971
+ if ( Array.isArray( material ) ) {
972
+
973
+ for ( i = 0, il = groups.length; i < il; i ++ ) {
974
+
975
+ group = groups[ i ];
976
+
977
+ start = Math.max( group.start, drawRange.start );
978
+ end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
979
+
980
+ for ( j = start, jl = end; j < jl; j += 3 ) {
981
+
982
+ a = index.getX( j );
983
+ b = index.getX( j + 1 );
984
+ c = index.getX( j + 2 );
985
+
986
+ _calculateMorphedAttributeData(
987
+ object,
988
+ positionAttribute,
989
+ morphPosition,
990
+ morphTargetsRelative,
991
+ a, b, c,
992
+ modifiedPosition
993
+ );
994
+
995
+ _calculateMorphedAttributeData(
996
+ object,
997
+ normalAttribute,
998
+ morphNormal,
999
+ morphTargetsRelative,
1000
+ a, b, c,
1001
+ modifiedNormal
1002
+ );
1003
+
1004
+ }
1005
+
1006
+ }
1007
+
1008
+ } else {
1009
+
1010
+ start = Math.max( 0, drawRange.start );
1011
+ end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
1012
+
1013
+ for ( i = start, il = end; i < il; i += 3 ) {
1014
+
1015
+ a = index.getX( i );
1016
+ b = index.getX( i + 1 );
1017
+ c = index.getX( i + 2 );
1018
+
1019
+ _calculateMorphedAttributeData(
1020
+ object,
1021
+ positionAttribute,
1022
+ morphPosition,
1023
+ morphTargetsRelative,
1024
+ a, b, c,
1025
+ modifiedPosition
1026
+ );
1027
+
1028
+ _calculateMorphedAttributeData(
1029
+ object,
1030
+ normalAttribute,
1031
+ morphNormal,
1032
+ morphTargetsRelative,
1033
+ a, b, c,
1034
+ modifiedNormal
1035
+ );
1036
+
1037
+ }
1038
+
1039
+ }
1040
+
1041
+ } else {
1042
+
1043
+ // non-indexed buffer geometry
1044
+
1045
+ if ( Array.isArray( material ) ) {
1046
+
1047
+ for ( i = 0, il = groups.length; i < il; i ++ ) {
1048
+
1049
+ group = groups[ i ];
1050
+
1051
+ start = Math.max( group.start, drawRange.start );
1052
+ end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
1053
+
1054
+ for ( j = start, jl = end; j < jl; j += 3 ) {
1055
+
1056
+ a = j;
1057
+ b = j + 1;
1058
+ c = j + 2;
1059
+
1060
+ _calculateMorphedAttributeData(
1061
+ object,
1062
+ positionAttribute,
1063
+ morphPosition,
1064
+ morphTargetsRelative,
1065
+ a, b, c,
1066
+ modifiedPosition
1067
+ );
1068
+
1069
+ _calculateMorphedAttributeData(
1070
+ object,
1071
+ normalAttribute,
1072
+ morphNormal,
1073
+ morphTargetsRelative,
1074
+ a, b, c,
1075
+ modifiedNormal
1076
+ );
1077
+
1078
+ }
1079
+
1080
+ }
1081
+
1082
+ } else {
1083
+
1084
+ start = Math.max( 0, drawRange.start );
1085
+ end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
1086
+
1087
+ for ( i = start, il = end; i < il; i += 3 ) {
1088
+
1089
+ a = i;
1090
+ b = i + 1;
1091
+ c = i + 2;
1092
+
1093
+ _calculateMorphedAttributeData(
1094
+ object,
1095
+ positionAttribute,
1096
+ morphPosition,
1097
+ morphTargetsRelative,
1098
+ a, b, c,
1099
+ modifiedPosition
1100
+ );
1101
+
1102
+ _calculateMorphedAttributeData(
1103
+ object,
1104
+ normalAttribute,
1105
+ morphNormal,
1106
+ morphTargetsRelative,
1107
+ a, b, c,
1108
+ modifiedNormal
1109
+ );
1110
+
1111
+ }
1112
+
1113
+ }
1114
+
1115
+ }
1116
+
1117
+ const morphedPositionAttribute = new Float32BufferAttribute( modifiedPosition, 3 );
1118
+ const morphedNormalAttribute = new Float32BufferAttribute( modifiedNormal, 3 );
1119
+
1120
+ return {
1121
+
1122
+ positionAttribute: positionAttribute,
1123
+ normalAttribute: normalAttribute,
1124
+ morphedPositionAttribute: morphedPositionAttribute,
1125
+ morphedNormalAttribute: morphedNormalAttribute
1126
+
1127
+ };
1128
+
1129
+ }
1130
+
1131
+ function mergeGroups( geometry ) {
1132
+
1133
+ if ( geometry.groups.length === 0 ) {
1134
+
1135
+ console.warn( 'THREE.BufferGeometryUtils.mergeGroups(): No groups are defined. Nothing to merge.' );
1136
+ return geometry;
1137
+
1138
+ }
1139
+
1140
+ let groups = geometry.groups;
1141
+
1142
+ // sort groups by material index
1143
+
1144
+ groups = groups.sort( ( a, b ) => {
1145
+
1146
+ if ( a.materialIndex !== b.materialIndex ) return a.materialIndex - b.materialIndex;
1147
+
1148
+ return a.start - b.start;
1149
+
1150
+ } );
1151
+
1152
+ // create index for non-indexed geometries
1153
+
1154
+ if ( geometry.getIndex() === null ) {
1155
+
1156
+ const positionAttribute = geometry.getAttribute( 'position' );
1157
+ const indices = [];
1158
+
1159
+ for ( let i = 0; i < positionAttribute.count; i += 3 ) {
1160
+
1161
+ indices.push( i, i + 1, i + 2 );
1162
+
1163
+ }
1164
+
1165
+ geometry.setIndex( indices );
1166
+
1167
+ }
1168
+
1169
+ // sort index
1170
+
1171
+ const index = geometry.getIndex();
1172
+
1173
+ const newIndices = [];
1174
+
1175
+ for ( let i = 0; i < groups.length; i ++ ) {
1176
+
1177
+ const group = groups[ i ];
1178
+
1179
+ const groupStart = group.start;
1180
+ const groupLength = groupStart + group.count;
1181
+
1182
+ for ( let j = groupStart; j < groupLength; j ++ ) {
1183
+
1184
+ newIndices.push( index.getX( j ) );
1185
+
1186
+ }
1187
+
1188
+ }
1189
+
1190
+ geometry.dispose(); // Required to force buffer recreation
1191
+ geometry.setIndex( newIndices );
1192
+
1193
+ // update groups indices
1194
+
1195
+ let start = 0;
1196
+
1197
+ for ( let i = 0; i < groups.length; i ++ ) {
1198
+
1199
+ const group = groups[ i ];
1200
+
1201
+ group.start = start;
1202
+ start += group.count;
1203
+
1204
+ }
1205
+
1206
+ // merge groups
1207
+
1208
+ let currentGroup = groups[ 0 ];
1209
+
1210
+ geometry.groups = [ currentGroup ];
1211
+
1212
+ for ( let i = 1; i < groups.length; i ++ ) {
1213
+
1214
+ const group = groups[ i ];
1215
+
1216
+ if ( currentGroup.materialIndex === group.materialIndex ) {
1217
+
1218
+ currentGroup.count += group.count;
1219
+
1220
+ } else {
1221
+
1222
+ currentGroup = group;
1223
+ geometry.groups.push( currentGroup );
1224
+
1225
+ }
1226
+
1227
+ }
1228
+
1229
+ return geometry;
1230
+
1231
+ }
1232
+
1233
+
1234
+ /**
1235
+ * Modifies the supplied geometry if it is non-indexed, otherwise creates a new,
1236
+ * non-indexed geometry. Returns the geometry with smooth normals everywhere except
1237
+ * faces that meet at an angle greater than the crease angle.
1238
+ *
1239
+ * @param {BufferGeometry} geometry
1240
+ * @param {number} [creaseAngle]
1241
+ * @return {BufferGeometry}
1242
+ */
1243
+ function toCreasedNormals( geometry, creaseAngle = Math.PI / 3 /* 60 degrees */ ) {
1244
+
1245
+ const creaseDot = Math.cos( creaseAngle );
1246
+ const hashMultiplier = ( 1 + 1e-10 ) * 1e2;
1247
+
1248
+ // reusable vectors
1249
+ const verts = [ new Vector3(), new Vector3(), new Vector3() ];
1250
+ const tempVec1 = new Vector3();
1251
+ const tempVec2 = new Vector3();
1252
+ const tempNorm = new Vector3();
1253
+ const tempNorm2 = new Vector3();
1254
+
1255
+ // hashes a vector
1256
+ function hashVertex( v ) {
1257
+
1258
+ const x = ~ ~ ( v.x * hashMultiplier );
1259
+ const y = ~ ~ ( v.y * hashMultiplier );
1260
+ const z = ~ ~ ( v.z * hashMultiplier );
1261
+ return `${x},${y},${z}`;
1262
+
1263
+ }
1264
+
1265
+ // BufferGeometry.toNonIndexed() warns if the geometry is non-indexed
1266
+ // and returns the original geometry
1267
+ const resultGeometry = geometry.index ? geometry.toNonIndexed() : geometry;
1268
+ const posAttr = resultGeometry.attributes.position;
1269
+ const vertexMap = {};
1270
+
1271
+ // find all the normals shared by commonly located vertices
1272
+ for ( let i = 0, l = posAttr.count / 3; i < l; i ++ ) {
1273
+
1274
+ const i3 = 3 * i;
1275
+ const a = verts[ 0 ].fromBufferAttribute( posAttr, i3 + 0 );
1276
+ const b = verts[ 1 ].fromBufferAttribute( posAttr, i3 + 1 );
1277
+ const c = verts[ 2 ].fromBufferAttribute( posAttr, i3 + 2 );
1278
+
1279
+ tempVec1.subVectors( c, b );
1280
+ tempVec2.subVectors( a, b );
1281
+
1282
+ // add the normal to the map for all vertices
1283
+ const normal = new Vector3().crossVectors( tempVec1, tempVec2 ).normalize();
1284
+ for ( let n = 0; n < 3; n ++ ) {
1285
+
1286
+ const vert = verts[ n ];
1287
+ const hash = hashVertex( vert );
1288
+ if ( ! ( hash in vertexMap ) ) {
1289
+
1290
+ vertexMap[ hash ] = [];
1291
+
1292
+ }
1293
+
1294
+ vertexMap[ hash ].push( normal );
1295
+
1296
+ }
1297
+
1298
+ }
1299
+
1300
+ // average normals from all vertices that share a common location if they are within the
1301
+ // provided crease threshold
1302
+ const normalArray = new Float32Array( posAttr.count * 3 );
1303
+ const normAttr = new BufferAttribute( normalArray, 3, false );
1304
+ for ( let i = 0, l = posAttr.count / 3; i < l; i ++ ) {
1305
+
1306
+ // get the face normal for this vertex
1307
+ const i3 = 3 * i;
1308
+ const a = verts[ 0 ].fromBufferAttribute( posAttr, i3 + 0 );
1309
+ const b = verts[ 1 ].fromBufferAttribute( posAttr, i3 + 1 );
1310
+ const c = verts[ 2 ].fromBufferAttribute( posAttr, i3 + 2 );
1311
+
1312
+ tempVec1.subVectors( c, b );
1313
+ tempVec2.subVectors( a, b );
1314
+
1315
+ tempNorm.crossVectors( tempVec1, tempVec2 ).normalize();
1316
+
1317
+ // average all normals that meet the threshold and set the normal value
1318
+ for ( let n = 0; n < 3; n ++ ) {
1319
+
1320
+ const vert = verts[ n ];
1321
+ const hash = hashVertex( vert );
1322
+ const otherNormals = vertexMap[ hash ];
1323
+ tempNorm2.set( 0, 0, 0 );
1324
+
1325
+ for ( let k = 0, lk = otherNormals.length; k < lk; k ++ ) {
1326
+
1327
+ const otherNorm = otherNormals[ k ];
1328
+ if ( tempNorm.dot( otherNorm ) > creaseDot ) {
1329
+
1330
+ tempNorm2.add( otherNorm );
1331
+
1332
+ }
1333
+
1334
+ }
1335
+
1336
+ tempNorm2.normalize();
1337
+ normAttr.setXYZ( i3 + n, tempNorm2.x, tempNorm2.y, tempNorm2.z );
1338
+
1339
+ }
1340
+
1341
+ }
1342
+
1343
+ resultGeometry.setAttribute( 'normal', normAttr );
1344
+ return resultGeometry;
1345
+
1346
+ }
1347
+
1348
+ function mergeBufferGeometries( geometries, useGroups = false ) {
1349
+
1350
+ console.warn( 'THREE.BufferGeometryUtils: mergeBufferGeometries() has been renamed to mergeGeometries().' ); // @deprecated, r151
1351
+ return mergeGeometries( geometries, useGroups );
1352
+
1353
+ }
1354
+
1355
+ function mergeBufferAttributes( attributes ) {
1356
+
1357
+ console.warn( 'THREE.BufferGeometryUtils: mergeBufferAttributes() has been renamed to mergeAttributes().' ); // @deprecated, r151
1358
+ return mergeAttributes( attributes );
1359
+
1360
+ }
1361
+
1362
+ export {
1363
+ computeMikkTSpaceTangents,
1364
+ mergeGeometries,
1365
+ mergeBufferGeometries,
1366
+ mergeAttributes,
1367
+ mergeBufferAttributes,
1368
+ interleaveAttributes,
1369
+ estimateBytesUsed,
1370
+ mergeVertices,
1371
+ toTrianglesDrawMode,
1372
+ computeMorphedAttributes,
1373
+ mergeGroups,
1374
+ toCreasedNormals
1375
+ };
static/GLTFLoader.js ADDED
The diff for this file is too large to render. See raw diff
 
static/OrbitControls.js ADDED
@@ -0,0 +1,1407 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ EventDispatcher,
3
+ MOUSE,
4
+ Quaternion,
5
+ Spherical,
6
+ TOUCH,
7
+ Vector2,
8
+ Vector3,
9
+ Plane,
10
+ Ray,
11
+ MathUtils
12
+ } from 'three';
13
+
14
+ // OrbitControls performs orbiting, dollying (zooming), and panning.
15
+ // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
16
+ //
17
+ // Orbit - left mouse / touch: one-finger move
18
+ // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
19
+ // Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
20
+
21
+ const _changeEvent = { type: 'change' };
22
+ const _startEvent = { type: 'start' };
23
+ const _endEvent = { type: 'end' };
24
+ const _ray = new Ray();
25
+ const _plane = new Plane();
26
+ const TILT_LIMIT = Math.cos( 70 * MathUtils.DEG2RAD );
27
+
28
+ class OrbitControls extends EventDispatcher {
29
+
30
+ constructor( object, domElement ) {
31
+
32
+ super();
33
+
34
+ this.object = object;
35
+ this.domElement = domElement;
36
+ this.domElement.style.touchAction = 'none'; // disable touch scroll
37
+
38
+ // Set to false to disable this control
39
+ this.enabled = true;
40
+
41
+ // "target" sets the location of focus, where the object orbits around
42
+ this.target = new Vector3();
43
+
44
+ // Sets the 3D cursor (similar to Blender), from which the maxTargetRadius takes effect
45
+ this.cursor = new Vector3();
46
+
47
+ // How far you can dolly in and out ( PerspectiveCamera only )
48
+ this.minDistance = 0;
49
+ this.maxDistance = Infinity;
50
+
51
+ // How far you can zoom in and out ( OrthographicCamera only )
52
+ this.minZoom = 0;
53
+ this.maxZoom = Infinity;
54
+
55
+ // Limit camera target within a spherical area around the cursor
56
+ this.minTargetRadius = 0;
57
+ this.maxTargetRadius = Infinity;
58
+
59
+ // How far you can orbit vertically, upper and lower limits.
60
+ // Range is 0 to Math.PI radians.
61
+ this.minPolarAngle = 0; // radians
62
+ this.maxPolarAngle = Math.PI; // radians
63
+
64
+ // How far you can orbit horizontally, upper and lower limits.
65
+ // If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI )
66
+ this.minAzimuthAngle = - Infinity; // radians
67
+ this.maxAzimuthAngle = Infinity; // radians
68
+
69
+ // Set to true to enable damping (inertia)
70
+ // If damping is enabled, you must call controls.update() in your animation loop
71
+ this.enableDamping = false;
72
+ this.dampingFactor = 0.05;
73
+
74
+ // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
75
+ // Set to false to disable zooming
76
+ this.enableZoom = true;
77
+ this.zoomSpeed = 1.0;
78
+
79
+ // Set to false to disable rotating
80
+ this.enableRotate = true;
81
+ this.rotateSpeed = 1.0;
82
+
83
+ // Set to false to disable panning
84
+ this.enablePan = true;
85
+ this.panSpeed = 1.0;
86
+ this.screenSpacePanning = true; // if false, pan orthogonal to world-space direction camera.up
87
+ this.keyPanSpeed = 7.0; // pixels moved per arrow key push
88
+ this.zoomToCursor = false;
89
+
90
+ // Set to true to automatically rotate around the target
91
+ // If auto-rotate is enabled, you must call controls.update() in your animation loop
92
+ this.autoRotate = false;
93
+ this.autoRotateSpeed = 2.0; // 30 seconds per orbit when fps is 60
94
+
95
+ // The four arrow keys
96
+ this.keys = { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' };
97
+
98
+ // Mouse buttons
99
+ this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
100
+
101
+ // Touch fingers
102
+ this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
103
+
104
+ // for reset
105
+ this.target0 = this.target.clone();
106
+ this.position0 = this.object.position.clone();
107
+ this.zoom0 = this.object.zoom;
108
+
109
+ // the target DOM element for key events
110
+ this._domElementKeyEvents = null;
111
+
112
+ //
113
+ // public methods
114
+ //
115
+
116
+ this.getPolarAngle = function () {
117
+
118
+ return spherical.phi;
119
+
120
+ };
121
+
122
+ this.getAzimuthalAngle = function () {
123
+
124
+ return spherical.theta;
125
+
126
+ };
127
+
128
+ this.getDistance = function () {
129
+
130
+ return this.object.position.distanceTo( this.target );
131
+
132
+ };
133
+
134
+ this.listenToKeyEvents = function ( domElement ) {
135
+
136
+ domElement.addEventListener( 'keydown', onKeyDown );
137
+ this._domElementKeyEvents = domElement;
138
+
139
+ };
140
+
141
+ this.stopListenToKeyEvents = function () {
142
+
143
+ this._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
144
+ this._domElementKeyEvents = null;
145
+
146
+ };
147
+
148
+ this.saveState = function () {
149
+
150
+ scope.target0.copy( scope.target );
151
+ scope.position0.copy( scope.object.position );
152
+ scope.zoom0 = scope.object.zoom;
153
+
154
+ };
155
+
156
+ this.reset = function () {
157
+
158
+ scope.target.copy( scope.target0 );
159
+ scope.object.position.copy( scope.position0 );
160
+ scope.object.zoom = scope.zoom0;
161
+
162
+ scope.object.updateProjectionMatrix();
163
+ scope.dispatchEvent( _changeEvent );
164
+
165
+ scope.update();
166
+
167
+ state = STATE.NONE;
168
+
169
+ };
170
+
171
+ // this method is exposed, but perhaps it would be better if we can make it private...
172
+ this.update = function () {
173
+
174
+ const offset = new Vector3();
175
+
176
+ // so camera.up is the orbit axis
177
+ const quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
178
+ const quatInverse = quat.clone().invert();
179
+
180
+ const lastPosition = new Vector3();
181
+ const lastQuaternion = new Quaternion();
182
+ const lastTargetPosition = new Vector3();
183
+
184
+ const twoPI = 2 * Math.PI;
185
+
186
+ return function update( deltaTime = null ) {
187
+
188
+ const position = scope.object.position;
189
+
190
+ offset.copy( position ).sub( scope.target );
191
+
192
+ // rotate offset to "y-axis-is-up" space
193
+ offset.applyQuaternion( quat );
194
+
195
+ // angle from z-axis around y-axis
196
+ spherical.setFromVector3( offset );
197
+
198
+ if ( scope.autoRotate && state === STATE.NONE ) {
199
+
200
+ rotateLeft( getAutoRotationAngle( deltaTime ) );
201
+
202
+ }
203
+
204
+ if ( scope.enableDamping ) {
205
+
206
+ spherical.theta += sphericalDelta.theta * scope.dampingFactor;
207
+ spherical.phi += sphericalDelta.phi * scope.dampingFactor;
208
+
209
+ } else {
210
+
211
+ spherical.theta += sphericalDelta.theta;
212
+ spherical.phi += sphericalDelta.phi;
213
+
214
+ }
215
+
216
+ // restrict theta to be between desired limits
217
+
218
+ let min = scope.minAzimuthAngle;
219
+ let max = scope.maxAzimuthAngle;
220
+
221
+ if ( isFinite( min ) && isFinite( max ) ) {
222
+
223
+ if ( min < - Math.PI ) min += twoPI; else if ( min > Math.PI ) min -= twoPI;
224
+
225
+ if ( max < - Math.PI ) max += twoPI; else if ( max > Math.PI ) max -= twoPI;
226
+
227
+ if ( min <= max ) {
228
+
229
+ spherical.theta = Math.max( min, Math.min( max, spherical.theta ) );
230
+
231
+ } else {
232
+
233
+ spherical.theta = ( spherical.theta > ( min + max ) / 2 ) ?
234
+ Math.max( min, spherical.theta ) :
235
+ Math.min( max, spherical.theta );
236
+
237
+ }
238
+
239
+ }
240
+
241
+ // restrict phi to be between desired limits
242
+ spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
243
+
244
+ spherical.makeSafe();
245
+
246
+
247
+ // move target to panned location
248
+
249
+ if ( scope.enableDamping === true ) {
250
+
251
+ scope.target.addScaledVector( panOffset, scope.dampingFactor );
252
+
253
+ } else {
254
+
255
+ scope.target.add( panOffset );
256
+
257
+ }
258
+
259
+ // Limit the target distance from the cursor to create a sphere around the center of interest
260
+ scope.target.sub( scope.cursor );
261
+ scope.target.clampLength( scope.minTargetRadius, scope.maxTargetRadius );
262
+ scope.target.add( scope.cursor );
263
+
264
+ // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
265
+ // we adjust zoom later in these cases
266
+ if ( scope.zoomToCursor && performCursorZoom || scope.object.isOrthographicCamera ) {
267
+
268
+ spherical.radius = clampDistance( spherical.radius );
269
+
270
+ } else {
271
+
272
+ spherical.radius = clampDistance( spherical.radius * scale );
273
+
274
+ }
275
+
276
+ offset.setFromSpherical( spherical );
277
+
278
+ // rotate offset back to "camera-up-vector-is-up" space
279
+ offset.applyQuaternion( quatInverse );
280
+
281
+ position.copy( scope.target ).add( offset );
282
+
283
+ scope.object.lookAt( scope.target );
284
+
285
+ if ( scope.enableDamping === true ) {
286
+
287
+ sphericalDelta.theta *= ( 1 - scope.dampingFactor );
288
+ sphericalDelta.phi *= ( 1 - scope.dampingFactor );
289
+
290
+ panOffset.multiplyScalar( 1 - scope.dampingFactor );
291
+
292
+ } else {
293
+
294
+ sphericalDelta.set( 0, 0, 0 );
295
+
296
+ panOffset.set( 0, 0, 0 );
297
+
298
+ }
299
+
300
+ // adjust camera position
301
+ let zoomChanged = false;
302
+ if ( scope.zoomToCursor && performCursorZoom ) {
303
+
304
+ let newRadius = null;
305
+ if ( scope.object.isPerspectiveCamera ) {
306
+
307
+ // move the camera down the pointer ray
308
+ // this method avoids floating point error
309
+ const prevRadius = offset.length();
310
+ newRadius = clampDistance( prevRadius * scale );
311
+
312
+ const radiusDelta = prevRadius - newRadius;
313
+ scope.object.position.addScaledVector( dollyDirection, radiusDelta );
314
+ scope.object.updateMatrixWorld();
315
+
316
+ } else if ( scope.object.isOrthographicCamera ) {
317
+
318
+ // adjust the ortho camera position based on zoom changes
319
+ const mouseBefore = new Vector3( mouse.x, mouse.y, 0 );
320
+ mouseBefore.unproject( scope.object );
321
+
322
+ scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) );
323
+ scope.object.updateProjectionMatrix();
324
+ zoomChanged = true;
325
+
326
+ const mouseAfter = new Vector3( mouse.x, mouse.y, 0 );
327
+ mouseAfter.unproject( scope.object );
328
+
329
+ scope.object.position.sub( mouseAfter ).add( mouseBefore );
330
+ scope.object.updateMatrixWorld();
331
+
332
+ newRadius = offset.length();
333
+
334
+ } else {
335
+
336
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' );
337
+ scope.zoomToCursor = false;
338
+
339
+ }
340
+
341
+ // handle the placement of the target
342
+ if ( newRadius !== null ) {
343
+
344
+ if ( this.screenSpacePanning ) {
345
+
346
+ // position the orbit target in front of the new camera position
347
+ scope.target.set( 0, 0, - 1 )
348
+ .transformDirection( scope.object.matrix )
349
+ .multiplyScalar( newRadius )
350
+ .add( scope.object.position );
351
+
352
+ } else {
353
+
354
+ // get the ray and translation plane to compute target
355
+ _ray.origin.copy( scope.object.position );
356
+ _ray.direction.set( 0, 0, - 1 ).transformDirection( scope.object.matrix );
357
+
358
+ // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid
359
+ // extremely large values
360
+ if ( Math.abs( scope.object.up.dot( _ray.direction ) ) < TILT_LIMIT ) {
361
+
362
+ object.lookAt( scope.target );
363
+
364
+ } else {
365
+
366
+ _plane.setFromNormalAndCoplanarPoint( scope.object.up, scope.target );
367
+ _ray.intersectPlane( _plane, scope.target );
368
+
369
+ }
370
+
371
+ }
372
+
373
+ }
374
+
375
+ } else if ( scope.object.isOrthographicCamera ) {
376
+
377
+ scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) );
378
+ scope.object.updateProjectionMatrix();
379
+ zoomChanged = true;
380
+
381
+ }
382
+
383
+ scale = 1;
384
+ performCursorZoom = false;
385
+
386
+ // update condition is:
387
+ // min(camera displacement, camera rotation in radians)^2 > EPS
388
+ // using small-angle approximation cos(x/2) = 1 - x^2 / 8
389
+
390
+ if ( zoomChanged ||
391
+ lastPosition.distanceToSquared( scope.object.position ) > EPS ||
392
+ 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ||
393
+ lastTargetPosition.distanceToSquared( scope.target ) > 0 ) {
394
+
395
+ scope.dispatchEvent( _changeEvent );
396
+
397
+ lastPosition.copy( scope.object.position );
398
+ lastQuaternion.copy( scope.object.quaternion );
399
+ lastTargetPosition.copy( scope.target );
400
+
401
+ zoomChanged = false;
402
+
403
+ return true;
404
+
405
+ }
406
+
407
+ return false;
408
+
409
+ };
410
+
411
+ }();
412
+
413
+ this.dispose = function () {
414
+
415
+ scope.domElement.removeEventListener( 'contextmenu', onContextMenu );
416
+
417
+ scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
418
+ scope.domElement.removeEventListener( 'pointercancel', onPointerUp );
419
+ scope.domElement.removeEventListener( 'wheel', onMouseWheel );
420
+
421
+ scope.domElement.removeEventListener( 'pointermove', onPointerMove );
422
+ scope.domElement.removeEventListener( 'pointerup', onPointerUp );
423
+
424
+
425
+ if ( scope._domElementKeyEvents !== null ) {
426
+
427
+ scope._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
428
+ scope._domElementKeyEvents = null;
429
+
430
+ }
431
+
432
+ //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
433
+
434
+ };
435
+
436
+ //
437
+ // internals
438
+ //
439
+
440
+ const scope = this;
441
+
442
+ const STATE = {
443
+ NONE: - 1,
444
+ ROTATE: 0,
445
+ DOLLY: 1,
446
+ PAN: 2,
447
+ TOUCH_ROTATE: 3,
448
+ TOUCH_PAN: 4,
449
+ TOUCH_DOLLY_PAN: 5,
450
+ TOUCH_DOLLY_ROTATE: 6
451
+ };
452
+
453
+ let state = STATE.NONE;
454
+
455
+ const EPS = 0.000001;
456
+
457
+ // current position in spherical coordinates
458
+ const spherical = new Spherical();
459
+ const sphericalDelta = new Spherical();
460
+
461
+ let scale = 1;
462
+ const panOffset = new Vector3();
463
+
464
+ const rotateStart = new Vector2();
465
+ const rotateEnd = new Vector2();
466
+ const rotateDelta = new Vector2();
467
+
468
+ const panStart = new Vector2();
469
+ const panEnd = new Vector2();
470
+ const panDelta = new Vector2();
471
+
472
+ const dollyStart = new Vector2();
473
+ const dollyEnd = new Vector2();
474
+ const dollyDelta = new Vector2();
475
+
476
+ const dollyDirection = new Vector3();
477
+ const mouse = new Vector2();
478
+ let performCursorZoom = false;
479
+
480
+ const pointers = [];
481
+ const pointerPositions = {};
482
+
483
+ function getAutoRotationAngle( deltaTime ) {
484
+
485
+ if ( deltaTime !== null ) {
486
+
487
+ return ( 2 * Math.PI / 60 * scope.autoRotateSpeed ) * deltaTime;
488
+
489
+ } else {
490
+
491
+ return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
492
+
493
+ }
494
+
495
+ }
496
+
497
+ function getZoomScale() {
498
+
499
+ return Math.pow( 0.95, scope.zoomSpeed );
500
+
501
+ }
502
+
503
+ function rotateLeft( angle ) {
504
+
505
+ sphericalDelta.theta -= angle;
506
+
507
+ }
508
+
509
+ function rotateUp( angle ) {
510
+
511
+ sphericalDelta.phi -= angle;
512
+
513
+ }
514
+
515
+ const panLeft = function () {
516
+
517
+ const v = new Vector3();
518
+
519
+ return function panLeft( distance, objectMatrix ) {
520
+
521
+ v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
522
+ v.multiplyScalar( - distance );
523
+
524
+ panOffset.add( v );
525
+
526
+ };
527
+
528
+ }();
529
+
530
+ const panUp = function () {
531
+
532
+ const v = new Vector3();
533
+
534
+ return function panUp( distance, objectMatrix ) {
535
+
536
+ if ( scope.screenSpacePanning === true ) {
537
+
538
+ v.setFromMatrixColumn( objectMatrix, 1 );
539
+
540
+ } else {
541
+
542
+ v.setFromMatrixColumn( objectMatrix, 0 );
543
+ v.crossVectors( scope.object.up, v );
544
+
545
+ }
546
+
547
+ v.multiplyScalar( distance );
548
+
549
+ panOffset.add( v );
550
+
551
+ };
552
+
553
+ }();
554
+
555
+ // deltaX and deltaY are in pixels; right and down are positive
556
+ const pan = function () {
557
+
558
+ const offset = new Vector3();
559
+
560
+ return function pan( deltaX, deltaY ) {
561
+
562
+ const element = scope.domElement;
563
+
564
+ if ( scope.object.isPerspectiveCamera ) {
565
+
566
+ // perspective
567
+ const position = scope.object.position;
568
+ offset.copy( position ).sub( scope.target );
569
+ let targetDistance = offset.length();
570
+
571
+ // half of the fov is center to top of screen
572
+ targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
573
+
574
+ // we use only clientHeight here so aspect ratio does not distort speed
575
+ panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
576
+ panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
577
+
578
+ } else if ( scope.object.isOrthographicCamera ) {
579
+
580
+ // orthographic
581
+ panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
582
+ panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
583
+
584
+ } else {
585
+
586
+ // camera neither orthographic nor perspective
587
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
588
+ scope.enablePan = false;
589
+
590
+ }
591
+
592
+ };
593
+
594
+ }();
595
+
596
+ function dollyOut( dollyScale ) {
597
+
598
+ if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) {
599
+
600
+ scale /= dollyScale;
601
+
602
+ } else {
603
+
604
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
605
+ scope.enableZoom = false;
606
+
607
+ }
608
+
609
+ }
610
+
611
+ function dollyIn( dollyScale ) {
612
+
613
+ if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) {
614
+
615
+ scale *= dollyScale;
616
+
617
+ } else {
618
+
619
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
620
+ scope.enableZoom = false;
621
+
622
+ }
623
+
624
+ }
625
+
626
+ function updateMouseParameters( event ) {
627
+
628
+ if ( ! scope.zoomToCursor ) {
629
+
630
+ return;
631
+
632
+ }
633
+
634
+ performCursorZoom = true;
635
+
636
+ const rect = scope.domElement.getBoundingClientRect();
637
+ const x = event.clientX - rect.left;
638
+ const y = event.clientY - rect.top;
639
+ const w = rect.width;
640
+ const h = rect.height;
641
+
642
+ mouse.x = ( x / w ) * 2 - 1;
643
+ mouse.y = - ( y / h ) * 2 + 1;
644
+
645
+ dollyDirection.set( mouse.x, mouse.y, 1 ).unproject( scope.object ).sub( scope.object.position ).normalize();
646
+
647
+ }
648
+
649
+ function clampDistance( dist ) {
650
+
651
+ return Math.max( scope.minDistance, Math.min( scope.maxDistance, dist ) );
652
+
653
+ }
654
+
655
+ //
656
+ // event callbacks - update the object state
657
+ //
658
+
659
+ function handleMouseDownRotate( event ) {
660
+
661
+ rotateStart.set( event.clientX, event.clientY );
662
+
663
+ }
664
+
665
+ function handleMouseDownDolly( event ) {
666
+
667
+ updateMouseParameters( event );
668
+ dollyStart.set( event.clientX, event.clientY );
669
+
670
+ }
671
+
672
+ function handleMouseDownPan( event ) {
673
+
674
+ panStart.set( event.clientX, event.clientY );
675
+
676
+ }
677
+
678
+ function handleMouseMoveRotate( event ) {
679
+
680
+ rotateEnd.set( event.clientX, event.clientY );
681
+
682
+ rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
683
+
684
+ const element = scope.domElement;
685
+
686
+ rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
687
+
688
+ rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
689
+
690
+ rotateStart.copy( rotateEnd );
691
+
692
+ scope.update();
693
+
694
+ }
695
+
696
+ function handleMouseMoveDolly( event ) {
697
+
698
+ dollyEnd.set( event.clientX, event.clientY );
699
+
700
+ dollyDelta.subVectors( dollyEnd, dollyStart );
701
+
702
+ if ( dollyDelta.y > 0 ) {
703
+
704
+ dollyOut( getZoomScale() );
705
+
706
+ } else if ( dollyDelta.y < 0 ) {
707
+
708
+ dollyIn( getZoomScale() );
709
+
710
+ }
711
+
712
+ dollyStart.copy( dollyEnd );
713
+
714
+ scope.update();
715
+
716
+ }
717
+
718
+ function handleMouseMovePan( event ) {
719
+
720
+ panEnd.set( event.clientX, event.clientY );
721
+
722
+ panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
723
+
724
+ pan( panDelta.x, panDelta.y );
725
+
726
+ panStart.copy( panEnd );
727
+
728
+ scope.update();
729
+
730
+ }
731
+
732
+ function handleMouseWheel( event ) {
733
+
734
+ updateMouseParameters( event );
735
+
736
+ if ( event.deltaY < 0 ) {
737
+
738
+ dollyIn( getZoomScale() );
739
+
740
+ } else if ( event.deltaY > 0 ) {
741
+
742
+ dollyOut( getZoomScale() );
743
+
744
+ }
745
+
746
+ scope.update();
747
+
748
+ }
749
+
750
+ function handleKeyDown( event ) {
751
+
752
+ let needsUpdate = false;
753
+
754
+ switch ( event.code ) {
755
+
756
+ case scope.keys.UP:
757
+
758
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
759
+
760
+ rotateUp( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
761
+
762
+ } else {
763
+
764
+ pan( 0, scope.keyPanSpeed );
765
+
766
+ }
767
+
768
+ needsUpdate = true;
769
+ break;
770
+
771
+ case scope.keys.BOTTOM:
772
+
773
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
774
+
775
+ rotateUp( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
776
+
777
+ } else {
778
+
779
+ pan( 0, - scope.keyPanSpeed );
780
+
781
+ }
782
+
783
+ needsUpdate = true;
784
+ break;
785
+
786
+ case scope.keys.LEFT:
787
+
788
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
789
+
790
+ rotateLeft( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
791
+
792
+ } else {
793
+
794
+ pan( scope.keyPanSpeed, 0 );
795
+
796
+ }
797
+
798
+ needsUpdate = true;
799
+ break;
800
+
801
+ case scope.keys.RIGHT:
802
+
803
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
804
+
805
+ rotateLeft( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
806
+
807
+ } else {
808
+
809
+ pan( - scope.keyPanSpeed, 0 );
810
+
811
+ }
812
+
813
+ needsUpdate = true;
814
+ break;
815
+
816
+ }
817
+
818
+ if ( needsUpdate ) {
819
+
820
+ // prevent the browser from scrolling on cursor keys
821
+ event.preventDefault();
822
+
823
+ scope.update();
824
+
825
+ }
826
+
827
+
828
+ }
829
+
830
+ function handleTouchStartRotate() {
831
+
832
+ if ( pointers.length === 1 ) {
833
+
834
+ rotateStart.set( pointers[ 0 ].pageX, pointers[ 0 ].pageY );
835
+
836
+ } else {
837
+
838
+ const x = 0.5 * ( pointers[ 0 ].pageX + pointers[ 1 ].pageX );
839
+ const y = 0.5 * ( pointers[ 0 ].pageY + pointers[ 1 ].pageY );
840
+
841
+ rotateStart.set( x, y );
842
+
843
+ }
844
+
845
+ }
846
+
847
+ function handleTouchStartPan() {
848
+
849
+ if ( pointers.length === 1 ) {
850
+
851
+ panStart.set( pointers[ 0 ].pageX, pointers[ 0 ].pageY );
852
+
853
+ } else {
854
+
855
+ const x = 0.5 * ( pointers[ 0 ].pageX + pointers[ 1 ].pageX );
856
+ const y = 0.5 * ( pointers[ 0 ].pageY + pointers[ 1 ].pageY );
857
+
858
+ panStart.set( x, y );
859
+
860
+ }
861
+
862
+ }
863
+
864
+ function handleTouchStartDolly() {
865
+
866
+ const dx = pointers[ 0 ].pageX - pointers[ 1 ].pageX;
867
+ const dy = pointers[ 0 ].pageY - pointers[ 1 ].pageY;
868
+
869
+ const distance = Math.sqrt( dx * dx + dy * dy );
870
+
871
+ dollyStart.set( 0, distance );
872
+
873
+ }
874
+
875
+ function handleTouchStartDollyPan() {
876
+
877
+ if ( scope.enableZoom ) handleTouchStartDolly();
878
+
879
+ if ( scope.enablePan ) handleTouchStartPan();
880
+
881
+ }
882
+
883
+ function handleTouchStartDollyRotate() {
884
+
885
+ if ( scope.enableZoom ) handleTouchStartDolly();
886
+
887
+ if ( scope.enableRotate ) handleTouchStartRotate();
888
+
889
+ }
890
+
891
+ function handleTouchMoveRotate( event ) {
892
+
893
+ if ( pointers.length == 1 ) {
894
+
895
+ rotateEnd.set( event.pageX, event.pageY );
896
+
897
+ } else {
898
+
899
+ const position = getSecondPointerPosition( event );
900
+
901
+ const x = 0.5 * ( event.pageX + position.x );
902
+ const y = 0.5 * ( event.pageY + position.y );
903
+
904
+ rotateEnd.set( x, y );
905
+
906
+ }
907
+
908
+ rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
909
+
910
+ const element = scope.domElement;
911
+
912
+ rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
913
+
914
+ rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
915
+
916
+ rotateStart.copy( rotateEnd );
917
+
918
+ }
919
+
920
+ function handleTouchMovePan( event ) {
921
+
922
+ if ( pointers.length === 1 ) {
923
+
924
+ panEnd.set( event.pageX, event.pageY );
925
+
926
+ } else {
927
+
928
+ const position = getSecondPointerPosition( event );
929
+
930
+ const x = 0.5 * ( event.pageX + position.x );
931
+ const y = 0.5 * ( event.pageY + position.y );
932
+
933
+ panEnd.set( x, y );
934
+
935
+ }
936
+
937
+ panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
938
+
939
+ pan( panDelta.x, panDelta.y );
940
+
941
+ panStart.copy( panEnd );
942
+
943
+ }
944
+
945
+ function handleTouchMoveDolly( event ) {
946
+
947
+ const position = getSecondPointerPosition( event );
948
+
949
+ const dx = event.pageX - position.x;
950
+ const dy = event.pageY - position.y;
951
+
952
+ const distance = Math.sqrt( dx * dx + dy * dy );
953
+
954
+ dollyEnd.set( 0, distance );
955
+
956
+ dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
957
+
958
+ dollyOut( dollyDelta.y );
959
+
960
+ dollyStart.copy( dollyEnd );
961
+
962
+ }
963
+
964
+ function handleTouchMoveDollyPan( event ) {
965
+
966
+ if ( scope.enableZoom ) handleTouchMoveDolly( event );
967
+
968
+ if ( scope.enablePan ) handleTouchMovePan( event );
969
+
970
+ }
971
+
972
+ function handleTouchMoveDollyRotate( event ) {
973
+
974
+ if ( scope.enableZoom ) handleTouchMoveDolly( event );
975
+
976
+ if ( scope.enableRotate ) handleTouchMoveRotate( event );
977
+
978
+ }
979
+
980
+ //
981
+ // event handlers - FSM: listen for events and reset state
982
+ //
983
+
984
+ function onPointerDown( event ) {
985
+
986
+ if ( scope.enabled === false ) return;
987
+
988
+ if ( pointers.length === 0 ) {
989
+
990
+ scope.domElement.setPointerCapture( event.pointerId );
991
+
992
+ scope.domElement.addEventListener( 'pointermove', onPointerMove );
993
+ scope.domElement.addEventListener( 'pointerup', onPointerUp );
994
+
995
+ }
996
+
997
+ //
998
+
999
+ addPointer( event );
1000
+
1001
+ if ( event.pointerType === 'touch' ) {
1002
+
1003
+ onTouchStart( event );
1004
+
1005
+ } else {
1006
+
1007
+ onMouseDown( event );
1008
+
1009
+ }
1010
+
1011
+ }
1012
+
1013
+ function onPointerMove( event ) {
1014
+
1015
+ if ( scope.enabled === false ) return;
1016
+
1017
+ if ( event.pointerType === 'touch' ) {
1018
+
1019
+ onTouchMove( event );
1020
+
1021
+ } else {
1022
+
1023
+ onMouseMove( event );
1024
+
1025
+ }
1026
+
1027
+ }
1028
+
1029
+ function onPointerUp( event ) {
1030
+
1031
+ removePointer( event );
1032
+
1033
+ if ( pointers.length === 0 ) {
1034
+
1035
+ scope.domElement.releasePointerCapture( event.pointerId );
1036
+
1037
+ scope.domElement.removeEventListener( 'pointermove', onPointerMove );
1038
+ scope.domElement.removeEventListener( 'pointerup', onPointerUp );
1039
+
1040
+ }
1041
+
1042
+ scope.dispatchEvent( _endEvent );
1043
+
1044
+ state = STATE.NONE;
1045
+
1046
+ }
1047
+
1048
+ function onMouseDown( event ) {
1049
+
1050
+ let mouseAction;
1051
+
1052
+ switch ( event.button ) {
1053
+
1054
+ case 0:
1055
+
1056
+ mouseAction = scope.mouseButtons.LEFT;
1057
+ break;
1058
+
1059
+ case 1:
1060
+
1061
+ mouseAction = scope.mouseButtons.MIDDLE;
1062
+ break;
1063
+
1064
+ case 2:
1065
+
1066
+ mouseAction = scope.mouseButtons.RIGHT;
1067
+ break;
1068
+
1069
+ default:
1070
+
1071
+ mouseAction = - 1;
1072
+
1073
+ }
1074
+
1075
+ switch ( mouseAction ) {
1076
+
1077
+ case MOUSE.DOLLY:
1078
+
1079
+ if ( scope.enableZoom === false ) return;
1080
+
1081
+ handleMouseDownDolly( event );
1082
+
1083
+ state = STATE.DOLLY;
1084
+
1085
+ break;
1086
+
1087
+ case MOUSE.ROTATE:
1088
+
1089
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1090
+
1091
+ if ( scope.enablePan === false ) return;
1092
+
1093
+ handleMouseDownPan( event );
1094
+
1095
+ state = STATE.PAN;
1096
+
1097
+ } else {
1098
+
1099
+ if ( scope.enableRotate === false ) return;
1100
+
1101
+ handleMouseDownRotate( event );
1102
+
1103
+ state = STATE.ROTATE;
1104
+
1105
+ }
1106
+
1107
+ break;
1108
+
1109
+ case MOUSE.PAN:
1110
+
1111
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1112
+
1113
+ if ( scope.enableRotate === false ) return;
1114
+
1115
+ handleMouseDownRotate( event );
1116
+
1117
+ state = STATE.ROTATE;
1118
+
1119
+ } else {
1120
+
1121
+ if ( scope.enablePan === false ) return;
1122
+
1123
+ handleMouseDownPan( event );
1124
+
1125
+ state = STATE.PAN;
1126
+
1127
+ }
1128
+
1129
+ break;
1130
+
1131
+ default:
1132
+
1133
+ state = STATE.NONE;
1134
+
1135
+ }
1136
+
1137
+ if ( state !== STATE.NONE ) {
1138
+
1139
+ scope.dispatchEvent( _startEvent );
1140
+
1141
+ }
1142
+
1143
+ }
1144
+
1145
+ function onMouseMove( event ) {
1146
+
1147
+ switch ( state ) {
1148
+
1149
+ case STATE.ROTATE:
1150
+
1151
+ if ( scope.enableRotate === false ) return;
1152
+
1153
+ handleMouseMoveRotate( event );
1154
+
1155
+ break;
1156
+
1157
+ case STATE.DOLLY:
1158
+
1159
+ if ( scope.enableZoom === false ) return;
1160
+
1161
+ handleMouseMoveDolly( event );
1162
+
1163
+ break;
1164
+
1165
+ case STATE.PAN:
1166
+
1167
+ if ( scope.enablePan === false ) return;
1168
+
1169
+ handleMouseMovePan( event );
1170
+
1171
+ break;
1172
+
1173
+ }
1174
+
1175
+ }
1176
+
1177
+ function onMouseWheel( event ) {
1178
+
1179
+ if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return;
1180
+
1181
+ event.preventDefault();
1182
+
1183
+ scope.dispatchEvent( _startEvent );
1184
+
1185
+ handleMouseWheel( event );
1186
+
1187
+ scope.dispatchEvent( _endEvent );
1188
+
1189
+ }
1190
+
1191
+ function onKeyDown( event ) {
1192
+
1193
+ if ( scope.enabled === false || scope.enablePan === false ) return;
1194
+
1195
+ handleKeyDown( event );
1196
+
1197
+ }
1198
+
1199
+ function onTouchStart( event ) {
1200
+
1201
+ trackPointer( event );
1202
+
1203
+ switch ( pointers.length ) {
1204
+
1205
+ case 1:
1206
+
1207
+ switch ( scope.touches.ONE ) {
1208
+
1209
+ case TOUCH.ROTATE:
1210
+
1211
+ if ( scope.enableRotate === false ) return;
1212
+
1213
+ handleTouchStartRotate();
1214
+
1215
+ state = STATE.TOUCH_ROTATE;
1216
+
1217
+ break;
1218
+
1219
+ case TOUCH.PAN:
1220
+
1221
+ if ( scope.enablePan === false ) return;
1222
+
1223
+ handleTouchStartPan();
1224
+
1225
+ state = STATE.TOUCH_PAN;
1226
+
1227
+ break;
1228
+
1229
+ default:
1230
+
1231
+ state = STATE.NONE;
1232
+
1233
+ }
1234
+
1235
+ break;
1236
+
1237
+ case 2:
1238
+
1239
+ switch ( scope.touches.TWO ) {
1240
+
1241
+ case TOUCH.DOLLY_PAN:
1242
+
1243
+ if ( scope.enableZoom === false && scope.enablePan === false ) return;
1244
+
1245
+ handleTouchStartDollyPan();
1246
+
1247
+ state = STATE.TOUCH_DOLLY_PAN;
1248
+
1249
+ break;
1250
+
1251
+ case TOUCH.DOLLY_ROTATE:
1252
+
1253
+ if ( scope.enableZoom === false && scope.enableRotate === false ) return;
1254
+
1255
+ handleTouchStartDollyRotate();
1256
+
1257
+ state = STATE.TOUCH_DOLLY_ROTATE;
1258
+
1259
+ break;
1260
+
1261
+ default:
1262
+
1263
+ state = STATE.NONE;
1264
+
1265
+ }
1266
+
1267
+ break;
1268
+
1269
+ default:
1270
+
1271
+ state = STATE.NONE;
1272
+
1273
+ }
1274
+
1275
+ if ( state !== STATE.NONE ) {
1276
+
1277
+ scope.dispatchEvent( _startEvent );
1278
+
1279
+ }
1280
+
1281
+ }
1282
+
1283
+ function onTouchMove( event ) {
1284
+
1285
+ trackPointer( event );
1286
+
1287
+ switch ( state ) {
1288
+
1289
+ case STATE.TOUCH_ROTATE:
1290
+
1291
+ if ( scope.enableRotate === false ) return;
1292
+
1293
+ handleTouchMoveRotate( event );
1294
+
1295
+ scope.update();
1296
+
1297
+ break;
1298
+
1299
+ case STATE.TOUCH_PAN:
1300
+
1301
+ if ( scope.enablePan === false ) return;
1302
+
1303
+ handleTouchMovePan( event );
1304
+
1305
+ scope.update();
1306
+
1307
+ break;
1308
+
1309
+ case STATE.TOUCH_DOLLY_PAN:
1310
+
1311
+ if ( scope.enableZoom === false && scope.enablePan === false ) return;
1312
+
1313
+ handleTouchMoveDollyPan( event );
1314
+
1315
+ scope.update();
1316
+
1317
+ break;
1318
+
1319
+ case STATE.TOUCH_DOLLY_ROTATE:
1320
+
1321
+ if ( scope.enableZoom === false && scope.enableRotate === false ) return;
1322
+
1323
+ handleTouchMoveDollyRotate( event );
1324
+
1325
+ scope.update();
1326
+
1327
+ break;
1328
+
1329
+ default:
1330
+
1331
+ state = STATE.NONE;
1332
+
1333
+ }
1334
+
1335
+ }
1336
+
1337
+ function onContextMenu( event ) {
1338
+
1339
+ if ( scope.enabled === false ) return;
1340
+
1341
+ event.preventDefault();
1342
+
1343
+ }
1344
+
1345
+ function addPointer( event ) {
1346
+
1347
+ pointers.push( event );
1348
+
1349
+ }
1350
+
1351
+ function removePointer( event ) {
1352
+
1353
+ delete pointerPositions[ event.pointerId ];
1354
+
1355
+ for ( let i = 0; i < pointers.length; i ++ ) {
1356
+
1357
+ if ( pointers[ i ].pointerId == event.pointerId ) {
1358
+
1359
+ pointers.splice( i, 1 );
1360
+ return;
1361
+
1362
+ }
1363
+
1364
+ }
1365
+
1366
+ }
1367
+
1368
+ function trackPointer( event ) {
1369
+
1370
+ let position = pointerPositions[ event.pointerId ];
1371
+
1372
+ if ( position === undefined ) {
1373
+
1374
+ position = new Vector2();
1375
+ pointerPositions[ event.pointerId ] = position;
1376
+
1377
+ }
1378
+
1379
+ position.set( event.pageX, event.pageY );
1380
+
1381
+ }
1382
+
1383
+ function getSecondPointerPosition( event ) {
1384
+
1385
+ const pointer = ( event.pointerId === pointers[ 0 ].pointerId ) ? pointers[ 1 ] : pointers[ 0 ];
1386
+
1387
+ return pointerPositions[ pointer.pointerId ];
1388
+
1389
+ }
1390
+
1391
+ //
1392
+
1393
+ scope.domElement.addEventListener( 'contextmenu', onContextMenu );
1394
+
1395
+ scope.domElement.addEventListener( 'pointerdown', onPointerDown );
1396
+ scope.domElement.addEventListener( 'pointercancel', onPointerUp );
1397
+ scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
1398
+
1399
+ // force an update at start
1400
+
1401
+ this.update();
1402
+
1403
+ }
1404
+
1405
+ }
1406
+
1407
+ export { OrbitControls };
static/teaser_video_final.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3508c7e22570e791783fcc0e66583b79e0bf6baa07f65836885a4519861c7e4b
3
+ size 13240865
static/three.module.js ADDED
The diff for this file is too large to render. See raw diff
 
static/three.module.min.js ADDED
The diff for this file is too large to render. See raw diff
 
unish/__pycache__/pipeline.cpython-310.pyc CHANGED
Binary files a/unish/__pycache__/pipeline.cpython-310.pyc and b/unish/__pycache__/pipeline.cpython-310.pyc differ
 
unish/heads/__pycache__/align_net.cpython-310.pyc CHANGED
Binary files a/unish/heads/__pycache__/align_net.cpython-310.pyc and b/unish/heads/__pycache__/align_net.cpython-310.pyc differ
 
unish/heads/__pycache__/dpt_head.cpython-310.pyc CHANGED
Binary files a/unish/heads/__pycache__/dpt_head.cpython-310.pyc and b/unish/heads/__pycache__/dpt_head.cpython-310.pyc differ
 
unish/heads/__pycache__/head_act.cpython-310.pyc CHANGED
Binary files a/unish/heads/__pycache__/head_act.cpython-310.pyc and b/unish/heads/__pycache__/head_act.cpython-310.pyc differ
 
unish/heads/__pycache__/human_head_cliff.cpython-310.pyc CHANGED
Binary files a/unish/heads/__pycache__/human_head_cliff.cpython-310.pyc and b/unish/heads/__pycache__/human_head_cliff.cpython-310.pyc differ
 
unish/heads/__pycache__/pose_transformer.cpython-310.pyc CHANGED
Binary files a/unish/heads/__pycache__/pose_transformer.cpython-310.pyc and b/unish/heads/__pycache__/pose_transformer.cpython-310.pyc differ
 
unish/heads/__pycache__/t_cond_mlp.cpython-310.pyc CHANGED
Binary files a/unish/heads/__pycache__/t_cond_mlp.cpython-310.pyc and b/unish/heads/__pycache__/t_cond_mlp.cpython-310.pyc differ
 
unish/heads/__pycache__/utils.cpython-310.pyc CHANGED
Binary files a/unish/heads/__pycache__/utils.cpython-310.pyc and b/unish/heads/__pycache__/utils.cpython-310.pyc differ
 
unish/heads/__pycache__/vit.cpython-310.pyc CHANGED
Binary files a/unish/heads/__pycache__/vit.cpython-310.pyc and b/unish/heads/__pycache__/vit.cpython-310.pyc differ
 
unish/heads/align_net.py CHANGED
@@ -80,7 +80,7 @@ class TimeStepRoPE1D(nn.Module):
80
 
81
 
82
  class TransformerDecoderLayer(nn.Module):
83
- """单层Transformer Decoder with RoPE support"""
84
 
85
  def __init__(self, hidden_dim=512, num_heads=8, ff_dim=1024, dropout=0.1, use_rope=True):
86
  super().__init__()
@@ -287,7 +287,7 @@ class CrossViewTransformerDecoderLayer(nn.Module):
287
  # RoPE for timestep embedding
288
  self.timestep_rope = TimeStepRoPE1D(freq=100.0)
289
  else:
290
- # Self Attention
291
  self.self_attention = nn.MultiheadAttention(
292
  embed_dim=hidden_dim,
293
  num_heads=num_heads,
@@ -295,7 +295,7 @@ class CrossViewTransformerDecoderLayer(nn.Module):
295
  batch_first=True
296
  )
297
 
298
- # Cross Attention
299
  self.cross_attention = nn.MultiheadAttention(
300
  embed_dim=hidden_dim,
301
  num_heads=num_heads,
 
80
 
81
 
82
  class TransformerDecoderLayer(nn.Module):
83
+ """Single layer Transformer Decoder with RoPE support"""
84
 
85
  def __init__(self, hidden_dim=512, num_heads=8, ff_dim=1024, dropout=0.1, use_rope=True):
86
  super().__init__()
 
287
  # RoPE for timestep embedding
288
  self.timestep_rope = TimeStepRoPE1D(freq=100.0)
289
  else:
290
+ # Self Attention layer
291
  self.self_attention = nn.MultiheadAttention(
292
  embed_dim=hidden_dim,
293
  num_heads=num_heads,
 
295
  batch_first=True
296
  )
297
 
298
+ # Cross Attention layer
299
  self.cross_attention = nn.MultiheadAttention(
300
  embed_dim=hidden_dim,
301
  num_heads=num_heads,
unish/pi3/models/__pycache__/pi3.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/__pycache__/pi3.cpython-310.pyc and b/unish/pi3/models/__pycache__/pi3.cpython-310.pyc differ
 
unish/pi3/models/dinov2/__pycache__/__init__.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/__pycache__/__init__.cpython-310.pyc and b/unish/pi3/models/dinov2/__pycache__/__init__.cpython-310.pyc differ
 
unish/pi3/models/dinov2/hub/__pycache__/__init__.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/hub/__pycache__/__init__.cpython-310.pyc and b/unish/pi3/models/dinov2/hub/__pycache__/__init__.cpython-310.pyc differ
 
unish/pi3/models/dinov2/hub/__pycache__/backbones.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/hub/__pycache__/backbones.cpython-310.pyc and b/unish/pi3/models/dinov2/hub/__pycache__/backbones.cpython-310.pyc differ
 
unish/pi3/models/dinov2/hub/__pycache__/utils.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/hub/__pycache__/utils.cpython-310.pyc and b/unish/pi3/models/dinov2/hub/__pycache__/utils.cpython-310.pyc differ
 
unish/pi3/models/dinov2/layers/__pycache__/__init__.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/layers/__pycache__/__init__.cpython-310.pyc and b/unish/pi3/models/dinov2/layers/__pycache__/__init__.cpython-310.pyc differ
 
unish/pi3/models/dinov2/layers/__pycache__/attention.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/layers/__pycache__/attention.cpython-310.pyc and b/unish/pi3/models/dinov2/layers/__pycache__/attention.cpython-310.pyc differ
 
unish/pi3/models/dinov2/layers/__pycache__/block.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/layers/__pycache__/block.cpython-310.pyc and b/unish/pi3/models/dinov2/layers/__pycache__/block.cpython-310.pyc differ
 
unish/pi3/models/dinov2/layers/__pycache__/dino_head.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/layers/__pycache__/dino_head.cpython-310.pyc and b/unish/pi3/models/dinov2/layers/__pycache__/dino_head.cpython-310.pyc differ
 
unish/pi3/models/dinov2/layers/__pycache__/drop_path.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/layers/__pycache__/drop_path.cpython-310.pyc and b/unish/pi3/models/dinov2/layers/__pycache__/drop_path.cpython-310.pyc differ
 
unish/pi3/models/dinov2/layers/__pycache__/layer_scale.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/layers/__pycache__/layer_scale.cpython-310.pyc and b/unish/pi3/models/dinov2/layers/__pycache__/layer_scale.cpython-310.pyc differ
 
unish/pi3/models/dinov2/layers/__pycache__/mlp.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/layers/__pycache__/mlp.cpython-310.pyc and b/unish/pi3/models/dinov2/layers/__pycache__/mlp.cpython-310.pyc differ
 
unish/pi3/models/dinov2/layers/__pycache__/patch_embed.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/layers/__pycache__/patch_embed.cpython-310.pyc and b/unish/pi3/models/dinov2/layers/__pycache__/patch_embed.cpython-310.pyc differ
 
unish/pi3/models/dinov2/layers/__pycache__/swiglu_ffn.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/layers/__pycache__/swiglu_ffn.cpython-310.pyc and b/unish/pi3/models/dinov2/layers/__pycache__/swiglu_ffn.cpython-310.pyc differ
 
unish/pi3/models/dinov2/models/__pycache__/__init__.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/models/__pycache__/__init__.cpython-310.pyc and b/unish/pi3/models/dinov2/models/__pycache__/__init__.cpython-310.pyc differ
 
unish/pi3/models/dinov2/models/__pycache__/vision_transformer.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/dinov2/models/__pycache__/vision_transformer.cpython-310.pyc and b/unish/pi3/models/dinov2/models/__pycache__/vision_transformer.cpython-310.pyc differ
 
unish/pi3/models/layers/__pycache__/attention.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/layers/__pycache__/attention.cpython-310.pyc and b/unish/pi3/models/layers/__pycache__/attention.cpython-310.pyc differ
 
unish/pi3/models/layers/__pycache__/block.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/layers/__pycache__/block.cpython-310.pyc and b/unish/pi3/models/layers/__pycache__/block.cpython-310.pyc differ
 
unish/pi3/models/layers/__pycache__/camera_head.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/layers/__pycache__/camera_head.cpython-310.pyc and b/unish/pi3/models/layers/__pycache__/camera_head.cpython-310.pyc differ
 
unish/pi3/models/layers/__pycache__/pos_embed.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/layers/__pycache__/pos_embed.cpython-310.pyc and b/unish/pi3/models/layers/__pycache__/pos_embed.cpython-310.pyc differ
 
unish/pi3/models/layers/__pycache__/transformer_head.cpython-310.pyc CHANGED
Binary files a/unish/pi3/models/layers/__pycache__/transformer_head.cpython-310.pyc and b/unish/pi3/models/layers/__pycache__/transformer_head.cpython-310.pyc differ
 
unish/pi3/models/layers/attention.py CHANGED
@@ -239,8 +239,8 @@ class FlashCrossAttentionRope(CrossAttentionRope):
239
  def forward(self, query: Tensor, key: Tensor, value: Tensor, attn_bias=None, qpos=None, kpos=None) -> Tensor:
240
  B, N, C = query.shape
241
  _, M, _ = key.shape
242
-
243
- # 1. 投射 query, key, value 并调整维度为 (B, num_heads, Seq_Len, head_dim)
244
  q = self.q_proj(query).reshape(B, N, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3)
245
  k = self.k_proj(key).reshape(B, M, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3)
246
  v = self.v_proj(value).reshape(B, M, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3)
 
239
  def forward(self, query: Tensor, key: Tensor, value: Tensor, attn_bias=None, qpos=None, kpos=None) -> Tensor:
240
  B, N, C = query.shape
241
  _, M, _ = key.shape
242
+
243
+ # 1. Project query, key, value and resize to (B, num_heads, Seq_Len, head_dim)
244
  q = self.q_proj(query).reshape(B, N, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3)
245
  k = self.k_proj(key).reshape(B, M, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3)
246
  v = self.v_proj(value).reshape(B, M, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3)
unish/pi3/utils/__pycache__/geometry.cpython-310.pyc CHANGED
Binary files a/unish/pi3/utils/__pycache__/geometry.cpython-310.pyc and b/unish/pi3/utils/__pycache__/geometry.cpython-310.pyc differ
 
unish/utils/__pycache__/constants.cpython-310.pyc CHANGED
Binary files a/unish/utils/__pycache__/constants.cpython-310.pyc and b/unish/utils/__pycache__/constants.cpython-310.pyc differ
 
unish/utils/__pycache__/data_utils.cpython-310.pyc CHANGED
Binary files a/unish/utils/__pycache__/data_utils.cpython-310.pyc and b/unish/utils/__pycache__/data_utils.cpython-310.pyc differ