hunterx2590

Untitled

Jul 15th, 2026
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.45 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Endless Sky - Flight Simulator</title>
  7. <style>
  8. html, body { margin: 0; padding: 0; overflow: hidden; background: #000; }
  9. #ui {
  10. position: absolute; bottom: 24px; left: 50%; transform: translateX(-50%);
  11. color: #fff; text-align: center; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  12. text-shadow: 0 2px 6px rgba(0,0,0,0.6); pointer-events: none; transition: opacity 1s ease;
  13. opacity: 0.9; line-height: 1.5;
  14. }
  15. #ui h1 { margin: 0 0 8px; font-size: 24px; font-weight: 600; letter-spacing: 1px; }
  16. #ui p { margin: 4px 0; font-size: 14px; opacity: 0.85; }
  17. #ui .hint { font-size: 12px; opacity: 0.7; margin-top: 6px; }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="ui">
  22. <h1>Endless Sky</h1>
  23. <p>W / &uarr; : Climb &nbsp;&nbsp; S / &darr; : Descend</p>
  24. <p>A / &larr; : Left &nbsp;&nbsp; D / &rarr; : Right</p>
  25. <p class="hint">Relax and enjoy the flight.</p>
  26. </div>
  27.  
  28. <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
  29. <script>
  30. // ---- Shared GLSL noise ----
  31. const noiseGLSL = `
  32. float hash(vec2 p) {
  33. float n = sin(p.x * 127.1 + p.y * 311.7) * 43758.5453123;
  34. return fract(n);
  35. }
  36. float noise(vec2 p) {
  37. vec2 i = floor(p);
  38. vec2 f = fract(p);
  39. float a = hash(i);
  40. float b = hash(i + vec2(1.0, 0.0));
  41. float c = hash(i + vec2(0.0, 1.0));
  42. float d = hash(i + vec2(1.0, 1.0));
  43. vec2 u = f*f*(3.0 - 2.0*f);
  44. return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
  45. }
  46. float fbm(vec2 p) {
  47. float v = 0.0;
  48. float amp = 0.5;
  49. vec2 pp = p;
  50. for (int i = 0; i < 5; i++) {
  51. v += amp * noise(pp);
  52. pp = vec2(0.8 * pp.x - 0.6 * pp.y, 0.6 * pp.x + 0.8 * pp.y) * 2.0;
  53. amp *= 0.5;
  54. }
  55. return v;
  56. }
  57. `;
  58.  
  59. // ---- Matching JS noise for terrain collision ----
  60. function hashJS(x, y) {
  61. let n = Math.sin(x * 127.1 + y * 311.7) * 43758.5453123;
  62. return n - Math.floor(n);
  63. }
  64. function noiseJS(x, y) {
  65. let ix = Math.floor(x), iy = Math.floor(y);
  66. let fx = x - ix, fy = y - iy;
  67. let a = hashJS(ix, iy), b = hashJS(ix+1, iy), c = hashJS(ix, iy+1), d = hashJS(ix+1, iy+1);
  68. let ux = fx*fx*(3-2*fx), uy = fy*fy*(3-2*fy);
  69. return (a*(1-ux)+b*ux)*(1-uy) + (c*(1-ux)+d*ux)*uy;
  70. }
  71. function fbmJS(x, y) {
  72. let v = 0, amp = 0.5, px = x, py = y;
  73. for (let i = 0; i < 5; i++) {
  74. v += amp * noiseJS(px, py);
  75. let qx = (0.8*px - 0.6*py)*2.0, qy = (0.6*px + 0.8*py)*2.0;
  76. px = qx; py = qy; amp *= 0.5;
  77. }
  78. return v;
  79. }
  80. function terrainHeight(x, z) {
  81. let n = fbmJS(x * 0.01, z * 0.01);
  82. let h = n * 800.0;
  83. let sea = 150.0;
  84. return (h < sea) ? sea : h;
  85. }
  86.  
  87. // ---- Renderer / Scene ----
  88. const scene = new THREE.Scene();
  89. const camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.1, 8000);
  90. const renderer = new THREE.WebGLRenderer({ antialias: true });
  91. renderer.setSize(window.innerWidth, window.innerHeight);
  92. renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
  93. document.body.appendChild(renderer.domElement);
  94.  
  95. const sunDir = new THREE.Vector3(0.6, 0.5, -0.7).normalize();
  96.  
  97. // ---- Sky dome ----
  98. const skyMat = new THREE.ShaderMaterial({
  99. uniforms: { sunDir: { value: sunDir } },
  100. vertexShader: `varying vec3 vDir; void main() { vDir = position; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }`,
  101. fragmentShader: noiseGLSL + `
  102. varying vec3 vDir; uniform vec3 sunDir;
  103. void main() {
  104. vec3 dir = normalize(vDir);
  105. float t = clamp(dir.y * 0.5 + 0.5, 0.0, 1.0);
  106. vec3 top = vec3(0.25, 0.5, 0.85);
  107. vec3 horizon = vec3(0.85, 0.8, 0.75);
  108. vec3 sky = mix(horizon, top, pow(t, 2.0));
  109. float s = max(dot(dir, sunDir), 0.0);
  110. sky += vec3(1.0,0.9,0.7) * pow(s, 256.0);
  111. sky += vec3(1.0,0.9,0.7) * pow(s, 4.0) * 0.3;
  112. float clouds = fbm(vec2(dir.x, dir.z) * 2.0 / (abs(dir.y) + 0.3));
  113. float cloud = smoothstep(0.45, 0.75, clouds) * smoothstep(-0.1, 0.2, dir.y);
  114. sky = mix(sky, vec3(1.0), cloud * 0.6);
  115. gl_FragColor = vec4(sky, 1.0);
  116. }`,
  117. side: THREE.BackSide,
  118. depthWrite: false
  119. });
  120. const skyMesh = new THREE.Mesh(new THREE.SphereGeometry(6000, 32, 16), skyMat);
  121. skyMesh.frustumCulled = false;
  122. scene.add(skyMesh);
  123.  
  124. // ---- Endless terrain ----
  125. const terrainSize = 4000, terrainSegments = 256;
  126. const terrainUniforms = {
  127. uOffset: { value: new THREE.Vector2() },
  128. uCameraPos: { value: new THREE.Vector3() },
  129. uHorizonColor: { value: new THREE.Vector3(0.85, 0.8, 0.75) },
  130. sunDir: { value: sunDir }
  131. };
  132. const terrainMat = new THREE.ShaderMaterial({
  133. uniforms: terrainUniforms,
  134. vertexShader: noiseGLSL + `
  135. uniform vec2 uOffset;
  136. varying vec3 vWorldPos; varying vec3 vNormal;
  137. float terrainHeight(vec2 xz) {
  138. float n = fbm(xz * 0.01);
  139. float h = n * 800.0;
  140. float sea = 150.0;
  141. return (h < sea) ? sea : h;
  142. }
  143. void main() {
  144. vec2 w = uOffset + position.xz;
  145. float h = terrainHeight(w);
  146. vec3 newPos = position; newPos.y = h;
  147. float d = 12.0;
  148. float hx = terrainHeight(w + vec2(d, 0.0));
  149. float hz = terrainHeight(w + vec2(0.0, d));
  150. vec3 norm = normalize(vec3(h - hx, d, h - hz));
  151. vec4 worldPos = modelMatrix * vec4(newPos, 1.0);
  152. vWorldPos = worldPos.xyz; vNormal = norm;
  153. gl_Position = projectionMatrix * viewMatrix * worldPos;
  154. }`,
  155. fragmentShader: `
  156. uniform vec3 uCameraPos; uniform vec3 uHorizonColor; uniform vec3 sunDir;
  157. varying vec3 vWorldPos; varying vec3 vNormal;
  158. void main() {
  159. float h = vWorldPos.y; float sea = 150.0;
  160. vec3 col;
  161. if (h <= sea + 0.5) {
  162. col = vec3(0.12, 0.35, 0.5);
  163. } else {
  164. float t = (h - sea) / 700.0;
  165. vec3 grass = vec3(0.35, 0.55, 0.25);
  166. vec3 rock = vec3(0.5, 0.45, 0.4);
  167. vec3 snow = vec3(0.95, 0.95, 0.97);
  168. col = grass;
  169. col = mix(col, rock, smoothstep(0.2, 0.5, t));
  170. col = mix(col, snow, smoothstep(0.7, 0.9, t));
  171. }
  172. vec3 norm = normalize(vNormal);
  173. vec3 sunColor = vec3(1.0, 0.95, 0.85);
  174. vec3 skyColor = vec3(0.4, 0.6, 0.9);
  175. vec3 groundAmb = vec3(0.3, 0.4, 0.2);
  176. float diff = max(dot(norm, sunDir), 0.0);
  177. vec3 amb = skyColor * 0.4 + groundAmb * 0.2;
  178. vec3 lit = col * (amb + diff * sunColor * 0.9);
  179. float dist = distance(vWorldPos, uCameraPos);
  180. float fog = clamp(1.0 - exp(-dist * 0.0009), 0.0, 1.0);
  181. lit = mix(lit, uHorizonColor, fog);
  182. gl_FragColor = vec4(lit, 1.0);
  183. }`
  184. });
  185. const terrainGeo = new THREE.PlaneGeometry(terrainSize, terrainSize, terrainSegments, terrainSegments);
  186. terrainGeo.rotateX(-Math.PI/2);
  187. const terrainMesh = new THREE.Mesh(terrainGeo, terrainMat);
  188. terrainMesh.frustumCulled = false;
  189. scene.add(terrainMesh);
  190.  
  191. // ---- Lights ----
  192. const dirLight = new THREE.DirectionalLight(0xffffff, 1.0);
  193. dirLight.position.copy(sunDir).multiplyScalar(100);
  194. scene.add(dirLight);
  195. scene.add(new THREE.HemisphereLight(0xbfd1e8, 0x403030, 0.6));
  196.  
  197. // ---- Airplane (simple low-poly) ----
  198. const planeGroup = new THREE.Group();
  199. const bodyMat = new THREE.MeshStandardMaterial({ color: 0xff5533, flatShading: true });
  200. const bodyGeo = new THREE.ConeGeometry(12, 60, 16); bodyGeo.rotateX(-Math.PI/2);
  201. planeGroup.add(new THREE.Mesh(bodyGeo, bodyMat));
  202. const wings = new THREE.Mesh(new THREE.BoxGeometry(80, 4, 16), bodyMat); wings.position.set(0,0,10); planeGroup.add(wings);
  203. const tail = new THREE.Mesh(new THREE.BoxGeometry(4, 20, 12), bodyMat); tail.position.set(0,10,25); planeGroup.add(tail);
  204. const tailH = new THREE.Mesh(new THREE.BoxGeometry(30, 4, 10), bodyMat); tailH.position.set(0,0,25); planeGroup.add(tailH);
  205. planeGroup.rotation.order = 'YXZ';
  206. scene.add(planeGroup);
  207.  
  208. // ---- Parallax cloud sprites ----
  209. function makeCloudTexture() {
  210. const c = document.createElement('canvas'); c.width = c.height = 256;
  211. const ctx = c.getContext('2d');
  212. for (let i=0;i<8;i++) {
  213. const x = 128+(Math.random()-0.5)*120, y = 128+(Math.random()-0.5)*120, r = 50+Math.random()*50;
  214. const g = ctx.createRadialGradient(x,y,0,x,y,r);
  215. g.addColorStop(0,'rgba(255,255,255,0.85)'); g.addColorStop(1,'rgba(255,255,255,0)');
  216. ctx.fillStyle = g; ctx.fillRect(0,0,256,256);
  217. }
  218. return new THREE.CanvasTexture(c);
  219. }
  220. const cloudMat = new THREE.SpriteMaterial({ map: makeCloudTexture(), transparent: true, opacity: 0.5, depthWrite: false });
  221. const clouds = [];
  222. for (let i=0;i<40;i++) {
  223. const s = new THREE.Sprite(cloudMat); s.scale.set(250,250,1);
  224. s.position.set((Math.random()-0.5)*2000, 200+Math.random()*400, (Math.random()-0.5)*2000);
  225. scene.add(s); clouds.push(s);
  226. }
  227.  
  228. // ---- Flight state & input ----
  229. const planeState = { position: new THREE.Vector3(0, 500, 0), yaw: 0, pitch: 0, roll: 0, speed: 250 };
  230. const currentLook = new THREE.Vector3(0,500,-80);
  231. const keys = {};
  232. window.addEventListener('keydown', e => {
  233. keys[e.code] = true;
  234. if (['ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(e.code)) e.preventDefault();
  235. });
  236. window.addEventListener('keyup', e => keys[e.code] = false);
  237. window.addEventListener('resize', () => {
  238. camera.aspect = window.innerWidth/window.innerHeight; camera.updateProjectionMatrix();
  239. renderer.setSize(window.innerWidth, window.innerHeight);
  240. });
  241.  
  242. // ---- Main loop ----
  243. const clock = new THREE.Clock();
  244. function animate() {
  245. const dt = Math.min(clock.getDelta(), 0.05);
  246.  
  247. // Controls
  248. let turn = 0, pitch = 0;
  249. if (keys['ArrowLeft'] || keys['KeyA']) turn -= 1;
  250. if (keys['ArrowRight'] || keys['KeyD']) turn += 1;
  251. if (keys['ArrowUp'] || keys['KeyW']) pitch += 1;
  252. if (keys['ArrowDown'] || keys['KeyS']) pitch -= 1;
  253. planeState.yaw += turn * 0.8 * dt;
  254. planeState.pitch += (pitch * 0.4 - planeState.pitch) * 3.0 * dt;
  255. planeState.roll += (-turn * 0.6 - planeState.roll) * 3.0 * dt;
  256.  
  257. // Move
  258. const forward = new THREE.Vector3(
  259. -Math.sin(planeState.yaw)*Math.cos(planeState.pitch),
  260. Math.sin(planeState.pitch),
  261. -Math.cos(planeState.yaw)*Math.cos(planeState.pitch)
  262. );
  263. planeState.position.addScaledVector(forward, planeState.speed * dt);
  264. const gh = terrainHeight(planeState.position.x, planeState.position.z);
  265. if (planeState.position.y < gh + 50) planeState.position.y = gh + 50;
  266. if (planeState.position.y > 1400) planeState.position.y = 1400;
  267.  
  268. planeGroup.position.copy(planeState.position);
  269. planeGroup.rotation.set(planeState.pitch, planeState.yaw, planeState.roll);
  270.  
  271. // Camera follow
  272. const camOffset = new THREE.Vector3(0, 60, 220).applyEuler(new THREE.Euler(planeState.pitch, planeState.yaw, planeState.roll, 'YXZ'));
  273. camera.position.lerp(planeState.position.clone().add(camOffset), 1 - Math.exp(-dt*3));
  274. currentLook.lerp(planeState.position.clone().add(forward.clone().multiplyScalar(80)), 1 - Math.exp(-dt*5));
  275. camera.up.copy(new THREE.Vector3(0,1,0).applyAxisAngle(forward, planeState.roll * 0.5));
  276. camera.lookAt(currentLook);
  277.  
  278. // Terrain recenter (endless)
  279. const snap = terrainSize / terrainSegments;
  280. const tx = Math.round(camera.position.x / snap) * snap;
  281. const tz = Math.round(camera.position.z / snap) * snap;
  282. terrainMesh.position.x = tx; terrainMesh.position.z = tz;
  283. terrainUniforms.uOffset.value.set(tx, tz);
  284. terrainUniforms.uCameraPos.value.copy(camera.position);
  285.  
  286. // Sky & clouds follow
  287. skyMesh.position.copy(camera.position);
  288. clouds.forEach(s => {
  289. s.position.x += 12 * dt;
  290. const dx = s.position.x - camera.position.x, dz = s.position.z - camera.position.z;
  291. if (Math.sqrt(dx*dx + dz*dz) > 1500) {
  292. 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);
  293. }
  294. });
  295.  
  296. renderer.render(scene, camera);
  297. requestAnimationFrame(animate);
  298. }
  299. animate();
  300. setTimeout(() => { document.getElementById('ui').style.opacity = 0; }, 8000);
  301. </script>
  302. </body>
  303. </html>
Advertisement
Add Comment
Please, Sign In to add comment