Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
21 |
f for f in files
|
22 |
if f.lower().endswith((".glb", ".gltf", ".obj"))
|
23 |
]
|
24 |
-
return textures, bump_maps,
|
25 |
|
26 |
def main():
|
27 |
-
st.title("🔳 A-Frame Tilemap with
|
28 |
grid_size = st.sidebar.slider("Grid Size", min_value=1, max_value=20, value=10)
|
29 |
-
textures, bump_maps,
|
30 |
|
31 |
-
if not textures or not
|
32 |
-
st.warning("⚠️
|
33 |
return
|
34 |
|
35 |
-
# ---
|
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 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
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 |
-
|
50 |
-
|
|
|
|
|
51 |
|
52 |
-
#
|
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 |
-
|
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
|
96 |
<a-entity id="tilemap"></a-entity>
|
97 |
</a-scene>
|
98 |
|
99 |
<script>
|
100 |
document.addEventListener('DOMContentLoaded', function() {{
|
101 |
-
var scene
|
102 |
-
var tilemap
|
103 |
var textures = [{texture_list}];
|
104 |
-
var models = [{
|
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 |
-
//
|
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 |
-
//
|
123 |
-
var
|
124 |
-
var
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
131 |
}}
|
132 |
}}
|
133 |
|
134 |
-
//
|
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__":
|