xinjie.wang commited on
Commit
95b0a82
·
1 Parent(s): 4890461

Add application file

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -13,7 +13,7 @@ import gradio as gr
13
  with gr.Blocks() as demo:
14
  gr.Markdown("## 3D Model Viewer")
15
  model = gr.Model3D(
16
- label="3D Model Viewer",
17
  # value="https://modelviewer.dev/shared-assets/models/Astronaut.glb", # 示例模型
18
  clear_color=(0.9, 0.9, 0.9, 1) # 先设置亮色背景提升基础亮度
19
  )
@@ -23,30 +23,35 @@ with gr.Blocks() as demo:
23
  clear_color=(0.1, 0.1, 0.1, 1) # 先设置亮色背景提升基础亮度
24
  )
25
 
26
- # demo.load(
27
- # js="""
28
- # () => {
29
- # // 等待组件加载完成(避免选择器找不到元素)
30
- # const observer = new MutationObserver(() => {
31
- # const viewer = document.querySelector('[data-testid="model3d-component"] .model-viewer');
32
- # if (viewer) {
33
- # const scene = viewer.modelView.scene;
34
- # observer.disconnect(); // 找到后停止观察
35
 
36
- # // 添加环境光(白色,强度 1.5)
37
- # const ambientLight = new THREE.AmbientLight(0xffffff, 1.5);
38
- # scene.add(ambientLight);
39
-
40
- # // 添加方向光(从右上方照射,模拟自然光)
41
- # const directionalLight = new THREE.DirectionalLight(0xffffff, 1.8);
42
- # directionalLight.position.set(8, 10, 5); // 调整光源位置
43
- # scene.add(directionalLight);
44
- # }
45
- # });
46
- # observer.observe(document.body, { childList: true, subtree: true });
47
- # }
48
- # """
49
- # )
 
 
 
 
 
 
 
 
50
 
51
 
52
  if __name__ == "__main__":
 
13
  with gr.Blocks() as demo:
14
  gr.Markdown("## 3D Model Viewer")
15
  model = gr.Model3D(
16
+ label="3D Model Viewer 1",
17
  # value="https://modelviewer.dev/shared-assets/models/Astronaut.glb", # 示例模型
18
  clear_color=(0.9, 0.9, 0.9, 1) # 先设置亮色背景提升基础亮度
19
  )
 
23
  clear_color=(0.1, 0.1, 0.1, 1) # 先设置亮色背景提升基础亮度
24
  )
25
 
26
+ demo.load(
27
+ js="""
28
+ () => {
29
+ // 定义光照函数(仅作用于单个 viewer)
30
+ function addLightingToViewer(viewer) {
31
+ const scene = viewer.modelView.scene;
 
 
 
32
 
33
+ // 添加环境光(白色,强度 3)
34
+ const ambientLight = new THREE.AmbientLight(0xffffff, 3);
35
+ scene.add(ambientLight);
36
+
37
+ // 添加方向光(从右上方照射)
38
+ const directionalLight = new THREE.DirectionalLight(0xffffff, 1.8);
39
+ directionalLight.position.set(8, 10, 5);
40
+ scene.add(directionalLight);
41
+ }
42
+
43
+ // 等待第一个组件加载完成(通过 label 定位)
44
+ const observer = new MutationObserver(() => {
45
+ const targetViewer = document.querySelector('[aria-label="3D Model Viewer 1"] .model-viewer');
46
+ if (targetViewer) {
47
+ observer.disconnect(); // 找到后停止监听
48
+ addLightingToViewer(targetViewer); // 仅处理第一个组件
49
+ }
50
+ });
51
+ observer.observe(document.body, { childList: true, subtree: true });
52
+ }
53
+ """
54
+ )
55
 
56
 
57
  if __name__ == "__main__":