Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Endless Sky - Flight Simulator</title>
- <style>
- html, body { margin: 0; padding: 0; overflow: hidden; background: #000; }
- #ui {
- position: absolute; bottom: 24px; left: 50%; transform: translateX(-50%);
- color: #fff; text-align: center; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
- text-shadow: 0 2px 6px rgba(0,0,0,0.6); pointer-events: none; transition: opacity 1s ease;
- opacity: 0.9; line-height: 1.5;
- }
- #ui h1 { margin: 0 0 8px; font-size: 24px; font-weight: 600; letter-spacing: 1px; }
- #ui p { margin: 4px 0; font-size: 14px; opacity: 0.85; }
- #ui .hint { font-size: 12px; opacity: 0.7; margin-top: 6px; }
- </style>
- </head>
- <body>
- <div id="ui">
- <h1>Endless Sky</h1>
- <p>W / ↑ : Climb S / ↓ : Descend</p>
- <p>A / ← : Left D / → : Right</p>
- <p class="hint">Relax and enjoy the flight.</p>
- </div>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
- <script>
- // ---- Shared GLSL noise ----
- const noiseGLSL = `
- float hash(vec2 p) {
- float n = sin(p.x * 127.1 + p.y * 311.7) * 43758.5453123;
- return fract(n);
- }
- float noise(vec2 p) {
- vec2 i = floor(p);
- vec2 f = fract(p);
- float a = hash(i);
- float b = hash(i + vec2(1.0, 0.0));
- float c = hash(i + vec2(0.0, 1.0));
- float d = hash(i + vec2(1.0, 1.0));
- vec2 u = f*f*(3.0 - 2.0*f);
- return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
- }
- float fbm(vec2 p) {
- float v = 0.0;
- float amp = 0.5;
- vec2 pp = p;
- for (int i = 0; i < 5; i++) {
- v += amp * noise(pp);
- pp = vec2(0.8 * pp.x - 0.6 * pp.y, 0.6 * pp.x + 0.8 * pp.y) * 2.0;
- amp *= 0.5;
- }
- return v;
- }
- `;
- // ---- Matching JS noise for terrain collision ----
- function hashJS(x, y) {
- let n = Math.sin(x * 127.1 + y * 311.7) * 43758.5453123;
- return n - Math.floor(n);
- }
- function noiseJS(x, y) {
- let ix = Math.floor(x), iy = Math.floor(y);
- let fx = x - ix, fy = y - iy;
- let a = hashJS(ix, iy), b = hashJS(ix+1, iy), c = hashJS(ix, iy+1), d = hashJS(ix+1, iy+1);
- let ux = fx*fx*(3-2*fx), uy = fy*fy*(3-2*fy);
- return (a*(1-ux)+b*ux)*(1-uy) + (c*(1-ux)+d*ux)*uy;
- }
- function fbmJS(x, y) {
- let v = 0, amp = 0.5, px = x, py = y;
- for (let i = 0; i < 5; i++) {
- v += amp * noiseJS(px, py);
- let qx = (0.8*px - 0.6*py)*2.0, qy = (0.6*px + 0.8*py)*2.0;
- px = qx; py = qy; amp *= 0.5;
- }
- return v;
- }
- function terrainHeight(x, z) {
- let n = fbmJS(x * 0.01, z * 0.01);
- let h = n * 800.0;
- let sea = 150.0;
- return (h < sea) ? sea : h;
- }
- // ---- Renderer / Scene ----
- const scene = new THREE.Scene();
- const camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.1, 8000);
- const renderer = new THREE.WebGLRenderer({ antialias: true });
- renderer.setSize(window.innerWidth, window.innerHeight);
- renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
- document.body.appendChild(renderer.domElement);
- const sunDir = new THREE.Vector3(0.6, 0.5, -0.7).normalize();
- // ---- Sky dome ----
- const skyMat = new THREE.ShaderMaterial({
- uniforms: { sunDir: { value: sunDir } },
- vertexShader: `varying vec3 vDir; void main() { vDir = position; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }`,
- fragmentShader: noiseGLSL + `
- varying vec3 vDir; uniform vec3 sunDir;
- void main() {
- vec3 dir = normalize(vDir);
- float t = clamp(dir.y * 0.5 + 0.5, 0.0, 1.0);
- vec3 top = vec3(0.25, 0.5, 0.85);
- vec3 horizon = vec3(0.85, 0.8, 0.75);
- vec3 sky = mix(horizon, top, pow(t, 2.0));
- float s = max(dot(dir, sunDir), 0.0);
- sky += vec3(1.0,0.9,0.7) * pow(s, 256.0);
- sky += vec3(1.0,0.9,0.7) * pow(s, 4.0) * 0.3;
- float clouds = fbm(vec2(dir.x, dir.z) * 2.0 / (abs(dir.y) + 0.3));
- float cloud = smoothstep(0.45, 0.75, clouds) * smoothstep(-0.1, 0.2, dir.y);
- sky = mix(sky, vec3(1.0), cloud * 0.6);
- gl_FragColor = vec4(sky, 1.0);
- }`,
- side: THREE.BackSide,
- depthWrite: false
- });
- const skyMesh = new THREE.Mesh(new THREE.SphereGeometry(6000, 32, 16), skyMat);
- skyMesh.frustumCulled = false;
- scene.add(skyMesh);
- // ---- Endless terrain ----
- const terrainSize = 4000, terrainSegments = 256;
- const terrainUniforms = {
- uOffset: { value: new THREE.Vector2() },
- uCameraPos: { value: new THREE.Vector3() },
- uHorizonColor: { value: new THREE.Vector3(0.85, 0.8, 0.75) },
- sunDir: { value: sunDir }
- };
- const terrainMat = new THREE.ShaderMaterial({
- uniforms: terrainUniforms,
- vertexShader: noiseGLSL + `
- uniform vec2 uOffset;
- varying vec3 vWorldPos; varying vec3 vNormal;
- float terrainHeight(vec2 xz) {
- float n = fbm(xz * 0.01);
- float h = n * 800.0;
- float sea = 150.0;
- return (h < sea) ? sea : h;
- }
- void main() {
- vec2 w = uOffset + position.xz;
- float h = terrainHeight(w);
- vec3 newPos = position; newPos.y = h;
- float d = 12.0;
- float hx = terrainHeight(w + vec2(d, 0.0));
- float hz = terrainHeight(w + vec2(0.0, d));
- vec3 norm = normalize(vec3(h - hx, d, h - hz));
- vec4 worldPos = modelMatrix * vec4(newPos, 1.0);
- vWorldPos = worldPos.xyz; vNormal = norm;
- gl_Position = projectionMatrix * viewMatrix * worldPos;
- }`,
- fragmentShader: `
- uniform vec3 uCameraPos; uniform vec3 uHorizonColor; uniform vec3 sunDir;
- varying vec3 vWorldPos; varying vec3 vNormal;
- void main() {
- float h = vWorldPos.y; float sea = 150.0;
- vec3 col;
- if (h <= sea + 0.5) {
- col = vec3(0.12, 0.35, 0.5);
- } else {
- float t = (h - sea) / 700.0;
- vec3 grass = vec3(0.35, 0.55, 0.25);
- vec3 rock = vec3(0.5, 0.45, 0.4);
- vec3 snow = vec3(0.95, 0.95, 0.97);
- col = grass;
- col = mix(col, rock, smoothstep(0.2, 0.5, t));
- col = mix(col, snow, smoothstep(0.7, 0.9, t));
- }
- vec3 norm = normalize(vNormal);
- vec3 sunColor = vec3(1.0, 0.95, 0.85);
- vec3 skyColor = vec3(0.4, 0.6, 0.9);
- vec3 groundAmb = vec3(0.3, 0.4, 0.2);
- float diff = max(dot(norm, sunDir), 0.0);
- vec3 amb = skyColor * 0.4 + groundAmb * 0.2;
- vec3 lit = col * (amb + diff * sunColor * 0.9);
- float dist = distance(vWorldPos, uCameraPos);
- float fog = clamp(1.0 - exp(-dist * 0.0009), 0.0, 1.0);
- lit = mix(lit, uHorizonColor, fog);
- gl_FragColor = vec4(lit, 1.0);
- }`
- });
- const terrainGeo = new THREE.PlaneGeometry(terrainSize, terrainSize, terrainSegments, terrainSegments);
- terrainGeo.rotateX(-Math.PI/2);
- const terrainMesh = new THREE.Mesh(terrainGeo, terrainMat);
- terrainMesh.frustumCulled = false;
- scene.add(terrainMesh);
- // ---- Lights ----
- const dirLight = new THREE.DirectionalLight(0xffffff, 1.0);
- dirLight.position.copy(sunDir).multiplyScalar(100);
- scene.add(dirLight);
- scene.add(new THREE.HemisphereLight(0xbfd1e8, 0x403030, 0.6));
- // ---- Airplane (simple low-poly) ----
- const planeGroup = new THREE.Group();
- const bodyMat = new THREE.MeshStandardMaterial({ color: 0xff5533, flatShading: true });
- const bodyGeo = new THREE.ConeGeometry(12, 60, 16); bodyGeo.rotateX(-Math.PI/2);
- planeGroup.add(new THREE.Mesh(bodyGeo, bodyMat));
- const wings = new THREE.Mesh(new THREE.BoxGeometry(80, 4, 16), bodyMat); wings.position.set(0,0,10); planeGroup.add(wings);
- const tail = new THREE.Mesh(new THREE.BoxGeometry(4, 20, 12), bodyMat); tail.position.set(0,10,25); planeGroup.add(tail);
- const tailH = new THREE.Mesh(new THREE.BoxGeometry(30, 4, 10), bodyMat); tailH.position.set(0,0,25); planeGroup.add(tailH);
- planeGroup.rotation.order = 'YXZ';
- scene.add(planeGroup);
- // ---- Parallax cloud sprites ----
- function makeCloudTexture() {
- const c = document.createElement('canvas'); c.width = c.height = 256;
- const ctx = c.getContext('2d');
- for (let i=0;i<8;i++) {
- const x = 128+(Math.random()-0.5)*120, y = 128+(Math.random()-0.5)*120, r = 50+Math.random()*50;
- const g = ctx.createRadialGradient(x,y,0,x,y,r);
- g.addColorStop(0,'rgba(255,255,255,0.85)'); g.addColorStop(1,'rgba(255,255,255,0)');
- ctx.fillStyle = g; ctx.fillRect(0,0,256,256);
- }
- return new THREE.CanvasTexture(c);
- }
- const cloudMat = new THREE.SpriteMaterial({ map: makeCloudTexture(), transparent: true, opacity: 0.5, depthWrite: false });
- const clouds = [];
- for (let i=0;i<40;i++) {
- const s = new THREE.Sprite(cloudMat); s.scale.set(250,250,1);
- s.position.set((Math.random()-0.5)*2000, 200+Math.random()*400, (Math.random()-0.5)*2000);
- scene.add(s); clouds.push(s);
- }
- // ---- Flight state & input ----
- const planeState = { position: new THREE.Vector3(0, 500, 0), yaw: 0, pitch: 0, roll: 0, speed: 250 };
- const currentLook = new THREE.Vector3(0,500,-80);
- const keys = {};
- window.addEventListener('keydown', e => {
- keys[e.code] = true;
- if (['ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(e.code)) e.preventDefault();
- });
- window.addEventListener('keyup', e => keys[e.code] = false);
- window.addEventListener('resize', () => {
- camera.aspect = window.innerWidth/window.innerHeight; camera.updateProjectionMatrix();
- renderer.setSize(window.innerWidth, window.innerHeight);
- });
- // ---- Main loop ----
- const clock = new THREE.Clock();
- function animate() {
- const dt = Math.min(clock.getDelta(), 0.05);
- // Controls
- let turn = 0, pitch = 0;
- if (keys['ArrowLeft'] || keys['KeyA']) turn -= 1;
- if (keys['ArrowRight'] || keys['KeyD']) turn += 1;
- if (keys['ArrowUp'] || keys['KeyW']) pitch += 1;
- if (keys['ArrowDown'] || keys['KeyS']) pitch -= 1;
- planeState.yaw += turn * 0.8 * dt;
- planeState.pitch += (pitch * 0.4 - planeState.pitch) * 3.0 * dt;
- planeState.roll += (-turn * 0.6 - planeState.roll) * 3.0 * dt;
- // Move
- const forward = new THREE.Vector3(
- -Math.sin(planeState.yaw)*Math.cos(planeState.pitch),
- Math.sin(planeState.pitch),
- -Math.cos(planeState.yaw)*Math.cos(planeState.pitch)
- );
- planeState.position.addScaledVector(forward, planeState.speed * dt);
- const gh = terrainHeight(planeState.position.x, planeState.position.z);
- if (planeState.position.y < gh + 50) planeState.position.y = gh + 50;
- if (planeState.position.y > 1400) planeState.position.y = 1400;
- planeGroup.position.copy(planeState.position);
- planeGroup.rotation.set(planeState.pitch, planeState.yaw, planeState.roll);
- // Camera follow
- const camOffset = new THREE.Vector3(0, 60, 220).applyEuler(new THREE.Euler(planeState.pitch, planeState.yaw, planeState.roll, 'YXZ'));
- camera.position.lerp(planeState.position.clone().add(camOffset), 1 - Math.exp(-dt*3));
- currentLook.lerp(planeState.position.clone().add(forward.clone().multiplyScalar(80)), 1 - Math.exp(-dt*5));
- camera.up.copy(new THREE.Vector3(0,1,0).applyAxisAngle(forward, planeState.roll * 0.5));
- camera.lookAt(currentLook);
- // Terrain recenter (endless)
- const snap = terrainSize / terrainSegments;
- const tx = Math.round(camera.position.x / snap) * snap;
- const tz = Math.round(camera.position.z / snap) * snap;
- terrainMesh.position.x = tx; terrainMesh.position.z = tz;
- terrainUniforms.uOffset.value.set(tx, tz);
- terrainUniforms.uCameraPos.value.copy(camera.position);
- // Sky & clouds follow
- skyMesh.position.copy(camera.position);
- clouds.forEach(s => {
- s.position.x += 12 * dt;
- const dx = s.position.x - camera.position.x, dz = s.position.z - camera.position.z;
- if (Math.sqrt(dx*dx + dz*dz) > 1500) {
- s.position.set(camera.position.x + (Math.random()-0.5)*2000, camera.position.y + 100 + Math.random()*400, camera.position.z + (Math.random()-0.5)*2000);
- }
- });
- renderer.render(scene, camera);
- requestAnimationFrame(animate);
- }
- animate();
- setTimeout(() => { document.getElementById('ui').style.opacity = 0; }, 8000);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment