Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os, base64, shutil, random
|
3 |
+
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/aframe-event-set-component@5.0.0/dist/aframe-event-set-component.min.js"></script>
|
10 |
+
<script>
|
11 |
+
let score = 0;
|
12 |
+
AFRAME.registerComponent('draggable', {
|
13 |
+
// ... (previous draggable component code)
|
14 |
+
});
|
15 |
+
AFRAME.registerComponent('bouncing', {
|
16 |
+
schema: {
|
17 |
+
speed: {type: 'vec3', default: {x: 0.1, y: 0.1, z: 0.1}},
|
18 |
+
dist: {type: 'vec3', default: {x: 0.5, y: 0.5, z: 0.5}}
|
19 |
+
},
|
20 |
+
init: function () {
|
21 |
+
this.originalPos = this.el.getAttribute('position');
|
22 |
+
this.dir = {x: 1, y: 1, z: 1};
|
23 |
+
},
|
24 |
+
tick: function (time, timeDelta) {
|
25 |
+
var currentPos = this.el.getAttribute('position');
|
26 |
+
var speed = this.data.speed;
|
27 |
+
var dist = this.data.dist;
|
28 |
+
['x', 'y', 'z'].forEach(axis => {
|
29 |
+
currentPos[axis] += speed[axis] * this.dir[axis] * (timeDelta / 1000);
|
30 |
+
if (Math.abs(currentPos[axis] - this.originalPos[axis]) > dist[axis]) {
|
31 |
+
this.dir[axis] *= -1;
|
32 |
+
}
|
33 |
+
});
|
34 |
+
this.el.setAttribute('position', currentPos);
|
35 |
+
},
|
36 |
+
boost: function() {
|
37 |
+
var speed = this.data.speed;
|
38 |
+
['x', 'y', 'z'].forEach(axis => {
|
39 |
+
speed[axis] *= 1.5;
|
40 |
+
});
|
41 |
+
this.data.speed = speed;
|
42 |
+
this.dir = {
|
43 |
+
x: Math.random() > 0.5 ? 1 : -1,
|
44 |
+
y: Math.random() > 0.5 ? 1 : -1,
|
45 |
+
z: Math.random() > 0.5 ? 1 : -1
|
46 |
+
};
|
47 |
+
}
|
48 |
+
});
|
49 |
+
AFRAME.registerComponent('moving-light', {
|
50 |
+
// ... (previous moving-light component code)
|
51 |
+
});
|
52 |
+
function moveCamera(direction) {
|
53 |
+
var camera = document.querySelector('[camera]');
|
54 |
+
var rig = document.querySelector('#rig');
|
55 |
+
var pos = rig.getAttribute('position');
|
56 |
+
var rot = rig.getAttribute('rotation');
|
57 |
+
var speed = 0.5;
|
58 |
+
var rotationSpeed = 5;
|
59 |
+
switch(direction) {
|
60 |
+
case 'up': pos.y += speed; break;
|
61 |
+
case 'down': pos.y -= speed; break;
|
62 |
+
case 'forward': pos.z -= speed; break;
|
63 |
+
case 'left': pos.x -= speed; break;
|
64 |
+
case 'right': pos.x += speed; break;
|
65 |
+
case 'rotateLeft': rot.y += rotationSpeed; break;
|
66 |
+
case 'rotateRight': rot.y -= rotationSpeed; break;
|
67 |
+
case 'reset': pos = {x: 0, y: 10, z: 0}; rot = {x: -90, y: 0, z: 0}; break;
|
68 |
+
case 'ground': pos = {x: 0, y: 1.6, z: 0}; rot = {x: 0, y: 0, z: 0}; break;
|
69 |
+
}
|
70 |
+
rig.setAttribute('position', pos);
|
71 |
+
rig.setAttribute('rotation', rot);
|
72 |
+
}
|
73 |
+
function fireRaycast() {
|
74 |
+
var camera = document.querySelector('[camera]');
|
75 |
+
var direction = new THREE.Vector3();
|
76 |
+
camera.object3D.getWorldDirection(direction);
|
77 |
+
var raycaster = new THREE.Raycaster();
|
78 |
+
raycaster.set(camera.object3D.position, direction);
|
79 |
+
var intersects = raycaster.intersectObjects(document.querySelectorAll('.raycastable').map(el => el.object3D), true);
|
80 |
+
if (intersects.length > 0) {
|
81 |
+
var hitObject = intersects[0].object.el;
|
82 |
+
if (hitObject.components.bouncing) {
|
83 |
+
hitObject.components.bouncing.boost();
|
84 |
+
score += 10;
|
85 |
+
document.getElementById('score').innerText = 'Score: ' + score;
|
86 |
+
}
|
87 |
+
}
|
88 |
+
}
|
89 |
+
document.addEventListener('keydown', function(event) {
|
90 |
+
switch(event.key.toLowerCase()) {
|
91 |
+
case 'w': moveCamera('up'); break;
|
92 |
+
case 's': moveCamera('forward'); break;
|
93 |
+
case 'x': moveCamera('down'); break;
|
94 |
+
case 'q': moveCamera('rotateLeft'); break;
|
95 |
+
case 'e': moveCamera('rotateRight'); break;
|
96 |
+
case 'z': moveCamera('reset'); break;
|
97 |
+
case 'c': moveCamera('ground'); break;
|
98 |
+
case ' ': fireRaycast(); break;
|
99 |
+
}
|
100 |
+
});
|
101 |
+
</script>
|
102 |
+
"""
|
103 |
+
|
104 |
+
# ... (previous helper functions remain the same)
|
105 |
+
|
106 |
+
def main():
|
107 |
+
st.set_page_config(layout="wide")
|
108 |
+
with st.sidebar:
|
109 |
+
st.markdown("### ๐ค 3D AI Using Claude 3.5 Sonnet for AI Pair Programming")
|
110 |
+
st.markdown("[Open 3D Animation Toolkit](https://huggingface.co/spaces/awacke1/3d_animation_toolkit)", unsafe_allow_html=True)
|
111 |
+
st.markdown("### โฌ๏ธ Upload")
|
112 |
+
uploaded_files = st.file_uploader("Add files:", accept_multiple_files=True, key="file_uploader")
|
113 |
+
st.markdown("### ๐ฎ Camera Controls")
|
114 |
+
col1, col2, col3 = st.columns(3)
|
115 |
+
with col1:
|
116 |
+
st.button("โฌ
๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'left'}))
|
117 |
+
st.button("๐โบ", on_click=lambda: st.session_state.update({'camera_move': 'rotateLeft'}))
|
118 |
+
st.button("๐", on_click=lambda: st.session_state.update({'camera_move': 'reset'}))
|
119 |
+
with col2:
|
120 |
+
st.button("โฌ๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'up'}))
|
121 |
+
st.button("๐", on_click=lambda: st.session_state.update({'camera_move': 'ground'}))
|
122 |
+
st.button("๐ซ", on_click=lambda: st.session_state.update({'camera_move': 'fire'}))
|
123 |
+
with col3:
|
124 |
+
st.button("โก๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'right'}))
|
125 |
+
st.button("โฌ๏ธ", on_click=lambda: st.session_state.update({'camera_move': 'down'}))
|
126 |
+
st.button("โฉ", on_click=lambda: st.session_state.update({'camera_move': 'forward'}))
|
127 |
+
st.markdown("### ๐บ๏ธ Grid Size")
|
128 |
+
grid_width = st.slider("Grid Width", 1, 8, 8)
|
129 |
+
grid_height = st.slider("Grid Height", 1, 5, 5)
|
130 |
+
st.markdown("### โน๏ธ Instructions")
|
131 |
+
st.write("- W: Camera up\n- S: Move forward\n- X: Camera down\n- Q/E: Rotate camera left/right\n- Z: Reset camera to top view\n- C: Move camera to ground level\n- Spacebar: Fire raycast\n- Click and drag to move objects\n- Mouse wheel to zoom\n- Right-click and drag to rotate view")
|
132 |
+
st.markdown("### ๐ Directory")
|
133 |
+
directory = st.text_input("Enter path:", ".", key="directory_input")
|
134 |
+
|
135 |
+
# ... (rest of the main function remains mostly the same)
|
136 |
+
|
137 |
+
aframe_scene = f"""
|
138 |
+
<a-scene embedded style="height: 600px; width: 100%;">
|
139 |
+
<a-entity id="rig" position="0 {max(grid_width, grid_height)} 0" rotation="-90 0 0">
|
140 |
+
<a-camera fov="60" look-controls wasd-controls="enabled: false" cursor="rayOrigin: mouse" raycaster="objects: .raycastable"></a-camera>
|
141 |
+
</a-entity>
|
142 |
+
<a-sky color="#87CEEB"></a-sky>
|
143 |
+
<a-entity moving-light="color: #FFD700; speed: 0.07 0.05 0.06; bounds: 4 3 4" position="2 2 -2"></a-entity>
|
144 |
+
<a-entity moving-light="color: #FF6347; speed: 0.06 0.08 0.05; bounds: 4 3 4" position="-2 1 2"></a-entity>
|
145 |
+
<a-entity moving-light="color: #00CED1; speed: 0.05 0.06 0.07; bounds: 4 3 4" position="0 3 0"></a-entity>
|
146 |
+
<a-text id="score" value="Score: 0" position="-1.5 1 -2" scale="0.5 0.5 0.5" color="white"></a-text>
|
147 |
+
"""
|
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':
|
154 |
+
aframe_scene += "<script>fireRaycast();</script>"
|
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__":
|
161 |
+
main()
|