File size: 18,189 Bytes
6ea2eb4
 
 
4f867c3
6ea2eb4
4f867c3
ec2be1b
6ea2eb4
1a55305
ec2be1b
 
1a55305
 
 
 
 
 
 
 
6ea2eb4
 
 
 
4f867c3
6ea2eb4
1a55305
 
 
 
6ea2eb4
 
 
 
 
 
 
 
 
1a55305
 
6ea2eb4
 
 
 
 
 
 
 
1a55305
 
6ea2eb4
 
 
4f867c3
6ea2eb4
 
 
 
 
 
8ac7a4d
6ea2eb4
 
 
 
 
 
 
1a55305
 
6ea2eb4
1a55305
8ac7a4d
6ea2eb4
 
 
 
1a55305
8ac7a4d
1a55305
6ea2eb4
1a55305
8ac7a4d
1a55305
8ac7a4d
1a55305
 
8ac7a4d
 
1a55305
8ac7a4d
1a55305
 
8ac7a4d
1a55305
8ac7a4d
1a55305
6ea2eb4
1a55305
8ac7a4d
6ea2eb4
8ac7a4d
6ea2eb4
8ac7a4d
ec2be1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f867c3
1a55305
 
6ea2eb4
1a55305
ec2be1b
 
8ac7a4d
1a55305
8ac7a4d
 
 
1a55305
8ac7a4d
 
ec2be1b
8ac7a4d
 
 
1a55305
6ea2eb4
 
 
 
 
 
 
 
8ac7a4d
6ea2eb4
 
8ac7a4d
6ea2eb4
1a55305
 
8ac7a4d
1a55305
 
8ac7a4d
1a55305
 
8ac7a4d
ec2be1b
4f867c3
8ac7a4d
 
 
 
 
 
1a55305
8ac7a4d
1a55305
 
8ac7a4d
 
1a55305
 
8ac7a4d
 
1a55305
6ea2eb4
1a55305
8ac7a4d
 
 
1a55305
8ac7a4d
1a55305
 
8ac7a4d
 
 
 
 
 
 
 
 
 
1a55305
4f867c3
1a55305
8ac7a4d
 
1a55305
 
8ac7a4d
ec2be1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a55305
8ac7a4d
1a55305
ec2be1b
1a55305
ec2be1b
 
 
 
 
 
 
1a55305
8ac7a4d
ec2be1b
8ac7a4d
1a55305
8ac7a4d
1a55305
8ac7a4d
1a55305
8ac7a4d
1a55305
8ac7a4d
 
1a55305
8ac7a4d
1a55305
8ac7a4d
 
 
6ea2eb4
1a55305
 
6ea2eb4
 
 
1a55305
4f867c3
1a55305
4f867c3
 
1a55305
4f867c3
 
6ea2eb4
 
 
1a55305
 
 
6ea2eb4
4f867c3
1a55305
 
ec2be1b
 
6ea2eb4
 
 
4f867c3
 
6ea2eb4
 
ec2be1b
 
 
 
 
6ea2eb4
 
ec2be1b
 
 
 
 
 
 
8ac7a4d
 
 
ec2be1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ac7a4d
 
4f867c3
8ac7a4d
1a55305
4f867c3
1a55305
8ac7a4d
 
4f867c3
ec2be1b
8ac7a4d
1a55305
8ac7a4d
4f867c3
8ac7a4d
1a55305
4f867c3
 
ec2be1b
 
4f867c3
8ac7a4d
ec2be1b
 
 
 
 
 
 
 
 
 
 
 
 
 
6ea2eb4
 
 
85e35de
6ea2eb4
4f867c3
ec2be1b
8ac7a4d
6ea2eb4
 
 
4f867c3
6ea2eb4
 
 
1a55305
6ea2eb4
1a55305
 
6ea2eb4
 
1a55305
6ea2eb4
1a55305
 
 
 
6ea2eb4
8ac7a4d
1a55305
6ea2eb4
1a55305
6ea2eb4
 
 
1a55305
6ea2eb4
4f867c3
6ea2eb4
 
 
 
 
1a55305
 
6ea2eb4
4f867c3
6ea2eb4
 
 
ec2be1b
6ea2eb4
 
8ac7a4d
6ea2eb4
8ac7a4d
 
 
1a55305
8ac7a4d
 
1a55305
ec2be1b
 
 
 
 
6ea2eb4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import streamlit as st
import streamlit.components.v1 as components

st.set_page_config(page_title="3D City Evolution Simulator", layout="wide")

st.title("3D City Evolution Simulator")
st.write("Watch a 3D city grow with roads, vegetation, and dynamic weather")

# Sliders for container size with initial 3:4 aspect ratio
max_width = min(1200, st.session_state.get('window_width', 1200))
max_height = min(1600, st.session_state.get('window_height', 1600))

col1, col2 = st.columns(2)
with col1:
    container_width = st.slider("Container Width (px)", 300, max_width, 768, step=50)
with col2:
    container_height = st.slider("Container Height (px)", 400, max_height, 1024, step=50)

html_code = f"""
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>3D City Evolution Simulator</title>
    <style>
        body {{ margin: 0; overflow: hidden; }}
        #container {{ width: {container_width}px; height: {container_height}px; margin: 0 auto; }}
        canvas {{ width: 100%; height: 100%; display: block; }}
        .ui-panel {{
            position: absolute;
            top: 10px;
            right: 10px;
            background: rgba(0,0,0,0.7);
            padding: 15px;
            border-radius: 5px;
            color: white;
            font-family: Arial, sans-serif;
            z-index: 1000;
        }}
        .ui-panel button {{
            margin: 5px 0;
            padding: 5px 10px;
            width: 100%;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }}
        .ui-panel button:hover {{ background: #45a049; }}
    </style>
</head>
<body>
    <div id="container"></div>
    <div class="ui-panel">
        <h3>City Controls</h3>
        <button id="evolve">Evolve City</button>
        <button id="reset">Reset View</button>
        <div id="stats">
            <p>Buildings: <span id="building-count">0</span></p>
            <p>Blocks: <span id="block-count">0</span></p>
            <p>Generation: <span id="generation">0</span></p>
        </div>
    </div>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r134/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
    <script>
        class BuildingLSystem {{
            constructor() {{
                this.axiom = "F";
                this.rules = {{
                    "F": ["F[+F]", "F[-F]", "FF", "F"],
                    "+": ["+"],
                    "-": ["-"],
                    "[": ["["],
                    "]": ["]"]
                }};
                this.angle = Math.PI / 6;
            }}

            generate() {{
                let result = this.axiom;
                for (let i = 0; i < 2; i++) {{
                    let newString = "";
                    for (let char of result) {{
                        if (this.rules[char]) {{
                            const possible = this.rules[char];
                            newString += possible[Math.floor(Math.random() * possible.length)];
                        }} else {{
                            newString += char;
                        }}
                    }}
                    result = newString;
                }}
                return result;
            }}

            build(scene, basePos, maxHeight) {{
                let height = 0;
                const stack = [];
                let position = basePos.clone();
                let direction = new THREE.Vector3(0, 1, 0);
                const structure = new THREE.Group();
                let baseWidth = 1.5;

                // Add small attached buildings horizontally
                const attachCount = Math.floor(Math.random() * 3); // 0-2 attachments
                for (let i = 0; i < attachCount; i++) {{
                    const attachWidth = baseWidth * 0.6;
                    const attachHeight = 1 + Math.random() * 2;
                    const geo = new THREE.BoxGeometry(attachWidth, attachHeight, attachWidth);
                    const mat = new THREE.MeshPhongMaterial({{
                        color: new THREE.Color(0.5 + Math.random() * 0.5, 
                                            0.5 + Math.random() * 0.5, 
                                            0.5 + Math.random() * 0.5)
                    }});
                    const attach = new THREE.Mesh(geo, mat);
                    const offsetX = (i + 1) * (baseWidth + attachWidth) * (Math.random() > 0.5 ? 1 : -1);
                    attach.position.set(position.x + offsetX, attachHeight / 2, position.z);
                    attach.castShadow = true;
                    structure.add(attach);
                }}

                for (let char of this.generate()) {{
                    switch(char) {{
                        case 'F':
                            if (height < maxHeight) {{
                                const width = baseWidth * (1 - height / maxHeight);
                                const floorHeight = 2 + Math.random() * 2;
                                const geo = new THREE.BoxGeometry(width, floorHeight, width);
                                const mat = new THREE.MeshPhongMaterial({{
                                    color: new THREE.Color(0.5 + Math.random() * 0.5, 
                                                        0.5 + Math.random() * 0.5, 
                                                        0.5 + Math.random() * 0.5)
                                }});
                                const floor = new THREE.Mesh(geo, mat);
                                floor.position.copy(position).add(new THREE.Vector3(0, floorHeight/2, 0));
                                floor.castShadow = true;
                                structure.add(floor);
                                position.y += floorHeight;
                                height += floorHeight;
                            }}
                            break;
                        case '+':
                            direction.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.angle);
                            break;
                        case '-':
                            direction.applyAxisAngle(new THREE.Vector3(0, 0, 1), -this.angle);
                            break;
                        case '[':
                            stack.push(position.clone());
                            break;
                        case ']':
                            if (stack.length > 0) position = stack.pop();
                            break;
                    }}
                }}
                return structure;
            }}
        }}

        class CitySimulator {{
            constructor() {{
                this.blocks = [];
                this.roads = [];
                this.blockSize = 10;
                this.maxBuildingsPerBlock = 5;
                this.generation = 0;
                this.lakeCenters = [
                    new THREE.Vector2(20, 20),
                    new THREE.Vector2(-30, 10)
                ];
            }}

            addBlock(scene, x, z) {{
                const block = {{
                    position: new THREE.Vector2(x, z),
                    buildings: [],
                    maxHeight: this.isWaterfront(x, z) ? 20 : 12
                }};
                this.blocks.push(block);
                this.evolveBlock(scene, block, true);
            }}

            isWaterfront(x, z) {{
                const pos = new THREE.Vector2(x, z);
                return this.lakeCenters.some(center => 
                    pos.distanceTo(center) < 15 && pos.distanceTo(center) > 5);
            }}

            evolveBlock(scene, block, initial = false) {{
                if (block.buildings.length < this.maxBuildingsPerBlock) {{
                    const lsystem = new BuildingLSystem();
                    const gridX = Math.floor(Math.random() * 3) - 1;
                    const gridZ = Math.floor(Math.random() * 3) - 1;
                    const basePos = new THREE.Vector3(
                        block.position.x + gridX * 2,
                        this.getTerrainHeight(block.position.x, block.position.y),
                        block.position.y + gridZ * 2
                    );
                    
                    const building = lsystem.build(scene, basePos, block.maxHeight);
                    if (this.isWaterfront(block.position.x, block.position.y)) {{
                        building.scale.set(1.5, 2, 1.5);
                    }}
                    scene.add(building);
                    block.buildings.push(building);
                }}
            }}

            addRoad(scene, start, end) {{
                const distance = start.distanceTo(end);
                const roadGeo = new THREE.PlaneGeometry(2, distance);
                const roadMat = new THREE.MeshPhongMaterial({{ color: 0x555555 }});
                const road = new THREE.Mesh(roadGeo, roadMat);
                road.rotation.x = -Math.PI / 2;
                const midPoint = start.clone().add(end).multiplyScalar(0.5);
                road.position.set(midPoint.x, 0.02, midPoint.y);
                road.lookAt(new THREE.Vector3(end.x, 0, end.y));
                road.receiveShadow = true;
                scene.add(road);
                this.roads.push(road);
            }}

            addVegetation(scene) {{
                const treeGeo = new THREE.ConeGeometry(1, 3, 8);
                const treeMat = new THREE.MeshPhongMaterial({{ color: 0x228B22 }});
                const shrubGeo = new THREE.SphereGeometry(0.5, 8, 8);
                const shrubMat = new THREE.MeshPhongMaterial({{ color: 0x32CD32 }});

                for (let i = 0; i < 10; i++) {{
                    const x = (Math.random() - 0.5) * 90;
                    const z = (Math.random() - 0.5) * 120;
                    if (!this.isInLake(x, z)) {{
                        const tree = new THREE.Mesh(treeGeo, treeMat);
                        tree.position.set(x, this.getTerrainHeight(x, z) + 1.5, z);
                        tree.castShadow = true;
                        scene.add(tree);

                        const shrub = new THREE.Mesh(shrubGeo, shrubMat);
                        shrub.position.set(x + 1, this.getTerrainHeight(x, z) + 0.5, z + 1);
                        shrub.castShadow = true;
                        scene.add(shrub);
                    }}
                }}
            }}

            evolve(scene) {{
                this.generation++;
                if (this.blocks.length < 20) {{
                    const x = (Math.random() - 0.5) * 90;
                    const z = (Math.random() - 0.5) * 120;
                    if (!this.isInLake(x, z)) {{
                        this.addBlock(scene, x, z);
                        if (this.blocks.length > 1) {{
                            const lastBlock = this.blocks[this.blocks.length - 2];
                            this.addRoad(scene, lastBlock.position, this.blocks[this.blocks.length - 1].position);
                        }}
                    }}
                }}
                this.blocks.forEach(block => this.evolveBlock(scene, block));
                this.addVegetation(scene);
                this.updateStats();
            }}

            getTerrainHeight(x, z) {{
                return Math.sin(x * 0.05) * Math.cos(z * 0.05) * 5;
            }}

            isInLake(x, z) {{
                const pos = new THREE.Vector2(x, z);
                return this.lakeCenters.some(center => pos.distanceTo(center) < 10);
            }}

            updateStats() {{
                const totalBuildings = this.blocks.reduce((sum, block) => sum + block.buildings.length, 0);
                document.getElementById('building-count').textContent = totalBuildings;
                document.getElementById('block-count').textContent = this.blocks.length;
                document.getElementById('generation').textContent = this.generation;
            }}
        }}

        let scene, camera, renderer, controls;

        function init() {{
            const container = document.getElementById('container');
            if (!container) {{
                console.error('Container not found');
                return;
            }}

            // Scene
            scene = new THREE.Scene();
            scene.background = new THREE.Color(0x87CEEB);

            // Camera with 3:4 aspect ratio
            camera = new THREE.PerspectiveCamera(75, 3 / 4, 0.1, 1000);
            camera.position.set(0, 50, 60);

            // Renderer
            renderer = new THREE.WebGLRenderer({{ antialias: true }});
            renderer.setSize({container_width}, {container_height});
            renderer.shadowMap.enabled = true;
            renderer.shadowMap.type = THREE.PCFSoftShadowMap;
            container.appendChild(renderer.domElement);

            // Lights
            const ambientLight = new THREE.AmbientLight(0x404040);
            scene.add(ambientLight);
            const sun = new THREE.DirectionalLight(0xffffff, 0.8);
            sun.position.set(50, 50, 50);
            sun.castShadow = true;
            sun.shadow.mapSize.width = 1024;
            sun.shadow.mapSize.height = 1024;
            sun.shadow.camera.near = 0.5;
            sun.shadow.camera.far = 500;
            scene.add(sun);

            // Ground with bump mapping
            const groundGeo = new THREE.PlaneGeometry(1000, 1000, 32, 32); // Extended to horizon
            const groundMat = new THREE.MeshPhongMaterial({{ 
                color: 0x4a7043,
                bumpScale: 0.5,
                shininess: 10
            }});
            const ground = new THREE.Mesh(groundGeo, groundMat);
            ground.rotation.x = -Math.PI / 2;
            ground.position.y = -0.1;
            ground.receiveShadow = true;

            // Simple bump map (noise)
            const canvas = document.createElement('canvas');
            canvas.width = 256;
            canvas.height = 256;
            const ctx = canvas.getContext('2d');
            for (let x = 0; x < 256; x++) {{
                for (let y = 0; y < 256; y++) {{
                    const value = Math.random() * 255;
                    ctx.fillStyle = `rgb(${value},${value},${value})`;
                    ctx.fillRect(x, y, 1, 1);
                }}
            }}
            const bumpTexture = new THREE.Texture(canvas);
            bumpTexture.needsUpdate = true;
            groundMat.bumpMap = bumpTexture;

            scene.add(ground);

            // Lakes
            const lakeGeo = new THREE.CircleGeometry(10, 32);
            const lakeMat = new THREE.MeshPhongMaterial({{ color: 0x4682b4 }});
            const lakeCenters = [new THREE.Vector2(20, 20), new THREE.Vector2(-30, 10)];
            lakeCenters.forEach(center => {{
                const lake = new THREE.Mesh(lakeGeo, lakeMat);
                lake.rotation.x = -Math.PI / 2;
                lake.position.set(center.x, 0.01, center.y);
                lake.receiveShadow = true;
                scene.add(lake);
            }});

            // Bridge
            const bridgeGeo = new THREE.BoxGeometry(5, 0.2, 15);
            const bridgeMat = new THREE.MeshPhongMaterial({{ color: 0x808080 }});
            const bridge = new THREE.Mesh(bridgeGeo, bridgeMat);
            bridge.position.set(15, 0.2, 20);
            bridge.castShadow = true;
            bridge.receiveShadow = true;
            scene.add(bridge);

            // Clouds
            const cloudGeo = new THREE.SphereGeometry(5, 8, 8);
            const cloudMat = new THREE.MeshPhongMaterial({{ color: 0xFFFFFF, opacity: 0.8, transparent: true }});
            for (let i = 0; i < 5; i++) {{
                const cloud = new THREE.Mesh(cloudGeo, cloudMat);
                cloud.position.set(
                    (Math.random() - 0.5) * 200,
                    50 + Math.random() * 20,
                    (Math.random() - 0.5) * 200
                );
                cloud.castShadow = true;
                scene.add(cloud);
            }}

            // Controls
            controls = new THREE.OrbitControls(camera, renderer.domElement);
            controls.enableDamping = true;
            controls.target.set(0, 0, 0);

            // City
            const city = new CitySimulator();
            city.addBlock(scene, 0, 0);

            // Events
            window.addEventListener('resize', onWindowResize);
            document.getElementById('evolve').addEventListener('click', () => city.evolve(scene));
            document.getElementById('reset').addEventListener('click', resetView);

            animate();
        }}

        function resetView() {{
            camera.position.set(0, 50, 60);
            controls.target.set(0, 0, 0);
            controls.update();
        }}

        function onWindowResize() {{
            const width = {container_width};
            const height = {container_height};
            camera.aspect = 3 / 4;
            camera.updateProjectionMatrix();
            renderer.setSize(width, height);
        }}

        function animate() {{
            requestAnimationFrame(animate);
            controls.update();
            renderer.render(scene, camera);
        }}

        window.onload = init;
    </script>
</body>
</html>
"""

# Render the HTML component with dynamic size
components.html(html_code, width=container_width, height=container_height)

st.sidebar.title("3D City Evolution Simulator")
st.sidebar.write("""
## How to Play

Watch a 3D city evolve with roads, vegetation, and dynamic weather.

### Controls:
- **Evolve City**: Grow the city
- **Reset View**: Return to default view
- **Left-click + drag**: Rotate
- **Right-click + drag**: Pan
- **Scroll**: Zoom
- **Sliders**: Adjust play area size

### Features:
- 3:4 initial play area (768x1024)
- Roads connect blocks
- Trees and shrubs added each evolution
- Extended green ground with bump mapping
- Clouds and sunlight with shadows
- Buildings with horizontal attachments
""")