(function (global) { 'use strict'; var SKY_RADIUS = 500; var DEG = Math.PI / 180; function webglAvailable() { try { var c = document.createElement('canvas'); return !!(global.WebGLRenderingContext && (c.getContext('webgl') || c.getContext('experimental-webgl'))); } catch (e) { return false; } } function clamp(v, lo, hi) { return Math.min(hi, Math.max(lo, v)); } function DomeView(container, options) { this.container = container; this.opts = options || {}; this.yaw = 0; this.pitch = 0; this.fov = this.opts.fov || 70; this.supported = false; this._raf = 0; this._disposed = false; this._skyTexture = null; this.exposure = 1; this._active = true; if (!global.THREE) { this._fail('Three.js is not loaded.'); return; } if (!webglAvailable()) { this._fail('3D view unavailable on this device.'); return; } this.supported = true; this._build(); } DomeView.isSupported = webglAvailable; DomeView.SKY_RADIUS = SKY_RADIUS; DomeView.prototype._fail = function (msg) { var el = document.createElement('div'); el.className = 'dome-unsupported'; el.setAttribute('role', 'status'); el.textContent = msg; this.container.appendChild(el); this._failEl = el; if (this.opts.onUnsupported) this.opts.onUnsupported(msg); }; DomeView.prototype._build = function () { var THREE = global.THREE; var self = this; this.scene = new THREE.Scene(); this.skyGroup = new THREE.Group(); this.skyGroup.name = 'sky.group'; this.skyGroup.matrixAutoUpdate = false; this.scene.add(this.skyGroup); this.camera = new THREE.PerspectiveCamera(this.fov, 1, 0.1, SKY_RADIUS * 4); this.camera.rotation.order = 'YXZ'; this.renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' }); this.renderer.setPixelRatio(Math.min(global.devicePixelRatio || 1, 2)); this.renderer.outputEncoding = THREE.sRGBEncoding; this.renderer.setClearColor(0x061522, 1); var canvas = this.renderer.domElement; canvas.style.display = 'block'; canvas.style.width = '100%'; canvas.style.height = '100%'; canvas.style.touchAction = 'none'; canvas.setAttribute('role', 'img'); canvas.setAttribute('aria-label', 'Interactive sky dome'); this.container.appendChild(canvas); var geo = new THREE.SphereGeometry(SKY_RADIUS, 96, 64); geo.scale(-1, 1, 1); this.skyMaterial = new THREE.MeshBasicMaterial({ color: 0x0b1c2c, depthWrite: false, fog: false }); this.sky = new THREE.Mesh(geo, this.skyMaterial); this.skyMaterial.color.convertSRGBToLinear(); this.sky.name = 'sky'; this.sky.renderOrder = -10; this.skyGroup.add(this.sky); this.overlayMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0, depthWrite: false, fog: false }); this.overlay = new THREE.Mesh(geo, this.overlayMaterial); this.overlay.scale.setScalar(0.99); this.overlay.renderOrder = -9; this.overlay.visible = false; this.overlay.name = 'sky.overlay'; this.skyGroup.add(this.overlay); var pts = []; for (var i = 0; i < 256; i++) { var a = (i / 256) * Math.PI * 2; pts.push(new THREE.Vector3(Math.cos(a) * SKY_RADIUS * 0.995, 0, Math.sin(a) * SKY_RADIUS * 0.995)); } var ringGeo = new THREE.BufferGeometry().setFromPoints(pts); this.equator = new THREE.LineLoop(ringGeo, new THREE.LineBasicMaterial({ color: 0x66b9ef, transparent: true, opacity: 0.35, fog: false })); this.equator.material.color.convertSRGBToLinear(); this.equator.name = 'ref.equator'; this.equator.renderOrder = -8; this.equator.visible = false; this.skyGroup.add(this.equator); this._onResize = function () { self.resize(); }; if (global.ResizeObserver) { this._ro = new ResizeObserver(this._onResize); this._ro.observe(this.container); } else { global.addEventListener('resize', this._onResize); } this._onVisible = function () { if (!document.hidden) self.requestRender(); }; document.addEventListener('visibilitychange', this._onVisible); this._onCtxLost = function (e) { e.preventDefault(); }; this._onCtxBack = function () { self.requestRender(); }; canvas.addEventListener('webglcontextlost', this._onCtxLost); canvas.addEventListener('webglcontextrestored', this._onCtxBack); this.setLook(this.yaw, this.pitch); this.resize(); }; DomeView.prototype.setObserver = function (obs) { if (!this.supported || this._disposed) return; this.observer = obs || null; var m = obs ? global.DomeCoords.observerMatrix(obs) : [[1, 0, 0], [0, 1, 0], [0, 0, 1]]; this.skyGroup.matrix.set(m[0][0], m[0][1], m[0][2], 0, m[1][0], m[1][1], m[1][2], 0, m[2][0], m[2][1], m[2][2], 0, 0, 0, 0, 1); this.skyGroup.matrixWorldNeedsUpdate = true; this.requestRender(); }; DomeView.prototype.setReferenceLines = function (on) { if (!this.supported || this._disposed) return; this.equator.visible = !!on; this.requestRender(); }; DomeView.prototype.setLook = function (yawDeg, pitchDeg) { if (!this.supported || this._disposed) return; this.yaw = yawDeg; this.pitch = clamp(pitchDeg, -89, 89); this.camera.rotation.y = this.yaw * DEG; this.camera.rotation.x = this.pitch * DEG; this.requestRender(); }; DomeView.prototype.setFov = function (fovDeg) { if (!this.supported || this._disposed) return; this.fov = clamp(fovDeg, 5, 120); this.camera.fov = this.fov; this.camera.updateProjectionMatrix(); this.requestRender(); }; DomeView.prototype.setSkyTexture = function (tex) { if (!this.supported || this._disposed) return; if (this._skyTexture && this._skyTexture !== tex) this._skyTexture.dispose(); this._prepTexture(tex); this._skyTexture = tex; this.skyMaterial.map = tex; this.skyMaterial.color.setScalar(this.exposure); this.skyMaterial.needsUpdate = true; this.requestRender(); }; DomeView.prototype._prepTexture = function (tex) { tex.encoding = global.THREE.sRGBEncoding; tex.anisotropy = Math.min(8, this.renderer.capabilities.getMaxAnisotropy()); tex.needsUpdate = true; return tex; }; DomeView.prototype.setOverlayTexture = function (tex) { if (!this.supported || this._disposed) return; this.cancelOverlay(); this._prepTexture(tex); this.overlayMaterial.map = tex; this.overlayMaterial.color.setScalar(this.exposure); this.overlayMaterial.opacity = 0; this.overlayMaterial.needsUpdate = true; this.overlay.visible = true; this.requestRender(); }; DomeView.prototype.setOverlayOpacity = function (a) { if (!this.supported || this._disposed) return; this.overlayMaterial.opacity = Math.max(0, Math.min(1, a)); this.requestRender(); }; DomeView.prototype.commitOverlay = function () { if (!this.supported || this._disposed || !this.overlayMaterial.map) return; var tex = this.overlayMaterial.map; if (this._skyTexture && this._skyTexture !== tex) this._skyTexture.dispose(); this._skyTexture = tex; this.skyMaterial.map = tex; this.skyMaterial.color.setScalar(this.exposure); this.skyMaterial.needsUpdate = true; this.overlayMaterial.map = null; this.overlayMaterial.opacity = 0; this.overlayMaterial.needsUpdate = true; this.overlay.visible = false; this.requestRender(); }; DomeView.prototype.cancelOverlay = function () { if (!this.supported || this._disposed) return; var tex = this.overlayMaterial.map; if (tex) tex.dispose(); this.overlayMaterial.map = null; this.overlayMaterial.opacity = 0; this.overlay.visible = false; }; DomeView.prototype.setExposure = function (x) { if (!this.supported || this._disposed) return; this.exposure = Math.max(0.4, Math.min(4, x)); if (this.skyMaterial.map) this.skyMaterial.color.setScalar(this.exposure); this.overlayMaterial.color.setScalar(this.exposure); this.requestRender(); }; DomeView.prototype.setActive = function (on) { this._active = on !== false; if (this._active) this.requestRender(); }; DomeView.prototype.resize = function () { if (!this.supported || this._disposed) return; var w = Math.max(1, this.container.clientWidth); var h = Math.max(1, this.container.clientHeight); this.renderer.setSize(w, h, false); this.camera.aspect = w / h; this.camera.updateProjectionMatrix(); this.requestRender(); }; DomeView.prototype.requestRender = function () { if (!this.supported || this._disposed || this._raf || !this._active) return; var self = this; this._raf = global.requestAnimationFrame(function () { self._raf = 0; if (self._disposed || !self._active) return; self.renderer.render(self.scene, self.camera); if (self.opts.onFrame) self.opts.onFrame(self); }); }; DomeView.prototype.dispose = function () { if (this._disposed) return; this._disposed = true; if (this._failEl && this._failEl.parentNode) this._failEl.parentNode.removeChild(this._failEl); if (!this.supported) return; if (this._raf) global.cancelAnimationFrame(this._raf); if (this._ro) this._ro.disconnect(); else global.removeEventListener('resize', this._onResize); document.removeEventListener('visibilitychange', this._onVisible); var canvas = this.renderer.domElement; canvas.removeEventListener('webglcontextlost', this._onCtxLost); canvas.removeEventListener('webglcontextrestored', this._onCtxBack); this.scene.traverse(function (o) { if (o.geometry) o.geometry.dispose(); if (o.material) o.material.dispose(); }); if (this._skyTexture) this._skyTexture.dispose(); if (this.overlayMaterial && this.overlayMaterial.map) this.overlayMaterial.map.dispose(); this.renderer.dispose(); if (this.renderer.forceContextLoss) this.renderer.forceContextLoss(); if (canvas.parentNode) canvas.parentNode.removeChild(canvas); }; DomeView.makeGraticuleTexture = function (width) { var THREE = global.THREE; var W = width || 4096, H = W / 2; var c = document.createElement('canvas'); c.width = W; c.height = H; var g = c.getContext('2d'); function U(ra) { return (1 - ra / 360) * W; } function V(dec) { return (90 - dec) / 180 * H; } var fs = Math.round(W / 128); g.fillStyle = '#061522'; g.fillRect(0, 0, W, H); var ra, dec; for (ra = 0; ra <= 360; ra += 15) { g.strokeStyle = (ra % 90 === 0) ? '#2d6a96' : '#17374f'; g.lineWidth = (ra % 90 === 0) ? 3 : 1.5; g.beginPath(); g.moveTo(U(ra), 0); g.lineTo(U(ra), H); g.stroke(); } for (dec = -75; dec <= 75; dec += 15) { g.strokeStyle = (dec === 0) ? '#66b9ef' : '#17374f'; g.lineWidth = (dec === 0) ? 3 : 1.5; g.beginPath(); g.moveTo(0, V(dec)); g.lineTo(W, V(dec)); g.stroke(); } g.font = fs + 'px monospace'; g.textAlign = 'left'; g.textBaseline = 'bottom'; g.fillStyle = '#9fd3f5'; for (ra = 0; ra < 360; ra += 30) { g.fillText(' RA ' + (ra / 15) + 'h', U(ra) - fs * 5.2, V(0) - 4); } g.fillStyle = '#d9d5c5'; for (dec = -60; dec <= 60; dec += 30) { if (dec !== 0) g.fillText(' Dec ' + (dec > 0 ? '+' : '') + dec, U(180) - fs * 2, V(dec) - 4); } var marks = [ ['SPHEREx field', 127.571, -39.214], ['M31', 10.6847, 41.2692], ['M42', 83.8221, -5.3911], ['Sgr A*', 266.4168, -29.0078], ['Polaris', 37.9546, 89.2641] ]; var r = fs * 0.9; for (var i = 0; i < marks.length; i++) { var x = U(marks[i][1]), y = V(marks[i][2]); g.strokeStyle = '#d94d35'; g.lineWidth = 3; g.beginPath(); g.moveTo(x - r, y); g.lineTo(x + r, y); g.moveTo(x, y - r); g.lineTo(x, y + r); g.stroke(); g.fillStyle = '#ffffff'; g.fillText(' ' + marks[i][0], x + r, Math.max(y - r * 0.3, fs + 2)); } return new THREE.CanvasTexture(c); }; global.DomeView = DomeView; })(window);