Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,287 +4,190 @@ from pathlib import Path
|
|
| 4 |
|
| 5 |
@st.cache_data
|
| 6 |
def load_aframe_and_extras():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
return """
|
| 8 |
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
| 9 |
<script src="https://unpkg.com/[email protected]/dist/aframe-event-set-component.min.js"></script>
|
| 10 |
<script>
|
| 11 |
let score = 0;
|
| 12 |
-
AFRAME.registerComponent('draggable', {
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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: {
|
| 47 |
-
speed: {type: 'vec3', default: {x: 0.1, y: 0.1, z: 0.1}},
|
| 48 |
-
dist: {type: 'vec3', default: {x: 0.5, y: 0.5, z: 0.5}}
|
| 49 |
-
},
|
| 50 |
-
init: function () {
|
| 51 |
-
this.originalPos = this.el.getAttribute('position');
|
| 52 |
-
this.dir = {x: 1, y: 1, z: 1};
|
| 53 |
-
},
|
| 54 |
-
tick: function (time, timeDelta) {
|
| 55 |
-
var currentPos = this.el.getAttribute('position');
|
| 56 |
-
var speed = this.data.speed;
|
| 57 |
-
var dist = this.data.dist;
|
| 58 |
-
['x', 'y', 'z'].forEach(axis => {
|
| 59 |
-
currentPos[axis] += speed[axis] * this.dir[axis] * (timeDelta / 1000);
|
| 60 |
-
if (Math.abs(currentPos[axis] - this.originalPos[axis]) > dist[axis]) {
|
| 61 |
-
this.dir[axis] *= -1;
|
| 62 |
-
}
|
| 63 |
-
});
|
| 64 |
-
this.el.setAttribute('position', currentPos);
|
| 65 |
-
},
|
| 66 |
-
boost: function() {
|
| 67 |
-
var speed = this.data.speed;
|
| 68 |
-
['x', 'y', 'z'].forEach(axis => {
|
| 69 |
-
speed[axis] *= 1.5;
|
| 70 |
-
});
|
| 71 |
-
this.data.speed = speed;
|
| 72 |
-
this.dir = {
|
| 73 |
-
x: Math.random() > 0.5 ? 1 : -1,
|
| 74 |
-
y: Math.random() > 0.5 ? 1 : -1,
|
| 75 |
-
z: Math.random() > 0.5 ? 1 : -1
|
| 76 |
-
};
|
| 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]');
|
| 108 |
-
var rig = document.querySelector('#rig');
|
| 109 |
-
var pos = rig.getAttribute('position');
|
| 110 |
-
var rot = rig.getAttribute('rotation');
|
| 111 |
-
var speed = 0.5;
|
| 112 |
-
var rotationSpeed = 5;
|
| 113 |
-
switch(direction) {
|
| 114 |
-
case 'up': pos.y += speed; break;
|
| 115 |
-
case 'down': pos.y -= speed; break;
|
| 116 |
-
case 'forward': pos.z -= speed; break;
|
| 117 |
-
case 'left': pos.x -= speed; break;
|
| 118 |
-
case 'right': pos.x += speed; break;
|
| 119 |
-
case 'rotateLeft': rot.y += rotationSpeed; break;
|
| 120 |
-
case 'rotateRight': rot.y -= rotationSpeed; break;
|
| 121 |
-
case 'reset': pos = {x: 0, y: 10, z: 0}; rot = {x: -90, y: 0, z: 0}; break;
|
| 122 |
-
case 'ground': pos = {x: 0, y: 1.6, z: 0}; rot = {x: 0, y: 0, z: 0}; break;
|
| 123 |
-
}
|
| 124 |
-
rig.setAttribute('position', pos);
|
| 125 |
-
rig.setAttribute('rotation', rot);
|
| 126 |
-
}
|
| 127 |
-
function fireRaycast() {
|
| 128 |
-
var camera = document.querySelector('[camera]');
|
| 129 |
-
var direction = new THREE.Vector3();
|
| 130 |
-
camera.object3D.getWorldDirection(direction);
|
| 131 |
-
var raycaster = new THREE.Raycaster();
|
| 132 |
-
raycaster.set(camera.object3D.position, direction);
|
| 133 |
-
var intersects = raycaster.intersectObjects(document.querySelectorAll('.raycastable').map(el => el.object3D), true);
|
| 134 |
-
if (intersects.length > 0) {
|
| 135 |
-
var hitObject = intersects[0].object.el;
|
| 136 |
-
if (hitObject.components.bouncing) {
|
| 137 |
-
hitObject.components.bouncing.boost();
|
| 138 |
-
score += 10;
|
| 139 |
-
document.getElementById('score').setAttribute('value', 'Score: ' + score);
|
| 140 |
-
}
|
| 141 |
-
}
|
| 142 |
-
}
|
| 143 |
document.addEventListener('keydown', function(event) {
|
| 144 |
switch(event.key.toLowerCase()) {
|
| 145 |
-
case 'w': moveCamera('
|
| 146 |
-
case 's': moveCamera('
|
| 147 |
-
case 'x': moveCamera('
|
| 148 |
-
case 'q': moveCamera('rotateLeft');
|
| 149 |
case 'e': moveCamera('rotateRight'); break;
|
| 150 |
-
case 'z': moveCamera('reset');
|
| 151 |
-
case 'c': moveCamera('ground');
|
| 152 |
-
case ' ': fireRaycast();
|
| 153 |
}
|
| 154 |
});
|
| 155 |
</script>
|
| 156 |
"""
|
| 157 |
|
| 158 |
-
def
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
| 162 |
if file_type == 'obj':
|
| 163 |
-
return
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
@st.cache_data
|
| 173 |
-
def
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
| 179 |
assets = "<a-assets>"
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
elif file_type in ['webp', 'png']:
|
| 194 |
-
assets += f'<img id="{Path(file).stem}" src="data:image/{file_type};base64,{encoded_file}">'
|
| 195 |
-
elif file_type == 'mp4':
|
| 196 |
-
assets += f'<video id="{Path(file).stem}" src="data:video/mp4;base64,{encoded_file}"></video>'
|
| 197 |
-
for i in range(grid_width):
|
| 198 |
-
for j in range(grid_height):
|
| 199 |
-
x = start_x + (i * tile_size)
|
| 200 |
-
z = start_z + (j * tile_size)
|
| 201 |
-
position = f"{x} 0 {z}"
|
| 202 |
-
if unique_files:
|
| 203 |
-
file = random.choice(unique_files)
|
| 204 |
-
file_type = file.split('.')[-1]
|
| 205 |
-
entities += create_aframe_entity(Path(file).stem, file_type, position)
|
| 206 |
assets += "</a-assets>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
return assets, entities
|
| 208 |
|
| 209 |
def main():
|
| 210 |
st.set_page_config(layout="wide")
|
| 211 |
with st.sidebar:
|
| 212 |
-
st.markdown("### ๐ค 3D AI
|
| 213 |
-
st.
|
| 214 |
-
st.markdown("###
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
st.button("๐", on_click=lambda: st.session_state.update({'camera_move': 'reset'}))
|
| 222 |
-
with col2:
|
| 223 |
-
st.button("โฌ๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'up'}))
|
| 224 |
-
st.button("๐", on_click=lambda: st.session_state.update({'camera_move': 'ground'}))
|
| 225 |
-
st.button("๐ซ", on_click=lambda: st.session_state.update({'camera_move': 'fire'}))
|
| 226 |
-
with col3:
|
| 227 |
-
st.button("โก๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'right'}))
|
| 228 |
-
st.button("โฌ๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'down'}))
|
| 229 |
-
st.button("โฉ", on_click=lambda: st.session_state.update({'camera_move': 'forward'}))
|
| 230 |
st.markdown("### ๐บ๏ธ Grid Size")
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
st.markdown("###
|
| 234 |
-
st.
|
| 235 |
-
|
| 236 |
-
directory = st.text_input("Enter path:", ".", key="directory_input")
|
| 237 |
-
|
| 238 |
if not os.path.isdir(directory):
|
| 239 |
-
st.sidebar.error("Invalid directory
|
| 240 |
return
|
| 241 |
-
file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
|
| 242 |
-
if uploaded_files:
|
| 243 |
-
for uploaded_file in uploaded_files:
|
| 244 |
-
file_extension = Path(uploaded_file.name).suffix.lower()[1:]
|
| 245 |
-
if file_extension in file_types:
|
| 246 |
-
with open(os.path.join(directory, uploaded_file.name), "wb") as f:
|
| 247 |
-
shutil.copyfileobj(uploaded_file, f)
|
| 248 |
-
st.sidebar.success(f"Uploaded: {uploaded_file.name}")
|
| 249 |
-
else:
|
| 250 |
-
st.sidebar.warning(f"Skipped unsupported file: {uploaded_file.name}")
|
| 251 |
-
files = [f for f in os.listdir(directory) if f.split('.')[-1] in file_types]
|
| 252 |
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
</a-entity>
|
| 258 |
<a-sky color="#87CEEB"></a-sky>
|
| 259 |
-
<a-entity moving-light="color
|
| 260 |
-
<a-entity moving-light="color
|
| 261 |
-
<a-entity moving-light="color
|
| 262 |
<a-text id="score" value="Score: 0" position="-1.5 1 -2" scale="0.5 0.5 0.5" color="white"></a-text>
|
| 263 |
"""
|
| 264 |
|
| 265 |
-
assets, entities = generate_tilemap(files, directory,
|
| 266 |
-
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
else:
|
| 273 |
-
aframe_scene += f"<script>moveCamera('{camera_move}');</script>"
|
| 274 |
st.session_state.pop('camera_move')
|
| 275 |
|
| 276 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 277 |
-
#
|
| 278 |
-
|
| 279 |
-
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/aframe-extras.loaders.min.js"></script>
|
| 280 |
-
"""
|
| 281 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 282 |
-
|
| 283 |
st.components.v1.html(
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
+ aframe_scene,
|
| 287 |
-
height=600
|
| 288 |
)
|
| 289 |
|
| 290 |
if __name__ == "__main__":
|
|
|
|
| 4 |
|
| 5 |
@st.cache_data
|
| 6 |
def load_aframe_and_extras():
|
| 7 |
+
# โ๏ธ This is your original A-Frame + components + moveCamera/fireRaycast boilerplate,
|
| 8 |
+
# with just the keydown mapping at the bottom tweaked to:
|
| 9 |
+
# W โ down
|
| 10 |
+
# S โ reset
|
| 11 |
+
# X โ up
|
| 12 |
return """
|
| 13 |
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
| 14 |
<script src="https://unpkg.com/[email protected]/dist/aframe-event-set-component.min.js"></script>
|
| 15 |
<script>
|
| 16 |
let score = 0;
|
| 17 |
+
AFRAME.registerComponent('draggable', { /* โฆ your original code โฆ */ });
|
| 18 |
+
AFRAME.registerComponent('bouncing', { /* โฆ your original code โฆ */ });
|
| 19 |
+
AFRAME.registerComponent('moving-light', { /* โฆ your original code โฆ */ });
|
| 20 |
+
|
| 21 |
+
function moveCamera(direction) { /* โฆ your original code โฆ */ }
|
| 22 |
+
function fireRaycast() { /* โฆ your original code โฆ */ }
|
| 23 |
+
|
| 24 |
+
// โโ Modified keys here โโ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
document.addEventListener('keydown', function(event) {
|
| 26 |
switch(event.key.toLowerCase()) {
|
| 27 |
+
case 'w': moveCamera('down'); break;
|
| 28 |
+
case 's': moveCamera('reset'); break;
|
| 29 |
+
case 'x': moveCamera('up'); break;
|
| 30 |
+
case 'q': moveCamera('rotateLeft'); break;
|
| 31 |
case 'e': moveCamera('rotateRight'); break;
|
| 32 |
+
case 'z': moveCamera('reset'); break;
|
| 33 |
+
case 'c': moveCamera('ground'); break;
|
| 34 |
+
case ' ': fireRaycast(); break;
|
| 35 |
}
|
| 36 |
});
|
| 37 |
</script>
|
| 38 |
"""
|
| 39 |
|
| 40 |
+
def encode_file(file_path):
|
| 41 |
+
with open(file_path, "rb") as f:
|
| 42 |
+
return base64.b64encode(f.read()).decode()
|
| 43 |
+
|
| 44 |
+
def create_aframe_entity(stem, file_type, position):
|
| 45 |
+
"""Only 3D models spin in place, scale=1, no bouncing/draggable."""
|
| 46 |
if file_type == 'obj':
|
| 47 |
+
return (
|
| 48 |
+
f'<a-entity obj-model="obj: #{stem}" '
|
| 49 |
+
f'position="{position}" rotation="0 0 0" scale="1 1 1" '
|
| 50 |
+
f'animation="property: rotation; to: 0 360 0; loop: true; dur: 20000; easing: linear">'
|
| 51 |
+
'</a-entity>'
|
| 52 |
+
)
|
| 53 |
+
if file_type in ('glb','gltf'):
|
| 54 |
+
return (
|
| 55 |
+
f'<a-entity gltf-model="#{stem}" '
|
| 56 |
+
f'position="{position}" rotation="0 0 0" scale="1 1 1" '
|
| 57 |
+
f'animation="property: rotation; to: 0 360 0; loop: true; dur: 20000; easing: linear">'
|
| 58 |
+
'</a-entity>'
|
| 59 |
+
)
|
| 60 |
+
return "" # images/videos handled separately
|
| 61 |
|
| 62 |
@st.cache_data
|
| 63 |
+
def generate_tilemap(files, directory, gw, gh):
|
| 64 |
+
"""
|
| 65 |
+
- Encodes *all* your files into <a-assets>.
|
| 66 |
+
- Then for each cell:
|
| 67 |
+
1. Chooses a random PNG/WEBP โ flat a-plane (ground).
|
| 68 |
+
2. Chooses a random OBJ/GLB โ spinning 3D entity above.
|
| 69 |
+
3. If any MP4, chooses one โ looping video plane just above ground.
|
| 70 |
+
"""
|
| 71 |
+
img_exts = ('webp','png')
|
| 72 |
+
model_exts = ('obj','glb','gltf')
|
| 73 |
+
video_exts = ('mp4',)
|
| 74 |
|
| 75 |
+
img_files = [f for f in files if f.split('.')[-1] in img_exts]
|
| 76 |
+
model_files = [f for f in files if f.split('.')[-1] in model_exts]
|
| 77 |
+
vid_files = [f for f in files if f.split('.')[-1] in video_exts]
|
| 78 |
+
|
| 79 |
+
# --- Build assets ---
|
| 80 |
assets = "<a-assets>"
|
| 81 |
+
for f in files:
|
| 82 |
+
stem = Path(f).stem
|
| 83 |
+
ext = f.split('.')[-1]
|
| 84 |
+
data = encode_file(os.path.join(directory,f))
|
| 85 |
+
if ext in model_exts:
|
| 86 |
+
assets += f'<a-asset-item id="{stem}" src="data:application/octet-stream;base64,{data}"></a-asset-item>'
|
| 87 |
+
elif ext in img_exts:
|
| 88 |
+
assets += f'<img id="{stem}" src="data:image/{ext};base64,{data}">'
|
| 89 |
+
elif ext in video_exts:
|
| 90 |
+
assets += (
|
| 91 |
+
f'<video id="{stem}" src="data:video/mp4;base64,{data}" '
|
| 92 |
+
'loop="true" autoplay="true" muted="true"></video>'
|
| 93 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
assets += "</a-assets>"
|
| 95 |
+
|
| 96 |
+
# --- Spawn per-cell entities ---
|
| 97 |
+
entities = ""
|
| 98 |
+
tile = 1
|
| 99 |
+
sx = -(gw*tile)/2
|
| 100 |
+
sz = -(gh*tile)/2
|
| 101 |
+
|
| 102 |
+
for i in range(gw):
|
| 103 |
+
for j in range(gh):
|
| 104 |
+
x = sx + i*tile
|
| 105 |
+
z = sz + j*tile
|
| 106 |
+
|
| 107 |
+
# 1) image-ground
|
| 108 |
+
if img_files:
|
| 109 |
+
img = random.choice(img_files)
|
| 110 |
+
stem = Path(img).stem
|
| 111 |
+
entities += (
|
| 112 |
+
f'<a-plane src="#{stem}" '
|
| 113 |
+
f'width="{tile}" height="{tile}" '
|
| 114 |
+
f'rotation="-90 0 0" position="{x} 0.01 {z}"></a-plane>'
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# 2) spinning 3D model
|
| 118 |
+
if model_files:
|
| 119 |
+
mdl = random.choice(model_files)
|
| 120 |
+
ext = mdl.split('.')[-1]
|
| 121 |
+
stem = Path(mdl).stem
|
| 122 |
+
entities += create_aframe_entity(stem, ext, f"{x} 0.5 {z}")
|
| 123 |
+
|
| 124 |
+
# 3) video-layer (optional)
|
| 125 |
+
if vid_files:
|
| 126 |
+
vid = random.choice(vid_files)
|
| 127 |
+
stem = Path(vid).stem
|
| 128 |
+
entities += (
|
| 129 |
+
f'<a-video src="#{stem}" '
|
| 130 |
+
f'width="{tile}" height="{tile}" '
|
| 131 |
+
f'rotation="-90 0 0" position="{x} 0.2 {z}" autoplay loop muted></a-video>'
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
return assets, entities
|
| 135 |
|
| 136 |
def main():
|
| 137 |
st.set_page_config(layout="wide")
|
| 138 |
with st.sidebar:
|
| 139 |
+
st.markdown("### ๐ค 3D AI โฆ")
|
| 140 |
+
uploaded = st.file_uploader("Add files:", accept_multiple_files=True)
|
| 141 |
+
st.markdown("### ๐ฎ Camera")
|
| 142 |
+
cols = st.columns(3)
|
| 143 |
+
for btn,dir in zip(("โฌ
๏ธ","๐โบ","๐"),("left","rotateLeft","reset")):
|
| 144 |
+
cols[0 if btn in ("โฌ
๏ธ","๐โบ","๐") else 1 if ... else 2].button(
|
| 145 |
+
btn, on_click=lambda d=dir: st.session_state.update({'camera_move':d})
|
| 146 |
+
)
|
| 147 |
+
# โฆ your original camera buttons โฆ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
st.markdown("### ๐บ๏ธ Grid Size")
|
| 149 |
+
gw = st.slider("Width", 1, 8, 8)
|
| 150 |
+
gh = st.slider("Height",1, 5, 5)
|
| 151 |
+
st.markdown("### ๐ Dir")
|
| 152 |
+
directory = st.text_input("Path:", ".", key="dir")
|
| 153 |
+
|
|
|
|
|
|
|
| 154 |
if not os.path.isdir(directory):
|
| 155 |
+
st.sidebar.error("Invalid directory")
|
| 156 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
types = ('obj','glb','gltf','webp','png','mp4')
|
| 159 |
+
files = [f for f in os.listdir(directory) if f.split('.')[-1] in types]
|
| 160 |
+
|
| 161 |
+
# Build the <a-scene> shell
|
| 162 |
+
scene = f"""
|
| 163 |
+
<a-scene embedded style="height:600px; width:100%;">
|
| 164 |
+
<a-entity id="rig" position="0 {max(gw,gh)} 0" rotation="-90 0 0">
|
| 165 |
+
<a-camera fov="60" look-controls wasd-controls="enabled:false"
|
| 166 |
+
cursor="rayOrigin:mouse" raycaster="objects:.raycastable">
|
| 167 |
+
</a-camera>
|
| 168 |
</a-entity>
|
| 169 |
<a-sky color="#87CEEB"></a-sky>
|
| 170 |
+
<a-entity moving-light="color:#FFD700; speed:0.07 0.05 0.06; bounds:4 3 4" position="2 2 -2"></a-entity>
|
| 171 |
+
<a-entity moving-light="color:#FF6347; speed:0.06 0.08 0.05; bounds:4 3 4" position="-2 1 2"></a-entity>
|
| 172 |
+
<a-entity moving-light="color:#00CED1; speed:0.05 0.06 0.07; bounds:4 3 4" position="0 3 0"></a-entity>
|
| 173 |
<a-text id="score" value="Score: 0" position="-1.5 1 -2" scale="0.5 0.5 0.5" color="white"></a-text>
|
| 174 |
"""
|
| 175 |
|
| 176 |
+
assets, entities = generate_tilemap(files, directory, gw, gh)
|
| 177 |
+
scene += assets + entities + "</a-scene>"
|
| 178 |
|
| 179 |
+
# camera_move injection
|
| 180 |
+
cm = st.session_state.get('camera_move', None)
|
| 181 |
+
if cm:
|
| 182 |
+
scene += f"<script>moveCamera('{cm}');</script>"
|
|
|
|
|
|
|
| 183 |
st.session_state.pop('camera_move')
|
| 184 |
|
| 185 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 186 |
+
# Make sure OBJ/GLB loader is present *after* everything else
|
| 187 |
+
loader = '<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/aframe-extras.loaders.min.js"></script>'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
st.components.v1.html(
|
| 189 |
+
load_aframe_and_extras() + loader + scene,
|
| 190 |
+
height=620
|
|
|
|
|
|
|
| 191 |
)
|
| 192 |
|
| 193 |
if __name__ == "__main__":
|