xinjie.wang commited on
Commit
3157f9a
·
1 Parent(s): 2f63ecb

Add application file

Browse files
Files changed (1) hide show
  1. app.py +46 -27
app.py CHANGED
@@ -7,54 +7,73 @@ def greet(n):
7
  print(zero.device) # <-- 'cuda:0' 🤗
8
  return f"Hello {zero + n} Tensor"
9
 
 
 
10
 
11
  lighting_js = """
12
  <script>
13
- function addLightingToModel3D() {
14
- const model3dCanvas = document.querySelector('gradio-app model-3d canvas');
15
- if (!model3dCanvas) {
16
- console.error('Model3D canvas not found');
17
- return;
18
- }
19
- const model3dElement = model3dCanvas.closest('model-3d');
20
- if (model3dElement && model3dElement.__vue__) {
21
- const vueInstance = model3dElement.__vue__;
22
- const scene = vueInstance.scene;
23
- if (!scene) {
24
- console.error('Three.js scene not accessible');
25
  return;
26
  }
27
- scene.children = scene.children.filter(child => !(child.isLight));
28
- const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
 
 
 
 
 
 
 
 
29
  scene.add(ambientLight);
30
- const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
 
31
  directionalLight.position.set(1, 1, 1).normalize();
32
  scene.add(directionalLight);
33
- console.log('Lighting added to Model3D');
34
- } else {
35
- console.error('Vue instance or scene not found');
36
  }
37
  }
38
  document.addEventListener('DOMContentLoaded', () => {
39
- setTimeout(addLightingToModel3D, 1000);
40
  });
41
  </script>
42
  """
43
 
44
  with gr.Blocks() as demo:
45
- gr.Markdown("## 3D Model Viewer")
46
- gr.HTML(lighting_js) # 注入光照脚本
 
 
 
47
  model = gr.Model3D(
48
  label="3D Model Viewer 1",
49
- # value="https://modelviewer.dev/shared-assets/models/Astronaut.glb", # 示例模型
50
- clear_color=(0.9, 0.9, 0.9, 1) # 先设置亮色背景提升基础亮度
 
 
 
 
51
  )
 
 
 
52
  model2 = gr.Model3D(
53
  label="3D Model Viewer2",
54
- # value="https://modelviewer.dev/shared-assets/models/Astronaut.glb", # 示例模型
55
- clear_color=(0.1, 0.1, 0.1, 1) # 先设置亮色背景提升基础亮度
56
  )
57
-
58
-
 
 
 
 
 
59
  if __name__ == "__main__":
60
  demo.launch()
 
7
  print(zero.device) # <-- 'cuda:0' 🤗
8
  return f"Hello {zero + n} Tensor"
9
 
10
+ import gradio as gr
11
+ import spaces
12
 
13
  lighting_js = """
14
  <script>
15
+ function addLightingToModel2() {
16
+ try {
17
+ // 仅选择第二个 model-3d(model2)
18
+ const model2Canvas = document.querySelector('gradio-app model-3d:nth-child(2) canvas');
19
+ if (!model2Canvas) {
20
+ console.error('Model2 canvas not found');
 
 
 
 
 
 
21
  return;
22
  }
23
+ const model3dElement = model2Canvas.closest('model-3d');
24
+ if (!model3dElement || !model3dElement.__vue__?.scene) {
25
+ console.error('Vue instance or scene not found for model2');
26
+ return;
27
+ }
28
+ const scene = model3dElement.__vue__.scene;
29
+ // 移除现有光源(可选,保持默认光照可跳过)
30
+ scene.children = scene.children.filter(child => !child.isLight);
31
+ // 添加环境光
32
+ const ambientLight = new THREE.AmbientLight(0xffffff, 1.2); // 高强度环境光
33
  scene.add(ambientLight);
34
+ // 添加平行光
35
+ const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); // 高强度平行光
36
  directionalLight.position.set(1, 1, 1).normalize();
37
  scene.add(directionalLight);
38
+ console.log('Enhanced lighting added to model2');
39
+ } catch (error) {
40
+ console.error('Error adding lighting to model2:', error.message);
41
  }
42
  }
43
  document.addEventListener('DOMContentLoaded', () => {
44
+ setTimeout(addLightingToModel2, 2000); // 延迟确保加载
45
  });
46
  </script>
47
  """
48
 
49
  with gr.Blocks() as demo:
50
+ gr.Markdown("## 3D Model Viewer\nUpload .glb or .gltf files to preview models.")
51
+ gr.HTML(lighting_js)
52
+
53
+ # model1(保持默认亮度)
54
+ model_input = gr.File(label="Upload 3D Model for Viewer 1 (.glb, .gltf)")
55
  model = gr.Model3D(
56
  label="3D Model Viewer 1",
57
+ clear_color=(0.9, 0.9, 0.9, 1) # 保持原背景
58
+ )
59
+ model_input.change(
60
+ lambda x: gr.Model3D(value=x.name),
61
+ inputs=model_input,
62
+ outputs=model
63
  )
64
+
65
+ # model2(提高亮度)
66
+ model_input2 = gr.File(label="Upload 3D Model for Viewer 2 (.glb, .gltf)")
67
  model2 = gr.Model3D(
68
  label="3D Model Viewer2",
69
+ clear_color=(0.9, 0.9, 0.9, 1), # 改为浅色背景,辅助提亮
 
70
  )
71
+ model_input2.change(
72
+ lambda x: gr.Model3D(value=x.name),
73
+ inputs=model_input2,
74
+ outputs=model2
75
+ )
76
+
77
+
78
  if __name__ == "__main__":
79
  demo.launch()