awacke1 commited on
Commit
390ce77
·
verified ·
1 Parent(s): d88550f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -26
app.py CHANGED
@@ -15,7 +15,7 @@ def scan_assets():
15
  and not any(tag in f.lower() for tag in ("bump", "normal"))
16
  ]
17
 
18
- # Bump/NORMAL map (just take the first one, if any)
19
  bump_maps = [
20
  f for f in files
21
  if f.lower().endswith(img_exts)
@@ -27,7 +27,11 @@ def scan_assets():
27
 
28
  # OBJ models + their MTL partners
29
  obj_models = [f for f in files if f.lower().endswith(".obj")]
30
- mtl_files = {os.path.splitext(f)[0]: f for f in files if f.lower().endswith(".mtl")}
 
 
 
 
31
 
32
  models = []
33
  idx = 0
@@ -35,13 +39,13 @@ def scan_assets():
35
  # Register glTF entries
36
  for gltf in gltf_models:
37
  models.append({
38
- "type": "gltf",
39
  "asset_id": f"model{idx}",
40
- "src": gltf
41
  })
42
  idx += 1
43
 
44
- # Register OBJ entries (with optional MTL)
45
  for obj in obj_models:
46
  base = os.path.splitext(obj)[0]
47
  mtl = mtl_files.get(base)
@@ -58,15 +62,15 @@ def scan_assets():
58
  return textures, bump_maps, models
59
 
60
  def main():
61
- st.title("🔳 A-Frame Tilemap with Random 3D Models")
62
  grid_size = st.sidebar.slider("Grid Size", 1, 20, 10)
63
 
64
  textures, bump_maps, models = scan_assets()
65
  if not textures or not models:
66
- st.warning("⚠️ Drop at least one image (jpg/png) **and** one glb/obj model (with optional mtl) into this folder.")
67
  return
68
 
69
- # Build <a-assets>
70
  asset_tags = []
71
  for i, tex in enumerate(textures):
72
  asset_tags.append(f'<img id="tex{i}" src="{tex}">')
@@ -76,42 +80,51 @@ def main():
76
 
77
  for m in models:
78
  if m["type"] == "gltf":
79
- asset_tags.append(f'<a-asset-item id="{m["asset_id"]}" src="{m["src"]}"></a-asset-item>')
 
 
80
  else:
81
- asset_tags.append(f'<a-asset-item id="{m["obj_id"]}" src="{m["obj"]}"></a-asset-item>')
 
 
82
  if m["mtl_id"]:
83
- asset_tags.append(f'<a-asset-item id="{m["mtl_id"]}" src="{m["mtl"]}"></a-asset-item>')
 
 
84
 
85
  assets_html = "\n ".join(asset_tags)
86
 
87
- # JS arrays
88
  tex_js = ", ".join(f'"#tex{i}"' for i in range(len(textures)))
89
- has_bump = "true" if bump_maps else "false"
90
- models_js = []
91
  for m in models:
92
  if m["type"] == "gltf":
93
- models_js.append(f'{{type:"gltf", id:"#%s"}}' % m["asset_id"])
94
  else:
95
- mtl_part = f', mtl:"#%s"' % m["mtl_id"] if m["mtl_id"] else ""
96
- models_js.append(f'{{type:"obj", obj:"#%s"{mtl}}}' % (m["obj_id"], mtl=mtl_part))
97
- models_js = ", ".join(models_js)
98
-
99
- # Ground material
 
 
 
 
 
 
100
  if bump_maps:
101
  ground_mat = "ground.setAttribute('material','color:#228B22; bumpMap:#bump0; bumpScale:0.2');"
102
  else:
103
  ground_mat = "ground.setAttribute('material','color:#228B22');"
104
 
105
- # Final HTML
106
  html = f"""
107
  <!DOCTYPE html>
108
  <html>
109
  <head>
110
  <meta charset="utf-8">
111
  <title>Tilemap Scene</title>
112
- <!-- Core A-Frame -->
113
  <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
114
- <!-- Loaders for OBJ & glTF -->
115
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/aframe-extras.loaders.min.js"></script>
116
  </head>
117
  <body>
@@ -122,13 +135,13 @@ def main():
122
 
123
  <!-- Lights -->
124
  <a-entity light="type: ambient; color: #BBB"></a-entity>
125
- <a-entity light="type: directional; color: #FFF; intensity: 0.6" position="1 1 0"></a-entity>
126
- <a-entity light="type: point; intensity: 0.6" position="0 5 0"></a-entity>
127
 
128
  <!-- Camera -->
129
  <a-entity camera look-controls position="0 {grid_size} {grid_size}"></a-entity>
130
 
131
- <!-- Container -->
132
  <a-entity id="tilemap"></a-entity>
133
  </a-scene>
134
 
 
15
  and not any(tag in f.lower() for tag in ("bump", "normal"))
16
  ]
17
 
18
+ # Bump/NORMAL map (take the first one, if any)
19
  bump_maps = [
20
  f for f in files
21
  if f.lower().endswith(img_exts)
 
27
 
28
  # OBJ models + their MTL partners
29
  obj_models = [f for f in files if f.lower().endswith(".obj")]
30
+ mtl_files = {
31
+ os.path.splitext(f)[0]: f
32
+ for f in files
33
+ if f.lower().endswith(".mtl")
34
+ }
35
 
36
  models = []
37
  idx = 0
 
39
  # Register glTF entries
40
  for gltf in gltf_models:
41
  models.append({
42
+ "type": "gltf",
43
  "asset_id": f"model{idx}",
44
+ "src": gltf
45
  })
46
  idx += 1
47
 
48
+ # Register OBJ entries
49
  for obj in obj_models:
50
  base = os.path.splitext(obj)[0]
51
  mtl = mtl_files.get(base)
 
62
  return textures, bump_maps, models
63
 
64
  def main():
65
+ st.title("🔳 A-Frame Tilemap with Mixed 3D Models")
66
  grid_size = st.sidebar.slider("Grid Size", 1, 20, 10)
67
 
68
  textures, bump_maps, models = scan_assets()
69
  if not textures or not models:
70
+ st.warning("⚠️ Drop at least one .jpg/.png and one .glb/.obj (with optional .mtl) in this folder.")
71
  return
72
 
73
+ # --- Build <a-assets> ---
74
  asset_tags = []
75
  for i, tex in enumerate(textures):
76
  asset_tags.append(f'<img id="tex{i}" src="{tex}">')
 
80
 
81
  for m in models:
82
  if m["type"] == "gltf":
83
+ asset_tags.append(
84
+ f'<a-asset-item id="{m["asset_id"]}" src="{m["src"]}"></a-asset-item>'
85
+ )
86
  else:
87
+ asset_tags.append(
88
+ f'<a-asset-item id="{m["obj_id"]}" src="{m["obj"]}"></a-asset-item>'
89
+ )
90
  if m["mtl_id"]:
91
+ asset_tags.append(
92
+ f'<a-asset-item id="{m["mtl_id"]}" src="{m["mtl"]}"></a-asset-item>'
93
+ )
94
 
95
  assets_html = "\n ".join(asset_tags)
96
 
97
+ # JS arrays for textures & models
98
  tex_js = ", ".join(f'"#tex{i}"' for i in range(len(textures)))
99
+ models_js_elems = []
 
100
  for m in models:
101
  if m["type"] == "gltf":
102
+ models_js_elems.append(f'{{type:"gltf", id:"#{m["asset_id"]}"}}')
103
  else:
104
+ if m["mtl_id"]:
105
+ models_js_elems.append(
106
+ f'{{type:"obj", obj:"#{m["obj_id"]}", mtl:"#{m["mtl_id"]}"}}'
107
+ )
108
+ else:
109
+ models_js_elems.append(
110
+ f'{{type:"obj", obj:"#{m["obj_id"]}"}}'
111
+ )
112
+ models_js = ", ".join(models_js_elems)
113
+
114
+ # Ground material (with optional bump)
115
  if bump_maps:
116
  ground_mat = "ground.setAttribute('material','color:#228B22; bumpMap:#bump0; bumpScale:0.2');"
117
  else:
118
  ground_mat = "ground.setAttribute('material','color:#228B22');"
119
 
120
+ # --- Final HTML ---
121
  html = f"""
122
  <!DOCTYPE html>
123
  <html>
124
  <head>
125
  <meta charset="utf-8">
126
  <title>Tilemap Scene</title>
 
127
  <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
 
128
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/aframe-extras.loaders.min.js"></script>
129
  </head>
130
  <body>
 
135
 
136
  <!-- Lights -->
137
  <a-entity light="type: ambient; color: #BBB"></a-entity>
138
+ <a-entity light="type: directional; color: #FFF; intensity:0.6" position="1 1 0"></a-entity>
139
+ <a-entity light="type: point; intensity:0.6" position="0 5 0"></a-entity>
140
 
141
  <!-- Camera -->
142
  <a-entity camera look-controls position="0 {grid_size} {grid_size}"></a-entity>
143
 
144
+ <!-- Tiles & Models -->
145
  <a-entity id="tilemap"></a-entity>
146
  </a-scene>
147