Spaces:
Running
Running
Download src/world.js from Afredbilli/Cilo: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/spaces/Afredbilli/Cilo/resolve/main/src/world.js
- Command line
-
hf download hf://spaces/Afredbilli/Cilo/src/world.js
-
curl -L -o world.js https://huggingface.co/spaces/Afredbilli/Cilo/resolve/main/src/world.js
11 kB
| import * as THREE from "three"; | |
| // ============================================= | |
| // WORLD CONFIGURATION | |
| // ============================================= | |
| const WORLD_SIZE = 180; | |
| const HALF_WORLD = WORLD_SIZE / 2; | |
| const TREE_COUNT = 90; | |
| const ROCK_COUNT = 110; | |
| const ARENA_RADIUS = 17; | |
| // ============================================= | |
| // SEEDED RANDOM GENERATOR | |
| // ============================================= | |
| function createRandom(seed = 12345) { | |
| let state = seed >>> 0; | |
| return function () { | |
| state = (state + 0x6D2B79F5) | 0; | |
| let value = state; | |
| value = Math.imul( | |
| value ^ (value >>> 15), | |
| value | 1 | |
| ); | |
| value ^= value + Math.imul( | |
| value ^ (value >>> 7), | |
| value | 61 | |
| ); | |
| return ( | |
| ((value ^ (value >>> 14)) >>> 0) / | |
| 4294967296 | |
| ); | |
| }; | |
| } | |
| // ============================================= | |
| // TERRAIN HEIGHT | |
| // ============================================= | |
| function getTerrainHeight(x, z) { | |
| const broad = | |
| Math.sin(x * 0.045) * 0.75 + | |
| Math.cos(z * 0.038) * 0.65; | |
| const rolling = | |
| Math.sin((x + z) * 0.075) * 0.3 + | |
| Math.cos((x - z) * 0.06) * 0.25; | |
| const hills = | |
| Math.sin(x * 0.018 + z * 0.025) * 1.2; | |
| const distance = Math.sqrt(x * x + z * z); | |
| // Keep the central combat arena relatively flat. | |
| const arenaBlend = THREE.MathUtils.smoothstep( | |
| distance, | |
| ARENA_RADIUS * 0.6, | |
| ARENA_RADIUS * 1.5 | |
| ); | |
| return (broad + rolling + hills) * arenaBlend; | |
| } | |
| // ============================================= | |
| // TERRAIN GEOMETRY | |
| // ============================================= | |
| function createTerrain() { | |
| const segments = 180; | |
| const geometry = new THREE.PlaneGeometry( | |
| WORLD_SIZE, | |
| WORLD_SIZE, | |
| segments, | |
| segments | |
| ); | |
| geometry.rotateX(-Math.PI / 2); | |
| const positions = geometry.attributes.position; | |
| const colors = []; | |
| const random = createRandom(42); | |
| const grassColors = [ | |
| new THREE.Color(0x53603b), | |
| new THREE.Color(0x626d42), | |
| new THREE.Color(0x71744b), | |
| new THREE.Color(0x4c5937), | |
| new THREE.Color(0x807752) | |
| ]; | |
| for (let i = 0; i < positions.count; i++) { | |
| const x = positions.getX(i); | |
| const z = positions.getZ(i); | |
| positions.setY( | |
| i, | |
| getTerrainHeight(x, z) | |
| ); | |
| const variation = Math.floor( | |
| random() * grassColors.length | |
| ); | |
| const color = grassColors[variation]; | |
| colors.push(color.r, color.g, color.b); | |
| } | |
| geometry.setAttribute( | |
| "color", | |
| new THREE.Float32BufferAttribute(colors, 3) | |
| ); | |
| geometry.computeVertexNormals(); | |
| const material = new THREE.MeshStandardMaterial({ | |
| vertexColors: true, | |
| roughness: 1, | |
| metalness: 0 | |
| }); | |
| const terrain = new THREE.Mesh( | |
| geometry, | |
| material | |
| ); | |
| terrain.receiveShadow = true; | |
| terrain.name = "terrain"; | |
| return terrain; | |
| } | |
| // ============================================= | |
| // ARENA FLOOR | |
| // ============================================= | |
| function createArena() { | |
| const group = new THREE.Group(); | |
| group.name = "combat-arena"; | |
| const floorGeometry = new THREE.CircleGeometry( | |
| ARENA_RADIUS, | |
| 64 | |
| ); | |
| floorGeometry.rotateX(-Math.PI / 2); | |
| const floorMaterial = new THREE.MeshStandardMaterial({ | |
| color: 0x76634a, | |
| roughness: 1, | |
| metalness: 0 | |
| }); | |
| const floor = new THREE.Mesh( | |
| floorGeometry, | |
| floorMaterial | |
| ); | |
| floor.position.y = 0.045; | |
| floor.receiveShadow = true; | |
| group.add(floor); | |
| // Outer arena boundary. | |
| const boundaryGeometry = new THREE.RingGeometry( | |
| ARENA_RADIUS - 0.12, | |
| ARENA_RADIUS + 0.12, | |
| 64 | |
| ); | |
| boundaryGeometry.rotateX(-Math.PI / 2); | |
| const boundaryMaterial = new THREE.MeshStandardMaterial({ | |
| color: 0x3c3428, | |
| roughness: 1 | |
| }); | |
| const boundary = new THREE.Mesh( | |
| boundaryGeometry, | |
| boundaryMaterial | |
| ); | |
| boundary.position.y = 0.07; | |
| group.add(boundary); | |
| // Stones surrounding parts of the arena. | |
| const random = createRandom(789); | |
| for (let i = 0; i < 36; i++) { | |
| const angle = ( | |
| i / 36 | |
| ) * Math.PI * 2; | |
| const radius = | |
| ARENA_RADIUS + 0.5 + random() * 1.2; | |
| const stone = createRock( | |
| 0.35 + random() * 0.5, | |
| random | |
| ); | |
| stone.position.set( | |
| Math.cos(angle) * radius, | |
| getTerrainHeight( | |
| Math.cos(angle) * radius, | |
| Math.sin(angle) * radius | |
| ), | |
| Math.sin(angle) * radius | |
| ); | |
| stone.rotation.y = random() * Math.PI * 2; | |
| group.add(stone); | |
| } | |
| return group; | |
| } | |
| // ============================================= | |
| // ROCK GENERATOR | |
| // ============================================= | |
| function createRock(size, random) { | |
| const geometry = new THREE.IcosahedronGeometry( | |
| size, | |
| 0 | |
| ); | |
| const positions = geometry.attributes.position; | |
| for (let i = 0; i < positions.count; i++) { | |
| const variation = | |
| 0.82 + random() * 0.35; | |
| positions.setXYZ( | |
| i, | |
| positions.getX(i) * variation, | |
| positions.getY(i) * variation, | |
| positions.getZ(i) * variation | |
| ); | |
| } | |
| geometry.computeVertexNormals(); | |
| const material = new THREE.MeshStandardMaterial({ | |
| color: new THREE.Color( | |
| 0x625d53 | |
| ).multiplyScalar( | |
| 0.8 + random() * 0.4 | |
| ), | |
| roughness: 1, | |
| flatShading: true | |
| }); | |
| const rock = new THREE.Mesh( | |
| geometry, | |
| material | |
| ); | |
| rock.scale.set( | |
| 1 + random() * 0.6, | |
| 0.5 + random() * 0.4, | |
| 0.8 + random() * 0.6 | |
| ); | |
| rock.castShadow = true; | |
| rock.receiveShadow = true; | |
| return rock; | |
| } | |
| // ============================================= | |
| // TREE GENERATOR | |
| // ============================================= | |
| function createTree(random) { | |
| const tree = new THREE.Group(); | |
| const height = 4 + random() * 4; | |
| const trunkRadius = 0.2 + random() * 0.15; | |
| const trunkGeometry = new THREE.CylinderGeometry( | |
| trunkRadius * 0.65, | |
| trunkRadius, | |
| height, | |
| 7 | |
| ); | |
| const trunkMaterial = new THREE.MeshStandardMaterial({ | |
| color: 0x59432e, | |
| roughness: 1 | |
| }); | |
| const trunk = new THREE.Mesh( | |
| trunkGeometry, | |
| trunkMaterial | |
| ); | |
| trunk.position.y = height / 2; | |
| trunk.castShadow = true; | |
| trunk.receiveShadow = true; | |
| tree.add(trunk); | |
| const foliageColors = [ | |
| 0x39472d, | |
| 0x465437, | |
| 0x53613b, | |
| 0x34432c | |
| ]; | |
| const foliageMaterial = new THREE.MeshStandardMaterial({ | |
| color: foliageColors[ | |
| Math.floor(random() * foliageColors.length) | |
| ], | |
| roughness: 1, | |
| flatShading: true | |
| }); | |
| const foliageLevels = 3 + Math.floor( | |
| random() * 2 | |
| ); | |
| for (let i = 0; i < foliageLevels; i++) { | |
| const levelHeight = | |
| height * (0.38 + i * 0.18); | |
| const radius = | |
| height * (0.32 - i * 0.055); | |
| const foliageGeometry = new THREE.ConeGeometry( | |
| radius, | |
| height * 0.43, | |
| 7 | |
| ); | |
| const foliage = new THREE.Mesh( | |
| foliageGeometry, | |
| foliageMaterial | |
| ); | |
| foliage.position.y = levelHeight; | |
| foliage.rotation.y = random() * Math.PI; | |
| foliage.castShadow = true; | |
| tree.add(foliage); | |
| } | |
| tree.rotation.y = random() * Math.PI * 2; | |
| return tree; | |
| } | |
| // ============================================= | |
| // ENVIRONMENT DECORATION | |
| // ============================================= | |
| function populateEnvironment(scene) { | |
| const random = createRandom(2026); | |
| const trees = new THREE.Group(); | |
| const rocks = new THREE.Group(); | |
| trees.name = "environment-trees"; | |
| rocks.name = "environment-rocks"; | |
| for (let i = 0; i < TREE_COUNT; i++) { | |
| const angle = random() * Math.PI * 2; | |
| // Keep trees outside the central battlefield. | |
| const radius = | |
| ARENA_RADIUS + 12 + random() * 55; | |
| const x = Math.cos(angle) * radius; | |
| const z = Math.sin(angle) * radius; | |
| if ( | |
| Math.abs(x) > HALF_WORLD - 8 || | |
| Math.abs(z) > HALF_WORLD - 8 | |
| ) { | |
| continue; | |
| } | |
| const tree = createTree(random); | |
| tree.position.set( | |
| x, | |
| getTerrainHeight(x, z), | |
| z | |
| ); | |
| const scale = 0.7 + random() * 0.7; | |
| tree.scale.setScalar(scale); | |
| trees.add(tree); | |
| } | |
| for (let i = 0; i < ROCK_COUNT; i++) { | |
| const angle = random() * Math.PI * 2; | |
| const radius = | |
| ARENA_RADIUS + 3 + random() * 55; | |
| const x = Math.cos(angle) * radius; | |
| const z = Math.sin(angle) * radius; | |
| if ( | |
| Math.abs(x) > HALF_WORLD - 3 || | |
| Math.abs(z) > HALF_WORLD - 3 | |
| ) { | |
| continue; | |
| } | |
| const rock = createRock( | |
| 0.25 + random() * 0.9, | |
| random | |
| ); | |
| rock.position.set( | |
| x, | |
| getTerrainHeight(x, z), | |
| z | |
| ); | |
| rocks.add(rock); | |
| } | |
| scene.add(trees); | |
| scene.add(rocks); | |
| return { trees, rocks }; | |
| } | |
| // ============================================= | |
| // SKY AND ATMOSPHERE | |
| // ============================================= | |
| function createAtmosphere(scene) { | |
| const sunGeometry = new THREE.SphereGeometry( | |
| 2.5, | |
| 16, | |
| 12 | |
| ); | |
| const sunMaterial = new THREE.MeshBasicMaterial({ | |
| color: 0xffdfad | |
| }); | |
| const sun = new THREE.Mesh( | |
| sunGeometry, | |
| sunMaterial | |
| ); | |
| sun.position.set(-65, 45, -90); | |
| scene.add(sun); | |
| // Soft atmospheric haze near the horizon. | |
| const hemisphere = new THREE.HemisphereLight( | |
| 0xd9d9c7, | |
| 0x554633, | |
| 0.25 | |
| ); | |
| scene.add(hemisphere); | |
| return { sun, hemisphere }; | |
| } | |
| // ============================================= | |
| // WORLD FACTORY | |
| // ============================================= | |
| export function createWorld(scene) { | |
| const terrain = createTerrain(); | |
| scene.add(terrain); | |
| const arena = createArena(); | |
| scene.add(arena); | |
| const environment = populateEnvironment(scene); | |
| const atmosphere = createAtmosphere(scene); | |
| return { | |
| terrain, | |
| arena, | |
| environment, | |
| atmosphere, | |
| update(delta) { | |
| // Reserved for environmental animation, | |
| // wind effects, and future world systems. | |
| }, | |
| dispose() { | |
| // Main cleanup is handled by main.js. | |
| } | |
| }; | |
| } |