awacke1 commited on
Commit
5f40a57
·
verified ·
1 Parent(s): 86942ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -40
app.py CHANGED
@@ -17,39 +17,42 @@ def scan_assets():
17
  if f.lower().endswith(img_exts)
18
  and any(tag in f.lower() for tag in ("bump", "normal"))
19
  ]
20
- models = [
21
  f for f in files
22
  if f.lower().endswith((".glb", ".gltf", ".obj"))
23
  ]
24
- return textures, bump_maps, models
25
 
26
  def main():
27
- st.title("🔳 A-Frame Tilemap with Random Models")
28
  grid_size = st.sidebar.slider("Grid Size", min_value=1, max_value=20, value=10)
29
- textures, bump_maps, models = scan_assets()
30
 
31
- if not textures or not models:
32
- st.warning("⚠️ Please add at least one texture image and one .glb/.obj file to this folder.")
33
  return
34
 
35
- # --- Prepare <a-assets> tags ---
36
  asset_tags = []
37
  for i, tex in enumerate(textures):
38
  asset_tags.append(f'<img id="tex{i}" src="{tex}">')
39
  if bump_maps:
40
- # just pick the first bump map
41
  asset_tags.append(f'<img id="grassBump" src="{bump_maps[0]}">')
42
- for i, mdl in enumerate(models):
43
- asset_tags.append(f'<a-asset-item id="model{i}" src="{mdl}"></a-asset-item>')
 
 
 
 
44
 
45
  assets_html = "\n ".join(asset_tags)
46
-
47
- # JavaScript arrays of IDs
48
  texture_list = ", ".join(f'"#tex{i}"' for i in range(len(textures)))
49
- model_list = ", ".join(f'"model{i}"' for i in range(len(models)))
50
- has_bump_js = "true" if bump_maps else "false"
 
 
51
 
52
- # Material setup for ground
53
  if bump_maps:
54
  ground_mat = (
55
  "ground.setAttribute('material',"
@@ -58,8 +61,7 @@ def main():
58
  else:
59
  ground_mat = "ground.setAttribute('material','color: #228B22');"
60
 
61
- # --- The A-Frame HTML template ---
62
- html_template = """
63
  <!DOCTYPE html>
64
  <html>
65
  <head>
@@ -92,16 +94,16 @@ def main():
92
  position="0 {grid_size} {grid_size}">
93
  </a-entity>
94
 
95
- <!-- Container for dynamic tiles & models -->
96
  <a-entity id="tilemap"></a-entity>
97
  </a-scene>
98
 
99
  <script>
100
  document.addEventListener('DOMContentLoaded', function() {{
101
- var scene = document.querySelector('a-scene');
102
- var tilemap = document.querySelector('#tilemap');
103
  var textures = [{texture_list}];
104
- var models = [{model_list}];
105
  var grid = {grid_size};
106
 
107
  for (var i = 0; i < grid; i++) {{
@@ -109,7 +111,7 @@ def main():
109
  var x = i - grid/2;
110
  var z = j - grid/2;
111
 
112
- // --- Base tile ---
113
  var tile = document.createElement('a-box');
114
  tile.setAttribute('width', 1);
115
  tile.setAttribute('height', 0.1);
@@ -119,19 +121,21 @@ def main():
119
  tile.setAttribute('position', x + ' 0 ' + z);
120
  tilemap.appendChild(tile);
121
 
122
- // --- Random model ---
123
- var mi = Math.floor(Math.random() * models.length);
124
- var mid = models[mi];
125
- var modelEl = document.createElement('a-entity');
126
- // load both .glb/.gltf and .obj via gltf-model (requires extras loader)
127
- modelEl.setAttribute('gltf-model', '#' + mid);
128
- modelEl.setAttribute('scale', '0.5 0.5 0.5');
129
- modelEl.setAttribute('position', x + ' 0.5 ' + z);
130
- tilemap.appendChild(modelEl);
 
 
131
  }}
132
  }}
133
 
134
- // --- Ground plane ---
135
  var ground = document.createElement('a-plane');
136
  ground.setAttribute('width', grid * 2);
137
  ground.setAttribute('height', grid * 2);
@@ -145,14 +149,6 @@ def main():
145
  </html>
146
  """
147
 
148
- html = html_template.format(
149
- assets_html=assets_html,
150
- texture_list=texture_list,
151
- model_list=model_list,
152
- grid_size=grid_size,
153
- ground_mat=ground_mat
154
- )
155
-
156
  st.components.v1.html(html, height=600, scrolling=False)
157
 
158
  if __name__ == "__main__":
 
17
  if f.lower().endswith(img_exts)
18
  and any(tag in f.lower() for tag in ("bump", "normal"))
19
  ]
20
+ model_files = [
21
  f for f in files
22
  if f.lower().endswith((".glb", ".gltf", ".obj"))
23
  ]
24
+ return textures, bump_maps, model_files
25
 
26
  def main():
27
+ st.title("🔳 A-Frame Tilemap with Mixed 3D Models")
28
  grid_size = st.sidebar.slider("Grid Size", min_value=1, max_value=20, value=10)
29
+ textures, bump_maps, model_files = scan_assets()
30
 
31
+ if not textures or not model_files:
32
+ st.warning("⚠️ Add at least one texture **and** one `.glb`/`.obj` file to this folder.")
33
  return
34
 
35
+ # --- Build <a-assets> entries ---
36
  asset_tags = []
37
  for i, tex in enumerate(textures):
38
  asset_tags.append(f'<img id="tex{i}" src="{tex}">')
39
  if bump_maps:
 
40
  asset_tags.append(f'<img id="grassBump" src="{bump_maps[0]}">')
41
+ models_info = []
42
+ for i, fname in enumerate(model_files):
43
+ ext = os.path.splitext(fname)[1].lower()
44
+ # strip the dot for ID
45
+ asset_tags.append(f'<a-asset-item id="model{i}" src="{fname}"></a-asset-item>')
46
+ models_info.append({"id": f"#model{i}", "ext": ext})
47
 
48
  assets_html = "\n ".join(asset_tags)
 
 
49
  texture_list = ", ".join(f'"#tex{i}"' for i in range(len(textures)))
50
+ # JS array of model objects
51
+ model_js_array = ", ".join(
52
+ f'{{id:"{m["id"]}", ext:"{m["ext"]}"}}' for m in models_info
53
+ )
54
 
55
+ # ground material JS
56
  if bump_maps:
57
  ground_mat = (
58
  "ground.setAttribute('material',"
 
61
  else:
62
  ground_mat = "ground.setAttribute('material','color: #228B22');"
63
 
64
+ html = f"""
 
65
  <!DOCTYPE html>
66
  <html>
67
  <head>
 
94
  position="0 {grid_size} {grid_size}">
95
  </a-entity>
96
 
97
+ <!-- Container for tiles & models -->
98
  <a-entity id="tilemap"></a-entity>
99
  </a-scene>
100
 
101
  <script>
102
  document.addEventListener('DOMContentLoaded', function() {{
103
+ var scene = document.querySelector('a-scene');
104
+ var tilemap = document.querySelector('#tilemap');
105
  var textures = [{texture_list}];
106
+ var models = [{model_js_array}];
107
  var grid = {grid_size};
108
 
109
  for (var i = 0; i < grid; i++) {{
 
111
  var x = i - grid/2;
112
  var z = j - grid/2;
113
 
114
+ // Base tile
115
  var tile = document.createElement('a-box');
116
  tile.setAttribute('width', 1);
117
  tile.setAttribute('height', 0.1);
 
121
  tile.setAttribute('position', x + ' 0 ' + z);
122
  tilemap.appendChild(tile);
123
 
124
+ // Random model
125
+ var m = models[Math.floor(Math.random() * models.length)];
126
+ var ent = document.createElement('a-entity');
127
+ if (m.ext === '.obj') {{
128
+ ent.setAttribute('obj-model', 'obj: ' + m.id);
129
+ }} else {{
130
+ ent.setAttribute('gltf-model', m.id);
131
+ }}
132
+ ent.setAttribute('scale', '0.5 0.5 0.5');
133
+ ent.setAttribute('position', x + ' 0.5 ' + z);
134
+ tilemap.appendChild(ent);
135
  }}
136
  }}
137
 
138
+ // Ground plane
139
  var ground = document.createElement('a-plane');
140
  ground.setAttribute('width', grid * 2);
141
  ground.setAttribute('height', grid * 2);
 
149
  </html>
150
  """
151
 
 
 
 
 
 
 
 
 
152
  st.components.v1.html(html, height=600, scrolling=False)
153
 
154
  if __name__ == "__main__":