Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,14 @@ from typing import Optional
|
|
| 4 |
import json
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
-
# Three.js template for the space simulation
|
| 8 |
THREE_JS_TEMPLATE = """
|
| 9 |
<!DOCTYPE html>
|
| 10 |
<html>
|
| 11 |
<head>
|
| 12 |
<title>Webspace Network 3D</title>
|
| 13 |
<style>
|
| 14 |
-
body { margin: 0; overflow: hidden; }
|
| 15 |
-
canvas { display: block; }
|
| 16 |
</style>
|
| 17 |
</head>
|
| 18 |
<body>
|
|
@@ -24,125 +23,26 @@ THREE_JS_TEMPLATE = """
|
|
| 24 |
// Scene setup
|
| 25 |
const scene = new THREE.Scene();
|
| 26 |
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
| 27 |
-
const renderer = new THREE.WebGLRenderer({ antialias: true });
|
| 28 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
| 29 |
document.body.appendChild(renderer.domElement);
|
| 30 |
|
| 31 |
-
//
|
| 32 |
-
const ambientLight = new THREE.AmbientLight(0x404040);
|
| 33 |
-
scene.add(ambientLight);
|
| 34 |
-
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
|
| 35 |
-
directionalLight.position.set(1, 1, 1);
|
| 36 |
-
scene.add(directionalLight);
|
| 37 |
-
|
| 38 |
-
// Generate universe from seed
|
| 39 |
const universeData = {universe_json};
|
| 40 |
-
const celestialBodies = [];
|
| 41 |
-
|
| 42 |
-
// Create star system
|
| 43 |
-
function createSystem(systemData) {{
|
| 44 |
-
const starGeometry = new THREE.SphereGeometry(2, 32, 32);
|
| 45 |
-
const starMaterial = new THREE.MeshBasicMaterial({{
|
| 46 |
-
color: new THREE.Color(systemData.star_color),
|
| 47 |
-
emissive: new THREE.Color(systemData.star_color).multiplyScalar(0.5)
|
| 48 |
-
}});
|
| 49 |
-
const star = new THREE.Mesh(starGeometry, starMaterial);
|
| 50 |
-
scene.add(star);
|
| 51 |
-
|
| 52 |
-
// Create planets
|
| 53 |
-
systemData.planets.forEach((planet, i) => {{
|
| 54 |
-
const planetGeometry = new THREE.SphereGeometry(planet.size, 32, 32);
|
| 55 |
-
const planetMaterial = new THREE.MeshStandardMaterial({{
|
| 56 |
-
color: new THREE.Color(planet.color),
|
| 57 |
-
roughness: 0.8,
|
| 58 |
-
metalness: 0.2
|
| 59 |
-
}});
|
| 60 |
-
const planetMesh = new THREE.Mesh(planetGeometry, planetMaterial);
|
| 61 |
-
|
| 62 |
-
// Position in orbit
|
| 63 |
-
const orbitRadius = 5 + (i * 3);
|
| 64 |
-
planetMesh.position.x = orbitRadius;
|
| 65 |
-
planetMesh.userData = planet;
|
| 66 |
-
|
| 67 |
-
// Add click event
|
| 68 |
-
planetMesh.addEventListener('click', (event) => {{
|
| 69 |
-
const planetInfo = event.target.userData;
|
| 70 |
-
parent.postMessage({{type: 'planetSelected', data: planetInfo}}, '*');
|
| 71 |
-
event.stopPropagation();
|
| 72 |
-
}});
|
| 73 |
-
|
| 74 |
-
// Create orbit path
|
| 75 |
-
const orbit = new THREE.Line(
|
| 76 |
-
new THREE.BufferGeometry().setFromPoints(
|
| 77 |
-
new THREE.EllipseCurve(0, 0, orbitRadius, orbitRadius, 0, Math.PI * 2, false).getPoints(100)
|
| 78 |
-
),
|
| 79 |
-
new THREE.LineBasicMaterial({ color: 0x888888, linewidth: 1 })
|
| 80 |
-
);
|
| 81 |
-
orbit.rotation.x = Math.PI / 2;
|
| 82 |
-
|
| 83 |
-
scene.add(planetMesh);
|
| 84 |
-
scene.add(orbit);
|
| 85 |
-
celestialBodies.push({{ mesh: planetMesh, orbitRadius, speed: 0.01 + Math.random() * 0.02 }});
|
| 86 |
-
}});
|
| 87 |
-
}}
|
| 88 |
-
|
| 89 |
-
// Initialize first system
|
| 90 |
-
createSystem(universeData.systems[0]);
|
| 91 |
-
|
| 92 |
-
// Player ship
|
| 93 |
-
let playerShip;
|
| 94 |
-
const loader = new THREE.GLTFLoader();
|
| 95 |
-
loader.load(
|
| 96 |
-
'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/SpaceShip/glTF/SpaceShip.gltf',
|
| 97 |
-
(gltf) => {{
|
| 98 |
-
playerShip = gltf.scene;
|
| 99 |
-
playerShip.scale.set(0.5, 0.5, 0.5);
|
| 100 |
-
playerShip.position.set(0, 0, 10);
|
| 101 |
-
scene.add(playerShip);
|
| 102 |
-
}}
|
| 103 |
-
);
|
| 104 |
-
|
| 105 |
-
// Camera controls
|
| 106 |
-
const controls = new THREE.OrbitControls(camera, renderer.domElement);
|
| 107 |
-
controls.target.set(0, 0, 0);
|
| 108 |
-
controls.maxDistance = 50;
|
| 109 |
-
camera.position.set(0, 20, 30);
|
| 110 |
|
| 111 |
-
//
|
| 112 |
-
window.addEventListener('resize', () => {{
|
| 113 |
-
camera.aspect = window.innerWidth / window.innerHeight;
|
| 114 |
-
camera.updateProjectionMatrix();
|
| 115 |
-
renderer.setSize(window.innerWidth, window.innerHeight);
|
| 116 |
-
}});
|
| 117 |
-
|
| 118 |
-
// Handle messages from Gradio
|
| 119 |
-
window.addEventListener('message', (event) => {{
|
| 120 |
-
if (event.data.type === 'travelToSystem') {{
|
| 121 |
-
// Transition to new system
|
| 122 |
-
scene.children = scene.children.filter(obj => obj.type !== 'Mesh');
|
| 123 |
-
createSystem(universeData.systems[event.data.systemIndex]);
|
| 124 |
-
}}
|
| 125 |
-
}});
|
| 126 |
-
|
| 127 |
-
// Animation loop
|
| 128 |
-
function animate() {{
|
| 129 |
-
requestAnimationFrame(animate);
|
| 130 |
-
|
| 131 |
-
// Rotate planets
|
| 132 |
-
celestialBodies.forEach(body => {{
|
| 133 |
-
body.mesh.position.x = Math.cos(Date.now() * body.speed) * body.orbitRadius;
|
| 134 |
-
body.mesh.position.z = Math.sin(Date.now() * body.speed) * body.orbitRadius;
|
| 135 |
-
}});
|
| 136 |
-
|
| 137 |
-
controls.update();
|
| 138 |
-
renderer.render(scene, camera);
|
| 139 |
-
}}
|
| 140 |
-
animate();
|
| 141 |
</script>
|
| 142 |
</body>
|
| 143 |
</html>
|
| 144 |
"""
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
class UniverseGenerator:
|
| 147 |
def __init__(self, seed=42):
|
| 148 |
self.rng = np.random.RandomState(seed)
|
|
|
|
| 4 |
import json
|
| 5 |
from pathlib import Path
|
| 6 |
|
|
|
|
| 7 |
THREE_JS_TEMPLATE = """
|
| 8 |
<!DOCTYPE html>
|
| 9 |
<html>
|
| 10 |
<head>
|
| 11 |
<title>Webspace Network 3D</title>
|
| 12 |
<style>
|
| 13 |
+
body {{ margin: 0; overflow: hidden; }}
|
| 14 |
+
canvas {{ display: block; }}
|
| 15 |
</style>
|
| 16 |
</head>
|
| 17 |
<body>
|
|
|
|
| 23 |
// Scene setup
|
| 24 |
const scene = new THREE.Scene();
|
| 25 |
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
| 26 |
+
const renderer = new THREE.WebGLRenderer({{ antialias: true }});
|
| 27 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
| 28 |
document.body.appendChild(renderer.domElement);
|
| 29 |
|
| 30 |
+
// Universe data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
const universeData = {universe_json};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
// Rest of your Three.js code...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
</script>
|
| 35 |
</body>
|
| 36 |
</html>
|
| 37 |
"""
|
| 38 |
|
| 39 |
+
def get_threejs_app():
|
| 40 |
+
"""Generate the Three.js HTML with current universe data"""
|
| 41 |
+
# Escape curly braces in the template first
|
| 42 |
+
template = THREE_JS_TEMPLATE.replace('{', '{{').replace('}', '}}')
|
| 43 |
+
# Then format only the universe_json part
|
| 44 |
+
return template.replace('{universe_json}', json.dumps(universe))
|
| 45 |
+
```
|
| 46 |
class UniverseGenerator:
|
| 47 |
def __init__(self, seed=42):
|
| 48 |
self.rng = np.random.RandomState(seed)
|