awacke1 commited on
Commit
680fd0b
·
verified ·
1 Parent(s): 8e91e71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -35,19 +35,19 @@ def load_aframe_and_extras():
35
  """
36
 
37
  # 🏗️ Create A-Frame entities for each file type
38
- def create_aframe_entity(file_path, file_type, position):
39
  rotation = f"0 {random.uniform(0, 360)} 0"
40
  bounce_speed = f"{random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)}"
41
  bounce_dist = f"0.1 0.1 0.1"
42
 
43
  if file_type == 'obj':
44
- return f'<a-entity position="{position}" rotation="{rotation}" scale="0.5 0.5 0.5" obj-model="obj: #{Path(file_path).stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>'
45
  elif file_type == 'glb':
46
- return f'<a-entity position="{position}" rotation="{rotation}" scale="0.5 0.5 0.5" gltf-model="#{Path(file_path).stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>'
47
  elif file_type in ['webp', 'png']:
48
- return f'<a-image position="{position}" rotation="-90 0 0" src="#{Path(file_path).stem}" width="0.5" height="0.5" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-image>'
49
  elif file_type == 'mp4':
50
- return f'<a-video position="{position}" rotation="-90 0 0" src="#{Path(file_path).stem}" width="0.5" height="0.5" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-video>'
51
  return ''
52
 
53
  # 🔐 Encode file contents to base64
@@ -58,33 +58,39 @@ def encode_file(file_path):
58
 
59
  # 🗺️ Generate tilemap grid
60
  @st.cache_data
61
- def generate_tilemap(files, directory, grid_width, grid_height):
62
  assets = "<a-assets>"
63
  entities = ""
64
  tile_size = 1
65
  start_x = -(grid_width * tile_size) / 2
66
  start_z = -(grid_height * tile_size) / 2
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  for i in range(grid_width):
69
  for j in range(grid_height):
70
  x = start_x + (i * tile_size)
71
  z = start_z + (j * tile_size)
72
  position = f"{x} 0 {z}"
73
 
74
- if files:
75
- file = random.choice(files)
76
- file_path = os.path.join(directory, file)
77
  file_type = file.split('.')[-1]
78
-
79
- if file not in assets:
80
- encoded_file = encode_file(file_path)
81
- if file_type in ['obj', 'glb']:
82
- assets += f'<a-asset-item id="{Path(file).stem}" src="data:application/octet-stream;base64,{encoded_file}"></a-asset-item>'
83
- elif file_type in ['webp', 'png', 'mp4']:
84
- mime_type = f"image/{file_type}" if file_type in ['webp', 'png'] else "video/mp4"
85
- assets += f'<{file_type} id="{Path(file).stem}" src="data:{mime_type};base64,{encoded_file}"></{file_type}>'
86
-
87
- entities += create_aframe_entity(file_path, file_type, position)
88
 
89
  assets += "</a-assets>"
90
  return assets, entities
@@ -113,8 +119,8 @@ def main():
113
  st.button("➡️", on_click=lambda: st.session_state.update({'camera_move': 'right'}))
114
 
115
  st.markdown("### 🗺️ Grid Size")
116
- grid_width = st.slider("Grid Width", 1, 10, 10)
117
- grid_height = st.slider("Grid Height", 1, 7, 7)
118
 
119
  st.markdown("### ℹ️ Instructions")
120
  st.write("- Click and drag to move objects")
@@ -155,7 +161,7 @@ def main():
155
  <a-entity moving-light="color: #00CED1; speed: 0.05 0.06 0.07; bounds: 4 3 4" position="0 3 0"></a-entity>
156
  """
157
 
158
- assets, entities = generate_tilemap(files, directory, grid_width, grid_height)
159
  aframe_scene += assets + entities + "</a-scene>"
160
 
161
  camera_move = st.session_state.get('camera_move', None)
 
35
  """
36
 
37
  # 🏗️ Create A-Frame entities for each file type
38
+ def create_aframe_entity(file_stem, file_type, position):
39
  rotation = f"0 {random.uniform(0, 360)} 0"
40
  bounce_speed = f"{random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)}"
41
  bounce_dist = f"0.1 0.1 0.1"
42
 
43
  if file_type == 'obj':
44
+ return f'<a-entity position="{position}" rotation="{rotation}" scale="0.5 0.5 0.5" obj-model="obj: #{file_stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>'
45
  elif file_type == 'glb':
46
+ return f'<a-entity position="{position}" rotation="{rotation}" scale="0.5 0.5 0.5" gltf-model="#{file_stem}" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-entity>'
47
  elif file_type in ['webp', 'png']:
48
+ return f'<a-image position="{position}" rotation="-90 0 0" src="#{file_stem}" width="0.5" height="0.5" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-image>'
49
  elif file_type == 'mp4':
50
+ return f'<a-video position="{position}" rotation="-90 0 0" src="#{file_stem}" width="0.5" height="0.5" class="raycastable" draggable bouncing="speed: {bounce_speed}; dist: {bounce_dist}"></a-video>'
51
  return ''
52
 
53
  # 🔐 Encode file contents to base64
 
58
 
59
  # 🗺️ Generate tilemap grid
60
  @st.cache_data
61
+ def generate_tilemap(files, directory, grid_width, grid_height, max_unique_models=5):
62
  assets = "<a-assets>"
63
  entities = ""
64
  tile_size = 1
65
  start_x = -(grid_width * tile_size) / 2
66
  start_z = -(grid_height * tile_size) / 2
67
 
68
+ # Limit the number of unique models
69
+ unique_files = random.sample(files, min(len(files), max_unique_models))
70
+ encoded_files = {}
71
+
72
+ for file in unique_files:
73
+ file_path = os.path.join(directory, file)
74
+ file_type = file.split('.')[-1]
75
+ encoded_file = encode_file(file_path)
76
+ encoded_files[file] = encoded_file
77
+
78
+ if file_type in ['obj', 'glb']:
79
+ assets += f'<a-asset-item id="{Path(file).stem}" src="data:application/octet-stream;base64,{encoded_file}"></a-asset-item>'
80
+ elif file_type in ['webp', 'png', 'mp4']:
81
+ mime_type = f"image/{file_type}" if file_type in ['webp', 'png'] else "video/mp4"
82
+ assets += f'<{file_type} id="{Path(file).stem}" src="data:{mime_type};base64,{encoded_file}"></{file_type}>'
83
+
84
  for i in range(grid_width):
85
  for j in range(grid_height):
86
  x = start_x + (i * tile_size)
87
  z = start_z + (j * tile_size)
88
  position = f"{x} 0 {z}"
89
 
90
+ if unique_files:
91
+ file = random.choice(unique_files)
 
92
  file_type = file.split('.')[-1]
93
+ entities += create_aframe_entity(Path(file).stem, file_type, position)
 
 
 
 
 
 
 
 
 
94
 
95
  assets += "</a-assets>"
96
  return assets, entities
 
119
  st.button("➡️", on_click=lambda: st.session_state.update({'camera_move': 'right'}))
120
 
121
  st.markdown("### 🗺️ Grid Size")
122
+ grid_width = st.slider("Grid Width", 1, 8, 8)
123
+ grid_height = st.slider("Grid Height", 1, 5, 5)
124
 
125
  st.markdown("### ℹ️ Instructions")
126
  st.write("- Click and drag to move objects")
 
161
  <a-entity moving-light="color: #00CED1; speed: 0.05 0.06 0.07; bounds: 4 3 4" position="0 3 0"></a-entity>
162
  """
163
 
164
+ assets, entities = generate_tilemap(files, directory, grid_width, grid_height, max_unique_models=5)
165
  aframe_scene += assets + entities + "</a-scene>"
166
 
167
  camera_move = st.session_state.get('camera_move', None)