Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,37 @@ def load_aframe_and_extras():
|
|
| 10 |
<script>
|
| 11 |
let score = 0;
|
| 12 |
AFRAME.registerComponent('draggable', {
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
});
|
| 15 |
AFRAME.registerComponent('bouncing', {
|
| 16 |
schema: {
|
|
@@ -47,7 +77,31 @@ def load_aframe_and_extras():
|
|
| 47 |
}
|
| 48 |
});
|
| 49 |
AFRAME.registerComponent('moving-light', {
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
});
|
| 52 |
function moveCamera(direction) {
|
| 53 |
var camera = document.querySelector('[camera]');
|
|
@@ -82,7 +136,7 @@ def load_aframe_and_extras():
|
|
| 82 |
if (hitObject.components.bouncing) {
|
| 83 |
hitObject.components.bouncing.boost();
|
| 84 |
score += 10;
|
| 85 |
-
document.getElementById('score').
|
| 86 |
}
|
| 87 |
}
|
| 88 |
}
|
|
@@ -101,7 +155,55 @@ def load_aframe_and_extras():
|
|
| 101 |
</script>
|
| 102 |
"""
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
def main():
|
| 107 |
st.set_page_config(layout="wide")
|
|
@@ -132,7 +234,20 @@ def main():
|
|
| 132 |
st.markdown("### ๐ Directory")
|
| 133 |
directory = st.text_input("Enter path:", ".", key="directory_input")
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
aframe_scene = f"""
|
| 138 |
<a-scene embedded style="height: 600px; width: 100%;">
|
|
@@ -148,6 +263,7 @@ def main():
|
|
| 148 |
|
| 149 |
assets, entities = generate_tilemap(files, directory, grid_width, grid_height, max_unique_models=5)
|
| 150 |
aframe_scene += assets + entities + "</a-scene>"
|
|
|
|
| 151 |
camera_move = st.session_state.get('camera_move', None)
|
| 152 |
if camera_move:
|
| 153 |
if camera_move == 'fire':
|
|
@@ -155,6 +271,7 @@ def main():
|
|
| 155 |
else:
|
| 156 |
aframe_scene += f"<script>moveCamera('{camera_move}');</script>"
|
| 157 |
st.session_state.pop('camera_move')
|
|
|
|
| 158 |
st.components.v1.html(load_aframe_and_extras() + aframe_scene, height=600)
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
|
|
|
| 10 |
<script>
|
| 11 |
let score = 0;
|
| 12 |
AFRAME.registerComponent('draggable', {
|
| 13 |
+
init: function () {
|
| 14 |
+
this.el.setAttribute('class', 'raycastable');
|
| 15 |
+
this.el.setAttribute('cursor-listener', '');
|
| 16 |
+
this.dragHandler = this.dragMove.bind(this);
|
| 17 |
+
this.el.sceneEl.addEventListener('mousemove', this.dragHandler);
|
| 18 |
+
this.el.addEventListener('mousedown', this.onDragStart.bind(this));
|
| 19 |
+
this.el.addEventListener('mouseup', this.onDragEnd.bind(this));
|
| 20 |
+
this.camera = document.querySelector('[camera]');
|
| 21 |
+
},
|
| 22 |
+
remove: function () {
|
| 23 |
+
this.el.removeAttribute('cursor-listener');
|
| 24 |
+
this.el.sceneEl.removeEventListener('mousemove', this.dragHandler);
|
| 25 |
+
},
|
| 26 |
+
onDragStart: function (evt) {
|
| 27 |
+
this.isDragging = true;
|
| 28 |
+
this.el.emit('dragstart');
|
| 29 |
+
},
|
| 30 |
+
onDragEnd: function (evt) {
|
| 31 |
+
this.isDragging = false;
|
| 32 |
+
this.el.emit('dragend');
|
| 33 |
+
},
|
| 34 |
+
dragMove: function (evt) {
|
| 35 |
+
if (!this.isDragging) return;
|
| 36 |
+
var camera = this.camera;
|
| 37 |
+
var vector = new THREE.Vector3(evt.clientX / window.innerWidth * 2 - 1, -(evt.clientY / window.innerHeight) * 2 + 1, 0.5);
|
| 38 |
+
vector.unproject(camera);
|
| 39 |
+
var dir = vector.sub(camera.position).normalize();
|
| 40 |
+
var distance = -camera.position.y / dir.y;
|
| 41 |
+
var pos = camera.position.clone().add(dir.multiplyScalar(distance));
|
| 42 |
+
this.el.setAttribute('position', pos);
|
| 43 |
+
}
|
| 44 |
});
|
| 45 |
AFRAME.registerComponent('bouncing', {
|
| 46 |
schema: {
|
|
|
|
| 77 |
}
|
| 78 |
});
|
| 79 |
AFRAME.registerComponent('moving-light', {
|
| 80 |
+
schema: {
|
| 81 |
+
color: {type: 'color', default: '#FFF'},
|
| 82 |
+
speed: {type: 'vec3', default: {x: 0.1, y: 0.1, z: 0.1}},
|
| 83 |
+
bounds: {type: 'vec3', default: {x: 5, y: 5, z: 5}}
|
| 84 |
+
},
|
| 85 |
+
init: function () {
|
| 86 |
+
this.dir = {x: 1, y: 1, z: 1};
|
| 87 |
+
this.light = document.createElement('a-light');
|
| 88 |
+
this.light.setAttribute('type', 'point');
|
| 89 |
+
this.light.setAttribute('color', this.data.color);
|
| 90 |
+
this.light.setAttribute('intensity', '0.75');
|
| 91 |
+
this.el.appendChild(this.light);
|
| 92 |
+
},
|
| 93 |
+
tick: function (time, timeDelta) {
|
| 94 |
+
var currentPos = this.el.getAttribute('position');
|
| 95 |
+
var speed = this.data.speed;
|
| 96 |
+
var bounds = this.data.bounds;
|
| 97 |
+
['x', 'y', 'z'].forEach(axis => {
|
| 98 |
+
currentPos[axis] += speed[axis] * this.dir[axis] * (timeDelta / 1000);
|
| 99 |
+
if (Math.abs(currentPos[axis]) > bounds[axis]) {
|
| 100 |
+
this.dir[axis] *= -1;
|
| 101 |
+
}
|
| 102 |
+
});
|
| 103 |
+
this.el.setAttribute('position', currentPos);
|
| 104 |
+
}
|
| 105 |
});
|
| 106 |
function moveCamera(direction) {
|
| 107 |
var camera = document.querySelector('[camera]');
|
|
|
|
| 136 |
if (hitObject.components.bouncing) {
|
| 137 |
hitObject.components.bouncing.boost();
|
| 138 |
score += 10;
|
| 139 |
+
document.getElementById('score').setAttribute('value', 'Score: ' + score);
|
| 140 |
}
|
| 141 |
}
|
| 142 |
}
|
|
|
|
| 155 |
</script>
|
| 156 |
"""
|
| 157 |
|
| 158 |
+
def create_aframe_entity(file_stem, file_type, position):
|
| 159 |
+
rotation = f"0 {random.uniform(0, 360)} 0"
|
| 160 |
+
bounce_speed = f"{random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)} {random.uniform(0.05, 0.1)}"
|
| 161 |
+
bounce_dist = f"0.1 0.1 0.1"
|
| 162 |
+
if file_type == 'obj':
|
| 163 |
+
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>'
|
| 164 |
+
elif file_type == 'glb':
|
| 165 |
+
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>'
|
| 166 |
+
elif file_type in ['webp', 'png']:
|
| 167 |
+
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>'
|
| 168 |
+
elif file_type == 'mp4':
|
| 169 |
+
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>'
|
| 170 |
+
return ''
|
| 171 |
+
|
| 172 |
+
@st.cache_data
|
| 173 |
+
def encode_file(file_path):
|
| 174 |
+
with open(file_path, "rb") as file:
|
| 175 |
+
return base64.b64encode(file.read()).decode()
|
| 176 |
+
|
| 177 |
+
@st.cache_data
|
| 178 |
+
def generate_tilemap(files, directory, grid_width, grid_height, max_unique_models=5):
|
| 179 |
+
assets = "<a-assets>"
|
| 180 |
+
entities = ""
|
| 181 |
+
tile_size = 1
|
| 182 |
+
start_x = -(grid_width * tile_size) / 2
|
| 183 |
+
start_z = -(grid_height * tile_size) / 2
|
| 184 |
+
unique_files = random.sample(files, min(len(files), max_unique_models))
|
| 185 |
+
encoded_files = {}
|
| 186 |
+
for file in unique_files:
|
| 187 |
+
file_path = os.path.join(directory, file)
|
| 188 |
+
file_type = file.split('.')[-1]
|
| 189 |
+
encoded_file = encode_file(file_path)
|
| 190 |
+
encoded_files[file] = encoded_file
|
| 191 |
+
if file_type in ['obj', 'glb']:
|
| 192 |
+
assets += f'<a-asset-item id="{Path(file).stem}" src="data:application/octet-stream;base64,{encoded_file}"></a-asset-item>'
|
| 193 |
+
elif file_type in ['webp', 'png', 'mp4']:
|
| 194 |
+
mime_type = f"image/{file_type}" if file_type in ['webp', 'png'] else "video/mp4"
|
| 195 |
+
assets += f'<{file_type} id="{Path(file).stem}" src="data:{mime_type};base64,{encoded_file}"></{file_type}>'
|
| 196 |
+
for i in range(grid_width):
|
| 197 |
+
for j in range(grid_height):
|
| 198 |
+
x = start_x + (i * tile_size)
|
| 199 |
+
z = start_z + (j * tile_size)
|
| 200 |
+
position = f"{x} 0 {z}"
|
| 201 |
+
if unique_files:
|
| 202 |
+
file = random.choice(unique_files)
|
| 203 |
+
file_type = file.split('.')[-1]
|
| 204 |
+
entities += create_aframe_entity(Path(file).stem, file_type, position)
|
| 205 |
+
assets += "</a-assets>"
|
| 206 |
+
return assets, entities
|
| 207 |
|
| 208 |
def main():
|
| 209 |
st.set_page_config(layout="wide")
|
|
|
|
| 234 |
st.markdown("### ๐ Directory")
|
| 235 |
directory = st.text_input("Enter path:", ".", key="directory_input")
|
| 236 |
|
| 237 |
+
if not os.path.isdir(directory):
|
| 238 |
+
st.sidebar.error("Invalid directory path")
|
| 239 |
+
return
|
| 240 |
+
file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
|
| 241 |
+
if uploaded_files:
|
| 242 |
+
for uploaded_file in uploaded_files:
|
| 243 |
+
file_extension = Path(uploaded_file.name).suffix.lower()[1:]
|
| 244 |
+
if file_extension in file_types:
|
| 245 |
+
with open(os.path.join(directory, uploaded_file.name), "wb") as f:
|
| 246 |
+
shutil.copyfileobj(uploaded_file, f)
|
| 247 |
+
st.sidebar.success(f"Uploaded: {uploaded_file.name}")
|
| 248 |
+
else:
|
| 249 |
+
st.sidebar.warning(f"Skipped unsupported file: {uploaded_file.name}")
|
| 250 |
+
files = [f for f in os.listdir(directory) if f.split('.')[-1] in file_types]
|
| 251 |
|
| 252 |
aframe_scene = f"""
|
| 253 |
<a-scene embedded style="height: 600px; width: 100%;">
|
|
|
|
| 263 |
|
| 264 |
assets, entities = generate_tilemap(files, directory, grid_width, grid_height, max_unique_models=5)
|
| 265 |
aframe_scene += assets + entities + "</a-scene>"
|
| 266 |
+
|
| 267 |
camera_move = st.session_state.get('camera_move', None)
|
| 268 |
if camera_move:
|
| 269 |
if camera_move == 'fire':
|
|
|
|
| 271 |
else:
|
| 272 |
aframe_scene += f"<script>moveCamera('{camera_move}');</script>"
|
| 273 |
st.session_state.pop('camera_move')
|
| 274 |
+
|
| 275 |
st.components.v1.html(load_aframe_and_extras() + aframe_scene, height=600)
|
| 276 |
|
| 277 |
if __name__ == "__main__":
|