import streamlit as st import os import random @st.cache_data def scan_assets(): """Discover textures, bump maps, glTF models, and OBJ(+MTL) pairs.""" files = [f for f in os.listdir() if os.path.isfile(f)] img_exts = (".jpg", ".jpeg", ".png", ".gif") # Textures (exclude bump/normal) textures = [ f for f in files if f.lower().endswith(img_exts) and not any(tag in f.lower() for tag in ("bump", "normal")) ] # Bump/NORMAL map (take the first one, if any) bump_maps = [ f for f in files if f.lower().endswith(img_exts) and any(tag in f.lower() for tag in ("bump", "normal")) ] # glTF models gltf_models = [f for f in files if f.lower().endswith((".glb", ".gltf"))] # OBJ models + their MTL partners obj_models = [f for f in files if f.lower().endswith(".obj")] mtl_files = { os.path.splitext(f)[0]: f for f in files if f.lower().endswith(".mtl") } models = [] idx = 0 # Register glTF entries for gltf in gltf_models: models.append({ "type": "gltf", "asset_id": f"model{idx}", "src": gltf }) idx += 1 # Register OBJ entries for obj in obj_models: base = os.path.splitext(obj)[0] mtl = mtl_files.get(base) entry = { "type": "obj", "obj_id": f"model{idx}-obj", "obj": obj, "mtl_id": f"model{idx}-mtl" if mtl else None, "mtl": mtl } models.append(entry) idx += 1 return textures, bump_maps, models def main(): st.title("🔳 A-Frame Tilemap with Mixed 3D Models") grid_size = st.sidebar.slider("Grid Size", 1, 20, 10) textures, bump_maps, models = scan_assets() if not textures or not models: st.warning("⚠️ Drop at least one .jpg/.png and one .glb/.obj (with optional .mtl) in this folder.") return # --- Build --- asset_tags = [] for i, tex in enumerate(textures): asset_tags.append(f'') if bump_maps: asset_tags.append(f'') for m in models: if m["type"] == "gltf": asset_tags.append( f'' ) else: asset_tags.append( f'' ) if m["mtl_id"]: asset_tags.append( f'' ) assets_html = "\n ".join(asset_tags) # JS arrays for textures & models tex_js = ", ".join(f'"#tex{i}"' for i in range(len(textures))) models_js_elems = [] for m in models: if m["type"] == "gltf": models_js_elems.append(f'{{type:"gltf", id:"#{m["asset_id"]}"}}') else: if m["mtl_id"]: models_js_elems.append( f'{{type:"obj", obj:"#{m["obj_id"]}", mtl:"#{m["mtl_id"]}"}}' ) else: models_js_elems.append( f'{{type:"obj", obj:"#{m["obj_id"]}"}}' ) models_js = ", ".join(models_js_elems) # Ground material (with optional bump) if bump_maps: ground_mat = "ground.setAttribute('material','color:#228B22; bumpMap:#bump0; bumpScale:0.2');" else: ground_mat = "ground.setAttribute('material','color:#228B22');" # --- Final HTML --- html = f""" Tilemap Scene {assets_html} """ st.components.v1.html(html, height=600, scrolling=False) if __name__ == "__main__": main()